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


Building my first doesn't execute orders, help!

  • Filter
  • Time
  • Show
Clear All
new posts

    Thanks for the reply.

    The stop loss is here: stopLoss = openingRangeLow - (openingRangeHigh - openingRangeLow) * 0.5;

    There is no OnBarUpdate() call because price is not calculated OnBarUpdate(). It's calculated on tick data. If I wait for a bar to close before taking the position, it's often too late and the trade is missed. That's why I used the (MarketDataEventArgs marketDataUpdate) arguments.

    Maybe I should set an OnBarUpdate() = false; argument?
    Last edited by mattlaguardia; 02-05-2023, 01:01 PM. Reason: Add thought.


      (I am just trying to help you finding a solution to your problem)

      I have to insist on the necessity of the OnBarUpdate() in your script.
      Don't confuse the OnBarUpdate() with the Calculate.OnBarClose.

      In NT we can have:

      In your script you can have Calculate.OnEachTick. That's fine, but you can't omit OnBarUpdate().
      Actually, a bar is updating in real time every moment a new tick is coming in, not only in bar close.


        I'll add an OnBarUpdate() method and see if it solves the issue--NO LUCK.

        Edit: I think there is a conflict between OnMarketData and Calculate.OnEachTick. I want to calculate on price changes, so Calculate.OnPriceChange method + OnMarketData seems like the better combination? Calculate.OnEachTick would need to be used with the OnBarUpdate method.

        The issue though, is that neither method actually works. I still get no logs, no ability to add the strategy to a chart, and it doesn't ever take trades in the backtester.

        When I look in the folder after compiling the strategy, I notice that all the other strategies have the @ symbol in front of their name, but the strategy I build in the Editor does not have the @ symbol.
        Last edited by mattlaguardia; 02-05-2023, 01:50 PM. Reason: add thoughts.


          Just for reference, here's the most recent iteration of the code with print statements added:

          region Using declarations
          using System.Linq;
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.ComponentModel.DataAnnotations;
          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;
          using NinjaTrader.NinjaScript.Strategies;

          class CrudeOilOpeningRangeBreakoutStrategy : Strategy
          private DateTime lastTradeTime = DateTime.MinValue;
          private double stopLoss;
          private double profitTarget;
          private int Quantity = 1;
          private bool longPositionOpened = false;
          private bool shortPositionOpened = false;
          private double openingRangeHigh = double.MinValue;
          private double openingRangeLow = double.MaxValue;

          protected override void OnStateChange()
          if (State == State.SetDefaults)
          Description = "Crude Oil Opening Range Breakout Strategy";
          Name = "CrudeOilOpeningRangeBreakoutStrategy";
          Calculate = Calculate.OnPriceChange;
          else if (State == State.Realtime)
          protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
          // Set opening range high and low
          if (Time[0].Hour == 8 && Time[0].Minute == 30)

          openingRangeHigh = High[1];
          openingRangeLow = Low[1];

          stopLoss = openingRangeLow - (openingRangeHigh - openingRangeLow) * 0.5;
          profitTarget = openingRangeHigh + (openingRangeHigh - openingRangeLow) * 0.5;

          Print("Opening Range High: " + openingRangeHigh);
          Print("Opening Range Low: " + openingRangeLow);
          Print("Stop Loss: " + stopLoss);
          Print("Profit Target: " + profitTarget);

          longPositionOpened = false;
          shortPositionOpened = false;
          openingRangeHigh = 0;
          openingRangeLow = 0;
          stopLoss = 0;
          profitTarget = 0;
          // Place a long trade if price rises above the opening range high and no long trade has been placed
          if (marketDataUpdate.Price > openingRangeHigh && !longPositionOpened)
          longPositionOpened = true;
          EnterLong(Quantity, "Long");
          lastTradeTime = Time[0];
          // Place a short trade if price falls below the opening range low and no short trade has been placed
          if (marketDataUpdate.Price < openingRangeLow && !shortPositionOpened)
          shortPositionOpened = true;
          EnterShort(Quantity, "Short");
          lastTradeTime = Time[0];

          // Reset trade flags and range at 2PM
          if (Time[0].Hour == 14);


            After the #endregion , add:

            ​namespace NinjaTrader.NinjaScript.Strategies
            public class CrudeOilOpeningRangeBreakoutStrategy : Strategy
            ...... your code here......
            Last edited by KonstantinosNT; 02-05-2023, 02:42 PM.


              ​OK, I'm not sure how I missed that critical line.

              I made the changes, sorted out a few CS errors, and got the code to compile.

              It shows up in the strategies menu, and I can add it to a chart.

              It still won't take a trade in the backtester, though​. The log shows:

              05-02-2023 15:04:33 Connection Using HDS (
              05-02-2023 15:04:33 Connection Time to auto close position='00:00:00', Enabled=False


                On some further investigation, it looks like there might be a conflict between the last declaration of the Region line:

                using NinjaTrader.NinjaScript.Strategies;

                and the Namespace declaration that follows the Region block:

                namespace NinjaTrader.NinjaScript.Strategies

                Before compiling, there's an error that shows for the Region line:

                Extern alias declarations, using clauses, assembly/module attributes, or namespace/type declarations expected.

                If I compile the code, the error goes away, but the code still isn't quite working properly. It will attach to a chart now, but it still doesn't generate trades in the backtester, and it doesn't really produce much information in the way of logs or print messages.

                UPDATE: I solved the error by moving the Namespace declaration above the Region block.

                Attached Files
                Last edited by mattlaguardia; 02-05-2023, 04:42 PM.


                  Hello mattlaguardia,

                  Moving forward, create your strategy in the NinjaScript Editor. This will automatically create the correct framework with the correct namespace and will automatically have OnBarUpdate().

                  Below is a link to a forum post with helpful resources on getting started with C# and NinjaScript. Please watch the two training videos.
                  Chelsea B.NinjaTrader Customer Service


                    Thanks for the tip.


                    Latest Posts


                    Topics Statistics Last Post
                    Started by trader24211341, 03-09-2023, 12:30 PM
                    2 responses
                    Last Post trader24211341  
                    Started by ct, Today, 04:56 PM
                    0 responses
                    1 view
                    Last Post ct
                    by ct
                    Started by qewcool, 03-19-2023, 05:51 PM
                    24 responses
                    1 like
                    Last Post jama12
                    by jama12
                    Started by GussJ, Today, 07:43 AM
                    3 responses
                    Last Post GussJ
                    by GussJ
                    Started by GussJ, Today, 09:30 AM
                    10 responses
                    Last Post GussJ
                    by GussJ