View Full Version : Calculate time in seconds between the close of two bars
OnePutt
12-18-2006, 01:55 PM
What is the NinjaScript code for printing the time in seconds between two bars? Thanks
NinjaTrader_Ray
12-18-2006, 02:20 PM
Try something like:
Print(Time[0].Subtract(Time[1]).Seconds.ToString());
Ray
OnePutt
12-18-2006, 02:46 PM
I added the statement below but when I do PRINT stops working (nothing prints). If I comment outthe statement you suggested PRINT works okay.
seconds = Time[0].Subtract(Time[1]).Seconds;
Print("Bar Time " + CurrentBar.ToString("00000"));
I also tried using just the PRINT statement and nothing prints.
Print(Time[0].Subtract(Time[1]).Seconds.ToString());
NinjaTrader_Ray
12-18-2006, 02:50 PM
When something does not work always check the Log tab to see what errors may have been generated.
I suspect the issue is that Time[1] is throwing an exception since on the 1st bar, Time[1 bar ago] does not yet exist. Therefore, try something like:
if (CurrentBar > 0)
Print(Time[0].Subtract(Time[1]).Seconds.ToString());
Ray
OnePutt
12-18-2006, 03:07 PM
That was it. Thanks for your help.
I notice that by adding the following statement that some bars exceed 60 seconds and therefore have a value in the minutes variable. Is there a way to return the "total elapsed seconds" including minutes and seconds?
Time[0].Subtract(Time[1]).Minutes.ToString() + " " +
NinjaTrader_Dierk
12-18-2006, 06:34 PM
Try
Time[0].Subtract(Time[1]).TotalSeconds.ToString()
OnePutt
12-19-2006, 01:37 PM
Works great. Thanks again
tquinn
02-19-2007, 04:19 PM
After Placing a limit Order I want to wait for a period of time for a fill, then cancel if not filled. I'm thinking of something like;
if ( Time[0] > OrderTime.AddSeconds( AverageBarInterval(5)) )
{
AsmStrategyCancelEntryOrder(OrderId)
}
NinjaTrader_Ray
02-20-2007, 02:16 AM
That looks like it should work.
tquinn
02-20-2007, 03:06 AM
Hi Ray,
How do I make the AverageBarInterval() method?
NinjaTrader_Ray
02-20-2007, 03:41 AM
Under variables:
private DataSeries barInterval = null;
WithinInitialize()
barInterval = new DataSeries(this);
Within OnBarUpdate()
barInterval.Set(CurrentBar > 0 ?(double) Time[0].Second - Time[1].Second : 0);
if (Time[0] > OrderTime.AddSeconds((int) SMA(barInterval, 5)[0])
// Do something....
mgin98
05-27-2008, 08:28 PM
I added the statement below but when I do PRINT stops working (nothing prints). If I comment outthe statement you suggested PRINT works okay.
seconds = Time[0].Subtract(Time[1]).Seconds;
Print("Bar Time " + CurrentBar.ToString("00000"));
I also tried using just the PRINT statement and nothing prints.
Print(Time[0].Subtract(Time[1]).Seconds.ToString());
Still learning ninjascript but how would I turn this into a simple histogram?
So far I have this in void Initialize()
Add(new Plot(new Pen(Color.Red, 3), PlotStyle.Bar, "seconds"));
And this in void OnBarUpdate()
Value.Set(Time[0].Subtract(Time[1]).Seconds);
It compiles ok but nothing displays on the chart.
NinjaTrader_Josh
05-28-2008, 03:10 AM
I suggest you check the Control Center logs for errors when you run it. I suspect you may run into an index error outlined in this tip: http://www.ninjatrader-support.com/vb/showthread.php?t=3170
mgin98
05-28-2008, 02:14 PM
I suggest you check the Control Center logs for errors when you run it. I suspect you may run into an index error outlined in this tip: http://www.ninjatrader-support.com/vb/showthread.php?t=3170
Thanks Josh. That did it.
if (CurrentBar < 1)
return;
if (CurrentBar >= 1)
Value.Set(Time[0].Subtract(Time[1]).Seconds);
NinjaTrader_Josh
05-29-2008, 02:38 AM
The second if-statement is unnecessary. If CurrentBar is less than 1 we return and stop processing the rest of the OnBarUpdate() method. Anything that comes after will only be processed if CurrentBar is >= 1 by default.