Why do i have to put back on the chart after every startup
#region Using declarations
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Core.FloatingPoint;
#endregion
// This namespace holds indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators.MEL
{
///
/// The Stochastic Oscillator is made up of two lines that oscillate between
/// a vertical scale of 0 to 100. The %K is the main line and it is drawn as
/// a solid line. The second is the %D line and is a moving average of %K.
/// The %D line is drawn as a dotted line. Use as a buy/sell signal generator,
/// buying when fast moves above slow and selling when fast moves below slow.
///
public class MelCycle : Indicator
{
private Series den;
private Series fastK;
private MIN min;
private MAX max;
private Series nom;
private SMA smaFastK;
private SMA smaK;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = Custom.Resource.NinjaScriptIndicatorDescriptionStochastics;
Name = "MelCycle";
IsSuspendedWhileInactive = true;
PeriodD = 6;
PeriodK = 10;
Smooth = 4;
//Cycle_Rising = Brushes.Green;
//Cycyle_Falling = Brushes.Red;
Calculate = Calculate.OnPriceChange;
AddPlot(Brushes.AliceBlue, Custom.Resource.StochasticsD);
AddPlot(Brushes.White, Custom.Resource.StochasticsK);
AddLine(Brushes.DimGray, 20, Custom.Resource.NinjaScriptIndicatorLower);
AddLine(Brushes.DimGray, 80, Custom.Resource.NinjaScriptIndicatorUpper);
AddLine(Brushes.DimGray, 45, "MiddleLower");
AddLine(Brushes.DimGray, 55, "MiddleUpper");
}
else if (State == State.DataLoaded)
{
den = new Series<double>(this);
nom = new Series<double>(this);
fastK = new Series<double>(this);
min = MIN(Low, PeriodK);
max = MAX(High, PeriodK);
smaFastK = SMA(fastK, Smooth);
smaK = SMA(K, PeriodD);
}
}
protected override void OnBarUpdate()
{
double min0 = min[0];
nom[0] = Close[0] - min0;
den[0] = max[0] - min0;
fastK[0] = den[0].ApproxCompare(0) == 0 ? CurrentBar == 0 ? 50 : fastK[1] : Math.Min(100, Math.Max(0, 100 * nom[0] / den[0]));
// Slow %K == Fast %D
K[0] = smaFastK[0];
D[0] = smaK[0];
if (IsFalling(D))
{
PlotBrushes[0][0] = Cycyle_Falling;
}
else if (IsRising(D))
{
PlotBrushes[0][0] = Cycle_Rising;
}
}
#region Properties
[Browsable(false)]
[XmlIgnore]
public Series<double> D => Values[0];
[Browsable(false)]
[XmlIgnore]
public Series<double> K => Values[1];
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "PeriodD", GroupName = "NinjaScriptParameters", Order = 0)]
public int PeriodD { get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "PeriodK", GroupName = "NinjaScriptParameters", Order = 1)]
public int PeriodK { get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 2)]
public int Smooth { get; set; }
[Display(Name="Cycle_Rising", Description="Color for Cycle", GroupName="Plots", Order = 3)]
public Brush Cycle_Rising {get; set;}
[Display(Name="Cycyle_Falling", Description="Color for Cycle", GroupName="Plots", Order = 4)]
public Brush Cycyle_Falling {get; set;}
#endregion