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

Strategy for Selling on Red Heikin Ashi Candle

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

    Strategy for Selling on Red Heikin Ashi Candle

    Hello,

    I am currently working on implementing a strategy that involves selling when a Heikin Ashi candle is red. The compilation process is successful, and I am able to add it to the chart. However, when attempting to enable it, I encounter an issue as it doesn't allow me to tick.

    I would appreciate any guidance or assistance you could provide in resolving this issue.

    Thank you​

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    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;
    #endregion
    
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class aValueBearOnlyHeikin : Strategy
        {
            double[] HAOpen;
            double[] HAClose;
            
            protected override void OnStateChange()
            {                
                
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Selling on red Heikin Ashi";
                    Name                                        = "aValueBearOnlyHeikin";
                    Calculate                                    = Calculate.OnBarClose;
                }
                
            }
    
            protected override void OnBarUpdate()
            {            
                HAClose[0]    =    ((Open[0] + High[0] + Low[0] + Close[0]) * 0.25); // Calculate the close
                HAOpen[0]    =    ((HAOpen[1] + HAClose[1]) * 0.5); // Calculate the open*/
            
                 if ( HAOpen[0]>HAClose[0])
                
                    {
                        EnterShort();
                    }
                                        
            }    
    
        }
    }
    ​

    #2
    Hello kiro1000,

    Could you provide more of an explanation about what problem you are seeing? I am not sure what you mean by it wont let you tick. Is the enable button gray and unclickable or are you seeing it disable and getting an error in the control center log tab?
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      Thank you for your prompt response.

      I'm encountering a peculiar issue where I'm unable to check the box to enable the strategy. Upon clicking the box, the checkmark briefly appears but then promptly disappears. This problem seems to be unique to this particular strategy, as I don't encounter it with others.

      Your insights or guidance on resolving this matter would be greatly appreciated.

      Click image for larger version

Name:	Strategies.png
Views:	60
Size:	34.3 KB
ID:	1288303

      Comment


        #4
        Hello kiro1000,

        In that case you would need to check the control centers log tab for errors. From the image you currently have the log tab disabled, you will need to click the + at the bottom of the window and select the log tab.

        On another note the code you provided does not seem to be valid. The two variables you defined are never defined. If you meant to create a series you need to define them like the page shows in the following link: https://ninjatrader.com/support/help...ightsub=series
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          Thank you for your guidance; I followed your instructions, and I encountered the following error:

          Strategy 'aValueBearOnlyHeikin': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.
          Your insights on resolving this matter would be invaluable.​

          Comment


            #6
            Hello kiro1000,

            Yes that is due to the reason I mentioned in my last reply, that code is not valid. If you are trying to make a series you can see an example of the correct way to define and use a series in the link from post 4.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello Jesse,

              I appreciate your guidance on defining the variables correctly, I defined the variables according to the guidelines provided in the link you shared. The compilation process is successful. Here the updated code :

              Code:
              #region Using declarations
              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.ComponentModel.DataAnnotations;
              using System.Linq;
              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;
              #endregion
              
              
              namespace NinjaTrader.NinjaScript.Strategies
              {
                  public class aValueBearOnlyHeikin : Strategy
                  {
                      private Series<double> HAOpen;
                      private Series<double> HAClose;
                      
                      protected override void OnStateChange()
                      {                
                          
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Selling on red Heikin Ashi";
                              Name                                        = "aValueBearOnlyHeikin";
                              Calculate                                    = Calculate.OnBarClose;
                              AddDataSeries(BarsPeriodType.Minute, 1);
                          }
                          else if (State == State.DataLoaded)
                  {
                       HAOpen = new Series<double>(this, MaximumBarsLookBack.Infinite);
                       HAClose = new Series<double>(BarsArray[1]);
                  }            
                      }
              
                      protected override void OnBarUpdate()
                      {            
                          HAClose[0]    =    ((Open[0] + High[0] + Low[0] + Close[0]) * 0.25); // Calculate the close
                          HAOpen[0]    =    ((HAOpen[1] + HAClose[1]) * 0.5); // Calculate the open*/
                      
                           if ( HAOpen[0]>HAClose[0])
                          
                              {
                                  EnterShort();
                              }
                                                  
                      }    
              
                  }
              }
              ​

              However, I encountered a new issue. When attempting to check the box to enable the strategy, I received the following error message:

              Click image for larger version

Name:	2024-02-02 22_14_01-NinjaScript Editor - Strategy - aValueBearOnlyHeikin.png
Views:	58
Size:	5.5 KB
ID:	1289596

              Unhandled exception: Impossible to cast an object of type 'System.Object' in type 'NinjaTrader.NinjaScript.StrategyBase'
              Thank you​

              Comment


                #8
                Hello kiro1000,

                The page that I had linked has samples for both single series scripts and multi series scripts, it looks like you used both samples instead of just the single series example. Both of your series need to be defined like the first one or:

                Code:
                HAOpen = new Series<double>(this, MaximumBarsLookBack.Infinite);
                ​HAClose= new Series<double>(this, MaximumBarsLookBack.Infinite);
                You also dont need to use AddDataSeries as your original script did not use that.

                You would also need to add a CurrentBar check because you are referencing the previous bar on the first bar where there is not going to be a previous bar yet.

                Code:
                protected override void OnBarUpdate()
                {​
                    if(CurrentBar < 1) return;
                JesseNinjaTrader Customer Service

                Comment


                  #9

                  Hello Jesse,

                  I wanted to express my sincere appreciation for your guidance. After revisiting the code and making the necessary adjustments as per your instructions, I'm pleased to inform you that the script is now functioning flawlessly – it works! : )

                  Here the code :
                  Code:
                  #region Using declarations
                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.ComponentModel.DataAnnotations;
                  using System.Linq;
                  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;
                  #endregion
                  
                  
                  namespace NinjaTrader.NinjaScript.Strategies
                  {
                      public class aValueHeikinTseries : Strategy
                      {
                          private Series<double> HAOpen;
                          private Series<double> HAClose;
                          
                          protected override void OnStateChange()
                          {                
                              
                              if (State == State.SetDefaults)
                              {
                                  Description                                    = @"Selling on red Heikin Ashi";
                                  Name                                        = "aValueHeikinTseries";
                                  Calculate                                    = Calculate.OnBarClose;
                              }
                              else if (State == State.DataLoaded)
                      {
                           HAOpen = new Series<double>(this, MaximumBarsLookBack.Infinite);
                           HAClose = new Series<double>(this, MaximumBarsLookBack.Infinite);
                                  
                      }
                          }
                  
                          protected override void OnBarUpdate()
                          {            
                              HAClose[0]    =    ((Open[0] + High[0] + Low[0] + Close[0]) * 0.25); // Calculate the close
                              HAOpen[0]    =    ((HAOpen[1] + HAClose[1]) * 0.5); // Calculate the open*/
                          
                               if ( HAOpen[0]>HAClose[0])
                              
                                  {
                                      EnterShort();
                                  }
                                                      
                          }    
                  
                      }
                  }
                  ​

                  Your assistance in resolving this issue has been invaluable, and I'm grateful for your support.

                  Thanks once again,​

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by fx.practic, 10-15-2013, 12:53 AM
                  5 responses
                  5,404 views
                  0 likes
                  Last Post Bidder
                  by Bidder
                   
                  Started by Shai Samuel, 07-02-2022, 02:46 PM
                  4 responses
                  95 views
                  0 likes
                  Last Post Bidder
                  by Bidder
                   
                  Started by DJ888, Yesterday, 10:57 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by MacDad, 02-25-2024, 11:48 PM
                  7 responses
                  159 views
                  0 likes
                  Last Post loganjarosz123  
                  Started by Belfortbucks, Yesterday, 09:29 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post Belfortbucks  
                  Working...
                  X