Topics
Forum Topics not found
Replies
MehranM
13 Dec 2023, 12:21
( Updated at: 14 Dec 2023, 06:43 )
Limit the calculation to recent Bars only
You can limit the calculation to recent bars to make it feasible. Here is the code I suggest:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class STOCHALLINONELoadMore : Indicator
{
//[Parameter("Nbrs Stoch Load ", DefaultValue = 28, Group = " Ribbon/BarsColors Setting")]
//public int NbrsStochLoad { get; set; }
[Parameter("Percent K", DefaultValue = 9, Group = "Stoch Setting")]
public int PeriodK { get; set; }
[Parameter("Percent K", DefaultValue = 3, Group = "Stoch Setting")]
public int PeriodD { get; set; }
[Parameter("Percent K", DefaultValue = 9, Group = "Stoch Setting")]
public int KSlowing { get; set; }
[Parameter("Nbrs Stoch Load", DefaultValue = 25, Group = "Nbrs Load")]
public int StochLoad { get; set; }
[Parameter("Periods Multi STOCH ", DefaultValue = 1.1, Group = "Nbrs Load")]
public double PeriodsMulti { get; set; }
[Parameter("Extra Level", DefaultValue = 80, Group = "Extra Level")]
public int HighLevel { get; set; }
[Parameter("Extra Level", DefaultValue = 20, Group = "Extra Level")]
public int LowLevel { get; set; }
[Output("Result", PlotType = PlotType.Line, LineColor = "Green", Thickness = 1)]
public IndicatorDataSeries Result { get; set; }
[Output("Signal", PlotType = PlotType.Line, LineColor = "Red", Thickness = 1)]
public IndicatorDataSeries Signal { get; set; }
[Parameter("Extend Calculation", DefaultValue = 10)]
public int ExtendCalculation { get; set; }
private StochasticOscillator[] stoch;
private IndicatorDataSeries[] resK, resD;
public IndicatorDataSeries totalLoad;
private int[] periodsStoch;
private double resIndicatorK, resIndicatorD, load;
protected override void Initialize()
{
resK = new IndicatorDataSeries[StochLoad];
resD = new IndicatorDataSeries[StochLoad];
stoch = new StochasticOscillator[StochLoad];
periodsStoch = new int[StochLoad];
totalLoad = CreateDataSeries();
resIndicatorK = 0.00;
resIndicatorD = 0.00;
load = 100 / StochLoad;
for (int i = 0; i < resK.Length; i++)
{
periodsStoch[i] = (int)(Math.Round(Math.Pow(PeriodsMulti, i) * PeriodK));
resK[i] = CreateDataSeries();
resD[i] = CreateDataSeries();
stoch[i] = Indicators.StochasticOscillator(Bars, periodsStoch[i], PeriodD, KSlowing, MovingAverageType.Simple);
}
}
public override void Calculate(int index)
{
if (index > Chart.FirstVisibleBarIndex - ExtendCalculation || ExtendCalculation >= 1000)
{
resIndicatorK = 0.00;
resIndicatorD = 0.00;
for (int i = 0; i < resK.Length; i++)
{
resK[i][index] = stoch[i].PercentK[index];
resD[i][index] = stoch[i].PercentD[index];
if (!double.IsNaN(resK[i][index]))
{
if (resK[i][index] >= 80)
{
if (IndicatorArea.FindObject("stoch" + i + index) != null)
IndicatorArea.RemoveObject("stoch" + i + index);
IndicatorArea.DrawTrendLine("stoch" + i + index, index, i * load, index - 1, i * load, Color.FromArgb(100, "8901FF01"), 5, LineStyle.DotsVeryRare);
}
else if (resK[i][index] < 80 && resK[i][index] >= 50)
{
if (IndicatorArea.FindObject("stoch" + i + index) != null)
IndicatorArea.RemoveObject("stoch" + i + index);
IndicatorArea.DrawTrendLine("stoch" + i + index, index, i * load, index - 1, i * load, Color.FromArgb(100, "8C02AFF1"), 5, LineStyle.DotsVeryRare);
}
else if (resK[i][index] < 50 && resK[i][index] >= 20)
{
if (IndicatorArea.FindObject("stoch" + i + index) != null)
IndicatorArea.RemoveObject("stoch" + i + index);
IndicatorArea.DrawTrendLine("stoch" + i + index, index, i * load, index - 1, i * load, Color.FromArgb(100, "A3FF6000"), 5, LineStyle.DotsVeryRare);
}
else if (resK[i][index] < 20)
{
if (IndicatorArea.FindObject("stoch" + i + index) != null)
IndicatorArea.RemoveObject("stoch" + i + index);
IndicatorArea.DrawTrendLine("stoch" + i + index, index, i * load, index - 1, i * load, Color.FromArgb(100, "A3FF6000"), 5, LineStyle.DotsVeryRare);
}
resIndicatorK += resK[i][index];
resIndicatorD += resD[i][index];
}
}
if (index == periodsStoch[resK.Length - 1])
IndicatorArea.DrawVerticalLine("StartIndicator" + index, index, "8901FF01", 5, LineStyle.DotsVeryRare);
Result[index] = (resIndicatorK / StochLoad);
Signal[index] = (resIndicatorD / StochLoad);
}
}
}
}
@MehranM
MehranM
01 Oct 2024, 12:57 ( Updated at: 02 Oct 2024, 04:57 )
RE: Found a bug using Indicators.. in Initialize method
PanagiotisCharalampous said:
Hi Dear Panagiotis
I have the same problem in the latest update. Please find the details in the video I sent to the Telegram group: https://t.me/cTrader_Official/131603
@MehranM