Bot to export historical data

Created at 19 Oct 2020, 12: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!
FA

fabio.cescon.97

Joined 19.10.2020

Bot to export historical data
19 Oct 2020, 12:20


Hi guys I want to share with you a bot I made that provide a simple way to export backtesting historical data from the ctrader platform to a .csv file

 

# Instructions

1. Add a new instance of the bot and on the symbol and the timeframe that you wish to export

2. Set in the bot's parameters the directory where will be created the .csv file (the directory needs to exist)

3. (optional) set the buffer size to optimize performance

4. run the backtesting for the time period you wish to export the data

5. A file will be created in the directory specified in the bot's parameters

 

# Notes

The format of the csv file will be

yyyy.mm.dd | HH:MM | Open | High | Low | Close | TickVolume

 

# Code

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class ExportHistData : Robot
    {

        [Parameter("Data directory", DefaultValue = "C:\\Data\\Ctrader_hist_data")]
        public string DataDir { get; set; }

        [Parameter("Buffer size", DefaultValue = 10000)]
        public int BufferSize { get; set; }

        private string Path { get; set; }
        private string Buffer { get; set; }
        private int BufferLen { get; set; }


        protected override void OnStart()
        {
            Path = DataDir + "\\" + Symbol.Name + "-" + Bars.TimeFrame.ToString() + ".csv";
            File.CreateText(Path);
            Buffer = "";
            BufferLen = 0;
        }

        protected override void OnBar()
        {
            if (BufferLen < BufferSize)
            {
                Buffer += Bars.OpenTimes.Last(1).ToString("yyyy.MM.dd") + ","
                        + Bars.OpenTimes.Last(1).ToString("HH:mm") + ","
                        + Bars.OpenPrices.Last(1).ToString() + ","
                        + Bars.HighPrices.Last(1).ToString() + ","
                        + Bars.LowPrices.Last(1).ToString() + ","
                        + Bars.ClosePrices.Last(1).ToString() + ","
                        + Bars.TickVolumes.Last(1).ToString() + "\n";
                BufferLen += 1;
            }
            else
            {
                using (StreamWriter sw = File.AppendText(Path))
                {
                    sw.Write(Buffer);
                }
                Buffer = "";
                BufferLen = 0;
            }
        }

        protected override void OnStop()
        {
            if (BufferLen > 0)
            {
                using (StreamWriter sw = File.AppendText(Path))
                {
                    sw.Write(Buffer);
                }
            }
        }
    }
}

 


@fabio.cescon.97
Replies

prosteel1
23 Oct 2020, 18:49

RE:

Awesome! I've been trying to figure this out lately, Thanks! I think we should join your writing code with my multitimeframe get more history code :)  I tried running mine on 4GB ram using Tick 10, Minute etc and it ran out of memory, 32Gb ram worked eventually. I'm considering storing historic data locally so if the data is available locally it wont try to load more history. Could help those without 32GB ram to have access to all the history just by sharing files or running the bot multiple times :)

It looks like Tick, minute etc (lower timeframe) data is only available for a certain time before it is no longer available. So having a way to store what is available now could be useful in the future - especially if it was part of a bot starting up.

 


@prosteel1

thanhtruong10693
06 Feb 2024, 06:38 ( Updated at: 06 Feb 2024, 07:31 )

Hi Fabio 

I'm not a tech guy. I want to export Bar information to excel include ( open, close, high and low ) of each bar. How can i do that


@thanhtruong10693