View Full Version : Spread Indicator
luitom
12-04-2007, 12:23 PM
I created a spread indicator to be used with Market Analyzer.
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.Firebrick, PlotStyle.Bar, "Spread"));
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
Value.Set(GetCurrentAsk() - GetCurrentBid());
}
I works perfectly if I add it in one of the columns. However the reason why I wanted to use it is that I wanted to a column to keep track of the difference between the advancing and declining issued on NYSE and NASDAQ. For some reason the indicator does not make the calculation for the two indexes but it does for all the other insutruments, as shown in the picture.
NinjaTrader_Ray
12-04-2007, 12:55 PM
This is likely since indexes do not have a bid/ask price since they are not tradeable.
luitom
12-04-2007, 01:44 PM
Thanks for your reply. Can you suggested another route to be able to watch the difference between advincing and declining issues in the market analyzer? Is there a way to use the actual instrument inside Ninjascript and create an indicator that can substruct the two instruments?
NinjaTrader_Ray
12-04-2007, 02:00 PM
Unfortunately multi-series indicators are not currently supported. This is on our list for future enhancements.
luitom
12-13-2007, 04:54 AM
Another little question on the Market Analyzer and Indicators: I created an indicator that keeps track keeps track of the difference between the last price and the open of the current bar and that works on Market analyzer (I set it to 30 min bar and Calculate on bar close set to False.
protected override void OnBarUpdate()
{
Value.Set(Close[0] - Open[0]);
}
How would go about creating an indicator that calculates the difference between the the current bar open and the last bar Open (30 min bars)? This is because I need to have in one Market Analyzer cell this number). I tried with
protected override void OnBarUpdate()
{
Value.Set(Open[0] - Open[1]);
}
but it does not seem to work. The only thing I was able to do is create an indicator (to be used in a Market analyzer cell) that has the value of the open of the previous bar by having Calculate on bar close set to True and then
protected override void OnBarUpdate()
{
Value.Set(Open[0])
}
How can I solve my problem?
Rollins
12-13-2007, 05:13 AM
IMO all you have to do is set calculate on bar close to false. The value should be calculate for the current bar, bit of a waste but that's how it is.
luitom
12-13-2007, 05:15 AM
Thanks for your reply. Actually I do not want it to change with every tick. I need to have a cell that has one number: the difference between the open of the current 30 minute bar and the open of the last third minute bar. Obviously every 30 minute the number will change, but only after there is new bar
Rollins
12-13-2007, 05:22 AM
Why don't you use Close[0] - Open[0]? This would produce about the same result when calculate on bar close is true?
Logic behind it is: the value is calculate when a new bar starts to print, 0 refers in this case to the previous bar and Close of the previous bar should theoretically be very close to the Open of the current bar.
When I say current, previous, next I mean the bars on the chart, NT is lagging one bar behind if calculate on bar close is true.
If caluclate on bar close is false: it's Open[ 0 ] - Open[ 1 ], Open[ 0 ] doesn't change, only Close[ 0 ], High, Low ... can change with every tick, sorry for confusing you.
luitom
12-13-2007, 05:52 AM
This is actually a very good idea, but in this way the number will be the difference between the close and the open of the previous bar; but the close of the previous bar and the open of the current bart may be VERY different. Unfortunately I relly need that particular number.
How is it possible that NT does not have a way to keep track and use the data of the previous bars in an indicator?
I looked at data Series
Add(Instrument, PeriodType.Minute, 30);
but they do not seem to work for indicators, only for strategies. I really do not know what to do
Rollins
12-13-2007, 06:10 AM
This is actually a very good idea, but in this way the number will be the difference between the close and the open of the previous bar; but the close of the previous bar and the open of the current bart may be VERY different. Unfortunately I relly need that particular number.
Intraday the difference should be minor, especially on long period bars like 30mins. Between sessions things are very different.
The 2nd approach with calculateonbarclose false should deliver the exact results. Open[ 0 ] - Open[ 1 ]
luitom
12-13-2007, 06:46 AM
Unfortunately, I am trying right now in real time and the difference is sometimes significant.
The second approach I have tried already but for some reason if I use
Open[ 0 ] - Open[ 1 ] with calculateonbarclose false what happens is that the number changes in real time and the Open[1] apparently does not work inside the indicator script
Rollins
12-13-2007, 06:53 AM
Either set BarsRequired to 2 in Initialize()
or use if( CurrentBar > 0 ) Value.Set(Open[0] - Open[1]);
Otherwise the indicator might crash.
I have no idea why Open[ 0 ] should change in real time, that would be a bug.
Unless it represents the Open of the last tick, which I don't think.
In that case add if( FirstTickOfBar )
NinjaTrader_Ray
12-13-2007, 08:08 AM
I have not read through all recent replies but to get what you want...
protected override void OnBarUpdate()
{
if (CurrentBar > 0)
Value.Set(Open[0] - Open[1]);
}
Then apply indicator to MA column and select 30 min series.
luitom
12-13-2007, 08:45 AM
I added the line as you suggested if( CurrentBar > 0 ) Value.Set(Open[0] - Open[1]); and it works now. Thanks a lot for your help. Even though I do not understand why it works.
NinjaTrader_Ray
12-13-2007, 08:52 AM
This will explain it.
http://www.ninjatrader-support.com/vb/showthread.php?t=3170