onPositionOpened
Hi guys,
I'm using onPositionOpened to setup stop orders and to do some other tasks just like in Tutorial03. However it seems that onPositionOpened is only called once throughout the whole backtest, in Tutorial03 and also my own strategy. Shouldn't it be called for every buy(.) and sell(.) call?
thanks
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Mathieu on 12 Apr, 2010 08:00 AM
Hi Jon,
onPositionOpen, as its name suggest, is called when your position goes from flat to anything not flat.
Beware that it is not called when the position grows or shrinks, but not from flat. For instance, suppose you have 4 CAC contracts, and buy 1. Your position goes from 4 to 5, thus onPositionOpen doest NOT get called.
Conversly, onPositionClosed gets called from anything not zero to zero.
And the last one, onPositionChanged, gets called everytime the position changes. Again, it doesn't get called if some orders got executed but did not change position. For instance, lets suppose you are flat, you buy 1, and at the same time, a remaining stop sells 1. The position is 0 + 1 - 1 = 0, and onPositionChange doesn't get called.
When designing this API, we have decided including helper methods only for what the quant is not responsible. You are not responsible for order execution, thus we provide these three methods to help you react to position changes.
But as you are responsible for order creation, you can interact directly where you create your order. For instance, instead of directly using sell(1), you could create your own method, lets say sellOneWithStop() and implement sell(1) and buyStop(1, level) by yourself.
Hope this helps.
Mathieu
Mathieu closed this discussion on 12 Apr, 2010 08:00 AM.
Jon re-opened this discussion on 12 Apr, 2010 06:50 PM
3 Posted by Jon on 12 Apr, 2010 06:50 PM
HI Mathieu,
Thanks for your answer, very helpful once again.
David closed this discussion on 12 Apr, 2010 07:18 PM.