Finding NaN
Finding NaN
11 Dec 2013, 12:45
Hi!
Is threre ayone who knows a good way to find a NaN in a code?
I have been woking on a indicator and everything seems to be working yet the indicator produces a NaN
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; namespace cAlgo.Indicators { [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC)] public class SineTest : Indicator { #region variables private double alpha1; private double a1; private double b1; private double c1; private double c2; private double c3; private IndicatorDataSeries Filt; private double Wave1; private double Pwr; private IndicatorDataSeries HP; #endregion #region output [Output("Trigger", Color = Colors.Purple)] public IndicatorDataSeries Wave { get; set; } #endregion #region input [Parameter()] public DataSeries Source { get; set; } [Parameter(DefaultValue = 40, MinValue = 1)] public int Period { get; set; } #endregion protected override void Initialize() { HP = CreateDataSeries(); Filt = CreateDataSeries(); Wave = CreateDataSeries(); alpha1 = (1 - Math.Sin(360 / Period)) / Math.Cos(360 / Period); a1 = Math.Exp(-1.414 * 3.14159 / 10); b1 = 2 * a1 * Math.Cos(1.414 * 180 / 10); c2 = b1; c3 = -a1 * a1; c1 = 1 - c2 - c3; } public override void Calculate(int index) { if (index >= Period) HP[index] = 0.5 * (1 + alpha1) * (Source[index] - Source[index - 1]) + alpha1 * HP[index - 1]; Filt[index] = c1 * (HP[index] + HP[index - 1]) / 2 + c2 * Filt[index - 1] + c3 * Filt[index - 2]; Wave1 = (Filt[index] + Filt[index - 1] + Filt[index - 2]) / 3; Pwr = (Filt[index] * Filt[index] + Filt[index - 1] * Filt[index - 1] + Filt[index - 2] * Filt[index - 2]) / 3; Wave[index] = Wave1 / Math.Sqrt(Pwr); } } }
Replies
ErikD
12 Dec 2013, 13:26
Ok so I didnt manage to get it to work...
Seems like there is something wrong with the calculation
HP[index] = 0.5 * (1 + alpha1) * (Source[index] - Source[index - 1]) + alpha1 * HP[index - 1];
It does however work in my tradestation where it looks like this
HP = .5*(1 + alpha1)*(Close - Close[1]) + alpha1*HP[1];
anyone know what might be wrong?
@ErikD
ErikD
12 Dec 2013, 19:12
RE:
Spotware said:
Remove this line from Initialize:
Wave = CreateDataSeries();and add this to the beginning of the Calculate method:
if (index < Period) { HP[index] = 0; Filt[index] = 0; return; }Instead of if (index >= Period)
Thank you this fixed the problem
Just to be clear
It fixed the problem by populating HP and Filt when the index is bigger than the period.
Does this always have to be done when dealing with calculations that need the past value of it self in order to complete?
@ErikD
Spotware
13 Dec 2013, 14:44
RE: RE:
ErikD said:
Does this always have to be done when dealing with calculations that need the past value of it self in order to complete?
Yes. The data series will be populated with NAN initially, so if you are trying to access past values they should contain some value such as zero or the closing price.
@Spotware
jani
30 Oct 2019, 12:02
RE: Double.IsNaN
jeex said:
With double.IsNaN( double ) you can test on NaN as a value.
With double d = double.NaN; you can set an indicator to NaN if it has no value.
double d = double.NaN; if( double.IsNaN( d ) ) { // Nul value }
If I want to reference in a cBot to indicator value NaN, how can I do it?
The logic would be that this would wotk:
get { return (up > double.NaN); } // OR
get { return (up > double.IsNaN); }
.. but it doesn't. cAlgo APi reference has no mention about it, or because there is no functioning search tool, I cannot find any references of how to call indicator NaN values.
What I want to do is open position when indicator value is NaN... simple thing like this seems like mission impossible with no instructions available...
@jani
PanagiotisCharalampous
30 Oct 2019, 12:14
Hi Jani,
See below how you can check if an indicator output value is NaN
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } SimpleMovingAverage _sma; protected override void OnStart() { _sma = Indicators.SimpleMovingAverage(MarketSeries.Close, 10); } protected override void OnTick() { if (double.IsNaN(_sma.Result.LastValue)) { // Execute your logic here } } protected override void OnStop() { // Put your deinitialization logic here } } }
Best Regards,
Panagiotis
@PanagiotisCharalampous
jeex
11 Dec 2013, 15:48
Double.IsNaN
With double.IsNaN( double ) you can test on NaN as a value.
With double d = double.NaN; you can set an indicator to NaN if it has no value.
@jeex