Why is the first code attached isn't working when there is no mistake in the syntax while the second code attached is working despite no perceived differences
they are not the same piece of code so comparing is not useful.
you say does not work, what does that mean, does not compile? does not trade? crashes the terminal?
they are not the same piece of code so comparing is not useful.
you say does not work, what does that mean, does not compile? does not trade? crashes the terminal?
They are the same, i just declared emaTrend() inside the entrySignal; No compiler errors, but the first code is not doing the purpose of emaTrend() while the second code does.
They are the same, i just declared emaTrend() inside the entrySignal; No compiler errors, but the first code is not doing the purpose of emaTrend() while the second code does.
based on what you have provided and said;
the first instance you have an extra variable entrySignal
but no evidence that you are resetting before you check it with emaTrend().
you should set it to say 0 before doing the emaTrend() check and instead of two IFs you should use else if
but why bother the second format is more efficent anyway... you could also swap out the "up trend" "downtrend" with an enumerator for better efficiency.
- www.mql5.com
if(PositionsTotal() == 0){ string trend = emaTrend(); if(trend == "uptrend") { ticketOrder = trade.Buy(lotsize,_Symbol,Ask,Ask-(stopLoss*getPipValue()),Ask+(takeProfit*getPipValue())); } else if(trend == "downtrend") { ticketOrder = trade.Sell(lotsize,_Symbol,Bid,Bid+(stopLoss*getPipValue()),Bid-(takeProfit*getPipValue())); } }
based on what you have provided and said;
the first instance you have an extra variable entrySignal
but no evidence that you are resetting before you check it with emaTrend().
you should set it to say 0 before doing the emaTrend() check and instead of two IFs you should use else if
but why bother the second format is more efficent anyway... you could also swap out the "up trend" "downtrend" with an enumerator for better efficiency.
Or you could simplify further:
What is the advantage of using else if? I tried using else if and if but it seems like there is no difference at all, do you mind enlightening me on this one.
simple...
in your case you call emaTrend() twice but using ELSE IF you would only call it the second time if the first IF was untrue, so more efficient
there is also the fact that the return value of emaTrend() may change between your two IF statements making them both true, so you would over-ride the first outcome - although maybe not an issue for you in this case in other scenarios this may cause issues because you are not controlling outcomes properly.
but you asked why your two versions were not working, the rest has been about efficiency and discipline. Your uncontrolled initial variable entrySignal was most likely your problem as I mentioned before, but as we don't have all your code we can't confirm only you can.
and if you like efficency then don't use strings as flags unless you have to, they take a lot more resources to check so just enumerate them- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Why is the first code attached isn't working when there is no mistake in the syntax while the second code attached is working despite no perceived differences