Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 frankthearm, Yesterday, 09:08 AM
      12 responses
      43 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by junkone, Today, 11:37 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by quantismo, 04-17-2024, 05:13 PM
      5 responses
      35 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by proptrade13, Today, 11:06 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by love2code2trade, 04-17-2024, 01:45 PM
      4 responses
      35 views
      0 likes
      Last Post love2code2trade  
      Working...
      X