Last(0) and MarketSeries.Close.Count - 1 index gving different values
Last(0) and MarketSeries.Close.Count - 1 index gving different values
28 Mar 2018, 00:16
ok, so I have an indicator:
Its very simple, just returns a value on the first bar of a new day. But all other values are nan.
I tried updating my code to use .Last and .LastValue but they do not act the same as using MarketSeries.Close.Count - 1 as the index.
So, usually you can say, MarketSeries.High[MarketSeries.Close.Count - 1] to get the current value. Alternatively you sghould be able to use, MarketSeries.High.Last(0) and get the same result. YOU DO!
HOWEVER! with my indicator Last(0) is NOT giving he same result. It seems to be returning the last NON-NAN value, not the last value.
I just insert a print statement into a cBot OnBar Event and you can see the behavior.
Print("Div:"+div.Dayend[MarketSeries.Close.Count - 1]+" "+div.Dayend.Last(0)+" "+div.Dayend.LastValue);
It seems like Last will work for indicators that consistently return a non-nan value. I don't think that this was by design, because I think last value was meant to eliminate the need to get a marketseries count. If it is then the API reference should let developers know the difference.
My recomendation is that they function the same.
Replies
PanagiotisCharalampous
02 Apr 2018, 12:11
Hi lec0456,
Can you share your indicator and an example that will allow us to reproduce the issue, so that we can investigate further?
Best Regards,
Panagiotis
@PanagiotisCharalampous
lec0456
02 Apr 2018, 17:56
The indicator is here:
Here is some code:
using System; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot] public class NewRobot : Robot { Dividers div; protected override void OnStart() { div = Indicators.GetIndicator<Dividers>(false, false, false); } protected override void OnBar() { Print("Div:"+div.Dayend[MarketSeries.Close.Count - 1]+" "+div.Dayend.Last(0)+" "+div.Dayend.LastValue); } } }
@lec0456
PanagiotisCharalampous
03 Apr 2018, 15:06
Hi lec0456,
Dayend IndicatorDataSeries and Robots MarketSeries have different lengths. When you call
div.Dayend[MarketSeries.Close.Count - 1]
you are requesting data out of the bound of Dayend series and that is why you receive NaN value. If you change it to
div.Dayend[div.Dayend.Count - 1]
it should work same as Last(0) and LastValue.
Best Regards,
Panagiotis
@PanagiotisCharalampous
lec0456
03 Apr 2018, 18:19
ok, but the problem is then that the index of the indicator does not match up with the index of the robot.
Dayend should return a non nan value on the first bar of a new day within a cbot. If the onBar is triggering in sync with the MarketSeries index and the indicator is using a different index the values will not matchup. That is why if I use div.Dayend.Last(0) it will not give me the corresponding value for the specific time of day that the cBot is at.
if (!double.IsNaN(div.Dayend[MarketSeries.Close.Count - 1])) { Print("****" + EESTDate(MarketSeries.OpenTime.Last(0)).Date + "*****" + EESTDate(MarketSeries.OpenTime.Last(0)).DayOfWeek.ToString()); }
The above code works fine. But if I change it to div.Dayend.Last(0) it does not work.
@lec0456
PanagiotisCharalampous
04 Apr 2018, 10:20
Hi lec0456,
Ok I think I understand your point. Maybe you could tweak a bit the indicator and replace the NaN values with 0 so that it behaves as you expected. See below
public override void Calculate(int index) { if (index - 1 < 0) return; DateTime CurrentDate = MarketSeries.OpenTime[index].AddHours(2); DateTime PreviousDate = MarketSeries.OpenTime[index - 1].AddHours(2); int DateDifference = (int)(CurrentDate.Date - PreviousDate.Date).TotalDays; //**Dayend if (CurrentDate.DayOfWeek != PreviousDate.DayOfWeek && PreviousDate.DayOfWeek != DayOfWeek.Sunday) { if (!HideDayLabels) ChartObjects.DrawText("DayLabel" + index, " " + Convert.ToString(CurrentDate.DayOfWeek), index, MarketSeries.Low.Minimum(PeriodsPerDay), VerticalAlignment.Bottom, HorizontalAlignment.Right); if (!HideDayDividers) ChartObjects.DrawVerticalLine("Dayend" + index, index, Colors.Orange, 1, LineStyle.DotsRare); Dayend[index] = MarketSeries.Median[index]; //Print(CurrentDate.Date+" "+DateDifference); } else { Dayend[index] = 0; } //** Weekend //if(DateDifference>1) if (CurrentDate.DayOfWeek < PreviousDate.DayOfWeek || DateDifference > 6) { if (!HideWeekDividers) ChartObjects.DrawVerticalLine("Weekend" + index, index, Colors.Red, 1, LineStyle.DotsRare); Weekend[index] = MarketSeries.Median[index]; //Print(CurrentDate.Date+" "+DateDifference); } }
With the above change, NaN values will be replaced with zeros and will not be skipped by last value functions.
Let me know if this works for you.
Best Regards,
Panagiotis
@PanagiotisCharalampous
lec0456
04 Apr 2018, 11:09
Well, problem is that that indicator is an Overlay, so If I put zeros in it, it will mess up the display.
I can survive by using the MarketSeries index. I was just trying to clean up some of my code with the newer API.
But at least I confirmed the behavior. And that it is by design, I suggest placing a note in the API documentation so that developers know how to work with it. Or including the nan values in the series.
It is confusing because lastvalue works fine with all the market series stuff and most indicators are based off of them. But some cases, where nan values are possible, it could take a while before figuring out the indexes are off.
@lec0456
lec0456
02 Apr 2018, 00:54
Hi, This is a simple but important issue, can I please get a response from spotware?
@lec0456