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 12-21-2017, 09:14 PM   #1
TexFly
Senior Member
 
Join Date: Aug 2012
Posts: 164
Thanks: 3
Thanked 4 times in 4 posts
Exclamation Simple calculation

Hello,

I'm trying to do a simple calculation on OnBarUpdate() but I keep getting errors. I have researched but I can't make it work.

I have three different indicator lengths and I want to calculate the average. Something like this:

This is declared in the class above the OnStateChange()
private SMA SMA1;
private SMA SMA2;
private SMA SMA3;

Then OnBarUpdate()
SMA1 = SMA(5);
SMA2 = SMA(20);
SMA3 = SMA(50);

AvgSMA=(SMA1+SMA2+SMA3)/3;
double highsignal = 90;


if (CrossAbove(AvgSMA, highsignal, 1))
EnterLong("AALong");


But I keep getting errors about converting double to a series and vice versa. I have tried to declare all doubles but the CrossAbove doesn't like it neither...

What's the best way to accomplish this?

Thanks for the help.
TexFly is offline  
Reply With Quote
Old 12-21-2017, 11:35 PM   #2
nkhoi
Senior Member
 
Join Date: Nov 2004
Location: , ,
Posts: 186
Thanks: 10
Thanked 38 times in 27 posts
Default

the message is misleading, you need to use [0] for the actual calculation..
example: ZerolagTEMA[0] = 2*tema1[0] - tema2[0];
AvgSMA[0]=(SMA1[0]+SMA2[0]+SMA3[0])/3
I include an indicator from fattail to show where you suppose to call the EMAs
Attached Files
File Type: cs amaZerolagTEMA.cs (6.2 KB, 3 views)
Last edited by nkhoi; 12-21-2017 at 11:39 PM.
nkhoi is offline  
Reply With Quote
Old 12-22-2017, 05:45 AM   #3
TexFly
Senior Member
 
Join Date: Aug 2012
Posts: 164
Thanks: 3
Thanked 4 times in 4 posts
Default

Quote:
Originally Posted by nkhoi View Post
the message is misleading, you need to use [0] for the actual calculation..
example: ZerolagTEMA[0] = 2*tema1[0] - tema2[0];
AvgSMA[0]=(SMA1[0]+SMA2[0]+SMA3[0])/3
I include an indicator from fattail to show where you suppose to call the EMAs
Thanks nkhoi. Unfortunately it still doesn't work...

SMA1 = SMA(10);
SMA2 = SMA(20);
SMA3 = SMA3(30);

AvgSMA[0]=(SMA1[0]+SMA2[0]+SMA3[0])/3

double highsignal = 90;
double lowsignal = 10;

if (CrossAbove(AvgSMA[0], highsignal, 1))
EnterLong();

if (CrossBelow(AvgSMA[0], lowsignal, 1))
EnterShort();


Throws "The best overloaded method match for 'NinjaTrader.Indicator.Indicator.SMA(NinjaTrader.N injaScript.ISeries<double>, int)' has some invalid arguments"

plus a second error

"Cannot implicitly convert type 'NinjaTrader.NinjaScript.Indicators.SMA' to 'double'"....

I have declared the AvgSMA in the properties

public Series<double> AvgSMA
{
get { return Values[0]; }
}

and the three SMA series on top right below the class definition...

private SMA SMA1;
private SMA SMA2;
private SMA SMA3;

Any other idea?
Last edited by TexFly; 12-22-2017 at 05:48 AM.
TexFly is offline  
Reply With Quote
Old 12-22-2017, 07:02 AM   #4
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 4,173
Thanks: 437
Thanked 1,091 times in 1,045 posts
Default

Hello TexFly,

Thanks for your post.

I will address your questions and code from the first post to avoid confusion.

You have declared 3 private SMAs which will be data series.

You would need to create them in the OnStateChange() method in State.DataLoaded like:

else if (State== State.DataLoaded)
{
SMA1 = SMA(5);
SMA2 = SMA(20);
SMA3 = SMA(50);
}
Reference: Best Practices, state management practices: https://ninjatrader.com/support/help..._practices.htm See the specific section titled, "Setting up resources that rely on market data"

In OnBarUpdate() you have:

AvgSMA=(SMA1+SMA2+SMA3)/3; which creates a double called avgSMA

The conditional statement: if (CrossAbove(AvgSMA, highsignal, 1)) will not work because AvgSMA and highsignal are both double values. AvgSMA would need to be a data series for this to work.

With reference to the previous helpguide link, set up AvgSMA as a private data series just like the helpguide shows for the "MySeries" example.

Then rework the OnBarUpdate() to show:

AvgSMA[0]=(SMA1[0]+SMA2[0]+SMA3[0])/3;

Now the conditional statement will work.
NinjaTrader_Paul is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Paul for this post:
Old 12-22-2017, 06:36 PM   #5
TexFly
Senior Member
 
Join Date: Aug 2012
Posts: 164
Thanks: 3
Thanked 4 times in 4 posts
Default

Paul,

Great explanation. It works now. Thanks!
TexFly 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
how to run certain calculation logic every tick, while only running entry calculation linzvb Platform Technical Support 7 08-25-2016 08:13 AM
simple calculation Laimis Strategy Development 3 06-01-2016 08:10 AM
Simple MACD Crossover Strategy that isn't so simple eberline Platform Technical Support 19 04-07-2015 12:53 PM
Is there a simple way to convert the RSI averages to exponential instead of Simple? forrestang Indicator Development 2 01-06-2010 09:19 PM
Simple question about a simple strategy. shakira Strategy Development 1 02-14-2009 11:56 AM


All times are GMT -6. The time now is 03:40 PM.

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.