Download historical Spread

Created at 22 Mar 2022, 13:43
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!
KO

kostya.bartchenkov

Joined 03.08.2020

Download historical Spread
22 Mar 2022, 13:43


Hi,

 

I wanted to check if there is a way to download/calculate historical Spread, as it seems it's only possible to get historical Bars or Ticks?

I know how to get it for live events by calculating the difference between ask and bid, but I'd like to get historical data on it as well.

 

Cheers

Kostya


@kostya.bartchenkov
Replies

amusleh
23 Mar 2022, 07:50

Hi,

You can use historical tick data to get historical spread data.

Just get the historical tick data by using API ProtoOAGetTickDataReq message, and then calculate the spread by using each tick bid/ask prices, spread is the different between bid and ask price.


@amusleh

YesOrNot2
20 Aug 2024, 02:11

Spread Calculation

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 SpreadPressure : Indicator
    {
        [Parameter("FastLimit", DefaultValue = "Tick")]
        public TimeFrame TF1 { get; set; }

        [Output("AskResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "Lime", Thickness = 1)]
        public IndicatorDataSeries AskResult { get; set; }
        [Output("BidResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries BidResult { get; set; }
        [Output("SpredResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "White", Thickness = 1)]
        public IndicatorDataSeries SpredResult { get; set; }

        private Ticks tick;

        protected override void Initialize()
        {
            tick = MarketData.GetTicks();
        }

        public override void Calculate(int index)
        {
            //var trying = (tick.Last(0).Ask - tick.Last(0).Bid) * Symbol.PipSize;

            //AskResult[index] = tick.Last(0).Ask;
            //BidResult[index] = tick.Last(0).Bid;
            SpredResult[index] = (tick.Last(0).Ask - tick.Last(0).Bid) / Symbol.PipSize;

        }
    }
}


@YesOrNot2

YesOrNot2
20 Aug 2024, 02:39

Spread v2

Better This :

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 SpreadPressure : Indicator
    {
        [Output("SpredResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "White", Thickness = 1)]
        public IndicatorDataSeries SpredResult { get; set; }

        private Ticks tick;
        private Bars barstick;

        protected override void Initialize()
        {
            tick = MarketData.GetTicks();
            barstick = MarketData.GetBars(TimeFrame.Tick);

            while (barstick.OpenTimes[0] > Bars.OpenTimes[0])
                barstick.LoadMoreHistory();
        }

        public override void Calculate(int index)
        {
            var indexTf = GetIndexByDate(Bars.OpenTimes.Last(0));
            SpredResult[index] = (tick[indexTf].Ask - tick[indexTf].Bid) / Symbol.PipSize;
        }

        private int GetIndexByDate(DateTime date)
        {
            var bars = MarketData.GetBars(TimeFrame.Tick);
            for (var i = bars.OpenTimes.Count - 1; i >= 0; i--)
            {
                if (bars.OpenTimes[i] <= date)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}

@YesOrNot2

PanagiotisCharalampous
20 Aug 2024, 05:01

RE: Spread v2

YesOrNot2 said: 

Better This :

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 SpreadPressure : Indicator    {        [Output("SpredResult", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "White", Thickness = 1)]        public IndicatorDataSeries SpredResult { get; set; }        private Ticks tick;        private Bars barstick;        protected override void Initialize()        {            tick = MarketData.GetTicks();            barstick = MarketData.GetBars(TimeFrame.Tick);            while (barstick.OpenTimes[0] > Bars.OpenTimes[0])                barstick.LoadMoreHistory();        }        public override void Calculate(int index)        {            var indexTf = GetIndexByDate(Bars.OpenTimes.Last(0));            SpredResult[index] = (tick[indexTf].Ask - tick[indexTf].Bid) / Symbol.PipSize;        }        private int GetIndexByDate(DateTime date)        {            var bars = MarketData.GetBars(TimeFrame.Tick);            for (var i = bars.OpenTimes.Count - 1; i >= 0; i--)            {                if (bars.OpenTimes[i] <= date)                {                    return i;                }            }            return -1;        }    }}

You can also use Symbol.Spread


@PanagiotisCharalampous