Offset of 1 required for condition orders

msammons

Member
I realize that when converting a strategy into a conditional order it is necessary to offset the strategy by one in the conditional order (so the conditional buy order looks at the close of the last candle for the trigger and executes at the open of the next candle, just like the strategy back-test does).

So assume the strategy for back-testing I have is this:

def s = StochasticFull();
def buy = s<10 and close<close[1];
plot z = buy;
AddOrder(OrderType.BUY_TO_OPEN, buy, open[-1], 100);

I tried to convert that to a conditional order:

def s = StochasticFull();
def buy = s<10 and close<close[1];
plot z = buy;

This conditional order triggers "before" the last candle has closed (thus not matching the results from the back-test).

But to make the conditional order actually execute at the same point as the strategy I read you must add an offset of 1. The TOS Conditional Wizard to add the offset adds "from 1 bars ago" - but when I add that to my conditional order (which is not built from the Wizard) I get a red error message.

My question is: Can I accomplish the necessary offset of one bar by using the following?:

def s = StochasticFull();
def buy = s[1]<10 and close[1]<close[2];
plot z = buy;

Would adding the [1] to s and close (an offset back additional one bar) cause the conditional buy order to trigger at the same point as the strategy back-testing?
 
Last edited:

rad14733

Well-known member
VIP
@msammons You should be able to look back at close[2] in conditional orders without issue but I haven't actually tried it... And I'm not sure whether testing with OnDemand would answer that question or not...
 

msammons

Member
I guess the most concise question is:

Is "def buy = s<10 and close<close[1] from 1 bars ago" equal to "def buy = s[1]<10 and close[1]<close[2]"

I do not think you can do conditional orders with OnDemand, so live trading is necessary to test - I just needed some reassurance in my theory before throwing out there live (I took your reply as agreeing with my basic theory that they are the same).
 

msammons

Member
@msammons I seem to recall Pete Hahn of Hahn-Tech.com covering something along these lines in his Thinkorswim AutoTrade Almost forum topic... It might be worth watching if you haven't already done so...
I have watched that very informative video (several times) - the problem is that he did the offset through the Condition Wizard (which adds "from one bars ago"); and for some reason I cannot simply add that addition phrase to my conditional order (get red error message). Unfortunately he never showed any code written from scratch dealing with the offset required to shift the trigger signal 1 bar.
 

rad14733

Well-known member
VIP
I have watched that very informative video (several times) - the problem is that he did the offset through the Condition Wizard (which adds "from one bars ago"); and for some reason I cannot simply add that addition phrase to my conditional order (get red error message). Unfortunately he never showed any code written from scratch dealing with the offset required to shift the trigger signal 1 bar.
Ok... Was just a thought because I have also watched it more than a few times in the past but not recently... Maybe I can watch it yet again later... As I just posted in another topic, I keep getting interrupted this afternoon...
 

Similar threads

Top