Thanks.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy for stock pairs
Collapse
X
-
Strategy for stock pairs
How could you make a strategy for a stock pair. I am looking to implement a strategy using bollinger bands on the ratio of two stock prices. How would I be able to do this and then backtest on that pair. It seems like strategy analyzer only allows you to backtest one stock at a time. Any Ideas?
Thanks.Tags: None
-
Hello,
Thank you for your post.
This requires custom NinjaScript programming.
Please see the multi-instrument instructions here: http://www.ninjatrader-support.com/H...struments.htmlRay S.NinjaTrader Customer Service
-
Ok it seems like what I have to do is use the Add method to add the two stocks in the script, index them. Then reference that in the bollinger indicator. But since i am explicitly stating the dataset i am using the script, I would have to run the backtest from it too. (because i wouldn't be able to select stocks in the strategy analyzer window). Is there a way to run the backtest in ninja script if so, can you link to the help article.
Comment
-
intuit2k2,
I am not sure I follow you exactly. What you want to do is add the series to make it a multi-series strategy and from there you can pass it to your Bollinger Bands.
In the Strategy Analyzer you can choose the first instrument in your backtest. All subsequent instruments have to be chosen by the code. Unfortunately you cannot trigger a backtest per se from the code. It has to be either done in the Strategy Analyzer or in a chart.Josh P.NinjaTrader Customer Service
Comment
-
Is there a way reference which stock you're referrign to by its index number. So lets say the stock that will be chosen by strategy analyzer is given index number 1, and the stock you add using the Add () method is index number 2. Is there a way to write something like the following.
Index (1)/Index (2).Bollinger(2,20) ... trying to say thats its price ratios bollinger band... ?
Comment
-
My question was specifically on the syntax of the bollinger method..
How would you be able to do:
Index (1)/Index (2).Bollinger(2,20) ... trying to refer to the price ratios bollinger band... ?
The alternative of just computing the quotient of the two bollinger bands wouldn't wokr because they wouldn't be equivalent.
Does this make sense?
Comment
-
intuit2k2, not sure I fully follow - you can compute your ratio as dataseries first and then use this as input data for your BollingerBands method - http://www.ninjatrader-support.com/H...Bollinger.htmlBertrandNinjaTrader Customer Service
Comment
-
This is what I have so far. I tried to do what you suggested to make a new dataSeries and followed the example in the link you posted. Do you see anything wrong in the following code...
I got an expected class error:
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class BolBands : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#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()
{
Add("MSFT",PeriodType.Day, 200);
CalculateOnBarClose = true;
}
#region Variables
private DataSeries myDataSeries;
#endregion
}
protected override void Initialize()
{ myDataSeries = new DataSeries(this); }
protected override void OnBarUpdate()
{ myDataSeries.set(close[1]/close[0]); }
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (Bollinger(2, 20).Upper[0] < GetCurrentAsk())
{
EnterShort(DefaultQuantity, "");
}
// Condition set 2
if (Bollinger(2, 20).Upper[0] > GetCurrentAsk())
{
ExitShort("", "");
}
// Condition set 3
if (Bollinger(2, 20).Lower[0] > GetCurrentAsk())
{
EnterLong(DefaultQuantity, "");
}
// Condition set 4
if (Bollinger(2, 20).Lower[0] < GetCurrentAsk())
{
ExitLong("", "");
}
}
#region Properties
[Description("")]
[Category("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
Comment
-
You did this:
protected override void Initialize()
{
Add("MSFT",PeriodType.Day, 200);
CalculateOnBarClose = true;
}
#region Variables
private DataSeries myDataSeries;
#endregion
}
protected override void Initialize()
{ myDataSeries = new DataSeries(this); }
You should not create Initialize() twice. Just move that myDataSeries line into the original Initialize() as a new line. You should also put the private DataSeries line in the original Variables region.Josh P.NinjaTrader Customer Service
Comment
-
Ok This is what I have..
// This namespace holds all strategies and is required. Do not change it.
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class BolBands : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
private DataSeries myDataSeries;
// User defined variables (add any user defined variables below)
#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()
{
Add("MSFT",PeriodType.Day, 200);
CalculateOnBarClose = true;
myDataSeries = new DataSeries(this); }
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{ myDataSeries.set(close[1]/close[0]); }
/// Condition set 1
if (Bollinger(myDataSeries,2, 20).Upper[myDataSeries] < CurrentBar() )
{ EnterShort(DefaultQuantity, ""); }
/// Condition set 2
if (Bollinger(myDataSeries,2, 20).Upper[myDataSeries] > CurrentBar() )
{
ExitShort("", "");
}
/// Condition set 3
if (Bollinger(myDataSeries,2, 20).Lower[myDataSeries] > CurrentBar() )
{
EnterLong(DefaultQuantity, "");
}
/// Condition set 4
if (Bollinger(myDataSeries,2, 20).Lower[0] < CurrentBar ())
{
ExitLong("", "");
}
}
#region Properties
[Description("")]
[Category("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
These are the errors:
Mostly in lines 49-59 including identifier expected (doesnt' recognize myDataSeries), Class member decleration expectation and other other errors "] expected"
Thanks.
Comment
-
I only have two errors left. Code is below.
error in line 80: Error "} expected"
error in line 83: Error "Expected class, delegate, enum, interface, or struct"
// This namespace holds all strategies and is required. Do not change it.
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class BolBands : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
private DataSeries myDataSeries;
// User defined variables (add any user defined variables below)
#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()
{
Add("MSFT",PeriodType.Day, 200);
CalculateOnBarClose = true;
myDataSeries = new DataSeries(this); }
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
{ myDataSeries.set(close[1]/close[0]); }
/// Condition set 1
if (Bollinger(myDataSeries,2, 20).Upper[myDataSeries] < CurrentBar() );
{ EnterShort(DefaultQuantity, ""); }
/// Condition set 2
if (Bollinger(myDataSeries,2, 20).Upper[myDataSeries] > CurrentBar() );
{
ExitShort("", "");
}
/// Condition set 3
if (Bollinger(myDataSeries,2, 20).Lower[myDataSeries] > CurrentBar() );
{
EnterLong(DefaultQuantity, "");
}
/// Condition set 4
if (Bollinger(myDataSeries,2, 20).Lower[0] < CurrentBar ());
{
ExitLong("", "");
}
}
}
#region Properties
[Description("")]
[Category("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); } }
#endregion
}
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by WHICKED, 03-27-2024, 12:33 PM
|
4 responses
41 views
0 likes
|
Last Post Today, 12:28 AM | ||
Started by f.saeidi, Today, 12:12 AM
|
0 responses
3 views
0 likes
|
Last Post
by f.saeidi
Today, 12:12 AM
|
||
Started by Conall, Today, 12:06 AM
|
0 responses
5 views
0 likes
|
Last Post
by Conall
Today, 12:06 AM
|
||
Started by proptradingshop, 03-21-2024, 09:50 AM
|
6 responses
27 views
0 likes
|
Last Post Today, 12:02 AM | ||
Started by mly916, Yesterday, 09:48 PM
|
1 response
14 views
0 likes
|
Last Post Yesterday, 11:54 PM |
Comment