PDA

View Full Version : Problem adding 2:nd instrument


FREEN
05-25-2010, 04:20 PM
Appreciate help on why the custom indicator "VolRATIO" uses the primary data series in this strategy.

The indicator is a multi instrument indicator that uses the same primary data series as the strategy and adds the "NAVOL" instrument as a second data series. So two questions:

1. Where do I go wrong?

2. When a multi instrument indicator adds a second instrument, does this second instrument has to be added once again in the strategy (as bellow), or is it allready added/called by the indicator?


protectedoverridevoid Initialize()
{
Add("NAVOL", PeriodType.Minute, 30);
Add(SMA(Fast));
Add(SMA(Slow));
Add(VolRATIO(2, 65));
Add(SMA(Fast));
Add(SMA(Slow));
Add(VolRATIO(2, 65));
CalculateOnBarClose = true;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// Condition set 1
if (CrossAbove(SMA(Fast), SMA(Slow), 29)
&& CrossAbove(VolRATIO(BarsArray[1], 2, 65).Plot0, Vol, 29))
{
EnterLong(DefaultQuantity, "");
}
// Condition set 2
if (CrossBelow(SMA(Fast), SMA(Slow), 29)
&& CrossAbove(VolRATIO(BarsArray[1], 2, 65).Plot0, Vol, 29))
{
EnterShort(DefaultQuantity, "");
}
}

//Thanks!

NinjaTrader_RyanM
05-25-2010, 05:00 PM
Hello Freen,

You will have to add the series in the strategy as well. For your strategy, BarsArray[1] should refer to the second series (NAVOL, 30 Minute).

FREEN
05-26-2010, 02:08 AM
So, the second data series of the strategy will be used as the primary data series in the indicator in my code?

What defines which of the dataseries in the strategy is primary and secondary for the indicator? Should I do something like this:

CrossAbove(VolRATIO(BarsArray[0], BarsArray[1], 2, 65).Plot0, Vol, 29))

NinjaTrader_Bertrand
05-26-2010, 06:23 AM
FREEN, the MultiSeries indicator would add / load whatever series were coded in there by you - it would use the dataseries as it's primary series that you run it on in your strategy, so with pointing to BarsArray[1] for the indicator, it would use the NAVOL instrument of your strategy as it's primary one.

FREEN
05-26-2010, 02:42 PM
FREEN, the MultiSeries indicator would add / load whatever series were coded in there by you - it would use the dataseries as it's primary series that you run it on in your strategy, so with pointing to BarsArray[1] for the indicator, it would use the NAVOL instrument of your strategy as it's primary one.

So, since the primary instrument is (should be) the same for the strategy and the indicator in the strategy, I donīt need to add the secondary instrument of the indicator in the strategy code? Did I get that right?

Hello Freen,

You will have to add the series in the strategy as well. For your strategy, BarsArray[1] should refer to the second series (NAVOL, 30 Minute).

Sorry, Iīm confused here. The strategy and the indicator runs (should run) on the same primary instrument. My question was refering to if I need to add the secondary instrument of the indicator into the strategy and, if so, how does the the indicator know wich one is primary and secondary.

NinjaTrader_RyanM
05-26-2010, 03:15 PM
Hello Freen,

You would need to add the secondary series to the strategy if you use the convention VolRATIO(BarsArray[1]) when evaluating your conditions for entry. BarsArray[1] does not exist in the context of the strategy unless you Add the series.

If your indicator plots use the secondary series, then you do not have to add to the strategy to reference this. It's not enough to add the series to the indicator, the secondary series must set plots based from it.

FREEN
05-30-2010, 07:00 PM
Sorry for all these rookie questions. I guess Iīm confused by the fact that the indicator intended for the second instrument plots the primary instrument in the backtest panel. Could you point me to the instructions how this is done?

I thought it simply could be done like this;

protectedoverridevoid Initialize()
{
Add("NAVOL", PeriodType.Minute, 30);
Add(SMA(Fast));
Add(SMA(Slow));
Add(VolRATIO(BarsArray[1], 2, 65));
Add(SMA(Fast));
Add(SMA(Slow));
Add(VolRATIO(BarsArray[1], 2, 65));
CalculateOnBarClose = true;
}

But that code wonīt run. (The VolRatio indicator is a single instrument indicator intended for "NAVOL", hence no instrument is added in the indicator code.)

NinjaTrader_Bertrand
05-31-2010, 05:23 AM
FREEN, this will unfortunately not work as you can't access the BarsArray object in the Initialize() method. You could calculate the indicator in the OnBarUpdate() on the the various BarsArrays and then enter trades based on those.

FREEN
05-31-2010, 05:42 AM
Ok, thanks. But how do I display the indicator plots for the second, third... instruments in the backtest panels?

Itīs kind of nice to eyeball how all intrument indicators in the strategy are behaving.

NinjaTrader_Bertrand
05-31-2010, 06:24 AM
You could for example use the strategy plot technique for this task -

http://www.ninjatrader.com/support/forum/showthread.php?t=6651

FREEN
05-31-2010, 09:56 AM
Thanks Bertrand! The instructions you point to is for "internal strategy calculations that are difficult to migrate to an indicator". What I want is to plot the indicators for the second instrument, just as in a chart. Is this concidered "internal strategy calculations that are difficult to migrate to an indicator"?

Is there a way to do this in the strategy analyzer or is this plot setup only possible in the charts?

NinjaTrader_Bertrand
05-31-2010, 10:35 AM
FREEN, yes you could use this approach to visualize MultiSeries indicator on the charts (however that would require extra coding as per the sample approach), for NT7 just create a MultiSeries indicator and then just add this to your strategy - this would not require the passing in of the BarsArray dataseries paramter in the strategy, as the indicator itself would add the series and do the necessary MultiSeries calculations needed.

MTFIndicator is your indicator running on for example three series in NT7 and plots those.

In the strategy Initialize() just go -

Add(MTFIndicator(Close)) > this will pass just the primary series to the indicator to use it, the added ones would be taken care of by the indicator internally.

FREEN
05-31-2010, 01:47 PM
FREEN, yes you could use this approach to visualize MultiSeries indicator on the charts (however that would require extra coding as per the sample approach), for NT7 just create a MultiSeries indicator and then just add this to your strategy - this would not require the passing in of the BarsArray dataseries paramter in the strategy, as the indicator itself would add the series and do the necessary MultiSeries calculations needed.

The indicator used for the second instrument in the strategy is a single instrument indicator. Do you mean that I could code this indicator as a multi instrument indicator, just not use the primary series in the indicator calculation?

FREEN
05-31-2010, 03:29 PM
Well, HURRAY! This worked:

The indicator used for the second instrument in the strategy is a single instrument indicator. Do you mean that I could code this indicator as a multi instrument indicator, just not use the primary series in the indicator calculation?

Itīs just that I have to have one indicator for chart plotting and another for strategy/backtest plotting, however doing the same thing.

Thanks lots and sorry for all my confusion in this.

NinjaTrader_Bertrand
06-01-2010, 05:03 AM
You're welcome no worries, great you got it worked out.

FREEN
06-06-2010, 12:33 PM
CarlZAZ, did you just delete your question?

Arenīt you using the wrong syntax for the "Add" method here?!:

Add(PeriodType.Minute, ATRBasisBars);

Should be:

Add("ATRBasisBars", PeriodType.Minute, 30);

(If ATRBasisBars is your second instrument and you use 30 min bars)

FREEN
06-20-2010, 06:01 PM
Iīm trying to get NT to send a mail at a specific time but it wonīt send, (NT test mail works fine). How often is this statement evaluated? Is the evauation only made on bar update so that I should code an interval rather than a specific time? Any other idea why this wonīt send?


if (ToTime(Time[0]) == ToTime(19, 0, 0))
{
SendMail("user@workplanner.se", "user@workplanner.se", "Test mail", "Test mail main text");
}


Thanks, Fredrik

NinjaTrader_Ben
06-20-2010, 10:33 PM
Hello,

If you have that code in the OnBarUpdate block of code it will be evaluated each new tick (each bar update). However, it may be that you are not getting a tick right at (19,0,0). You might want to use

if (ToTime(Time[0]) >= ToTime(19, 0, 0) && not_emailed)
{
SendMail("user@workplanner.se", "user@workplanner.se", "Test mail", "Test mail main text");

not_emailed = false;

}

Or course you will need to reset not_emailed at some point to true when you want to start looking for the time again. The not_emailed variable is a bool flag that toggles to prevent multiple emails from being sent.

FREEN
06-21-2010, 11:47 AM
Thanks Ben,

That would work fine. Would it also be possible to have the code before the "OnBarUpdate" to have it evaluated in realtime as bellow?

CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>

if (ToTime(Time[0]) == ToTime(19, 0, 0))
{
SendMail("user@workplanner.se", "user@workplanner.se", "Test mail", "Test mail main text");
}

protected override void OnBarUpdate()
{
// Condition set 1

NinjaTrader_RyanM
06-21-2010, 01:37 PM
Hello Freen,

No, that wouldn't work there. You would want to put in OnBarUpdate() or develop your own custom method.

If you want to base the calculation on your PC clock rather than bar time stamps, you can use DateTime.Now

if (ToTime(DateTime.Now) == ToTime(19, 0, 0))

FREEN
06-21-2010, 04:00 PM
Thanks,

Would that be evaluted in realtime? Doesnīt the "OnBarUpdate()" method make the code run in the bar duration intervals wich would make it almost impossible to hit the second in the "ToTime(19, 0, 0)" method?!

NinjaTrader_RyanM
06-21-2010, 04:16 PM
Yes, OnBarUpdate() will update on every incoming tick or on bar close if you have CalculateOnBarClose = true.

My suggestion was to check your computers date/time rather than a specific bar time stamp. A bar might not end exactly at 19:00:00, but your computer clock will at some point. There's still precision needed if the market you're viewing isn't actively trading. OnBarUpdate could trigger just before or after the specified time.

You can create custom events that are not tied to OnBarUpdate(). This reference sample (http://www.ninjatrader.com/support/forum/showthread.php?t=5965)offers a sample implementation.

FREEN
06-27-2010, 09:14 AM
I donīt get this working. If I have set the Time Series to 1 min in the Strategies Tab, wouldnīt this code send a mail at some point (each minute/bar update?) after 11:05 based on the server/computer clock? "not_emailed" is reset to "true" a all times to make sure this wasnīt the problem:

#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)

private bool not_emailed;

#endregion

/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{

not_emailed = true;

if (ToTime(DateTime.Now) >= ToTime(11, 05, 0) && not_emailed)
{
SendMail("user@workplanner.se", "user@workplanner.se", "Test mail", "Test mail main text");

not_emailed = false;
}

NinjaTrader_Ben
06-27-2010, 10:19 PM
Hello,

Print() out your two time values in your criteria to see what they are showing. Also, try using Time[0] instead of .Now.

FREEN
06-28-2010, 01:44 PM
Tnx Ben,

It runs, donīt know what I did wrong the first time. Might just have forgotten to "Enabled" the strategy. Sorry for the inconvenience.

Does the "Print()" method display in the primary series panel? I noticed you often suggest this approach in debugging.

NinjaTrader_RyanM
06-28-2010, 02:19 PM
Hi Freen,

Print will output to the Tools > Output Window. Additional debugging help is available at the link below:

http://www.ninjatrader.com/support/forum/showthread.php?t=3418