ema = EMA((Open[0] - Close[0]) / (Time[0] - Time[1]), 10);
Announcement
Collapse
No announcement yet.
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.<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
-
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 Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
649 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
370 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
573 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
576 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment