Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EnterLong Order is created 1 bar after plot of the same logic??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    EnterLong Order is created 1 bar after plot of the same logic??

    Hi

    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

    Code:
    				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've also added a screen shot where you can see the chart from the Strategy Analyser.

    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
    Attached Files

    #2
    Hi

    I've actually managed to work through this in terms of the order & why the order is placed on the bar that it is, what I don't understand now though, is why the plot is placed one bar behind

    If you could explain that would be great

    Comment


      #3
      Hello Gav_G,

      Thank you for your note.

      Assuming you’re running your strategy with calculate set to OnBarClose, then when the bar which is being evaluated closes, if the condition is true, will fire an order on the next bar.

      See Calculate Section of our helpguide,


      If you are running your strategy with Calculate set to OnEachTick, then on each tick of data you’re conditions will be evaluated for true or false. This setting would fire your order on the current bar in real time. If you are testing with calculate set to OnEachTick, historically, you would need to pull in a secondary data series for the strategy to act historically as it would in real time.

      See the following explanation,


      You may be interested in the following sample for pulling in a secondary tick series,


      Please let us know if you need further assistance.
      Alan P.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      64 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      139 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X