My indicator will not get a result for Functions.Sum()
My indicator will not get a result for Functions.Sum()
08 Mar 2023, 11:10
I am getting NaN for the result on a IndicatorDataSeries when using Sum() and can not see why?
There is a Print statement to see what I mean.
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, AccessRights = AccessRights.None)]
public class x2HLBarCount : Indicator
{
[Parameter("Lookback", Group = "Setting", DefaultValue = 100)]
public int Lookback { get; set; }
[Output("Count of Green Bars", LineColor = "#FFFF00", PlotType = PlotType.Points)]
public IndicatorDataSeries HLGreenBars { get; set; }
[Output("Count of Red Bars", LineColor = "#FFFF00", PlotType = PlotType.Points )]
public IndicatorDataSeries HLRedBars{ get; set; }
[Output("HL Normalised Green Red", LineColor = "#FFFF00", PlotType = PlotType.Points )]
public IndicatorDataSeries HLBarsNormalised { get; set; }
private int lastIndex = 0;
private double countRed, countGreen;
protected override void Initialize()
{
countGreen = 0;
countRed = 0;
}
public override void Calculate(int index)
{
if (index > lastIndex)
{
lastIndex = index;
if(Bars.ClosePrices.Last(2) < Bars.ClosePrices.Last(1))
{
countGreen++;
HLGreenBars[index - 1] = countGreen;
HLBarsNormalised[index - 1] = HLGreenBars.Sum(Lookback) / ( HLGreenBars.Sum(Lookback) + HLRedBars.Sum(Lookback) ) * 100;
countRed = 0;
}
if(Bars.ClosePrices.Last(2) > Bars.ClosePrices.Last(1))
{
countRed++;
HLRedBars[index - 1] = countRed;
HLBarsNormalised[index - 1] = HLRedBars.Sum(Lookback) / ( HLGreenBars.Sum(Lookback) + HLRedBars.Sum(Lookback) ) * 100;
countGreen = 0;
}
Print("Green count {0}" , HLGreenBars.Count() );
Print("Red count {0}" , HLRedBars.Sum(10) );
}
}
}
}
Replies
stuart
08 Mar 2023, 12:42
RE:
pick said:
The issue is that you aren't storing a value for each index. Within each if block, you're only choosing to assign the value to one of the two series, so the other will be unpopulated. Also, I can imagine there will be cases where neither if block is entered (if close prices are equal), so both will be unpopulated at that index.
I believe that it may also be an issue that you're not setting the current index, you're always setting the one prior. I believe the sum function goes up to and includes the final value.
Try this change and see if you get a usable result:
public override void Calculate(int index) { // HLGreenBars[index] = 0; HLRedBars[index] = 0; // if (index > lastIndex) {
Yes, that was it.. thank you :-)
@stuart
pick
08 Mar 2023, 11:43
The issue is that you aren't storing a value for each index. Within each if block, you're only choosing to assign the value to one of the two series, so the other will be unpopulated. Also, I can imagine there will be cases where neither if block is entered (if close prices are equal), so both will be unpopulated at that index.
I believe that it may also be an issue that you're not setting the current index, you're always setting the one prior. I believe the sum function goes up to and includes the final value.
Try this change and see if you get a usable result:
@pick