Possible LoadMoreHistory bug
Possible LoadMoreHistory bug
09 Apr 2020, 11:21
Dear Spotware,
I built a covariance indicator some time ago, it all worked fine but now it looks like it is not working anymore, and i haven't changed the code since i made it, so i was wondering what was going on.
The indicator does not seem to display any output, instead, once put on chart, it goes on loading and loading forever without ever stopping, this is show in the pic below, where the chart cursor has become very small from all the bars loaded:
This is odd, since the indicator calls LoadMoreHistory() in Initialize just once, and not in a continuous, infinite loop.
Here is source code for the indicator:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Levels(0, 0.2, -0.2, 1, -1, 0.8, -0.8)]
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Covariance : Indicator
{
[Parameter("Slow Period", DefaultValue = 120)]
public int Period { get; set; }
[Parameter("Fast Period", DefaultValue = 50)]
public int FastPeriod { get; set; }
[Parameter()]
public string FirstSource { get; set; }
[Parameter()]
public string SecondSource { get; set; }
[Parameter()]
public bool DisplayCorrelation { get; set; }
[Output("Fast", LineColor = "Cyan")]
public IndicatorDataSeries Fast { get; set; }
[Output("Slow", LineColor = "Orange")]
public IndicatorDataSeries Slow { get; set; }
private Bars FirstSeries, SecondSeries;
protected override void Initialize()
{
FirstSource = FirstSource == "" ? Symbol.Name : FirstSource;
SecondSource = SecondSource == "" ? Symbol.Name : SecondSource;
FirstSeries = MarketData.GetBars(TimeFrame, FirstSource);
SecondSeries = MarketData.GetBars(TimeFrame, SecondSource);
FirstSeries.LoadMoreHistory();
SecondSeries.LoadMoreHistory();
IndicatorArea.DrawHorizontalLine("0.8", 0.8, Color.Green);
IndicatorArea.DrawHorizontalLine("-0.8", -0.8, Color.DarkRed);
}
public override void Calculate(int index)
{
Fast[index] = ResultAt(index, FastPeriod);
Slow[index] = ResultAt(index, Period);
}
public double ResultAt(int index, int period)
{
int index1 = FirstSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
int index2 = SecondSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
double result;
///Expected values for X and Y
double firstMean = 0, secondMean = 0;
for (int i = 0; i < period; ++i)
{
firstMean += FirstSeries.ClosePrices[index1 - i];
secondMean += SecondSeries.ClosePrices[index2 - i];
}
firstMean /= period;
secondMean /= period;
///Expected value for (X - E[X])(Y - E[Y])
double productMean = 0;
for (int i = 0; i < period; ++i)
{
productMean += (FirstSeries.ClosePrices[index1 - i] - firstMean) * (SecondSeries.ClosePrices[index2 - i] - secondMean);
}
productMean /= period;
///Display covariance
result = productMean;
if (DisplayCorrelation)
{
double firstStd = 0, secondStd = 0;
for (int i = 0; i < period; ++i)
{
firstStd += Math.Pow(FirstSeries.ClosePrices[index1 - i] - firstMean, 2);
secondStd += Math.Pow(SecondSeries.ClosePrices[index2 - i] - secondMean, 2);
}
firstStd = Math.Sqrt(firstStd * 1 / (period));
secondStd = Math.Sqrt(secondStd * 1 / (period));
result = productMean / (firstStd * secondStd);
}
return result;
}
}
}
Replies
cysecsbin.01
09 Apr 2020, 19:53
( Updated at: 21 Dec 2023, 09:22 )
Hi, thanks for the reply.
i missed to tell you that first and second source parameters are to be filled in with two assets name, like "GBPUSD" and "XAUUSD", the functioning version of the indicator, with the parameters set as said and with the LoadMoreHistory commented out will display like this
Thanks in advance for any help
PanagiotisCharalampous
10 Apr 2020, 07:35
( Updated at: 21 Dec 2023, 09:22 )
Hi cysecsbin.01,
I tried that as well and seems to work fine.
Which broker do you use?
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
09 Apr 2020, 11:54 ( Updated at: 21 Dec 2023, 09:22 )
Hi cysecsbin.01,
I could not reproduce such a behavior. Instead I get an exception in the log. Did you debug it to find out what it is causing it?
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous