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

Building my first strategy...it doesn't execute orders, help!

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

    #16
    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.

    Comment


      #17
      (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:
      Calculate.OnBarClose
      Calculate.OnEachTick
      Calculate.OnPriceChange

      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.

      Comment


        #18
        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.

        Comment


          #19
          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;
          #endregion

          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)
          {
          }
          base.OnStateChange();
          }
          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);
          }}
          }

          Comment


            #20
            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.

            Comment


              #21
              ​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 (hds-us-nt-004.ninjatrader.com/31655)
              and
              05-02-2023 15:04:33 Connection Time to auto close position='00:00:00', Enabled=False

              Comment


                #22
                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.

                Comment


                  #23
                  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

                  Comment


                    #24
                    Thanks for the tip.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by algospoke, Today, 06:40 PM
                    0 responses
                    9 views
                    0 likes
                    Last Post algospoke  
                    Started by maybeimnotrader, Today, 05:46 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post maybeimnotrader  
                    Started by quantismo, Today, 05:13 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post quantismo  
                    Started by AttiM, 02-14-2024, 05:20 PM
                    8 responses
                    168 views
                    0 likes
                    Last Post jeronymite  
                    Started by cre8able, Today, 04:22 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post cre8able  
                    Working...
                    X