Download MetaTrader 5
To add comments, please log in or register
Deposit to your account via Visa QIWI Wallet
David Francis
86
David Francis 2016.11.22 14:23 

Below is a short piece of code as I try IF statement for the first time MQL4 - It takes the difference between he high and low of the range to determine the stop loss value in points.  Currently, this is only running for specified pairs (but I will amend it to run on any pair at some point and work on the digits issue)

 I want to be able to carry out certain actions if the stop loss value is greater then 50 pips (for example) and leave as is if less than 50 pips.

When I run the below, it prints the StopLossDiff value and then the MISSED THE IF STATEMENT, but processes none of the IF statement.  No errors appear on the compile of the script, so what have I done wrong on this one?

 Thanks for any answers in advance. 

double StopLossDiff = High_Range - Low_Range;
double StopLossTarget = 0.0050;

   Print ("Stop Loss in Points=",StopLossDiff);

   if (StopLossDiff > StopLossTarget)
      {
      Print ("STOP LOSS ERROR");
      }
   else if (StopLossDiff > StopLossTarget)
      {
      Print ("STOP LOSS GOOD");
      }

Print ("MISSED THE IF STATEMENT");
Alain Verleyen
Moderator
29414
Alain Verleyen 2016.11.22 14:39  
David Francis:

Below is a short piece of code as I try IF statement for the first time MQL4 - It takes the difference between he high and low of the range to determine the stop loss value in points.  Currently, this is only running for specified pairs (but I will amend it to run on any pair at some point and work on the digits issue)

 I want to be able to carry out certain actions if the stop loss value is greater then 50 pips (for example) and leave as is if less than 50 pips.

When I run the below, it prints the StopLossDiff value and then the MISSED THE IF STATEMENT, but processes none of the IF statement.  No errors appear on the compile of the script, so what have I done wrong on this one?

 Thanks for any answers in advance. 

double StopLossDiff = High_Range - Low_Range;
double StopLossTarget = 0.0050;

   Print ("Stop Loss in Points=",StopLossDiff);

   if (StopLossDiff > StopLossTarget)
      {
      Print ("STOP LOSS ERROR");
      }
   else if (StopLossDiff > StopLossTarget)
      {
      Print ("STOP LOSS GOOD");
      }

Print ("MISSED THE IF STATEMENT");
Same condition, same result.
InvestSoft
8009
Janusz Trojca 2016.11.22 14:39  

You have two identical conditions:

(StopLossDiff > StopLossTarget)

 Try this code:

if (StopLossDiff > StopLossTarget)
      {
      // execute when StopLossDiff is greater than StopLossTarget
      Print ("STOP LOSS ERROR");
      }
   else
      {
      // execute when StopLossDiff is lower or equal than StopLossTarget
      Print ("STOP LOSS GOOD");
      }


 

David Francis
86
David Francis 2016.11.22 14:43  
Alain Verleyen:
Same condition, same result.
Thanks - one of those looking at it and not seeing the flaming obvious.
whroeder1
13929
whroeder1 2016.11.22 14:57  
David Francis:

It takes the difference between he high and low of the range to determine the stop loss value in points

I want to be able to carry out certain actions if the stop loss value is greater then 50 pips (for example) and leave as is if less than 50 pips.

double StopLossDiff = High_Range - Low_Range;
double StopLossTarget = 0.0050;
Print ("Stop Loss in Points=",StopLossDiff);
  1. You are not converting anything to a value ($.) Just getting the range (0.00xyz.) Value would require lots, range and DeltaPerLot.
    • 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/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip)
    • 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
  2. You are not getting it in points either. You'd have to divide by _Point to get points xy.z.
  3. You are hard coding 50 pips (0.0050) thus the code breaks on any JPY pair.
    double   pip        = StringFind(_Symbol,"JPY") < 0 ? 0.01 : 0.0001;
    int      slippage   = 3 * int(pip / _Point); // Adjust SL/TP and slippage.
    double   StopLossTarget = 50 * pip;
/
To add comments, please log in or register