View Full Version : Making A Multi Instrument % Spread Indicator
vt2009
04-13-2010, 03:08 PM
I have almost no experience and I am trying to make an indicator that measures % deviation from another instrument. I have read the multi instrument series chapter. I have in my Initialize method:
Add("XXX",PeriodType.Minute, 1);
Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Spread"));
CalculateOnBarClose = true;
Overlay = true;
What math logic in my OnBarUpdate will take the percent change from the first bars price? I want the y axis to be percent with 0 as the baseline. For an example, go to google finance and compare 2 stocks...I want this type of graph.
NinjaTrader_Austin
04-13-2010, 03:34 PM
vt2009, we can help you out with specific errors and such but due to time and bandwidth limitations, we can't create specific indicators/strategies. If you get something going and you feel like it isn't working how it should, let us know and we'll take a look.
However, I can give you hints/tips:
You will need to create two variables that will store the first bar's price. Lets call these values inst0start and inst1start.
In OnBarUpdate(), when CurrentBar == 0, set those values.
You can use either Closes[1][0] to obtain the secondary instrument's most recent closing price or in OnBarUpdate() with an if (BarsInProgress == 1) { inst1start = Close[0];}
For the primary instrument's most recent price, you can use Closes[0][0]. Or the same check as above but with BarsInProgress == 0.
To get the percent change, you just have to manipulate the current prices verse the price from the start.
Please let us know if you have any other questions.
vt2009
04-13-2010, 03:43 PM
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
If CurrentBar == 0;
int inst0start=Close[0][0];
int inst1start=Close[1][0];
}
Like this? Please be more clear. I only need to know 1 way to do it.
NinjaTrader_Austin
04-13-2010, 03:58 PM
vt2009, I'm not supposed to do this and do not expect complete scripts from us in the future, but I had this script laying around so here it is:
#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.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class PercentDifference : Indicator
{
#region Variables
private string secondInstrument = @"TF 06-10"; // Default setting for SecondInstrument
private double inst0start, inst1start = 0;
private double inst0chg, inst1chg = 0;
private double inst0pct, inst1pct = 0;
private double difference = 0;
#endregion
/// <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(secondInstrument, PeriodType.Minute, 1);
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "PercentPlot"));
Add(new Line(Color.FromKnownColor(KnownColor.Black), 0, "ZeroLine"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0 && BarsInProgress == 1)
{
inst0start = Closes[0][0];
inst1start = Closes[1][0];
}
inst0chg = Closes[0][0] - inst0start;
inst1chg = Closes[1][0] - inst1start;
inst0pct = inst0chg / inst0start * 100;
inst1pct = inst1chg / inst1start * 100;
difference = inst1pct - inst0pct;
PercentPlot.Set(difference);
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries PercentPlot
{
get { return Values[0]; }
}
[Description("")]
[Category("Parameters")]
public string SecondInstrument
{
get { return secondInstrument; }
set { secondInstrument = value; }
}
#endregion
}
}
Hopefully you'll be able to modify it to suit your needs.
vt2009
04-14-2010, 07:13 AM
Extremely helpful. The spread I am making uses a logarithmic ratio- here is how I modified the code but for some reason it doesnt want to plot. Any suggestions?
protected override void OnBarUpdate()
{
if (CurrentBar == 0 && BarsInProgress == 1)
{
inst0start = Closes[0][0];
inst1start = Closes[1][0];
}
inst0chg = Closes[0][0] - inst0start;
inst1chg = Closes[1][0] - inst1start;
inst0pct = inst0chg / inst0start * 100;
inst1pct = inst1chg / inst1start * 100;
double LogRatio= Math.Log(inst1chg/inst0chg);
PercentPlot.Set(LogRatio);
}
NinjaTrader_Bertrand
04-14-2010, 08:10 AM
At the first bar of the OnBarUpdate() you reference one bar back, this will throw an error to the log tab -
http://www.ninjatrader-support2.com/vb/showthread.php?t=3170
NinjaTrader_Austin
04-14-2010, 04:10 PM
vt2009, you can't take a log of a negative number, which is probably why it isn't plotting. If either inst0chg or inst1chg is negative, that will make the ratio negative, which would give you an imaginary answer.
dennho
05-24-2010, 01:26 PM
I am also looking into a multiinstrument indicator and I tried to compile the attached script posted by Austin. The part in the script I am having trouble with is this part:
protected override void Initialize()
{
Add(secondInstrument, PeriodType.Minute, 1);
...
}
I am getting a compile time error that says "No overload for method 'Add' takes '3' arguments. So I am curious as to how you compiled this indicator? Add only takes a plot or a line as far as I can see. I understand that in strategies, there is an Add() function that takes the three parameters but not in indicators, and I thought this was an indicator that is being talked about?
NinjaTrader_RyanM
05-24-2010, 01:39 PM
Hello dennho,
Multiseries support has been added to indicators only in version 7. For charting multiseries in version 6.5 you will have to plot from a strategy. (http://www.ninjatrader-support.com/vb/showthread.php?t=6651)
Your code should compile in an indicator in version 7. This version is in public beta and you can download at the link below:
http://www.ninjatrader.com/webnew/download_trading_software.htm (http://www.ninjatrader.com/webnew/download_trading_software.htm)
dennho
05-24-2010, 01:42 PM
Oh I see then. Thanks for the fast reply!
crmtrade
07-06-2010, 12:19 PM
Hi, New to the forum.
Can anyone paste the completed version of this code or PM it to me? Do not have much program knowledge.
crmtrade
07-06-2010, 12:53 PM
Used code above and it has compiled. How do I use it as an indicator now? It isn't showing up in the indicator list and Ive saved it.
NinjaTrader_Austin
07-06-2010, 01:06 PM
crmtrade, just to double check, you've hit the F5 key to compile it and received no errors? After that, when you're trying to apply it to a chart, it is not appearing in the indicator list? Could you try restarting NinjaTrader? The indicator would be called PercentDifference in the indicator list.
crmtrade
07-06-2010, 01:19 PM
Alright have it going. Now is it possible to have the chart display % change like google finance? That way I can see the percentage difference between the 2 % change numbers.
NinjaTrader_Austin
07-06-2010, 01:23 PM
crmtrade, you can just apply the indicator twice and pick two different instruments in the indicator settings.
crmtrade
07-06-2010, 01:24 PM
For example.
Since Jan 1 SP down 8.44% while Dow down 7.03% I'm interested in that difference. Dow %- SP % so -1.41
crmtrade
07-06-2010, 01:39 PM
Basically I would do percentage difference between Jan1st close on SP500 to now. Then repeat for Dow. Then the difference between those 2 numbers.
crmtrade
07-06-2010, 01:43 PM
What would I need to modify in the code so in "percentage difference indicator" it allows me to enter a single number under Input Series? So I can input the SP500 close on Jan1st in there and have the second instrument reading ES 09-10.
NinjaTrader_RyanM
07-06-2010, 06:14 PM
Hello Crmtrade,
We do not provide custom coding services. We are happy to guide you in the right direction and offer feedback on any attempts you make at coding on your own.
The following reference sample can help you get an indicator value at a specific date.
Getting indicator values from a specified time (http://www.ninjatrader-support2.com/vb/showthread.php?t=19176)
You use GetBar (http://www.ninjatrader-support.com/HelpGuideV6/GetBar.html) to get the bars ago value for a specific date / time. You can then perform calculations off these values.
User defined input parameters are most easily setup through the indicator wizard. For help on adding these in code, see this link. (http://www.ninjatrader.com/support/forum/showthread.php?t=5782)
If you would like to hire someone to code this for you, please see the link below for a list of our 3rd party NinjaScript consultants.
http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm
nightriderx
07-14-2010, 01:59 PM
I am having a similar issue here...im trying to plot pp1 in NT7, under the bar chart. Can you please help? I think I tried everything I could think of
double pp1 = ((WMA(BarsArray[1], 20)[0]) - (WMA(BarsArray[2], 20)[0]));
NinjaTrader_Austin
07-14-2010, 02:46 PM
nightriderx, could you please be more specific? How are you trying to plot "pp1"? In an indicator? As an indicator itself? Have you tried with just the two bar arrays: BarsArray[0] and BarsArray[1]? Are there any errors in the logs (right-most tab of Control Center)?
nightriderx
07-14-2010, 03:44 PM
I am trying to plot it in my strategy.
I tried adding:
Add(new Plot(Color.Red, PlotStyle.Line, "PercentPlot"));
in protected override void Initialize()
and
PercentPlot.Set(pp1);
in protected override void OnBarUpdate()
but just gives me the following errors when i try to compile:
1) Strategy\x.cs The best overloaded method match for 'NinjaTrader.Strategy.StrategyBase.Add(NinjaTrader .Indicator.IndicatorBase)' has some invalid arguments CS1502 - click for info 79 4
2) Strategy\x.cs Argument '1': cannot convert from 'NinjaTrader.Gui.Chart.Plot' to 'NinjaTrader.Indicator.IndicatorBase' CS1503 - click for info 79 8
3) Strategy\x.cs The name 'PercentPlot' does not exist in the current context CS0103 - click for info 119 4
also tried plotting:
double pp1 = (Closes[1][0] - Closes[2][0]);
but same errors are produced
thank you greatly for your help in advance!
NinjaTrader_Austin
07-14-2010, 03:47 PM
nightriderx, it is not possible to plot from within a strategy. You will have to code it out in an indicator and then add that indicator to your strategy.
resist
09-17-2010, 04:32 AM
I´m currently urgent looking for a spread displaying indicator - e.g. cable to euro (using NT 7) - I found this thread but I´ve no experience in coding - has anyone coded this or a better link were to ask.
Extra I´ve constantly needs / ideas I would like to´ve coded but I´ve neither the knowledge nor the time to learn it - so if there a any programming gurus which are interested in ninja trader / trading, too and would like to collaborate please contact me - I´m located in Berlin / Germany and Valencia / Spain, also.