NexusFi: Find Your Edge


Home Menu

 





Logarithm & SMA Issue


Discussion in NinjaTrader

Updated
    1. trending_up 2,414 views
    2. thumb_up 1 thanks given
    3. group 1 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 
srgtroy's Avatar
 srgtroy 
Los Angeles, California Republic
Legendary  R.I.P. 1965-2023 
 
Experience: None
Platform: Sierra Chart
Broker: CQG
Trading: ES
Posts: 1,928 since Jan 2011
Thanks Given: 1,375
Thanks Received: 3,723

I'm new to NinjaScript and not the world's greatest programmer so if this is a stupid question please excuse me.

I would like to plot the value of the 21 period SMA of the logarithms of the close values of each bar.

However, the Math.Log function won't let me use a DataSeries value. And the SMA function won't let me use a double value. How can I get around this.

Plot0.Set(SMA(Math.Log(Close[0])), 21)); is a no go.

Here's some simple code I tried to get around it but it didn't work. It should help illustrate the problem though.

double cl = Close[0];
double Lcl = Math.Log(cl);
DSOne.Set(Lcl);
Plot0.Set(SMA(DSOne,
21));

Any help would be greatly appreciated!


Started this thread Reply With Quote

 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: Futures & Stocks
Posts: 9,887 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,114



srgtroy View Post
I'm new to NinjaScript and not the world's greatest programmer so if this is a stupid question please excuse me.

I would like to plot the value of the 21 period SMA of the logarithms of the close values of each bar.

However, the Math.Log function won't let me use a DataSeries value. And the SMA function won't let me use a double value. How can I get around this.

Plot0.Set(SMA(Math.Log(Close[0])), 21)); is a no go.

Here's some simple code I tried to get around it but it didn't work. It should help illustrate the problem though.

double cl = Close[0];
double Lcl = Math.Log(cl);
DSOne.Set(Lcl);
Plot0.Set(SMA(DSOne,
21));

Any help would be greatly appreciated!

You need to declare a new DataSeries in the Variables section, for example
 
Code
private DataSeries logPrice;
then initialize this DataSeries object in the Initialize() Section
 
Code
logPrice = new DataSeries(this);
Then you can use it in OnBarUpdate():
 
Code
logPrice.Set(Math.Log(Input[0]));
Value.Set(SMA(logPrice, 21)[0]);
The point is that you need a DataSeries object to be able to apply the SMA; you cannot apply it to a variable of type double.

But what do you want to do with a SMA of the logarithm of price?

You cannot display it on the main panel, and for intraday charts you definitely do not need it.


Reply With Quote
Thanked by:
  #4 (permalink)
 
srgtroy's Avatar
 srgtroy 
Los Angeles, California Republic
Legendary  R.I.P. 1965-2023 
 
Experience: None
Platform: Sierra Chart
Broker: CQG
Trading: ES
Posts: 1,928 since Jan 2011
Thanks Given: 1,375
Thanks Received: 3,723

Thanks for the help.

I did setup the necessary DataSeries info in the Variables and Initialize sections. That didn't turn out to be the problem. Nevertheless, you're example helped me figure it out. I noticed the first letter in logPrice was not capitalized, so I changed all the first letters I had to small caps and it worked.

Thanks, again.


Started this thread Reply With Quote




Last Updated on June 22, 2011


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts