My strategy updates an int "T" in each call to OnBarUpdate() and then attempts to place orders which would make the Position.Quantity and Position.Side equal Math.Abs(T) and Math.Sign(T) [notwithstanding that MarketPosition is not an int].
I.e. I want the strategy Position (and ultimately the account position) to track T.
But when I try to do so, NT sometimes completly ignores my calls to ExitShort and ExitLong. But it does not always ignore them. I have not worked out what factors influence whether or not the calls are ignored.
The output below shows the problem being generated by the example code below that. The program did output the string "ExitShort(9001)", so it must have called the function ExitShort(9001). The Position was 9001 short, so ExitShort(9001) should have closed the position.
My question is... why was the call to ExitShort(9001) completely ignored by NT? There was not output to the log tab and no other indication of an error.
Also, is there an easier way to make the strategy position track T?
Thanks,
Matthew.
Output from program illustrating that ExitShort(9001) was called but not executed.... [no order was placed into the order list - it is not just that the order did not get filled, there is no ticket to fill].
...
T:0
P:-9001
oldT:-9001
D:9001
C:False
ExitShort(9001)
T:10000
P:-9001
oldT:0
D:19001
C:True
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EnterLong(10000)
...
*/
#region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Indicator; using NinjaTrader.Gui.Chart; using NinjaTrader.Strategy; #endregion namespace NinjaTrader.Strategy { [Description("Enter the description of your strategy here")] public class trackPosition : Strategy { #region Variables #endregion protected override void Initialize() { CalculateOnBarClose = true; } int oldT = 0; protected override void OnBarUpdate() { int T = (int) Math.Floor( 1000.0 * (Close[0]-Close[1]) / TickSize); // target position int P = Position.Quantity; int side = 0; switch (this.Position.MarketPosition) { case MarketPosition.Short : { side = -1; break;} case MarketPosition.Flat : { side = 0; break;} case MarketPosition.Long : { side = 1; break;} } P = P * side; int D = T - P; bool C = (Math.Sign(P) != Math.Sign(T)) && (T != 0); Print(" "); Print("T:" + T); // target position Print("P:" + P); // Current position Print("oldT:" + oldT); // what P should be Print("D:" + D); // Change required Print("C:" + C); // Cross to other side? if(P != oldT) Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); oldT = T; if(T > 0) { if (C) { Print ( "EnterLong("+T+")"); EnterLong(T); } else { if ( D > 0 ) { Print ( "EnterLong("+D+")"); EnterLong(D); } if ( D < 0 ) { Print ( "ExitLong("+(-D)+")"); ExitLong(-D); } } } if(T < 0) { if (C) { Print ( "EnterShort("+(-T)+")"); EnterShort(-T); } else { if ( D > 0 ) { Print ( "ExitShort("+D+")"); ExitShort(D); } if ( D < 0 ) { Print ( "EnterShort("+(-D)+")"); EnterShort(-D); } } } if(T == 0) { if (P>0) { Print("ExitLong("+(-D)+")"); ExitLong(-D); } if(P<0) { Print("ExitShort("+D+")"); ExitShort(D); } } } #region Properties #endregion } }
Comment