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

Using a variable for Stochastic Input

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

    Using a variable for Stochastic Input

    Hello,

    I have the following line of code but Ninjatrader is complaining that it can't convert double to int. I am running this in OnBarUpdate()

    Stochastics1= Stochastics(dom_cycle, dom_cycle, 5)[0];

    All of the values are integers though.

    Any idea as to what could be wrong?

    -Omer

    #2
    Hello omermirza,

    That means you defined the variables you are using as doubles. You need to change the variable or input to int instead of double.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi - the only variable is dom_cycle and it is declared as an int.

      private int dom_cycle=1;

      I am however doing some calculations, dividing the variable by 12 and then converting it back to int.


      Here's the actual error message:

      Click image for larger version

Name:	image.png
Views:	87
Size:	2.3 KB
ID:	1269194


      Attached Files

      Comment


        #4
        Hello omermirza,

        You will need to provide a more complete example here and include the line number in the error message.

        The stochastic wants an int variable, you are passing it a a double which is why its saying that.

        Double and int are not the same. An int is a whole number, a double is a number with a decimal.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Here's my example code, I've highlighted the line that has the error.

          cycle1 = highBarsAgo1 - lowBarsAgo1;
          cycle2 = highBarsAgo2 - lowBarsAgo2;
          cycle3 = highBarsAgo3 - lowBarsAgo3;
          cycle4 = highBarsAgo4 - lowBarsAgo4;

          cycle5 = highBarsAgo5 - lowBarsAgo5;
          cycle6 = highBarsAgo6 - lowBarsAgo6;
          cycle7 = highBarsAgo7 - lowBarsAgo7;
          cycle8 = highBarsAgo8 - lowBarsAgo8;

          cycle9 = highBarsAgo9 - lowBarsAgo9;
          cycle10 = highBarsAgo10 - lowBarsAgo10;
          cycle11 = highBarsAgo11 - lowBarsAgo11;
          cycle12 = highBarsAgo12 - lowBarsAgo12;

          dom_cycle = (cycle1 + cycle2 + cycle3 + cycle4 + cycle5 + cycle6 + cycle7 + cycle8 + cycle9 + cycle10 + cycle11 + cycle12) / 12;
          dom_cycle = Math.Abs(dom_cycle);
          dom_cycle = Convert.ToInt32(dom_cycle);

          //dom_cycle = Convert.ToInt32(SMA1[0]);


          if (dom_cycle==0)
          {
          dom_cycle=5;
          }


          Draw.TextFixed(this, "LowRight", "The Dominant Cycle is " + dom_cycle + " Bars", TextPosition.BottomRight);
          Draw.TextFixed(this, "TopLeft", "The Cycles Identifed are " + cycle1 + "," + cycle2 + "," + cycle3 + "," + cycle4 + "," + cycle5 + "," + cycle6 + "," + cycle7 + "," + cycle8 + "," + cycle9 + "," + cycle10 + "," + cycle11 + "," + cycle12, TextPosition.TopLeft);



          Stochastics1= Stochastics(Close, dom_cycle, dom_cycle, 5)[0];

          Comment


            #6
            Hello omermirza,

            Unfortunately that doesn't help because i don't know what line you have an error on. I would need to see a complete script that shows the variables and where they are defined in addition to knowing what line the error happened on.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Complete script below, error is on line 200:

              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

              //This namespace holds Strategies in this folder and is required. Do not change it.
              namespace NinjaTrader.NinjaScript.Strategies
              {
              public class AdaptiveZigZag2023 : Strategy
              {

              private TrendScoreSIMPLEDomCycle TrendScoreSIMPLEDomCycle1;
              private DPO DPO1;
              private EMA EMA1;
              private Stochastics Stochastics1;

              private ZigZag ZigZag1;
              private int ZigZagLowBarsAgo;
              private int ZigZagHighBarsAgo;


              private Swing Swing1;

              private int cycle1, cycle2, cycle3, cycle4, cycle5, cycle6, cycle7, cycle8, cycle9, cycle10, cycle11, cycle12;

              private int dom_cycle=1;

              private int highBarsAgo1, lowBarsAgo1, highBarsAgo2, lowBarsAgo2, highBarsAgo3, lowBarsAgo3, highBarsAgo4, lowBarsAgo4, highBarsAgo5, lowBarsAgo5, highBarsAgo6, lowBarsAgo6, highBarsAgo7, lowBarsAgo7;
              private int highBarsAgo8, lowBarsAgo8, highBarsAgo9, lowBarsAgo9, highBarsAgo10, lowBarsAgo10, highBarsAgo11, lowBarsAgo11, highBarsAgo12, lowBarsAgo12;
              private ATR ATR1;

              public Series<double> CalculatedKValue
              {
              get { return Values[0]; }
              }
              //public Stochastics Stochastics1;

              public int StochSmooth, StochPeriod;
              private int temp;
              private double trend_score=0;
              private int i =0;

              private double smooth;

              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Enter the description for your new custom Strategy here.";
              Name = "AdaptiveZigZag2023";
              Calculate = Calculate.OnBarClose;
              EntriesPerDirection = 6;
              EntryHandling = EntryHandling.AllEntries;
              IsExitOnSessionCloseStrategy = true;
              ExitOnSessionCloseSeconds = 30;
              IsFillLimitOnTouch = false;
              MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
              OrderFillResolution = OrderFillResolution.Standard;
              Slippage = 0;
              StartBehavior = StartBehavior.WaitUntilFlat;
              TimeInForce = TimeInForce.Gtc;
              TraceOrders = false;
              RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
              StopTargetHandling = StopTargetHandling.PerEntryExecution;
              BarsRequiredToTrade = 20;
              // Disable this property for performance gains in Strategy Analyzer optimizations
              // See the Help Guide for additional information
              IsInstantiatedOnEachOptimizationIteration = true;
              StochPeriod = 5;
              StochSmooth = 3;
              AddPlot(Brushes.Orange, "CalculatedKValue");
              AddLine(Brushes.Green, 80, "90Line");
              AddLine(Brushes.Red, 20, "10Line");
              IsOverlay = false;
              //AddPlot(Brushes.Orange, "Plot1");
              }
              else if (State == State.DataLoaded)
              {
              DPO1 = DPO(Close, 21);
              //Swing1 = Swing(DPO1, 7);
              EMA1 = EMA(Close, 200);
              Swing1 = Swing(5);
              TrendScoreSIMPLEDomCycle1 = TrendScoreSIMPLEDomCycle(Close);
              TrendScoreSIMPLEDomCycle1.Plots[0].Brush = Brushes.Olive;
              AddChartIndicator(TrendScoreSIMPLEDomCycle1);
              SetStopLoss("", CalculationMode.Currency, 75, false);
              ATR1 = ATR(Close, 20);

              ZigZag1 = ZigZag(DPO1, DeviationType.Points, 4, false);

              EMA1.Plots[0].Brush = Brushes.DarkOrchid;
              //AddChartIndicator(EMA1);
              //ZigZag1 = ZigZag(Close, DeviationType.Points, 4, false);
              Stochastics1.Plots[0].Brush = Brushes.DodgerBlue;
              Stochastics1.Plots[1].Brush = Brushes.Goldenrod;
              //AddChartIndicator(EMA1);
              AddChartIndicator(Stochastics1);
              }
              }

              protected override void OnBarUpdate()
              {



              if (CurrentBar < 20)
              return;


              highBarsAgo1 = ZigZag1.HighBar(1, 1, 999);
              lowBarsAgo1 = ZigZag1.LowBar(1, 1, 999);

              highBarsAgo2 = ZigZag1.HighBar(1, 2, 999);
              lowBarsAgo2 = ZigZag1.LowBar(1, 2, 999);

              highBarsAgo3 = ZigZag1.HighBar(1, 3, 999);
              lowBarsAgo3 = ZigZag1.LowBar(1, 3, 999);

              highBarsAgo4 = ZigZag1.HighBar(1, 4, 999);
              lowBarsAgo4 = ZigZag1.LowBar(1, 4, 999);

              highBarsAgo5 = ZigZag1.HighBar(1, 5, 999);
              lowBarsAgo5 = ZigZag1.LowBar(1, 5, 999);

              highBarsAgo6 = ZigZag1.HighBar(1, 6, 999);
              lowBarsAgo6 = ZigZag1.LowBar(1, 6, 999);

              highBarsAgo7 = ZigZag1.HighBar(1, 7, 999);
              lowBarsAgo7 = ZigZag1.LowBar(1, 7, 999);

              highBarsAgo8 = ZigZag1.HighBar(1, 8, 999);
              lowBarsAgo8 = ZigZag1.LowBar(1, 8, 999);

              highBarsAgo9 = ZigZag1.HighBar(1, 9, 999);
              lowBarsAgo9 = ZigZag1.LowBar(1, 9, 999);

              highBarsAgo10 = ZigZag1.HighBar(1, 10, 999);
              lowBarsAgo10 = ZigZag1.LowBar(1, 10, 999);

              highBarsAgo11 = ZigZag1.HighBar(1, 11, 999);
              lowBarsAgo11 = ZigZag1.LowBar(1, 11, 999);

              highBarsAgo12 = ZigZag1.HighBar(1, 12, 999);
              lowBarsAgo12 = ZigZag1.LowBar(1, 12, 999);



              cycle1 = highBarsAgo1 - lowBarsAgo1;
              cycle2 = highBarsAgo2 - lowBarsAgo2;
              cycle3 = highBarsAgo3 - lowBarsAgo3;
              cycle4 = highBarsAgo4 - lowBarsAgo4;

              cycle5 = highBarsAgo5 - lowBarsAgo5;
              cycle6 = highBarsAgo6 - lowBarsAgo6;
              cycle7 = highBarsAgo7 - lowBarsAgo7;
              cycle8 = highBarsAgo8 - lowBarsAgo8;

              cycle9 = highBarsAgo9 - lowBarsAgo9;
              cycle10 = highBarsAgo10 - lowBarsAgo10;
              cycle11 = highBarsAgo11 - lowBarsAgo11;
              cycle12 = highBarsAgo12 - lowBarsAgo12;

              dom_cycle = (cycle1 + cycle2 + cycle3 + cycle4 + cycle5 + cycle6 + cycle7 + cycle8 + cycle9 + cycle10 + cycle11 + cycle12) / 12;
              dom_cycle = Math.Abs(dom_cycle);
              dom_cycle = Convert.ToInt32(dom_cycle);

              //dom_cycle = Convert.ToInt32(SMA1[0]);


              if (dom_cycle==0)
              {
              dom_cycle=5;
              }


              Draw.TextFixed(this, "LowRight", "The Dominant Cycle is " + dom_cycle + " Bars", TextPosition.BottomRight);
              Draw.TextFixed(this, "TopLeft", "The Cycles Identifed are " + cycle1 + "," + cycle2 + "," + cycle3 + "," + cycle4 + "," + cycle5 + "," + cycle6 + "," + cycle7 + "," + cycle8 + "," + cycle9 + "," + cycle10 + "," + cycle11 + "," + cycle12, TextPosition.TopLeft);



              Stochastics1= Stochastics(Close, dom_cycle, dom_cycle, 5)[0];

              //CalculatedKValue[0] = Stochastics(dom_cycle, dom_cycle, dom_cycle/2).D[0];


              // Enter Orders

              if (CrossAbove(CalculatedKValue, 20, 1))// && (Close[0] > EMA1[0]))
              {
              EnterLong(1);
              //EnterLong(0, 1, @"FPC");
              }


              if (CrossBelow(CalculatedKValue, 80, 1))
              {
              ExitLong(1);
              }





              }
              }
              }

              Comment


                #8
                Hello omermirza,

                Thanks for providing that.

                The problem is that you are trying to set a double to your stochastics variable and not anything to do with ints.

                Code:
                private Stochastics Stochastics1;
                ​
                Stochastics1= Stochastics(Close, dom_cycle, dom_cycle, 5)[0];
                You used a BarsAgo on the Stochastics, that is getting the current bar price and trying to set it to the Stochastics variable. You need to remove the bars ago:

                Code:
                Stochastics1= Stochastics(Close, dom_cycle, dom_cycle, 5);
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thank you - the strategy will compile but won't activate. The log Tab has the following error:

                  Click image for larger version

Name:	image.png
Views:	84
Size:	4.7 KB
ID:	1269208

                  Comment


                    #10

                    Hello omermirza,

                    Looking at your OnStateChange it looks like you don't have a stochastics being defined in OnStateChange but you are trying to set plots and add it to the chart.

                    else if (State == State.DataLoaded)
                    {
                    DPO1 = DPO(Close, 21);
                    //Swing1 = Swing(DPO1, 7);
                    EMA1 = EMA(Close, 200);
                    Swing1 = Swing(5);
                    TrendScoreSIMPLEDomCycle1 = TrendScoreSIMPLEDomCycle(Close);
                    TrendScoreSIMPLEDomCycle1.Plots[0].Brush = Brushes.Olive;
                    AddChartIndicator(TrendScoreSIMPLEDomCycle1);
                    SetStopLoss("", CalculationMode.Currency, 75, false);
                    ATR1 = ATR(Close, 20);

                    ZigZag1 = ZigZag(DPO1, DeviationType.Points, 4, false);

                    EMA1.Plots[0].Brush = Brushes.DarkOrchid;
                    //AddChartIndicator(EMA1);
                    //ZigZag1 = ZigZag(Close, DeviationType.Points, 4, false);
                    Stochastics1.Plots[0].Brush = Brushes.DodgerBlue;
                    Stochastics1.Plots[1].Brush = Brushes.Goldenrod;

                    //AddChartIndicator(EMA1);
                    AddChartIndicator(Stochastics1);
                    }


                    Based on how you are creating the indicator in OnBarUpdate you need to remove those 3 lines from OnStateChange. ​
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Ok great - that works now but I can't see the indicator.

                      Comment


                        #12
                        Hello omermirza,

                        Yes in that case you would have to plot the indicator yourself if you wanted to see it. You are calculating dom_cycle in OnBarupdate so that is happening after the OnStateChange where you can add the indicator. You can use a strategy plot or also make a dummy indicator that plots the value from the strategy.

                        https://ninjatrader.com/support/help..._a_ninjasc.htm
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Thank you so much for your help. I got it working.

                          Comment


                            #14
                            H,

                            Back to this discussion - I am having a minor issue. Some of the trades are not being taken. I've attached a pic and highlighted where that's happening.

                            Here's the snippet of code where my orders are executed:


                            Stochastics1= Stochastics(Close, dom_cycle, dom_cycle, 5);
                            Value[0] = Stochastics(Close, dom_cycle, dom_cycle, 5)[0];


                            // Enter Orders

                            if (CrossAbove(Stochastics1.D, 20, 1) && Position.MarketPosition == MarketPosition.Flat)
                            {
                            EnterLong(1);
                            }

                            // Set 2
                            if (CrossBelow(Stochastics1.D, 80, 1) && Position.MarketPosition == MarketPosition.Long)
                            {
                            ExitLong(1);
                            }

                            if (CrossBelow(Stochastics1.D, 80, 1) && Position.MarketPosition == MarketPosition.Flat)
                            {
                            EnterShort(1);
                            }

                            if (CrossAbove(Stochastics1.D, 20, 1) && Position.MarketPosition == MarketPosition.Short)
                            {
                            ExitShort(1);
                            }​








                            Click image for larger version

Name:	image.png
Views:	53
Size:	66.6 KB
ID:	1271648

                            Comment


                              #15
                              Hello omermirza,

                              If you feel that something different should have happened you would need to use prints in your code to better understand why the conditions we true or false at those times.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Philippe56140, Today, 02:35 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Philippe56140  
                              Started by 00nevest, Today, 02:27 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post 00nevest  
                              Started by Jonafare, 12-06-2012, 03:48 PM
                              5 responses
                              3,986 views
                              0 likes
                              Last Post rene69851  
                              Started by Fitspressorest, Today, 01:38 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Fitspressorest  
                              Started by Jonker, Today, 01:19 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Jonker
                              by Jonker
                               
                              Working...
                              X