• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Please help -- cannot convert from "double" to NinjaTrader.NinjaScript.Series<double>

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

    Please help -- cannot convert from "double" to NinjaTrader.NinjaScript.Series<double>

    Hi,

    I'm trying to create a new indicator and having a little problem.
    I keep getting these error messages at: Noise = SUM (Diff, Period)[0]; When I Print Diff or Period I can see them but when I want to add them up I get:
    The best overloaded method match for 'NinjaTrader.NinjaScript.Strategies.SUM(NinjaTrade r.NinjaScript.ISeries<double>, int)' has some invalid arguments

    and
    cannot convert from "double" to NinjaTrader.NinjaScript.Series<double>
    but when I put Close instead of Diff, then everything is fine.
    So far I output everything and all variables print just fine till this point.
    i appreciate any help.
    here is the whole code which is not much...it's just the start.

    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
    
    //This namespace holds Strategies in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class adaptiveSht3 : Strategy
        {
            private Brush Brush1;
            private EMA EMA1;
            private EMA EMA2;
            
            //
            int Period                = 10;
            
            // vars
            
            double Diff                = 0;
            
            //change later
            
            double Finalout                = 0;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Buy and Sell trading strategy";
                    Name                                        = "adaveSht3";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                    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;
                    FastMA                    = 7;
                    SlowMA                    = 14;
                }
                else if (State == State.Configure)
                {
                    Brush1 = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF403F45"));
                    Brush1.Freeze();
                }
                else if (State == State.DataLoaded)
                {                
                    EMA1                = EMA(Close, Convert.ToInt32(FastMA));
                    EMA1.Plots[0].Brush = Brushes.Red;
                    AddChartIndicator(EMA1);
                    EMA2                = EMA(Close, Convert.ToInt32(SlowMA));
                    EMA2.Plots[0].Brush = Brushes.Goldenrod;
                    AddChartIndicator(EMA2);
                    EMA2                = EMA(Close, Convert.ToInt32(SlowMA));
                    
                    
        
                            
                // these objects and their related members are not available until State.DataLoaded
                Print(Bars.Count);
                Print(Instrument.FullName);
                                
                    Print(Period);
                    Print(Filter);
                    Print(Close[0]);
                    
                
                    
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0) 
                    return;
    
                if (CurrentBars[0] < 1)
                return;
                
                
            //New Indicater
      
            
            //Calculate  Ratio
                
            Diff = (Math.Abs (Close[0] - Close[1]));
                if (CurrentBars[0] <= Period)
                {
                    Finalout = Close[0];
                }
                
                if (CurrentBars[0] > Period)
                {
                    Signal = (Math.Abs (Close[0] - Close[Period]));
                    Noise = SUM (Diff, Period)[0];
                    
                    
                            
                }        
                
                
                Print(Noise);
                Print(Diff);
                        
                        
                 // Set 1 _-_ To Buy
                if (EMA1[0] >= EMA2[0])
                {
                    
                    //background color
                    BackBrush = Brush1;
                    //to buy
                    EnterLong(Convert.ToInt32(50), "");
                }
                
                 // Set 2 _-_ To Sell
                if (EMA1[0] < EMA2[0])
                {
                    //to sell
                    ExitLong(Convert.ToInt32(50), "", "");
                }
                
                
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="FastMA", Description="Fast moving average", Order=1, GroupName="Parameters")]
            public int FastMA
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="SlowMA", Description="Slow moving average", Order=2, GroupName="Parameters")]
            public int SlowMA
            { get; set; }
            #endregion
    
        }
    }

    #2
    Hey Sonyson,

    Originally posted by Sonyson View Post
    ...I keep getting these error messages at: Noise = SUM (Diff, Period)[0]; When I Print Diff or Period I can see them but when I want to add them up...
    It looks like you're trying to add Diff (which is a double) and Period (which is an int). This can be done by using the "+" operator. So I would change
    Code:
    Noise = SUM (Diff, Period)[0];
    to be
    Code:
    // Declare Noise somewhere as a double like..
    // double Noise;
    Noise = Diff + Period;
    Was that the intent of your code?

    Comment


      #3
      Originally posted by Sonyson View Post
      Hi,
      you are trying to code Kaufman's Adaptive Moving Average. Take a look at ninja built-in KAMA indicator.

      Comment


        #4
        Hi wadams and thank you for your response,
        For short version of the code I have:

        int Period = 10;
        double Diff = 0;
        double Finalout = 0;


        As variables and

        //Calculate Ratio

        Diff = (Math.Abs (Close[0] - Close[1]));
        if (CurrentBars[0] <= Period)
        {
        Finalout = Close[0];
        }

        if (CurrentBars[0] > Period)
        {
        Signal = (Math.Abs (Close[0] - Close[Period]));
        Noise = SUM (Diff, Period)[0];
        }
        Print(Noise);
        Print(Diff);

        as argument.
        I would like to take a difference of close prices of two days, Diff = (Math.Abs (Close[0] - Close[1])); and add them all up for the period of 10 bars or days.Noise = SUM (Diff, Period)[0];

        Now what is really confusing me is if I put Close instead of Diff, I get all the close prices for past ten period added up tighter just fine and close prices have decimal points too so I would think they are double data type too but not the variable Diff? I get those error messages.
        I'm wandering if variable Diff needs to be in some sort of indexed array? What am I missing? Or what am I doing wrong?

        Comment


          #5
          Hi nkhoi, Will do thanks.

          Comment


            #6
            Yes it is but not complete like the book. But I can work with this. Thank you so much

            Comment


              #7
              Originally posted by Sonyson View Post
              I'm wandering if variable Diff needs to be in some sort of indexed array? What am I missing? Or what am I doing wrong?
              Ah, I see what you're trying to do now.

              Yes, Diff does need to be in some sort of indexed array. In this case, it's a Series<double>. There are three steps to using these:

              1. Declare the variable. So in your list of fields at the top, add these two lines:
              Code:
              private Series<double> diff;
              private Series<double> noise;
              2. Define the values. So in the block of code where "State == State.Configure", add these two lines:
              Code:
              diff = new Series<double>(this, MaximumBarsLookBack);
              noise = new Series<double>(this, MaximumBarsLookBack);
              3. Set the values within the Series. So, in the the "OnBarUpdate" method, you'll need to change this...
              Code:
              Diff = (Math.Abs (Close[0] - Close[1]));
              to this...
              Code:
              diff[0] = (Math.Abs(Close[0] - Close[1]));
              And this...
              Code:
              Noise = SUM (Diff, Period)[0];
              to this...
              Code:
              noise[0] = SUM(diff, Period)[0];
              ... I think

              For more information on the Series class, check out the documentation here: https://ninjatrader.com/support/help...s/?seriest.htm

              Comment


                #8
                Hello Sonyson and thank you wadams.

                The compiler is informing you that the method is expecting a variable of type Series<double> as the parameter and the variable Diff of type double cannot be converted to a type of Series<double>. You could follow wadam's instruction for using a Series<double> instead.

                I would also like to point out a sample that is openly available in our help guide that can provide some direction on using a Series object.

                SampleCustomSeries - https://ninjatrader.com/support/help...taseries_o.htm

                Please let me know if I can be of further assistance.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Thank you very much all for your time. Will look into it on my day off.

                  Comment


                    #10
                    here is the translation from trade station, almost line by line for ease of compare. Only the problem if you load 5 day of data it won't show but it will show on 1 or 2 or 7 day of data?. If you plot it side by side with Ninja KAMA you will see they are the same.
                    Attached Files

                    Comment


                      #11
                      Thank you nkhoi

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by dariuszszyc, Today, 09:08 AM
                      1 response
                      7 views
                      0 likes
                      Last Post NinjaTrader_JoshG  
                      Started by Sadat, Today, 09:09 AM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_JoshG  
                      Started by login_dejavu, Today, 07:57 AM
                      4 responses
                      19 views
                      0 likes
                      Last Post login_dejavu  
                      Started by Lender, Yesterday, 05:15 PM
                      1 response
                      13 views
                      0 likes
                      Last Post NinjaTrader_Jim  
                      Started by rcmcd, Today, 07:52 AM
                      1 response
                      7 views
                      0 likes
                      Last Post NinjaTrader_PatrickG  
                      Working...
                      X