With NinjaScript, you accomplish this with conditional logic, usually if statements. OnBarUpdate is a "clock signal", meaning that it will trigger over and over again as time goes on. Every tick or bar has a unique time that it triggers. You can test every bar's trigger time to see if it is within a range, and only perform tasks if this test passes.
I am including a link to the ToTime() section of the Help Guide.
I will also adapt its sample code to cover the three situations you mentioned.
// Only trade between 18:00 and 16:00 (so not between 16:00 and 18:00) if (! (ToTime(Time[0]) > 160000 && ToTime(Time[0]) < 180000)) { if(MarketPosition == MarketPosition.Flat) { EnterLong(); } }
// Only trade between midnight and 16:00, or between 18:00 and midnight if (ToTime(Time[0]) <= 160000 || ToTime(Time[0]) >= 180000) { if(MarketPosition == MarketPosition.Flat) { EnterLong(); } }
// Close all positions at 16:10 if (ToTime(Time[0]) >= 161000) { ExitLong(); ExitShort(); }
If we need this to flatten exactly at 16:10, we can provide this guarantee by setting CalculationMode to Ticks (or CalculateOnBarClose = false in NT7).
Leave a comment: