Download MetaTrader 5

Is there a better way to do this?

To add comments, please log in or register
Donald Gibson
499
Donald Gibson  

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void StopLoss_Management()
  {
/*-----------------------------------------------------------------------------------------------*/
   MA=iMA(NULL,0,MAperiod,MAindshift,MAcalcMethod,MAappliedprice,MAbufferShift);
   MA2=iMA(NULL,0,MA2period,MA2indshift,MA2calcMethod,MA2appliedprice,MA2bufferShift);
   if(MarketInfo(Symbol(),MODE_DIGITS)>=4)
     {
      RndNumber_Upper=NormalizeDouble(Bid+(100*pips),2);
      RndNumber_Lower=NormalizeDouble(Bid-(100*pips),2);
     }
   if(MarketInfo(Symbol(),MODE_DIGITS)<4)
     {
      RndNumber_Upper=NormalizeDouble(Bid+(100*pips),0);
      RndNumber_Lower=NormalizeDouble(Bid-(100*pips),0);
     }
/*-------------------------------------------------------------------------------------------------*/
   double Buy_SL_Group_1=MathMin(RndNumber_Upper,RndNumber_Lower);
   double Buy_SL_Group_2=MathMin(MA,MA2);
   Buy_SL_Decision=MathMin(Buy_SL_Group_1,Buy_SL_Group_2);

   double Sell_SL_Group_1=MathMin(RndNumber_Upper,RndNumber_Lower);
   double Sell_SL_Group_2=MathMin(MA,MA2);
   Sell_SL_Decision=MathMin(Sell_SL_Group_1,Sell_SL_Group_2);
/*-----------------------------------------------------------------------------------------------*/
   Print("RndNumber_Upper  "+DoubleToStr(RndNumber_Upper,Digits));
   Print("RndNumber_Lower  "+DoubleToStr(RndNumber_Lower,Digits));
   Print("MA  "+DoubleToStr(MA,Digits));
   Print("MA2  "+DoubleToStr(MA2,Digits));
/*-----------------------------------------------------------------------------------------------*/

   if(PSAR>OrderOpenPrice())
     {
      StopLoss_Long=Buy_SL_Decision;
      TakeProfit_Long=Resistence;
      StopLoss_Short=Sell_SL_Decision;
      TakeProfit_Short=Support;
     }
   if(PSAR<OrderOpenPrice())
     {
      StopLoss_Long=Buy_SL_Decision;
      TakeProfit_Long=Resistence;
      StopLoss_Short=Sell_SL_Decision;
      TakeProfit_Short=Support;
     }
/*-----------------------------------------------------------------------------------------------*/
   if(OrderStopLoss()==0 || OrderTakeProfit()==0)
      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY)
              {
               Ticket_Modify=OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss_Long,TakeProfit_Long,0,0);
              }
            if(OrderType()==OP_SELL)
              {
               Ticket_Modify=OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss_Short,TakeProfit_Short,0,0);
              }
           }
/*-------------------------------------------------------------------------------------------------*/
//Error Control
   GetLastError();
   if(GetLastError()>=3)
     {
      Alert("StopLoss_Management() ..Error  : "+GetLastError());
      Print("StopLoss_Management() ..Error  : "+GetLastError());
     }
/*-----------------------------------------------------------------------------------------------*/
  }
Is there a better way of determining the minimum of 4 different values than this?
Mladen Rakic
72048
Mladen Rakic  
Donald Gibson:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void StopLoss_Management()
  {
/*-----------------------------------------------------------------------------------------------*/
   MA=iMA(NULL,0,MAperiod,MAindshift,MAcalcMethod,MAappliedprice,MAbufferShift);
   MA2=iMA(NULL,0,MA2period,MA2indshift,MA2calcMethod,MA2appliedprice,MA2bufferShift);
   if(MarketInfo(Symbol(),MODE_DIGITS)>=4)
     {
      RndNumber_Upper=NormalizeDouble(Bid+(100*pips),2);
      RndNumber_Lower=NormalizeDouble(Bid-(100*pips),2);
     }
   if(MarketInfo(Symbol(),MODE_DIGITS)<4)
     {
      RndNumber_Upper=NormalizeDouble(Bid+(100*pips),0);
      RndNumber_Lower=NormalizeDouble(Bid-(100*pips),0);
     }
/*-------------------------------------------------------------------------------------------------*/
   double Buy_SL_Group_1=MathMin(RndNumber_Upper,RndNumber_Lower);
   double Buy_SL_Group_2=MathMin(MA,MA2);
   Buy_SL_Decision=MathMin(Buy_SL_Group_1,Buy_SL_Group_2);

   double Sell_SL_Group_1=MathMin(RndNumber_Upper,RndNumber_Lower);
   double Sell_SL_Group_2=MathMin(MA,MA2);
   Sell_SL_Decision=MathMin(Sell_SL_Group_1,Sell_SL_Group_2);
/*-----------------------------------------------------------------------------------------------*/
   Print("RndNumber_Upper  "+DoubleToStr(RndNumber_Upper,Digits));
   Print("RndNumber_Lower  "+DoubleToStr(RndNumber_Lower,Digits));
   Print("MA  "+DoubleToStr(MA,Digits));
   Print("MA2  "+DoubleToStr(MA2,Digits));
/*-----------------------------------------------------------------------------------------------*/

   if(PSAR>OrderOpenPrice())
     {
      StopLoss_Long=Buy_SL_Decision;
      TakeProfit_Long=Resistence;
      StopLoss_Short=Sell_SL_Decision;
      TakeProfit_Short=Support;
     }
   if(PSAR<OrderOpenPrice())
     {
      StopLoss_Long=Buy_SL_Decision;
      TakeProfit_Long=Resistence;
      StopLoss_Short=Sell_SL_Decision;
      TakeProfit_Short=Support;
     }
/*-----------------------------------------------------------------------------------------------*/
   if(OrderStopLoss()==0 || OrderTakeProfit()==0)
      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY)
              {
               Ticket_Modify=OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss_Long,TakeProfit_Long,0,0);
              }
            if(OrderType()==OP_SELL)
              {
               Ticket_Modify=OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss_Short,TakeProfit_Short,0,0);
              }
           }
/*-------------------------------------------------------------------------------------------------*/
//Error Control
   GetLastError();
   if(GetLastError()>=3)
     {
      Alert("StopLoss_Management() ..Error  : "+GetLastError());
      Print("StopLoss_Management() ..Error  : "+GetLastError());
     }
/*-----------------------------------------------------------------------------------------------*/
  }
Is there a better way of determining the minimum of 4 different values than this?
Fill an array of values and sort the array - then you will not have to make code monsters even if you have more than 4 values to compare
Carl Schreiber
Moderator
7954
Carl Schreiber  
what about using defines?

#define fmin4(v1,v2,v3,v4) fmin(v1,fmin(v2,fmin(v3,v4)))
..
// and your:  
//  double Buy_SL_Group_1=MathMin(RndNumber_Upper,RndNumber_Lower);
//  double Buy_SL_Group_2=MathMin(MA,MA2);
//  Buy_SL_Decision=MathMin(Buy_SL_Group_1,Buy_SL_Group_2);
// becomes just:

   Buy_SL_Decision=fmin4(RndNumber_Upper,RndNumber_Lower,MA,MA2);
Donald Gibson
499
Donald Gibson  
Thank you both I will study up on both.
Donald Gibson
499
Donald Gibson  

   string PrintDesc="";
   double MinSL_Long=0;
   double MinSL_Short=0;
   double Psar=iSAR(NULL,PERIOD_CURRENT,0.02,0.2,0);
   double sma21=iMA(NULL,0,21,0,0,0,1);
   double sma34=iMA(NULL,0,34,0,0,0,1);
   double Low_5=Low[iLowest(NULL,0,MODE_LOW,5,1)];
   double High_5=High[iHighest(NULL,0,MODE_HIGH,5,1)];
/*-----------------------------------------------------------------------------------------------*/
   double SLArray[5];
/*-------------------------------------------------------------------------------------------------*/
   if(sma21>sma34)
     {
      MinSL_Long=Bid-(20*pips);
      if(Psar<MinSL_Long)   {SLArray[0]=Psar;   PrintDesc="Psar";}    else SLArray[0]=MinSL_Long;  PrintDesc="MinSL_Long";
      if(sma21<MinSL_Long)  {SLArray[1]=sma21;  PrintDesc="sma21";}   else SLArray[1]=MinSL_Long;  PrintDesc="MinSL_Long";
      if(sma34<MinSL_Long)  {SLArray[2]=sma34;  PrintDesc="sma34";}   else SLArray[2]=MinSL_Long;  PrintDesc="MinSL_Long";
      if(Low_5<MinSL_Long)  {SLArray[3]=Low_5;  PrintDesc="Low_5";}   else SLArray[3]=MinSL_Long;  PrintDesc="MinSL_Long";
      if(High_5<MinSL_Long) {SLArray[4]=High_5; PrintDesc="High_5";}  else SLArray[4]=MinSL_Long;  PrintDesc="MinSL_Long";
     }
   if(sma21<sma34)
     {
      MinSL_Short=Ask+(20*pips);
      if(Psar>MinSL_Short)   {SLArray[0]=Psar;   PrintDesc="Psar";}     else SLArray[0]=MinSL_Short;  PrintDesc="MinSL_Short";
      if(sma21>MinSL_Short)  {SLArray[1]=sma21;  PrintDesc="sma21";}    else SLArray[1]=MinSL_Short;  PrintDesc="MinSL_Short";
      if(sma34>MinSL_Short)  {SLArray[2]=sma34;  PrintDesc="sma34";}    else SLArray[2]=MinSL_Short;  PrintDesc="MinSL_Short";
      if(Low_5>MinSL_Short)  {SLArray[3]=Low_5;  PrintDesc="Low_5";}    else SLArray[3]=MinSL_Short;  PrintDesc="MinSL_Short";
      if(High_5>MinSL_Short) {SLArray[4]=High_5; PrintDesc="High_5";}   else SLArray[4]=MinSL_Short;  PrintDesc="MinSL_Short";
     }
/*-------------------------------------------------------------------------------------------------*/
   int MinValue=ArrayMinimum(SLArray,WHOLE_ARRAY,0);
/*-------------------------------------------------------------------------------------------------*/
   switch(MinValue)
     {
      case 0:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
      case 1:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
      case 2:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
      case 3:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
      case 4:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
     }

If there is a difficult way of doing things I will find it.

All I want to do is check and see if any indicator listed or Low/High candle is less than a min stop loss. If so I wish to Print that value.  

I like the Array idea but is it the most efficient way? 

I know nothing (Yet) about defines but I am willing to learn.

I just need the most common sense way of doing this. 

Mladen Rakic
72048
Mladen Rakic  
Donald Gibson:

   string PrintDesc="";
   double MinSL_Long=0;
   double MinSL_Short=0;
   double Psar=iSAR(NULL,PERIOD_CURRENT,0.02,0.2,0);
   double sma21=iMA(NULL,0,21,0,0,0,1);
   double sma34=iMA(NULL,0,34,0,0,0,1);
   double Low_5=Low[iLowest(NULL,0,MODE_LOW,5,1)];
   double High_5=High[iHighest(NULL,0,MODE_HIGH,5,1)];
/*-----------------------------------------------------------------------------------------------*/
   double SLArray[5];
/*-------------------------------------------------------------------------------------------------*/
   if(sma21>sma34)
     {
      MinSL_Long=Bid-(20*pips);
      if(Psar<MinSL_Long)   {SLArray[0]=Psar;   PrintDesc="Psar";}    else SLArray[0]=MinSL_Long;  PrintDesc="MinSL_Long";
      if(sma21<MinSL_Long)  {SLArray[1]=sma21;  PrintDesc="sma21";}   else SLArray[1]=MinSL_Long;  PrintDesc="MinSL_Long";
      if(sma34<MinSL_Long)  {SLArray[2]=sma34;  PrintDesc="sma34";}   else SLArray[2]=MinSL_Long;  PrintDesc="MinSL_Long";
      if(Low_5<MinSL_Long)  {SLArray[3]=Low_5;  PrintDesc="Low_5";}   else SLArray[3]=MinSL_Long;  PrintDesc="MinSL_Long";
      if(High_5<MinSL_Long) {SLArray[4]=High_5; PrintDesc="High_5";}  else SLArray[4]=MinSL_Long;  PrintDesc="MinSL_Long";
     }
   if(sma21<sma34)
     {
      MinSL_Short=Ask+(20*pips);
      if(Psar>MinSL_Short)   {SLArray[0]=Psar;   PrintDesc="Psar";}     else SLArray[0]=MinSL_Short;  PrintDesc="MinSL_Short";
      if(sma21>MinSL_Short)  {SLArray[1]=sma21;  PrintDesc="sma21";}    else SLArray[1]=MinSL_Short;  PrintDesc="MinSL_Short";
      if(sma34>MinSL_Short)  {SLArray[2]=sma34;  PrintDesc="sma34";}    else SLArray[2]=MinSL_Short;  PrintDesc="MinSL_Short";
      if(Low_5>MinSL_Short)  {SLArray[3]=Low_5;  PrintDesc="Low_5";}    else SLArray[3]=MinSL_Short;  PrintDesc="MinSL_Short";
      if(High_5>MinSL_Short) {SLArray[4]=High_5; PrintDesc="High_5";}   else SLArray[4]=MinSL_Short;  PrintDesc="MinSL_Short";
     }
/*-------------------------------------------------------------------------------------------------*/
   int MinValue=ArrayMinimum(SLArray,WHOLE_ARRAY,0);
/*-------------------------------------------------------------------------------------------------*/
   switch(MinValue)
     {
      case 0:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
      case 1:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
      case 2:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
      case 3:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
      case 4:Print("SL_ArrayMinimum  ",DoubleToStr(SLArray[MinValue],Digits)+"  "+PrintDesc);break;
     }

If there is a difficult way of doing things I will find it.

All I want to do is check and see if any indicator listed or Low/High candle is less than a min stop loss. If so I wish to Print that value.  

I like the Array idea but is it the most efficient way? 

I know nothing (Yet) about defines but I am willing to learn.

I just need the most common sense way of doing this. 

Using arrays is the simplest and, as far as code is concerned, most efficient way, but I have a feeling that you are doing it the wrong way (if I understand what are you trying to achieve)
Do something like this :

double SLArray[5];
        SLArray[0] = iSAR(NULL,PERIOD_CURRENT,0.02,0.2,0);
        SLArray[1] = iMA(NULL,0,21,0,0,0,1);
        SLArray[2] = iMA(NULL,0,34,0,0,0,1);
        SLArray[3] = Low[iLowest(NULL,0,MODE_LOW,5,1)];
        SLArray[4] = High[iHighest(NULL,0,MODE_HIGH,5,1)];
        ArraySort(SLArray);
    
And after that you have minimal value in SLArray[0] and maximal value in SLArray[4]. Just compare those to the value you wish it to be compared against
To add comments, please log in or register