Limit loading historical data

Created at 13 Feb 2025, 17:20
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
91

910247359

Joined 19.12.2021

Limit loading historical data
13 Feb 2025, 17:20


Hello:

    When the indicator is loading, I only want it to calculate and display the results for the most recent 300 candlesticks. How can I limit this? Too much historical data is very resource-intensive and often causes crashes, resulting in nothing being displayed. It would be great if there could be a feature like this: I enter a number, such as 300, and it automatically calculates from the 300th candlestick towards the older historical candlesticks. For example, it automatically extends by 200 candlesticks, so 300 + 200 = 500, and then it calculates and displays the results for the candlesticks from 300 to 500. If I enter 0, it should calculate the data for the 200 candlesticks preceding the current one. Could you please help me modify the code? Thank you very much. 

I also have other more complex indicators that require a significant amount of computation and are very resource-intensive. Therefore, it is necessary to add this limitation.

below are codes:

//========================================================

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System.Linq;
using System;

[Indicator(IsOverlay = true, AutoRescale = true, AccessRights = AccessRights.None)]
public class BuySellSignalIndicator : Indicator
{
    [Parameter("Buy Symbol", DefaultValue = "↑")]
    public string BuySymbol { get; set; }

    [Parameter("Sell Symbol", DefaultValue = "↓")]
    public string SellSymbol { get; set; }

    [Parameter("Buy Color", DefaultValue = "Lime")]
    public string BuyColor { get; set; }

    [Parameter("Sell Color", DefaultValue = "Red")]
    public string SellColor { get; set; }

    [Parameter("MA Period", DefaultValue = 20)]
    public int MaPeriod { get; set; }

    [Parameter("Lookback Period", DefaultValue = 800, MinValue = 100, MaxValue = 1000)]
    public int LookbackPeriod { get; set; }

    private SimpleMovingAverage _ma;

    protected override void Initialize()
    {
        _ma = Indicators.SimpleMovingAverage(Bars.ClosePrices, MaPeriod);

       
        ClearOldSignals();
    }

    public override void Calculate(int index)
    {
        
        int startIndex = Math.Max(0, Bars.Count - LookbackPeriod);

       
        if (index < startIndex) return;

  
        if (index < 1) return;

       
        double currentClose = Bars.ClosePrices[index];
        double currentMA = _ma.Result[index];
        double previousClose = Bars.ClosePrices[index - 1];
        double previousMA = _ma.Result[index - 1];

   
        if (previousClose < previousMA && currentClose > currentMA)
        {
            DrawSignal(BuySymbol, index, BuyColor);
        }
        else if (previousClose > previousMA && currentClose < currentMA)
        {
            DrawSignal(SellSymbol, index, SellColor);
        }
    }

    private void DrawSignal(string symbol, int barIndex, string colorName)
    {
     
        int startIndex = Math.Max(0, Bars.Count - LookbackPeriod);
        if (barIndex < startIndex) return;

        var yPosition = Bars.LowPrices[barIndex] - 10 * Symbol.PipSize;
        Chart.DrawText(
            $"Signal_{barIndex}", //
            symbol,
            barIndex,
            yPosition,
            Color.FromName(colorName)
        );
    }

    private void ClearOldSignals()
    {
        var objects = Chart.Objects.Where(obj => obj.Name.StartsWith("Signal_")).ToList();
        foreach (var obj in objects)
        {
            Chart.RemoveObject(obj.Name);
        }
    }
}

//============================================


@910247359
Replies

firemyst
18 Feb 2025, 04:54

Out of curiosity, why are you calculating startIndex twice?

int startIndex = Math.Max(0, Bars.Count - LookbackPeriod);

Just do it once in Calculate, and then pass the “startIndex” as a parameter to DrawSignal.

 

By doing so, it also means you no longer need this conditional check in DrawSignal:

if (barIndex < startIndex) return;


@firemyst