English

Question for those smarter than I

 106 4x_Gypsy 2016.05.11 23: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.
 1200 FMIC 2016.05.14 14: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: 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. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD) Do NOT use TickValue by itself - DeltaPerlot You must normalize lots properly and check against min and max. You must also check FreeMargin to avoid stop out