Go Back   NinjaTrader Support Forum > NinjaTrader 8 > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 07-05-2018, 06:57 PM   #1
Sonyson
Junior Member
 
Join Date: Jul 2018
Posts: 6
Thanks: 5
Thanked 0 times in 0 posts
Default 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

    }
}
Sonyson is offline  
Reply With Quote
Old 07-05-2018, 09:59 PM   #2
wadams
Junior Member
 
Join Date: Dec 2008
Posts: 20
Thanks: 9
Thanked 4 times in 3 posts
Default

Hey Sonyson,

Quote:
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?
wadams is offline  
Reply With Quote
The following user says thank you to wadams for this post:
Old 07-05-2018, 10:35 PM   #3
nkhoi
Senior Member
 
Join Date: Nov 2004
Location: , ,
Posts: 184
Thanks: 10
Thanked 35 times in 24 posts
Default

Quote:
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.
nkhoi is offline  
Reply With Quote
The following user says thank you to nkhoi for this post:
Old 07-05-2018, 10:47 PM   #4
Sonyson
Junior Member
 
Join Date: Jul 2018
Posts: 6
Thanks: 5
Thanked 0 times in 0 posts
Default

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?
Sonyson is offline  
Reply With Quote
Old 07-05-2018, 10:50 PM   #5
Sonyson
Junior Member
 
Join Date: Jul 2018
Posts: 6
Thanks: 5
Thanked 0 times in 0 posts
Default

Hi nkhoi, Will do thanks.
Sonyson is offline  
Reply With Quote
Old 07-05-2018, 11:02 PM   #6
Sonyson
Junior Member
 
Join Date: Jul 2018
Posts: 6
Thanks: 5
Thanked 0 times in 0 posts
Default

Yes it is but not complete like the book. But I can work with this. Thank you so much
Sonyson is offline  
Reply With Quote
Old 07-05-2018, 11:38 PM   #7
wadams
Junior Member
 
Join Date: Dec 2008
Posts: 20
Thanks: 9
Thanked 4 times in 3 posts
Default

Quote:
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
wadams is offline  
Reply With Quote
The following 2 users say thank you to wadams for this post:
Old 07-06-2018, 06:57 AM   #8
NinjaTrader_Jim
NinjaTrader Customer Service
 
NinjaTrader_Jim's Avatar
 
Join Date: Nov 2016
Location: Denver, CO
Posts: 1,804
Thanks: 96
Thanked 487 times in 462 posts
Default

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.
NinjaTrader_Jim is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Jim for this post:
Old 07-06-2018, 05:05 PM   #9
Sonyson
Junior Member
 
Join Date: Jul 2018
Posts: 6
Thanks: 5
Thanked 0 times in 0 posts
Default

Thank you very much all for your time. Will look into it on my day off.
Sonyson is offline  
Reply With Quote
Old 07-06-2018, 09:41 PM   #10
nkhoi
Senior Member
 
Join Date: Nov 2004
Location: , ,
Posts: 184
Thanks: 10
Thanked 35 times in 24 posts
Default

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
File Type: cs AMA.cs (4.7 KB, 3 views)
nkhoi is offline  
Reply With Quote
The following user says thank you to nkhoi for this post:
Old 07-10-2018, 05:42 AM   #11
Sonyson
Junior Member
 
Join Date: Jul 2018
Posts: 6
Thanks: 5
Thanked 0 times in 0 posts
Default

Thank you nkhoi
Sonyson is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Series<double> problems kkc2015 Add-On Development 4 02-05-2017 01:31 PM
Type 'double' can't implicitly be converted to 'NinjaTrader.NinjaScript.Series<double The_Wiz Indicator Development 3 10-07-2016 07:40 AM
assign Series<double> a double value in NT8 stearno Version 8 Beta 5 02-22-2016 10:12 PM
How/When do you pass PriceSeries<double>, ISeries<double> or Series<double>, DaFish Version 8 Beta 10 08-14-2015 01:56 PM
Cannot impliciily convert type double to NinjaScript Series,double Richard168 Version 8 Beta 3 07-28-2015 10:18 AM


All times are GMT -6. The time now is 03:15 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.