coding in ninjascript is really not the easiest thing in the world. in nt7 i have been able to achieve some fairly basic things like having indicators change colors, setting a fixed width for them, modifying some sample strategies to create some elementary strategies of my own and so on.
the most complicated piece of code i have tried to put together would be a custom kind of trailing stop. this stop would be progressively moved in case price went in my favor but not tick by tick (which follows price too tightly and leads to most winning trades being killed prematurely) but rather in medium sized increments that could be adjusted for every instrument and even optimized if need be. such a piece of code turns out to be quite a challenge in ninjascript, i had to include iorder execution and other special commands and even needed the help of a programmer friend of mine in order to be able to finish this short script and get it to compile.
i would appreciate the assistance of nt's client support personnel or other users to modify this code as necessary so that it:
1 - would open a long trade if the conditions for an entry are present
2 - places an initial stop loss order a number of ticks below my entry price
3 - moves this stop as indicated if the position is still open and price is moving favorably
4 - after the last increment (pf06), introduces a regular trailing stop (tstops01 ticks behind) to follow my position if it continues to move favorably
5 - in case any of the multiple stops was triggered, the strategy should reset all values, cancel all orders and go back to its initial state to wait for the entry conditions to be present again to open another position
this is the code i developed for nt7, it does compile and run but it does not deliver the desired results at all. one of the malfunctions that can occur is for the strategy to place one single first trade and then zero further trades even when the conditions for subsequent entries are present. i only include the long commands as i think i would be able to invert these commands for short trades on my own once the strategy works as intended.
i would also like to know what kind of modifications would be necessary for a similar custom trailing stop to work on nt8 as i intend to perform some extensive tests on both nt7 and nt8.
thanks a lot, all the best, regards.
#region Variables private int pf01= 10; private int pf02= 16; private int pf03= 20; private int pf04= 24; private int pf05= 30; private int pf06= 45; private int ps01 = 1; private int stops01 = 6; private int stops02 = 2; private int stops03 = 6; private int stops04 = 10; private int stops05 = 14; private int stops06 = 20; private int tstops01 = 15; private IOrder p01 = null; private double pp01; #endregion protected override void OnBarUpdate() { if ( CrossAbove(SMA(Fast), SMA(Slow), 1) && Position.MarketPosition == MarketPosition.Flat && p01 == null ) { p01 = EnterLong(ps01, "p01"); SetStopLoss("p01", CalculationMode.Price, pp01 - stops01 * TickSize, false); } if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf01*TickSize && Position.MarketPosition == MarketPosition.Long) { SetStopLoss("p01", CalculationMode.Price, pp01 + stops02 * TickSize, false); } if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf02*TickSize && Position.MarketPosition == MarketPosition.Long) { SetStopLoss("p01", CalculationMode.Price, pp01 + stops03 * TickSize, false); } if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf03*TickSize && Position.MarketPosition == MarketPosition.Long) { SetStopLoss("p01", CalculationMode.Price, pp01 + stops04 * TickSize, false); } if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf04*TickSize && Position.MarketPosition == MarketPosition.Long) { SetStopLoss("p01", CalculationMode.Price, pp01 + stops05 * TickSize, false); } if ( Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > pf05*TickSize { SetStopLoss("p01", CalculationMode.Price, pp01 + stops06 * TickSize, false); } if (CrossBelow(SMA(Fast), SMA(Slow), 1)) EnterShort(ps01, "p01"); } protected override void OnExecution(IExecution execution) { if(Position.MarketPosition == MarketPosition.Flat) p01 = null; pp01 = 0; if(p01 != null && p01 == execution.Order && Position.MarketPosition == MarketPosition.Long) pp01 = execution.Price; }
Comment