PDA

View Full Version : Calculations in OnStartUp()


MXASJ
02-25-2010, 11:42 PM
Hey guys,

Two questions:

I'm running a strategy that does a spread calculation between two intruments, with the code in OnStartUp() and the custom methods in OnBarUpdate(). Is this a good use for OnStartUp()? The spread plots as desired in the strategy.

But... in OnStartUp I've also tried to code a zscore plot using the spread as its input. The code is commented out below as it won't compile. Any pointers on what I'm doing wrong?

Thanks!



protected override void OnStartUp()///////////////////////////////////////////////////////////
{
//Do something else...
}
private void SpreadCalculator()
{
spreadRough = (Closes[0][0]*quotedQuantity)-(Closes[1][0]*hedgeQuantity);
spread = Math.Round (spreadRough, 2);
}

private void SpreadPlotter()
{
StrategyPlot(0).Value.Set(spread);
}

//private void ZScoreCalculator()
//{
//SMA mySMA = SMA(spread, zPeriod);
//StdDev myStdDev = StdDev(spread, zPeriod);

//zscore = (spread-mySMA)/(myStdDev);
//}

private void GoLongSpread()
{etcetc}

NinjaTrader_Josh
02-26-2010, 10:11 AM
MXASJ,

What you have pasted has nothing to do with OnStartUp() as all of your code is outside in other methods and such. Why your ZScoreCalculator doesn't work would need to be addressed directly. Please provide the exact error messages.

MXASJ
02-26-2010, 06:18 PM
My errors involved operations with doubles and DataSeries which have been corrected since my post. If anyone is interested here is the code for plotting a ZScore in a strategy (or my interpretation of the ZScore). In this case I'm plotting the ZScore of another DataSeries called spread:


in Variables
...
private int zPeriod = 14; //Loopback period for z-score calculation

private double spreadRough;
private DataSeries spread;
private double zscore;
private DataSeries zplot;

in Initialize()
...
Add(StrategyPlot(0));
Add(StrategyPlot(1));
StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
StrategyPlot(0).PanelUI = 2;
StrategyPlot(1).Plots[0].Pen.Color = Color.Red;
StrategyPlot(1).PanelUI = 3;
spread = new DataSeries(this);
zplot = new DataSeries(this);

in OnStartUp()
...
private void ZScoreCalculator()
{
double i,j;
i=SMA(spread, 14)[0];
j=StdDev(spread,14)[0];
zscore = ( (spread[0]-i) / j );
zplot.Set(zscore);
}

private void SpreadPlotter()
{
StrategyPlot(0).Value.Set(spread[0]);
StrategyPlot(1).Value.Set(zplot[0]);
}

...

In OnBarUpdate()

SpreadCalculator();
ZScoreCalculator();
SpreadPlotter();
...



Is there a way to change the name of the StrategyPlots in the chart panels in NT7?

NinjaTrader_Josh
03-01-2010, 09:05 AM
MXASJ,

Just create yourself another indicator with whatever name you want and have it done exactly the same way as StrategyPlot. Then in your code call that indicator instead of StrategyPlot.

gabga100
03-13-2010, 05:23 PM
I am using this

[ Add(StrategyPlot(0));
StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
StrategyPlot(0).PanelUI = 2;
CalculateOnBarClose = true;
myDataSeries = new DataSeries(this, MaximumBarsLookBack.Infinite); ]


And I get the error the name StrategyPlot does not exist in the current context .... any idea why ?

NinjaTrader_Ben
03-13-2010, 11:43 PM
Hello,

Since you are asking about NT7, I will have someone reply to you on Monday. Thank you for your patience.

NinjaTrader_Josh
03-15-2010, 08:16 AM
gabga100,

You actually have to have an indicator called "StrategyPlot" for this to work. Please install it from the reference samples section of the forum.

http://www.ninjatrader-support2.com/vb/showthread.php?t=6651