Super Ichi by LuxAlgo - Help with Variables
Super Ichi by LuxAlgo - Help with Variables
27 Jun 2022, 12:42
Hi guys,
Trying to replicate the Tradingview indicator Superichi by LuxAlgo. This is my first complicated indicator. I am having trouble with the variables in the public double, i dont know how to declare the variable 'upper' and 'lower' to look back at itself at an index - 1. my issues start at line 95. It isnt finished so please point out any addition errors improvements.
Thanks
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
public class AtrChannels : Indicator
{
private AverageTrueRange atr;
private MovingAverage ma;
[Parameter()]
public DataSeries Source { get; set; }
[Parameter("tenkan period", DefaultValue = 9)]
public int tenkan_period { get; set; }
[Parameter("tenkan_mult", DefaultValue = 2)]
public int tenkan_mult { get; set; }
[Parameter("kijun_period", DefaultValue = 26)]
public int kijun_period { get; set; }
[Parameter("kijun_mult", DefaultValue = 4)]
public int kijun_mult { get; set; }
[Parameter("spanB period", DefaultValue = 52)]
public int spanB_period { get; set; }
[Parameter("spanB_mult", DefaultValue = 6)]
public int spanB_mult { get; set; }
[Parameter(DefaultValue = MovingAverageType.Weighted)]
public MovingAverageType MAType { get; set; }
[Output("TenkanSen", Color = Colors.Red)]
public IndicatorDataSeries tenkan { get; set; }
[Output("Kijunsen", Color = Colors.Blue)]
public IndicatorDataSeries kijun { get; set; }
[Output("SenkouSpanB", Color = Colors.Red, LineStyle = LineStyle.Lines)]
public IndicatorDataSeries senkouB { get; set; }
[Output("SenkouSpanA", Color = Colors.Green, LineStyle = LineStyle.Lines)]
public IndicatorDataSeries senkouA { get; set; }
public AverageTrueRange atr_trial { get; set; }
protected override void Initialize()
{
int atrperiods = tenkan_period;
atr_trial = Indicators.AverageTrueRange(atrperiods, MovingAverageType.Simple);
//atrtenkan = Indicators.AverageTrueRange(tenkan_period, MovingAverageType.Simple);
//atrkijun = Indicators.AverageTrueRange(kijun_period, MovingAverageType.Simple);
//atrspanB = Indicators.AverageTrueRange(spanB_period, MovingAverageType.Simple);
}
public override void Calculate(int index)
{
//Tenkan[index] = _avgtenkan(index);
tenkan[index] = _avgtenkan(index, tenkan_period, tenkan_mult);
kijun[index] = _avgtenkan(index, kijun_period, kijun_mult);
senkouB[index] = _avgtenkan(index, spanB_period, spanB_mult);
senkouA[index] = (kijun[index] + tenkan[index]) / 2;
}
public double _avgtenkan(int index, int periods, double multiplier)
{
int atrperiods = periods;
//double upper = uppers;
//double _atr = atrtenkan.Result.Last[index] * tenkan_mult;
double _atr = atr_trial.Result[index] * multiplier;
double up = (Bars.HighPrices[index] + Bars.LowPrices[index]) / 2 + _atr;
double dn = (Bars.HighPrices[index] + Bars.LowPrices[index]) / 2 - _atr;
var upper;
var lower;
var os;
var spt;
var _min;
var _max;
var upper1 = Math.Min(up, upper[index - 1]);
if (Bars.ClosePrices[index - 1] < upper[index - 1])
{
upper = Math.Min(up, upper[index - 1]);
}
else
{
upper[index] = up;
}
if (Bars.ClosePrices[index + 1] > lower[index - 1])
lower[index] = Math.Max(up, upper[index - 1]);
else
{
lower[index] = dn;
}
if (Bars.ClosePrices[index] > upper[index])
{
os[index] = 1;
}
else if (Bars.ClosePrices[index] < lower[index])
{
os[index] = 0;
}
else
{ os[index] = os[index - 1]; }
if (os[index] == 1)
{
spt = lower[index];
}
else
{
spt = upper[index];
}
//def _max = if src crosses spt then Max(src, _max[index - 1]) else if os == 1 then Max(src, _max[index - 1]) else spt;
if (((Bars.ClosePrices[index - 1] > spt[index - 1] && Bars.ClosePrices[index] < spt[index]) || (Bars.ClosePrices[index - 1] < spt[index - 1] && Bars.ClosePrices[index] > spt[index])))
{
_max = Math.Max(Bars.ClosePrices[index], _max[index - 1]);
}
else if (os[index] == 1)
{
_max = Math.Max(Bars.ClosePrices[index], _max[index - 1]);
}
else
{
_max = spt[index];
}
//def _min = if src crosses spt then Min(src, _min[index - 1]) else if os == 0 then Min(src, _min[index - 1]) else spt;
if (((Bars.ClosePrices[index - 1] > spt[index - 1] && Bars.ClosePrices[index] < spt[index]) || (Bars.ClosePrices[index - 1] < spt[index - 1] && Bars.ClosePrices[index] > spt[index])))
{
_min = Math.Min(Bars.ClosePrices[index], _min[index - 1]);
}
else if (os[index] == 0)
{
_min = Math.Min(Bars.ClosePrices[index], _min[index - 1]);
}
else
{
_min = spt[index];
}
return (_max + _min) / 2;
}
}
}