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 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");
You have two identical conditions:
Try this code:
{
// 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");
}
Same condition, same result.
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 StopLossTarget = 0.0050;
Print ("Stop Loss in Points=",StopLossDiff);
- 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
- You are not getting it in points either. You'd have to divide by _Point to get points xy.z.
- 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;
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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 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");