i need help with indi.
i need help with indi.
08 Jan 2020, 13:02
This indicator does not work on the backtest, but it works on a live chart.
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Threading;
using System.Collections.Generic;
using System.Windows;namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, ScalePrecision = 5, AutoRescale = true)]
public class Indexator : Indicator
{
[Parameter("Master Currency", DefaultValue = "EURUSD")]
public string masterName { get; set; }[Parameter("Slave Currency", DefaultValue = "AUDUSD")]
public string slaveName { get; set; }[Parameter("TimeFrame")]
public TimeFrame timeFrame { get; set; }[Parameter("Period", DefaultValue = 90, MinValue = 0, Step = 1)]
public int Period { get; set; }[Parameter("Draw Chart", DefaultValue = true)]
public bool drawChartz { get; set; }[Output("Index", LineColor = "Blue")]
public IndicatorDataSeries Index { get; set; }private Bars masterBars;
private Symbol masterSymbol;
private Bars slaveBars;
private Symbol slaveSymbol;
public double backSlaveValue;
public double PercentDiffrence;private ChartTrendLine masterLine;
private ChartTrendLine slaveLine;
private ChartTrendLine differenceLine;
private ChartText masterNameChartText;
private ChartText slaveNameChartText;
private ChartText differenceChartText;public double masterMedianOut;
public double slaveMedianOut;List<double> ResultClose = new List<double>();
protected override void Initialize()
{
masterSymbol = Symbols.GetSymbol(masterName);
masterBars = MarketData.GetBars(timeFrame, masterName);slaveSymbol = Symbols.GetSymbol(slaveName);
slaveBars = MarketData.GetBars(timeFrame, slaveName);while (slaveBars.Count < Period * 2)
{
var loadedCount = slaveBars.LoadMoreHistory();
if (loadedCount == 0)
break;
}
while (masterBars.Count < Period * 2)
{
var loadedCount = masterBars.LoadMoreHistory();
if (loadedCount == 0)
break;
}
}public override void Calculate(int index)
{
if (index < Period * 2)
{
Index[index] = Double.NaN;
return;
}
double masterSum = 0;
double slaveSum = 0;
for (int i = 0; i < Period; i++)
{
var masterBarsIndex = masterBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - i;if (!double.IsNaN(masterBarsIndex))
masterSum = masterSum + masterBars[masterBarsIndex].Close;var slaveBarsIndex = slaveBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - i;
if (!double.IsNaN(slaveBarsIndex))
slaveSum = slaveSum + slaveBars[slaveBarsIndex].Close;
}
var masterMedian = masterSum / Period;
masterMedianOut = masterMedian;
var slaveMedian = slaveSum / Period;
slaveMedianOut = slaveMedian;double k = masterMedian / slaveMedian;
var masterBarsIndexMain = masterBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
var slaveBarsIndexMain = slaveBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);ResultClose.Add(slaveBars.ClosePrices[slaveBarsIndexMain] * k);
Index[index] = PercentDiffrence = (masterBars.ClosePrices[masterBarsIndexMain] - slaveBars.ClosePrices[slaveBarsIndexMain] * k) * 100 / masterBars.ClosePrices[masterBarsIndexMain];
if (drawChartz == true)
{
if (Chart.FindObject("Point" + (Bars.ClosePrices.Count - 1000).ToString()) != null)
Chart.RemoveObject("Point" + (Bars.ClosePrices.Count - 1000).ToString());Chart.DrawTrendLine("Point" + (Bars.ClosePrices.Count - 1).ToString(), Bars.ClosePrices.Count - 2, ResultClose[ResultClose.Count -2], Bars.ClosePrices.Count - 1, ResultClose[ResultClose.Count - 1], "Blue", 2);
masterLine = Chart.DrawTrendLine("MasterTool", index, masterBars.ClosePrices[masterBarsIndexMain], index + 20, masterBars.ClosePrices[masterBarsIndexMain], "Green");
slaveLine = Chart.DrawTrendLine("SlaveTool", index, slaveBars.ClosePrices[slaveBarsIndexMain] * k, index + 20, slaveBars.ClosePrices[slaveBarsIndexMain] * k, "Green");
differenceLine = Chart.DrawTrendLine("Difference", index + 15, masterBars.ClosePrices[masterBarsIndexMain], index + 15, slaveBars.ClosePrices[slaveBarsIndexMain] * k, "Green");
masterNameChartText = Chart.DrawText("MasterNameChartText", masterName, index + 5, masterBars.ClosePrices[masterBarsIndexMain], "Red");
slaveNameChartText = Chart.DrawText("SlaveNameChartText", slaveName, index + 5, slaveBars.ClosePrices[slaveBarsIndexMain] * k, "Red");
differenceChartText = Chart.DrawText("DifferenceChartText", string.Format("Diff: {0}%", Math.Round(PercentDiffrence, 3)), index + 20, masterBars.ClosePrices[masterBarsIndexMain] + (slaveBars.ClosePrices[slaveBarsIndexMain] * k - masterBars.ClosePrices[masterBarsIndexMain]) / 2, "Red");
}
}
}
}
Replies
v.kredov
08 Jan 2020, 14:23
( Updated at: 21 Dec 2023, 09:21 )
RE:
PanagiotisCharalampous said:
Hi v.kredov,
Your indicator throws an exception here
ResultClose.Count is 1 and you are trying to access index -1. You should fix this before we can proceed.
Best Regards,
Panagiotis
This line can be commented out, it is not critical. In the real chart, it does not swear, in backtest mode, execution does not reach this. Or we can use this filter
if(ResultClose.Count > 2 && Bars.ClosePrices.Count > 1)
Chart.DrawTrendLine("Point" + (Bars.ClosePrices.Count - 1).ToString(), Bars.ClosePrices.Count - 2, ResultClose[ResultClose.Count -2], Bars.ClosePrices.Count - 1, ResultClose[ResultClose.Count - 1], "Blue", 2);
@v.kredov
PanagiotisCharalampous
08 Jan 2020, 14:07
Hi v.kredov,
Your indicator throws an exception here
ResultClose.Count is 1 and you are trying to access index -1. You should fix this before we can proceed.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous