Download MetaTrader 5
To add comments, please log in or register
Write articles and earn money. We pay 200 USD for them!
Donald Gibson
483
Donald Gibson 2016.05.11 21:49 
double StopLoss()
  {
/*-------------------------------------------------------------------------------------------------*/
   double Calc=0;
   double TickDigits=MarketInfo(Symbol(),MODE_TICKSIZE);
   if(TickDigits==0.00001 || TickDigits==0.0001)
      Calc=.001;
   else Calc=.1;
/*-------------------------------------------------------------------------------------------------*/
   double Max_StopLoss=NormalizeDouble(AccountInfoDouble(ACCOUNT_EQUITY)*(2/100)*Calc,Digits);
/*-------------------------------------------------------------------------------------------------*/
//Fast Moving Average
   double FastMA=iMA(Symbol(),0,21,0,0,0,1);
/*-------------------------------------------------------------------------------------------------*/
//Slow Moving Average
   double SlowMA=iMA(Symbol(),0,50,0,0,0,1);
/*-------------------------------------------------------------------------------------------------*/
//Trend Moving Average
   double TrendMA=iMA(Symbol(),0,200,0,0,0,1);
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
   return(StopLoss);
  }

Above is just an example, I haven't written any actual code yet.

Consider this scenario.

I open a pending trade, long/short (doesn't matter). When the order opens I need to modify the order by adding a stop loss. 

I have multiple targets for the stop loss, maximum distance based on a risk/reward ratio, a couple of moving averages, support/resistance levels, Bollinger Bands etc.

The question is, I need to know which target is closest and use it as a stop loss.  

Example... 

I open a long position.

The maximum stop loss according to a risk ratio is 100pips below,the fast moving average is 120pips below, the support level is 75 pips below. 

 

How can I find the closest target without writing a massive number of if statements. 

Keith Watford
Moderator
7906
Keith Watford 2016.05.11 23:52  

You would probably need a minimum stoploss as well.

You could put the values in an array and use ArrayMin for a Sell or ArrayMax for a buy

Fernando Carreiro
1451
Fernando Carreiro 2016.05.12 06:53  
GumRai:

You would probably need a minimum stoploss as well.

You could put the values in an array and use ArrayMin for a Sell or ArrayMax for a buy

I disagree with ArrayMax/Min solution, because some of the values may be on the opposite end of the open price (e.g. Above the open of a Buy Order or Below the Open of a Sell Order) .

I do agree however, that an Array is possibly the simplest solution for reducing code lines, but I would create my own function, to go through the array comparing the values but taking into account the open price and direction of the order and thus finding the closest that does not violate the Minimum Stop allowed.

A more elegant but complex solution would be the use of "classes".

Donald Gibson
483
Donald Gibson 2016.05.12 06:54  
GumRai:

You would probably need a minimum stoploss as well.

You could put the values in an array and use ArrayMin for a Sell or ArrayMax for a buy

The idea presented to me is to use the Risk/Reward ratio as a comparison. 

Donald Gibson
483
Donald Gibson 2016.05.12 07:24  
FMIC:

I disagree with ArrayMax/Min solution, because some of the values may be on the opposite end of the open price (e.g. Above the open of a Buy Order or Below the Open of a Sell Order) .

I do agree however, that an Array is possibly the simplest solution for reducing code lines, but I would create my own function, to go through the array comparing the values but taking into account the open price and direction of the order and thus finding the closest that does not violate the Minimum Stop allowed.

A more elegant but complex solution would be the use of "classes".

Great, I will look into the Array type of solution. I don't know a thing about classes. As far as programming goes I have studied migrating from mql4 to mql5, but right now my time is taken up by another platform.
Donald Gibson
483
Donald Gibson 2016.05.14 12:12  
FMIC:

I disagree with ArrayMax/Min solution, because some of the values may be on the opposite end of the open price (e.g. Above the open of a Buy Order or Below the Open of a Sell Order) .

I do agree however, that an Array is possibly the simplest solution for reducing code lines, but I would create my own function, to go through the array comparing the values but taking into account the open price and direction of the order and thus finding the closest that does not violate the Minimum Stop allowed.

A more elegant but complex solution would be the use of "classes".

Instead of writing an EA I chose a Custom Indicator instead, just for display purposes only. 

Works fine with max and min values, but I am not sure how to proceed with the comparison of the different values. I wish to use a percentage of an account as a maximum value for comparison. If any of the values are less than the maximum, display it . 

Can someone point me in the right direction, comparison of the values with regard to the direction of the last candle? 

Thanks advance

Fernando Carreiro
1451
Fernando Carreiro 2016.05.14 12:43  

Why this:

ArrayFill(Choose_SL,0,1,Max_SL);
ArrayFill(Choose_SL,1,1,FastMA);
ArrayFill(Choose_SL,2,1,SlowMA);
ArrayFill(Choose_SL,3,1,TrendMA);

When this is simpler and faster:

Choose_SL[0] = Max_SL;
Choose_SL[1] = FastMA;
Choose_SL[2] = SlowMA;
Choose_SL[3] = TrendMA;

Or alternatively, just assign the values directly:

Choose_SL[1]=iMA(Symbol(),0,Fast_MAperiod,Fast_MAindshift,Fast_MAcalcMethod,Fast_MAappliedprice,Fast_MAbufferShift);
Choose_SL[2]=iMA(Symbol(),0,Slow_MAperiod,Slow_MAindshift,Slow_MAcalcMethod,Slow_MAappliedprice,Slow_MAbufferShift);
Choose_SL[3]=iMA(Symbol(),0,Trend_MAperiod,Trend_MAindshift,Trend_MAcalcMethod,Trend_MAappliedprice,Trend_MAbufferShift);

Also, I warned about NOT using the "ArrayMinimum()" or "ArrayMaximum()", because depending on the strategy, the values of the moving averages may be on the opposite end of the order open price. Rather make your own function to go over every value in the array and make a decision based on min/max but taking into account whether it is on the correct side of the open price. Only in the special case, that your strategy GUARANTEES that the moving averages are ALWAYS on the correct side, can you then use "ArrayMaximum()" and "ArrayMinimum()" to find the closest match.

To calculate the risk based on certain stop-loss size, you have to use both the TICK_SIZE and TICK_VALUE to calculate the desired number of lots. I pulled this off another thread. The credit goes to WHRoeder:

WHRoeder:
  1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  3. Do NOT use TickValue by itself - DeltaPerlot
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out
Donald Gibson
483
Donald Gibson 2016.05.14 14:41  
FMIC:

Why this:

When this is simpler and faster:

Or alternatively, just assign the values directly:

Also, I warned about NOT using the "ArrayMinimum()" or "ArrayMaximum()", because depending on the strategy, the values of the moving averages may be on the opposite end of the order open price. Rather make your own function to go over every value in the array and make a decision based on min/max but taking into account whether it is on the correct side of the open price. Only in the special case, that your strategy GUARANTEES that the moving averages are ALWAYS on the correct side, can you then use "ArrayMaximum()" and "ArrayMinimum()" to find the closest match.

To calculate the risk based on certain stop-loss size, you have to useboth the TICK_SIZE and TICK_VALUE to calculate the desired number oflots. I pulled this off another thread. The credit goes to WHRoeder:

WHRoeder:
  1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  3. Do NOT use TickValue by itself - DeltaPerlot
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out

Ok, thanks for the push in the right direction. 

/
To add comments, please log in or register