|
发表于 2017-5-31 10:16 PM
|
显示全部楼层
你要先写一个两个dataseries相除的indicator,然后对这个indicator去bollinger.
dataseries相除的indicator我正好写了一个,你在这基础上加bollinger就可以了。以下是NJ7的代码。
#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>
/// Plot relative strength between two symbols.
/// </summary>
[Description("Plot relative strength between two symbols.")]
public class RelativeStrength : Indicator
{
#region Variables
// Wizard generated variables
private string dividendSymbol = @""; // Default setting for DividendSymbol
private string dividerSymbol = @""; // Default setting for DividerSymbol
// User defined variables (add any user defined variables below)
private bool invertOnly = false ;
#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()
{
if( String.Compare( this.dividendSymbol, "^ONE", true ) == 0 )
this.invertOnly = true ;
else if( this.dividendSymbol != "" )
//Add( this.dividendSymbol, this.BarsPeriod.Id, this.BarsPeriod.Value ) ;
Add( this.dividendSymbol, PeriodType.Day, 1 ) ;
if( this.dividerSymbol != "" )
//Add( this.dividerSymbol, this.BarsPeriod.Id, this.BarsPeriod.Value ) ;
Add( this.dividerSymbol, PeriodType.Day, 1 ) ;
Add( new Plot( Color.Orange, "RelativeStrength" ) ) ;
Overlay = false ;
DisplayInDataBox = false ;
PaintPriceMarkers = false ;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// We have multiple bars, so skip if this is not called for primay bar.
if( this.BarsInProgress != 0 )
return ;
Value.Set( 0 ) ;
// No parameter specified, then simply invert the main data.
if( this.dividendSymbol == "" && this.dividerSymbol == "" ||
this.invertOnly && this.dividerSymbol == "" )
{
Value.Set( 1.0 / Close[0] ) ;
return ;
}
if( this.dividendSymbol != "" && this.dividerSymbol != "" )
{
// No data for the first symbol yet.
if( this.CurrentBars[1] < 0 )
return ;
// No data for the 2nd symbol yet.
if( !this.invertOnly && this.CurrentBars[2] < 0 )
return ;
}
if( this.invertOnly )
Value.Set( 1.0 / Closes[1][0] ) ;
else
Value.Set( Closes[1][0] / Closes[2][0] ) ;
}
#region Properties
[Description("Dividend symbol. Use ^ONE to reverse the DividerSymobl.")]
[GridCategory("Parameters")]
public string DividendSymbol
{
get { return dividendSymbol; }
set { dividendSymbol = value; }
}
[Description("Divider symbol.")]
[GridCategory("Parameters")]
public string DividerSymbol
{
get { return dividerSymbol; }
set { dividerSymbol = value; }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private RelativeStrength[] cacheRelativeStrength = null;
private static RelativeStrength checkRelativeStrength = new RelativeStrength();
/// <summary>
/// Plot relative strength between two symbols.
/// </summary>
/// <returns></returns>
public RelativeStrength RelativeStrength(string dividendSymbol, string dividerSymbol)
{
return RelativeStrength(Input, dividendSymbol, dividerSymbol);
}
/// <summary>
/// Plot relative strength between two symbols.
/// </summary>
/// <returns></returns>
public RelativeStrength RelativeStrength(Data.IDataSeries input, string dividendSymbol, string dividerSymbol)
{
if (cacheRelativeStrength != null)
for (int idx = 0; idx < cacheRelativeStrength.Length; idx++)
if (cacheRelativeStrength[idx].DividendSymbol == dividendSymbol && cacheRelativeStrength[idx].DividerSymbol == dividerSymbol && cacheRelativeStrength[idx].EqualsInput(input))
return cacheRelativeStrength[idx];
lock (checkRelativeStrength)
{
checkRelativeStrength.DividendSymbol = dividendSymbol;
dividendSymbol = checkRelativeStrength.DividendSymbol;
checkRelativeStrength.DividerSymbol = dividerSymbol;
dividerSymbol = checkRelativeStrength.DividerSymbol;
if (cacheRelativeStrength != null)
for (int idx = 0; idx < cacheRelativeStrength.Length; idx++)
if (cacheRelativeStrength[idx].DividendSymbol == dividendSymbol && cacheRelativeStrength[idx].DividerSymbol == dividerSymbol && cacheRelativeStrength[idx].EqualsInput(input))
return cacheRelativeStrength[idx];
RelativeStrength indicator = new RelativeStrength();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.DividendSymbol = dividendSymbol;
indicator.DividerSymbol = dividerSymbol;
Indicators.Add(indicator);
indicator.SetUp();
RelativeStrength[] tmp = new RelativeStrength[cacheRelativeStrength == null ? 1 : cacheRelativeStrength.Length + 1];
if (cacheRelativeStrength != null)
cacheRelativeStrength.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheRelativeStrength = tmp;
return indicator;
}
}
}
}
// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Plot relative strength between two symbols.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.RelativeStrength RelativeStrength(string dividendSymbol, string dividerSymbol)
{
return _indicator.RelativeStrength(Input, dividendSymbol, dividerSymbol);
}
/// <summary>
/// Plot relative strength between two symbols.
/// </summary>
/// <returns></returns>
public Indicator.RelativeStrength RelativeStrength(Data.IDataSeries input, string dividendSymbol, string dividerSymbol)
{
return _indicator.RelativeStrength(input, dividendSymbol, dividerSymbol);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Plot relative strength between two symbols.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.RelativeStrength RelativeStrength(string dividendSymbol, string dividerSymbol)
{
return _indicator.RelativeStrength(Input, dividendSymbol, dividerSymbol);
}
/// <summary>
/// Plot relative strength between two symbols.
/// </summary>
/// <returns></returns>
public Indicator.RelativeStrength RelativeStrength(Data.IDataSeries input, string dividendSymbol, string dividerSymbol)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
return _indicator.RelativeStrength(input, dividendSymbol, dividerSymbol);
}
}
}
#endregion
|
评分
-
1
查看全部评分
-
|