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

Major issue with Ninjatrader ( Candle Close and Open Calculation )

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

    Major issue with Ninjatrader ( Candle Close and Open Calculation )


    Hi NT,

    I am working on a Strategy which utilizes tick data and in that I am facing a huge issue with Ninjatrader Strategy code.

    My strategy works on First Tick Of Every Bar and I tried using the IsFirstTickOfBar default method which is giving me inaccurate results. For every candle the code should output only one string but as you can see in the 2.PNG for a candle it is returning 3 strings (Generally for first few ticks it is giving wrong results ).

    Also I tried to replace the IsFirstTickOfBar method with my own and I found out that sometimes the Close[1] (Previous candle close value) is not matching the real close of the previous candle.


    I request you to please look into the issue.


    I am using ** Calculate = Calculate.OnPriceChange; ** (Tried OnEachTick too but the results were same )


    Please refer to the final.PNG for details about my code.
    Attached Files
    Last edited by dastaan; 06-26-2020, 06:27 AM.

    #2


    Hi, thanks for your question.

    I am testing this code on a Renko chart and it's working as expected:

    Code:
    protected override void OnBarUpdate()
            {
                if(IsFirstTickOfBar && CurrentBar > 0)
                {
                    if(Close[1] >= Open[1])
                    {
                        Print("Bullish" + " " + Time[1] + " " + CurrentBar);
                    }
                    else
                    {
                        Print("Bearish" + " " + Time[1] + " " + CurrentBar);
                    }
                }
            }
    Can you run this code and let me know of your output?

    I look forward to hearing from you.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris L.,
      Thank you for timely reply.

      I have tried the above code and the issue is still the same. I am attaching the output of the code below, Please find it in the attachment.
      I noticed that very often it is printing previous candle time and going backwards in the time.

      I would appreciate your valuable feedback on it.
      Attached Files

      Comment


        #4
        Hi dastaan,

        I'm not able to reproduce what you're seeing. Try clearing your cache out and testing again:

        Close out of NinjaTrader> Navigate to Documents\NinjaTrader 8\db> Delete the "cache" folder> Restart NinjaTrader and test the script again.

        If this happens again, try downloading a new set of replay data as well.

        Kind regards.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi ChrisL,
          I tried the above solution and I found out there are issue with my script. It is still giving the same output. I am pasting my source code here for better understanding :
          Code:
          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.Data;
          using NinjaTrader.NinjaScript;
          using NinjaTrader.Core.FloatingPoint;
          using NinjaTrader.NinjaScript.Indicators;
          using NinjaTrader.NinjaScript.DrawingTools;
          
          namespace NinjaTrader.NinjaScript.Strategies
          {
              public class Pascal : Strategy
              {
                  #region Strategy Variables
          
                  enum PosType
                  {
                      Long,
                      Short
                  };
          
                  private const string StrategyName = "Pascal2";
          
                  private Order _enterLongOrder;
                  private Order _enterShortOrder;
                  private Order _exitLongStopOrder;
                  private Order _exitShortStopOrder;
                  private Order _exitLongProfitOrder;
                  private Order _exitLongTrailOrder;
                  private Order _exitShortProfitOrder;
                  private Order _exitShortTrailOrder;
          
                  private int Stage;
                  private double UpRange;
                  private double DownRange;
                  private double OpenPrice;
                  private int RenkoSize = 0;
                  private double PointA, PointB, PointC, PointD;
          
          
                  #endregion
          
                  #region Strategy Parameters
          
                  [NinjaScriptProperty]
                  [Display(Name = "Quantity", GroupName = "Money Management", Order = 1)]
                  public int InitialLotSize { get; set; }
          
                  [NinjaScriptProperty]
                  [Display(Name = "Profit Target Fixed", GroupName = "Stop and Profit", Order = 1)]
                  public int ProfitTargetFixed { get; set; }
          
                  [NinjaScriptProperty]
                  [Display(Name = "OpeningTRange", GroupName = "Entry Module", Order = 7)]
                  public int OpeningTRange { get; set; }
          
                  [NinjaScriptProperty]
                  [Display(Name = "A", GroupName = "Entry Module", Order = 8)]
                  public double A { get; set; }
          
                  [NinjaScriptProperty]
                  [Display(Name = "B", GroupName = "Entry Module", Order = 9)]
                  public double B { get; set; }
          
                  [NinjaScriptProperty]
                  [Display(Name = "C", GroupName = "Entry Module", Order = 10)]
                  public double C { get; set; }
          
                  #endregion
          
                  protected override void OnStateChange()
                  {
          
                      if (State == State.SetDefaults)
                      {
                          SetDefaultSettings();
                          SetDefaultStrategyParameters();
                          TraceOrders = true;
          
                      }
          
                      else if (State == State.Configure)
                      {
          
                      }
                  }
          
                  private void SetDefaultSettings()
                  {
                      Name = StrategyName;
                      Calculate = Calculate.OnPriceChange;
                      IsExitOnSessionCloseStrategy = true;
                      ExitOnSessionCloseSeconds = 100;
                      ClearOutputWindow();
                  }
          
                  private void SetDefaultStrategyParameters()
                  {
                      BarsRequiredToTrade = 0;
                      //StopLossTicks = 20;
                      ProfitTargetFixed = 100;
                      TraceOrders = true;
                      OpeningTRange = 15;
                      //Divider = ProfitTargetTicks;
                      InitialLotSize = 4;
                      TimeInForce = TimeInForce.Gtc;
                      OpenPrice = 0.0;
                      A = 0.5;
                      B = 0.0;
                      C = -0.5;
          
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (IsFirstTickOfBar && CurrentBar > 0)
                      {
                          if (Close[1] >= Open[1])
                          {
                              Print("Bullish" + " " + Time[1] + " " + CurrentBar);
                          }
                          else
                          {
                              Print("Bearish" + " " + Time[1] + " " + CurrentBar);
                          }
                      }
                  }
          
          
              }
          }
          Please try executing the above code. I am sure you will find the same issue with it.

          Last edited by dastaan; 06-27-2020, 04:47 AM.

          Comment


            #6
            Chris,
            I tried changing calculate to BarClose following by a restart then it worked fine but as soon as I shift calculate to OnPriceChange or OnEachTick and restart the script it throws the same issue.
            I think there is some issue in *calculate* module.

            Comment


              #7
              Hello dastaan,

              This is Jim responding as Chris asked me to take a look at this behavior.

              It will be expected to see multiple prints checking IsFirstTickOfBar with Calculate.OnEachTick/OnPriceChange because the Renko Bar uses RemoveLastBar in the BarsType. IsFirstTickOfBar will be true each time RemoveLAstBar is called.

              To make more sense of the prints seen, I suggest testing like I have below so we can see the associated prints match what is seen on the chart:



              Code:
              protected override void OnBarUpdate()
              {
                  if (State == State.Historical)
                      return;
                  if (IsFirstTickOfBar)
                      Print(String.Format("CurrentBar-1: {0} Time[1]: {1} Open[1]: {2} High[1]: {3} Low[1]: {4} Close[1]: {5}", (CurrentBar-1), Time[1], Open[1], High[1], Low[1], Close[1]));
              }
              You may also make a copy of the Renko BarsType and add prints before AddBar(), UpdateBar() and RemoveLastBar() to better understand how the bars are built and how it affects IsFirstTickOfBar. I have attached a modified Renko Bar that can demonstrate. I recommend testing on a chart that has Bars To Load set to 0 so we focus on realtime bar development.

              We look forward to assisting.
              Attached Files
              JimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DanielTynera, Today, 01:14 AM
              0 responses
              2 views
              0 likes
              Last Post DanielTynera  
              Started by yertle, 04-18-2024, 08:38 AM
              9 responses
              40 views
              0 likes
              Last Post yertle
              by yertle
               
              Started by techgetgame, Yesterday, 11:42 PM
              0 responses
              12 views
              0 likes
              Last Post techgetgame  
              Started by sephichapdson, Yesterday, 11:36 PM
              0 responses
              2 views
              0 likes
              Last Post sephichapdson  
              Started by bortz, 11-06-2023, 08:04 AM
              47 responses
              1,615 views
              0 likes
              Last Post aligator  
              Working...
              X