Is "if statement" possible using AddOrder



New member
In OnDemand TOS back testing is it possible to use an if statement for an AddOrder?

Something that would, for example, sell short every cross-below but buy long every cross-above (and closes trade when it crosses the other way): all in one TOS strategy

Something like:

def cross-above= cross above EMA;
def cross-below = cross below EMA;

if cross-above then AddOrder(OrderType.BUY_TO_OPEN, cross-above, open[-1], 100) else if cross-below then AddOrder(OrderType.SELL_TO_OPEN, cross-below, open[-1], 100) else no;
if close > EMA then AddOrder(OrderType.SELL_TO_CLOSE, cross-below, open[-1], 100) else if close < EMA then AddOrder(OrderType.BUY_TO_CLOSE, cross-above, open[-1], 100) else no;


Well-known member
Don't think that is a valid syntax and from all the different AddOrder statements I have seen, it is generally used without any if-then-else construct. Probably best to vary the condition upon which a buy/sell order is triggered, so if the condition is not triggered, no buy/sell order is effected.
Last edited:


New member
Thank you - is there any way to back test 2 (separate) strategies at the same time using TOS OnDemand back testing?


New member
One way to do it is to keep your logic out of AddOrder statement. Here is an example:

input EMALenShort = 9;
input EMALenLong = 20;

def EMAPlus = (MovAvgExponential(close, EMALenShort) - MovAvgExponential(close, EMALenLong)) >= 0;

AddOrder(OrderType.BUY_AUTO, EMAPlus, open[1], tickcolor = GetColor(0), arrowcolor = GetColor(0),name = "BAuto: " + BarNumber());
AddOrder(OrderType.SELL_AUTO, !EMAPlus, open[1], tickcolor = GetColor(1), arrowcolor = GetColor(1),name = "SAuto: " + BarNumber());

Also note BUY_AUTO and SELL_AUTO will automatically switch when EMAPlus goes from positive to negative and vv.

Similar threads