This thread demonstrates how you can use the AddOrder logic to create your own ThinkorSwim backtesting script. It also contains requests and code examples made by other members—worth looking over.
Hi Everyone,
Inspired by @RobertPayne and this post, for a while, I been trying to replicate the auto functionality in the AddOrder. Seems the behavior is different when from Auto and Manual on how AddOrder is executed.
I'm trying to simulate the buy and sell and simulate the reversal from going long to short or vice-versa in the same bar. Auto it seems the entry is taken at the same bar while manual is on the next bar.
Let's use this as an example:
Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;
AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);
AddOrder(OrderType.BUY_AUTO,no);
In this example, if close > ema then go long else go short, if you're in a long trade and trend changes, reverse and go short, vice-versa.
1) Using Auto:
Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;
AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);
AddOrder(OrderType.BUY_AUTO,no);
##Automatic
AddOrder(OrderType.BUY_AUTO, condition = (STATE == 100), name = "BAUTO: " + BarNumber());
AddOrder(OrderType.SELL_AUTO, condition = (STATE == -100), name = "SAUTO: " + BarNumber());
This works as expected, notice that is opening and closing the position at the same bar with a buy to open and a sell to close, vice-versa.
2) Using Manual
Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;
AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);
##Long
AddOrder(OrderType.BUY_TO_OPEN, condition = (STATE == 100), name = "BTO: " + BarNumber());
AddOrder(OrderType.SELL_TO_CLOSE, condition = (STATE == -100), name = "STC: " + BarNumber());
##Short
AddOrder(OrderType.SELL_TO_OPEN, condition = (STATE == -100), name = "STO: " + BarNumber());
AddOrder(OrderType.BUY_TO_CLOSE, condition = (STATE == 100), name = "BTC: " + BarNumber());
The behavior is different, it executes in the next bar as such:
The reason, is i'm trying to build a strategy that will reverse on trend, but at times, I will pause getting into new positions, but want to close the position when trend change occurs BUT entire in the direction of the trend when that condition is cleared.
Any suggestions or ideas would be greatly appreciated.
Attachments
Last edited by a moderator: