I am using Strategy Builder with On Each Tick
And I want to close my Long position after +/- $2 dollars
I started testing at 13402.25
When I use the strategy in real-time it seems to be working.
For example from the attachment picture
Long Entry on 13402.25 and exit on TP 13403.25 ( not $2 but anyways )
Long Entry on 13404.25 and exit on TP 13406.00 ( closer to $2 )
Long Entry on 13406.25 and exit on TP 13408.00 ( consistent at least )
BUT, as you can see, it doesn't work for past data.
The strategy waits for the bar to close
For example from the attachment picture
Long Entry on 13412.00 and exit on TP 13416.75 ( it waited for the bar to close, it should've closed on 13414.00 )
Long Entry on 13416.75 and exit on SL 13413.50 ( it waited for the bar to close but at least it opened a new position at the same price )
Long Entry on 13406.00 and exit on SL 13401.25 ( it waited for the bar to close, it should've closed on 13404.00 )
When I use the Strategy Analyzer, the same thing happens, the strategy waits for the bar to close, then updates the position
this is a problem because that means I cannot really get real-time results from the Strategy Analyzer.
If I want to know how my strategy does in real-time, I have to run it in real-time. That sounds like it makes sense but
I want to get results from past data
I hope to be wrong. what am I doing wrong? how can I use Strategy Analyzer to get real-time results?
#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class eeeee : Strategy { private double TP; private double SL; private DEMA DEMA1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "eeeee"; Calculate = Calculate.OnEachTick; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; TP = 1; SL = 1; } else if (State == State.Configure) { } else if (State == State.DataLoaded) { DEMA1 = DEMA(Close, 4); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; // Set 1 if ((Position.MarketPosition == MarketPosition.Flat) && (Close[0] > DEMA1[0])) { EnterLong(Convert.ToInt32(DefaultQuantity), @"LONG"); TP = (Close[0] + 2) ; SL = (Close[0] - 2) ; } // Set 2 if ((Position.MarketPosition == MarketPosition.Long) && (Close[0] >= TP)) { ExitLong(Convert.ToInt32(DefaultQuantity), @"TP", @"LONG"); } // Set 3 if ((Position.MarketPosition == MarketPosition.Long) && (Close[0] <= SL)) { ExitLong(Convert.ToInt32(DefaultQuantity), @"SL", @"LONG"); } } } }
Comment