Category Other  Published on 16/12/2020

CSV Data Writer

Description

Currently this is how we perform backtesting and analysis of renko and range strategies in cTrader. We use this indicator to download the data prior to analysis in Python:

To see the projects (indicators, cBots and data science analysis) we're currently working on, you can join our Telegram chat here: t.me/quantFXAlgos

We also have a slight ask from people who download: we're currently looking to get renko shadows / wicks / range / tails introduced as a feature in cTrader.
You can help them be introduced by voting in the cTrader suggestions forum for this post: https://ctrader.com/forum/suggestions/29955

Cheers all

 

 

This Indi Does require FullAccess rights, but only so it can save a csv file of the data.

Output: A comma delimited csv file with Open Time, OHLC Data, and data of up to 3 indicator series for all the loaded bars on the chart (or loaded to the input date).
The indicator will automatically save the file to desktop.

Inputs:

  • Filename: The name that you wish the CSV file to have (eg. "EURGBP_Renko10"). Make sure this doesn't include punctuation like fullstops and commas that Windows won't allow in file names.
  • Start Writing: A boolean parameter just in case you accidentally click okay before you were ready to. False will prevent a csv from being saved allowing you to go back in and edit parameters.
  • Load To: The date that you wish to have data going back to in the format dd/mm/yyy eg. 01/01/2020 will save the maximum of: bars alread loaded, or bars back to 01/01/2020.
  • Source (1,2,3): These are the Source Series and the Names that you want them to have in the CSV. Make sure not to include commas in the source names else they'll be scattered over the columns (as it's a comma delimited csv).

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class CSVDataWriter : Indicator
    {
        [Parameter("Source 1", Group = "Source 1")]
        public DataSeries Source1 { get; set; }

        [Parameter("Source Name 1", DefaultValue = "Indi_Name1", Group = "Source 1")]
        public string SourceName1 { get; set; }

        [Parameter("Source 2", Group = "Source 2")]
        public DataSeries Source2 { get; set; }

        [Parameter("Source Name 2", DefaultValue = "Indi_Name2", Group = "Source 2")]
        public string SourceName2 { get; set; }

        [Parameter("Source 3", Group = "Source 3")]
        public DataSeries Source3 { get; set; }

        [Parameter("Source Name 3", DefaultValue = "Indi_Name3", Group = "Source 3")]
        public string SourceName3 { get; set; }


        [Parameter("File Name", DefaultValue = "CSV_Output")]
        public string FileName { get; set; }


        [Parameter("Start Writing", DefaultValue = false)]
        public bool StartWriting { get; set; }

        [Parameter("Load To Date?", DefaultValue = false)]
        public bool LoadToDate { get; set; }

        [Parameter("Load To (dd/mm/yyyy):", DefaultValue = "01/01/2019")]
        public string LoadDate { get; set; }


        [Output("Zero Line")]
        public IndicatorDataSeries ZeroLine { get; set; }

        private string fullPath;
        private StringBuilder csv;
        private bool haveWritten;
        private DateTime dateTime;

        protected override void Initialize()
        {
            // Set up the string writer for CSV:
            haveWritten = false;
            fullPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + FileName + ".csv";
            csv = new StringBuilder();
            string intro = string.Format("Time,Open,High,Low,Close,{0},{1},{2}", SourceName1, SourceName2, SourceName3);
            csv.AppendLine(intro);

            // Get datetime to load until:
            string[] pieces = LoadDate.Split('/');
            dateTime = new DateTime(int.Parse(pieces[2]), int.Parse(pieces[1]), int.Parse(pieces[0]));

            Print("{0} bar on the chart", Bars.Count);

            if (LoadToDate)
                LoadMoreBars(dateTime);

        }

        private void LoadMoreBars(DateTime dateTime)
        {
            while (Bars.OpenTimes.Reverse().LastOrDefault() > dateTime)
            {
                var loadedCount = Bars.LoadMoreHistory();
                Print("Loaded {0} bars", loadedCount);
                if (loadedCount == 0)
                    break;
            }
            Print("Finished, earliest open: {0}", Bars.OpenTimes.Reverse().LastOrDefault());
        }

        public override void Calculate(int index)
        {

            ZeroLine[index] = 0;

            if (StartWriting)
            {
                string open = Bars.OpenPrices[index].ToString();
                string high = Bars.HighPrices[index].ToString();
                string low = Bars.LowPrices[index].ToString();
                string close = Bars.ClosePrices[index].ToString();
                string sourceValue1 = Source1[index].ToString();
                string sourceValue2 = Source2[index].ToString();
                string sourceValue3 = Source3[index].ToString();

                var newLine = string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", Bars.OpenTimes[index], open, high, low, close, sourceValue1, sourceValue2, sourceValue3);
                csv.AppendLine(newLine);

                if (!haveWritten & IsLastBar)
                {
                    File.WriteAllText(fullPath, csv.ToString());
                    haveWritten = true;
                }
            }



        }
    }
}


fxtradersystems's avatar
fxtradersystems

Joined on 10.09.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: CSV Data Writer.algo
  • Rating: 0
  • Installs: 1121
Comments
Log in to add a comment.
No comments found.