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

Requesting help with the Strategy Wizard

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

    Requesting help with the Strategy Wizard

    Good day Ninja Forum, this is the first time I'm writing a post here.
    In the past I've read through many threads, but only now I've decided to request help with a piece of code from the Strategy Wizard.

    The code right now is as follows...
    HTML Code:
            {
                // Condition set 1
                if (Close[0] > Open[0])
                {
                    EnterLong(DefaultQuantity, "LongEntry");
                    ExitShort("ShortExit", "ShortEntry");
                }
    
                // Condition set 2
                if (Close[0] < Open[0])
                {
                    EnterShort(DefaultQuantity, "ShortEntry");
                    ExitLong("LongExit", "LongEntry");
                }
    
            }
    It's a very simple code and it executes perfectly, but I want it to do something specific that I haven't managed to figure out yet.

    This code will enter a Long trade whenever a green Renko bar is formed, and it will exit a Long trade and enter a Short trade when a red Renko bar is formed, and vice versa.

    But what I want it to do is not enter one trade for only one bar, I want the strategy to enter a Long trade for every consecutive green Renko bar that is formed until one single red Renko bar is formed.

    Right now it only enters one Long trade whenever one green Renko bar is formed, and it maintains this Long position until the next red Renko bar is formed, even if 10 other green Renko bars are formed.

    How would I go about to make it repeat itself like that?

    Thank you for your time, looking forward to the replies.

    Otis.

    #2
    Hello Otis,

    Welcome to the NinjaTrader forums!

    To continue placing orders in the same direction, either the EntryHandling must
    be set to UniqueEntries and the orders must use unique tag names, or you can have EntryHandling set to AllEntries and EntriesPerDirection set to the highest number of orders you would like to be allowed in a direction. (So you could set that to a very high number to allow a high number of orders)

    That said, when using the managed approach, calling an entry order will automatically close a position if the order is in the opposite direction.

    In your code you are calling EnterLong() and ExitShort() on the same bar. This will cause issues.

    When calling an entry method in the opposite direction of your position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.

    If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. This will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

    The result is that either the script will double the quantity when it reverses or it will cause an overfill and stop the script.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea, thank you for your reply.

      I've gone ahead and tried some of the things you mentioned but I don't think that resulted in what I want to achieve. This time I will include a chart image of what the Strategy is doing so far.



      As you can see the Strategy reverses with each new color bar formed, in its respective direction. But that's all it does, and it maintains the single position until an opposite brick is formed. What I'd like my Strategy to do is to enter a Long trade for every single green brick that is formed until a single red brick is formed, and then it continues to enter Short trades for each red brick unless a new green brick is formed. So it would open new trades along the direction the market is moving. Any idea how I would achieve this goal?

      Thank you,
      Otis.

      Edit: I tried uploading an image with the [IMG] tags and a link from Imgur, but it doesn't seem to work. Is there any other method I can upload images here?
      Last edited by otislauwaert; 11-22-2017, 09:07 AM.

      Comment


        #4
        Hello Otis,

        To clarify, the strategy is placing a single order in a direction, is this correct?

        You are wanting a new order for each new bar, is this also correct?

        May I confirm you have tried setting Entry Handling to UniqueEntries and used a new signal name for each new order and this has not accomplished your goal of placing a new order in the direction of the bar for each new bar?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea.

          Yes, the strategy should place a single order for each new bar, into the respective long/short direction, but it doesn't yet.

          Yes, I want one new order entry for each newly formed bar. For an example: The market makes a Long move of 10 renko bricks, then it should have 10 orders going Long for each established brick. Once an opposite Short brick is formed it should exit all 10 former Long orders and then continue into the other direction.

          Within the Strategy Wizard I have entered multiple OrderEntry actions for the Long trades and gave each of them a unique name. Going from ''LongEntry1'' to ''LongEntry5''. Then I have activated the strategy with the UniqueEntries, but this still only showed one trade for each direction.

          Here's the image that I included in my former reply: https://imgur.com/a/wUpgP

          Comment


            #6
            Hello otislauwaert,

            May I see the code you have written as well as a screenshot of the Strategy parameters in the Strategy Window (showing the Order handling section)?

            Your screenshot is showing the entry order name as LongEntry.

            Are you certain that you have modified the code to use LongEntry1?

            If so, have you removed the instance of the script and added a new instance?
            Are you certain you are modifying the correct script?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Here is the full code:

              {
              /// <summary>
              /// Enter the description of your strategy here
              /// </summary>
              [Description("Enter the description of your strategy here")]
              public class Coromandel : Strategy
              {
              #region Variables
              // Wizard generated variables
              private int entriesPerDirection = 5555; // Default setting for EntriesPerDirection
              // User defined variables (add any user defined variables below)
              #endregion

              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
              CalculateOnBarClose = true;
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              // Condition set 1
              if (Close[0] > Open[0])
              {
              EnterLong(DefaultQuantity, "LongEntry");
              }

              // Condition set 2
              if (Close[0] < Open[0])
              {
              EnterShort(DefaultQuantity, "ShortEntry");
              }

              }

              #region Properties
              [Description("")]
              [GridCategory("Parameters")]
              public int EntriesPerDirection
              {
              get { return entriesPerDirection; }
              set { entriesPerDirection = Math.Max(1, value); }
              }
              #endregion
              }
              }
              And here is an Imgur album with 3 screenshots from things you've requested: https://imgur.com/a/f6V8N

              As you can see all 5 Long entries happen at the same signal, instead of one for each newly formed Renko bar.

              Comment


                #8
                Hello otislauwaert,

                I'm not showing that you are using unique signal names in the code you have posted. The same signal name is used for every order. You have used the signal name "LongEntry".

                Below is a link to a forum post that demonstrates using a counter to make unique signal names.


                Also, you will need to set EntriesPerDirection (which is a NinjaScript property) in Initialize().

                Below is a publicly available link to the help guide.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Chelsea, thanks for your reply.

                  I've tried to implement the information you gave me into my strategy, and this is what I resulted with.

                  #region Using declarations
                  using System;
                  using System.ComponentModel;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Data;
                  using NinjaTrader.Indicator;
                  using NinjaTrader.Gui.Chart;
                  using NinjaTrader.Strategy;
                  #endregion

                  // This namespace holds all strategies and is required. Do not change it.
                  namespace NinjaTrader.Strategy
                  {
                  /// <summary>
                  /// Enter the description of your strategy here
                  /// </summary>
                  [Description("Enter the description of your strategy here")]
                  public class Coromandel : Strategy
                  {
                  #region Variables
                  // Wizard generated variables
                  private int entriesPerDirection = 5555; // Default setting for EntriesPerDirection
                  // User defined variables (add any user defined variables below)
                  #endregion

                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  /// </summary>
                  protected override void Initialize()

                  }
                  private int ordersCount = 0;
                  {
                  {
                  EntriesPerDirection = 1;
                  EntryHandling = EntryHandling.UniqueEntries;

                  }
                  {
                  CalculateOnBarClose = true;
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  // Condition set 1
                  if (Close[0] > Open[0])
                  {
                  ordersCount++;
                  EnterLong(1, LongEntry+ordersCount.ToString());
                  }

                  // Condition set 2
                  if (Close[0] < Open[0])
                  {
                  ordersCount++;
                  EnterShort(1, ShortEntry+ordersCount.ToString());
                  }

                  }

                  #region Properties
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int EntriesPerDirection
                  {
                  get { return entriesPerDirection; }
                  set { entriesPerDirection = Math.Max(1, value); }
                  }
                  #endregion
                  }
                  }
                  I expect some issues will arise when compiling this script, as I'm not sure where exactly all this information and values should be placed.

                  Comment


                    #10
                    Hello otislauwaert,

                    The syntax is not correct

                    Change:
                    Code:
                    protected override void Initialize()
                    
                    }
                    private int ordersCount = 0;
                    {
                    {
                    EntriesPerDirection = 1; 
                    EntryHandling = EntryHandling.UniqueEntries; 
                    
                    }
                    {
                    CalculateOnBarClose = true;
                    }
                    To:
                    Code:
                    private int ordersCount = 0;
                    
                    protected override void Initialize()
                    {
                    EntryHandling = EntryHandling.UniqueEntries; 
                    CalculateOnBarClose = true;
                    }
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Chelsea, thanks for your reply.

                      That did resolve some errors I was running into.

                      It seems like I only have two errors left remaining in this following code.

                      {
                      // Condition set 1
                      if (Close[0] > Open[0])
                      {
                      ordersCount++;
                      EnterLong(1, LongEntry+ordersCount.ToString());
                      }

                      // Condition set 2
                      if (Close[0] < Open[0])
                      {
                      ordersCount++;
                      EnterShort(1, ShortEntry+ordersCount.ToString());
                      }

                      }
                      It doesn't recognize the names ''LongEntry'' and ''ShortEntry'' and gives the error code CS0103. Any ideas on how to resolve this?

                      EDIT: I seem to have found the issue there, I need to put those names within the "correct" signs instead of the ''bad'' ones.
                      Last edited by otislauwaert; 11-22-2017, 02:47 PM.

                      Comment


                        #12
                        Hello otislauwaert,

                        The signal names need to be strings which are surrounded by quotation marks.

                        EnterLong(1, "LongEntry" + ordersCount.ToString());

                        Below is a link to a helpful forum post with information about getting started learning how to create NinjaScript Strategies and Addons.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          That does indeed work. Thank you for your help so far. The code as it is now looks like this.

                          #region Using declarations
                          using System;
                          using System.ComponentModel;
                          using System.Diagnostics;
                          using System.Drawing;
                          using System.Drawing.Drawing2D;
                          using System.Xml.Serialization;
                          using NinjaTrader.Cbi;
                          using NinjaTrader.Data;
                          using NinjaTrader.Indicator;
                          using NinjaTrader.Gui.Chart;
                          using NinjaTrader.Strategy;
                          #endregion

                          // This namespace holds all strategies and is required. Do not change it.
                          namespace NinjaTrader.Strategy
                          {
                          /// <summary>
                          /// Enter the description of your strategy here
                          /// </summary>
                          [Description(".")]
                          public class Coromandel : Strategy
                          {
                          #region Variables
                          // Wizard generated variables
                          private int entriesPerDirection = 5555; // Default setting for EntriesPerDirection
                          // User defined variables (add any user defined variables below)
                          #endregion

                          /// <summary>
                          /// This method is used to configure the strategy and is called once before any strategy method is called.
                          /// </summary>

                          private int ordersCount = 0;

                          protected override void Initialize()
                          {
                          EntryHandling = EntryHandling.UniqueEntries;
                          CalculateOnBarClose = true;
                          }

                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                          // Condition set 1
                          if (Close[0] > Open[0])
                          {
                          ordersCount++;
                          EnterLong(1, "LongEntry"+ordersCount.ToString());
                          }

                          // Condition set 2
                          if (Close[0] < Open[0])
                          {
                          ordersCount++;
                          EnterShort(1, "ShortEntry"+ordersCount.ToString());
                          }

                          }

                          #region Properties
                          [Description("")]
                          [GridCategory("Parameters")]
                          public int EntriesPerDirection
                          {
                          get { return entriesPerDirection; }
                          set { entriesPerDirection = Math.Max(1, value); }
                          }
                          #endregion
                          }
                          }
                          I tried to make it work with the MTDS framework, I'm not sure if you're familiar with that? But here's how that looks, and it only enters one trade for each turn of the bricks rather than entering one trade for each brick. Any idea what I'm doing wrong here, with this MTDS framework code?

                          [Description(".")]
                          public class Coromandel : MTDoubleShotStrategy
                          {
                          #region Variables
                          // Wizard generated variables
                          private int entriesPerDirection = 5555; // Default setting for EntriesPerDirection
                          // User defined variables (add any user defined variables below)
                          #endregion

                          /// <summary>
                          /// This method is used to configure the strategy and is called once before any strategy method is called.
                          /// </summary>

                          private int ordersCount = 0;

                          protected override void Initialize()
                          {
                          EntryHandling = EntryHandling.UniqueEntries;
                          CalculateOnBarClose = true;
                          base.Initialize();
                          }

                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                          base.OnBarUpdate();
                          }
                          public override bool TradeEntryValidate (bool isLong)
                          {
                          if (isLong)
                          {
                          // Condition set 1
                          if (Close[0] > Open[0])
                          {
                          ordersCount++;
                          return true;
                          }
                          }
                          else
                          {
                          // Condition set 2
                          if (Close[0] < Open[0])
                          {
                          ordersCount++;
                          return true;
                          }
                          }
                          return false;
                          }

                          #region Properties
                          [Description("")]
                          [GridCategory("Parameters")]
                          public int EntriesPerDirection
                          {
                          get { return entriesPerDirection; }
                          set { entriesPerDirection = Math.Max(1, value); }
                          }
                          #endregion
                          }

                          Comment


                            #14
                            Hello otislauwaert,

                            It appears you are trying to extend a custom class named MTDoubleShotStrategy.
                            public class Coromandel : MTDoubleShotStrategy

                            This would be outside of what is supported by NinjaTrader Support to do and I will not be able to assist with this.

                            Attempting to make an indicator or strategy that extends any class other than the Indicator or Strategy class will often lead to errors.
                            NinjaTrader auto-generates some code, and it does this based on the script inheriting directly from Indicator or Strategy (depending on the script type).

                            While it may be possible to inherit from a class that is not Indicator or Strategy, this would be far outside of what is supported by NinjaTrader Support to do.
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by kevinenergy, 02-17-2023, 12:42 PM
                            115 responses
                            2,699 views
                            1 like
                            Last Post kevinenergy  
                            Started by prdecast, Today, 06:07 AM
                            1 response
                            4 views
                            0 likes
                            Last Post NinjaTrader_LuisH  
                            Started by Christopher_R, Today, 12:29 AM
                            1 response
                            14 views
                            0 likes
                            Last Post NinjaTrader_LuisH  
                            Started by chartchart, 05-19-2021, 04:14 PM
                            3 responses
                            577 views
                            1 like
                            Last Post NinjaTrader_Gaby  
                            Started by bsbisme, Yesterday, 02:08 PM
                            1 response
                            15 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Working...
                            X