How to prevent repainting with this code
How to prevent repainting with this code
03 Feb 2023, 13:39
Hi everyone, I'm trying to transpose a tradingview indicator in cTrader
so far that's how I got
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class KalmanFilterLoxx : Indicator
{
[Parameter("Sharpness", DefaultValue = 1.0)]
public double Sharpness { get; set; }
[Parameter("K", DefaultValue = 1.0)]
public double K { get; set; }
[Output("KF [Loxx]", Color = Colors.Lime)]
public IndicatorDataSeries kfilt { get; set; }
private double velocity;
protected override void Initialize()
{
velocity = 0.0;
}
public override void Calculate(int index)
{
var src = (MarketSeries.Close[index] + MarketSeries.Open[index] + MarketSeries.Low[index] + MarketSeries.High[index]) / 4;
Print("src is " + src);
Print("prev value " + double.IsNaN(kfilt.LastValue) );
var prevKfilt = (double.IsNaN(kfilt.LastValue) ? 0 : kfilt.LastValue) ;
Print("prevk is " + prevKfilt);
var distance = src - (prevKfilt == 0 ? src : prevKfilt);
Print("Distance is " + distance);
var error = (prevKfilt == 0 ? src : prevKfilt) + distance * Math.Sqrt(Sharpness * K / 100);
Print("Error is " + error);
velocity += distance * K / 100;
Print("Velocity is "+velocity);
kfilt[index] = error + velocity;
}
}
}
it works fine when launched but seconds later the last value keeps repainting into oblivion, how to prevent that ?
Original pinescript :
indicator('Kalman Filter [Loxx]', overlay = true, shorttitle="KF [Loxx]", timeframe="", timeframe_gaps=true)
src = input(ohlc4)
Sharpness = input.float(1.0)
K = input.float(1.0)
greencolor = color.lime
redcolor = color.red
velocity = 0.0
kfilt = 0.0
Distance = src - nz(kfilt[1], src)
Error = nz(kfilt[1], src) + Distance * math.sqrt(Sharpness*K/ 100)
velocity := nz(velocity[1], 0) + Distance*K / 100
kfilt := Error + velocity
plot(kfilt, color=velocity > 0 ? greencolor : redcolor, linewidth = 2)
Replies
amirus.stark
05 Feb 2023, 21:07
RE:
firemyst said:
You keep adding to the value of velocity:
velocity += distance * K / 100;
Every tick you say velocity = velocity + distance * K / 100;
You never reset it to another value, so of course it's going to increase into infinity.
Thanks I fixed it
btw I'm now drawing two lines (one should have a different color) as following :
kfilt[index] = error + velocityDict[index];
if (velocityDict[index]> 0 )
{
kfiltG[index] = kfilt[index];
kfiltR[index] = double.NaN;
Print("R = " + kfiltR[index]);
}
if (velocityDict[index] !> 0)
{
kfiltG[index] = double.NaN;
kfiltR[index] = kfilt[index];
Print("G = " + kfiltG[index]);
}
but when the values are NaN it draws a line that goes from the previous defined value to the last one
how to prevent this ?
@amirus.stark
firemyst
05 Feb 2023, 15:13
You keep adding to the value of velocity:
Every tick you say velocity = velocity + distance * K / 100;
You never reset it to another value, so of course it's going to increase into infinity.
@firemyst