Signal and Momentum
Signal and Momentum
23 Apr 2013, 01:03
I'm trying to plot momentum oscillator (histogram) together with a signal (as points in the same graph), which is 1 when the bollinger upper band is lower than the keltner upper band, and the boll low is higher than the keltner low; otherwise the signal is 0.
However, nothing is plotted...
Could someone help?
(code below)
Tks.
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = false)]
public classSignal1 : Indicator
{
private int Period = 20;
private IndicatorDataSeries atr;
private MovingAverage expo;
private TrueRange tri;
private MovingAverage _movingAverage;
private StandardDeviation _standardDeviation;
private MomentumOscillator _momentum;
[Parameter(DefaultValue = 20)]
public int KeltnerPeriod { get; set; }
[Parameter(DefaultValue = 1.5)]
public double BandDistance { get; set; }
[Parameter("MAType")]
public MovingAverageType matype { get; set; }
[Parameter("BB Period", DefaultValue = 20)]
public int Period2 { get; set; }
[Parameter("SD Weight Coef", DefaultValue = 2)]
public int K { get; set; }
[Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
public MovingAverageType MaType { get; set; }
[Parameter]
public DataSeries Price { get; set; }
// [Output("Main")]
// public IndicatorDataSeries KeltnerMain { get; set; }
// [Output("ChannelUp", Color = Colors.Red)]
// public IndicatorDataSeries ChannelUp { get; set; }
// [Output("ChannelLow", Color = Colors.Blue)]
// public IndicatorDataSeries ChannelLow { get; set; }
// [Output("Main BB", Color = Colors.Blue)]
// public IndicatorDataSeries Main { get; set; }
// [Output("Upper", Color = Colors.Red)]
// public IndicatorDataSeries Upper { get; set; }
// [Output("Lower")]
// public IndicatorDataSeries Lower { get; set; }
[Output("Momentum", Color = Colors.Blue, PlotType = PlotType.Histogram, Thickness = 5)]
public IndicatorDataSeries Result { get; set; }
[Output("Signal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
public IndicatorDataSeries Result2 { get; set; }
protected override void Initialize()
{
atr = CreateDataSeries();
tri = Indicators.TrueRange();
expo = Indicators.MovingAverage(MarketSeries.Typical, KeltnerPeriod, matype);
_momentum = Indicators.MomentumOscillator(MarketSeries.Close, 14);
_movingAverage = Indicators.MovingAverage(Price, Period2, MaType);
_standardDeviation = Indicators.StandardDeviation(Price, Period2, K, MaType);
}
public override void Calculate(int index)
{
if (index < Period + 1)
{
atr[index] = tri.Result[index];
}
if (index >= Period)
{
atr[index] = (atr[index - 1] * (Period - 1) + tri.Result[index]) / Period;
}
// KeltnerMain[index] = expo.Result[index];
// ChannelUp[index] = expo.Result[index] + BandDistance * atr[index];
// ChannelLow[index] = expo.Result[index] - BandDistance * atr[index];
// Main[index] = _movingAverage.Result[index];
// Upper[index] = _movingAverage.Result[index] + K * _standardDeviation.Result[index];
// Lower[index] = _movingAverage.Result[index] - K * _standardDeviation.Result[index];
double momentum = _momentum.Result[index];
if (_movingAverage.Result[index] + K * _standardDeviation.Result[index] - expo.Result[index] + BandDistance * atr[index] <= 0)
{
if (_movingAverage.Result[index] - K * _standardDeviation.Result[index] - expo.Result[index] - BandDistance * atr[index] > 0)
{
Result2[index] = 10;
}
else
{
Result2[index] = 20;
}
}
}
}
}
Replies
cAlgo_Fanatic
23 Apr 2013, 14:14
You can set it to double.NaN for each value you want to hide.
@cAlgo_Fanatic
cAlgo_Fanatic
23 Apr 2013, 11:34
You can reference the Keltner Channels Indicator (need to download and build) and the Bollinger Bands (build in) Indicator. The Momentum Oscillator oscillates to 100, so you would have to scale it down if you want your signal output to oscillate between 1 and 0. Also you may want to oscillate between 1 and 0.99 instead of zero so that you can see the momentum histogram properly.
@cAlgo_Fanatic