I am creating a simple strategy for testing, based on a MACD cross, entering long when the fast period crosses above the slow period & vice versa
I am also plotting a triangle up or down based on the same logic in the OnBarUpdate method, however the EnterLong order is placed at the next bar open, after the plot is displayed. I want the EnterLong order to be placed bar open price, where the triangle is plotted.
Please see the code I'm using below, where you can see the plots defined & also the strategy logic
AddPlot(new Stroke(Brushes.Crimson, 10), PlotStyle.TriangleDown, "Short"); AddPlot(new Stroke(Brushes.Lime, 10), PlotStyle.TriangleUp, "Long"); } else if (State == State.Configure) { SetProfitTarget(CalculationMode.Ticks, 450); SetStopLoss(CalculationMode.Ticks, 300); } else if (State == State.DataLoaded) { MACD1 = MACD(FastPeriod, SlowPeriod, Smooth); AddChartIndicator(MACD1); } } protected override void OnBarUpdate() { //Add your custom strategy logic here. if (CurrentBars[0] < BarsRequiredToTrade) return; // Long Entry /*if (CrossAbove(MACD1.Default, MACD1.Avg, 1)) { EnterLong(); Long[0] = Low[0] - TickSize * 100; }*/ if (MACD1.Default[1] < MACD1.Avg[1] & MACD1.Default[0] >= MACD1.Avg[0]) { EnterLong(); Long[0] = Low[0] - TickSize * 100; } // Short Entry if (CrossBelow(MACD1.Default, MACD1.Avg, 1)) { EnterShort(); Short[0] = High[0] + TickSize * 100; } }
I'm sure it must be some kind of config on how I'm calculating or something.
I've tried using the CrossAbove function but also using a simple calculation to make sure there wasn't any difference, you can see that in the commented code.
I'd be grateful if you could let me know how to achieve this thanks
Gav
Comment