ema = EMA((Open[0] - Close[0]) / (Time[0] - Time[1]), 10);
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!
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
NinjaTrader
Using division on a double and System.Timespan
Collapse
X
-
Using division on a double and System.Timespan
I am trying to create an indicator that plots how far a volume bar traveled Open[0] - Close[0] divided by the time it took to complete Time[0] - Time[1] but I have not been able to figure out how to convert the timespan to a double to complete the calculation
Code:Tags: None
-
Hello MisterGee,
Thanks for your post.
To accomplish this, you would need to save the TimeSpan to a variable called something like "myTime" and then use myTime.TotalHours or myTime.TotalMinutes depending on the most significant measurement you want to get.
For example, if Time[0] - Time[1] is a value of 1 minute (00:01:00) and you use myTime.TotalMinutes, this will return a double value of 1.
Another example is if Time[0] - Time[1] is a value of 1 minute and you use myTime.TotalHours, this will return a double value of 0.0166666666666667.
Further, EMA() requires you to pass in a Series<double> and an int. You would need to create a custom Series<double> variable, assign your calculations to that Series<double> variable, and use the Series<double> when calling EMA.
For example:
//class-level variables
private TimeSpan myTime;
private Series<double> mySeriesDouble;
//OnStateChange() State.DataLoaded
mySeriesDouble = new Series<double>(this);
//OnBarUpdate()
myTime = Time[0] - Time[1];
mySeriesDouble[0] = (Open[0] - Close[0]) / myTime.TotalMinutes;
Print(EMA(mySeriesDouble, 10)[0]);
See the help guide documentation below for more information.
Creating Custom Series: https://ninjatrader.com/support/help...t8/seriest.htm
EMA: https://ninjatrader.com/support/help...onential_e.htm
For more information about converting TimeSpans to Doubles, you would need to research this topic more by doing a Google search for something like "Convert TimeSpan to Double C#".
Last edited by NinjaTrader_BrandonH; 06-13-2023, 06:58 AM.Brandon H.NinjaTrader Customer Service
-
Have you considered It seems, reading between the lines, that you're either trying to divide the candle body size by some unit of time e.g. the number of minutes or the number of seconds. Have you considered doing it that way e.g. ema = EMA((Open[0] - Close[0]) / (Time[0] - Time[1].TotalSeconds), 10); ? You'll want to use a Math.Max in there to control for if the times are the same so you don't get a divide by zero... or you could use minutes... or hours... or something else appropriate. I would put this into a series first then EMA that.
Edit: re-reading this thread I see now that NinjaTrader_BrandonH suggested much the same thing here and I read past that at first. The only thing I would add to that is where he's saying "mySeriesDouble = (Open[0] - Close[0] etc." it should be "mySeriesDouble[0] = (Open[0] - Close[0] etc." because you can't assign a series variable to be a double value.Last edited by QuantKey_Bruce; 06-12-2023, 05:06 AM.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by AaronKoRn, Today, 09:49 PM
|
0 responses
2 views
0 likes
|
Last Post
by AaronKoRn
Today, 09:49 PM
|
||
Started by carnitron, Today, 08:42 PM
|
0 responses
8 views
0 likes
|
Last Post
by carnitron
Today, 08:42 PM
|
||
Started by strategist007, Today, 07:51 PM
|
0 responses
9 views
0 likes
|
Last Post Today, 07:51 PM | ||
Started by StockTrader88, 03-06-2021, 08:58 AM
|
44 responses
3,975 views
3 likes
|
Last Post
by jhudas88
Today, 06:59 PM
|
||
Started by rbeckmann05, Today, 06:48 PM
|
0 responses
9 views
0 likes
|
Last Post
by rbeckmann05
Today, 06:48 PM
|
Comment