Реальные ТИКОВЫЕ котировки (History data)

ever2

Местный житель
Как-то давно хотел протестировать свой советник на реальных ТИКОВЫХ котировках в Metatrader 4, но в интернете не нашел, где можно их скачать. А тут на днях установил cTrader от IC Markets, и удивился что в нем есть реальные ТИКОВЫЕ котировки истории с ихнего сервера начиная с 2014 года. Осталось только выгрузить их в CSV и использовать скрипт CSV2FXT.

Код для выгрузки котировок:
C#:
using System;
using System.Linq;
using System.IO;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class DataExportTicks : Robot
    {
        // QUESTION:  How do I set default Source Timeframe to T1 (1 tick)
        //[Parameter("TimeFrame ")]
        //public TimeFrame TFrame { get; set; }

        // QUESTION: How do I get Backteset to default to 1 tick from server
        //  so don't have to manually reset?


        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Data Dir", DefaultValue = "c:\\download\\calgo")]
        public string DataDir { get; set; }


        private string fiName;
        private System.IO.FileStream fstream;
        private System.IO.StreamWriter fwriter;
        private string csvhead = "date,bid,ask,bidvol,askvol\n";


        protected override void OnStart()
        {
            var ticktype = MarketSeries.TimeFrame.ToString();
            fiName = DataDir + "\\" + "exp-" + Symbol.Code + "-ticks.csv";
            Print("fiName=" + fiName);

            if (System.IO.File.Exists(fiName) == false)
            {
                // generate new file with CSV header only if
                // one does not already exist.
                System.IO.File.WriteAllText(fiName, csvhead);
            }

            // had to open file this way to prevent .net from locking it and preventing
            // access by other processes when using to download live ticks.
            fstream = File.Open(fiName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
            // setup to append to end of file
            Print("File is Open");
            fstream.Seek(0, SeekOrigin.End);
            // write stream has to be created after seek due to .net wierdness
            // creating with 0 prevents buffering since we want tick data
            // to be available to consumers right away.
            fwriter = new System.IO.StreamWriter(fstream, System.Text.Encoding.UTF8, 1);
            // QUESTION:  How to tell when in Backtest mode so we
            //  can create the stream with a large buffer and turn off
            // auto flush to improve IO performance.
            Print("Fwriter is created");
            fwriter.AutoFlush = true;
            // with autoflush true will autocleanup
            // since we can not close since we may run forever
            Print("done onStart()");
        }

        protected double vol_weighted_price(cAlgo.API.Collections.IReadonlyList<cAlgo.API.MarketDepthEntry> mkentries)
        {
            double weightdiv = 0.0;
            double tsum = 0.0;
            for (int i = 0; i < mkentries.Count; i++)
            {
                var aent = mkentries[i];
                tsum += (double)aent.Price * (double)aent.Volume;
                weightdiv += (double)aent.Volume;
            }
            if (weightdiv == 0)
            {
                return 0;
            }
            else
            {
                return tsum / weightdiv;
            }
        }


        protected double vol_weighted_cnt(cAlgo.API.Collections.IReadonlyList<cAlgo.API.MarketDepthEntry> mkentries)
        {
            double weightdiv = 0.0;
            double tsum = 0.0;
            for (int i = 0; i < mkentries.Count; i++)
            {
                var aent = mkentries[i];
                tsum += (double)aent.Volume * (double)aent.Price;
                weightdiv += (double)aent.Price;
            }
            if (weightdiv == 0)
            {
                return 0;
            }
            else
            {
                return tsum / weightdiv;
            }
        }

        protected override void OnTick()
        {
            var sa = new System.Collections.Generic.List<string>();
            //var barTime = MarketSeries.OpenTime.LastValue;
            var barTime = Server.Time;
            var timestr = barTime.ToString("yyyy.MM.dd HH:mm:ss.fff");

            //MarketDepth mkdepth = MarketData.GetMarketDepth(Symbol.Code);
            var volAdjAsk = Symbol.Ask;
            var volAdjBid = Symbol.Bid;
            //var volAdjSpread = volAdjAsk - volAdjBid;
            //var volCntAsk = vol_weighted_cnt(mkdepth.AskEntries) / 100000.0;
            //var volCntBid = vol_weighted_cnt(mkdepth.BidEntries) / 100000.0;
            //var vonCntBuyVsSell = volCntAsk - volCntBid;

            //if ((volAdjAsk == 0.0) && (volAdjBid == 0.0))
            //{
            //    return;
            //}

            sa.Add(timestr);
            sa.Add(volAdjBid.ToString("F6").Replace(",", "."));
            sa.Add(volAdjAsk.ToString("F6").Replace(",", "."));
            sa.Add("1.0");
            sa.Add("1.0");


            var sout = string.Join(",", sa);
            //System.IO.File.AppendAllText(fiName, sout);
            fwriter.WriteLine(sout);
            fwriter.Flush();

        }


        protected override void OnStop()
        {
            Print("OnStop()");
            fwriter.Close();
            fstream.Close();
            // Put your deinitialization logic here
        }
    }
}

Если создал топик не в той теме, просьба модераторов не ругать сильно и перенести.
 

Вложения

  • csv2fxt_v0.50.zip
    190,8 КБ · Просмотры: 35
Последнее редактирование модератором:

ever2

Местный житель
нашел ошибку в скрипте выгрузки, из-за нее скрипт CSV2FXT не обрабатывал CSV файл. Исправляю:

C#:
using System;
using System.Linq;
using System.IO;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class DataExportTicks : Robot
    {
        // QUESTION:  How do I set default Source Timeframe to T1 (1 tick)
        //[Parameter("TimeFrame ")]
        //public TimeFrame TFrame { get; set; }

        // QUESTION: How do I get Backteset to default to 1 tick from server
        //  so don't have to manually reset?


        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Data Dir", DefaultValue = "c:\\download\\calgo")]
        public string DataDir { get; set; }


        private string fiName;
        private System.IO.FileStream fstream;
        private System.IO.StreamWriter fwriter;
        private string csvhead = "date,bid,ask,bidvol,askvol\n";


        protected override void OnStart()
        {
            var ticktype = MarketSeries.TimeFrame.ToString();
            fiName = DataDir + "\\" + "exp-" + Symbol.Code + "-ticks.csv";
            Print("fiName=" + fiName);

            if (System.IO.File.Exists(fiName) == false)
            {
                // generate new file with CSV header only if
                // one does not already exist.
                System.IO.File.WriteAllText(fiName, csvhead);
            }

            // had to open file this way to prevent .net from locking it and preventing
            // access by other processes when using to download live ticks.
            fstream = File.Open(fiName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
            // setup to append to end of file
            Print("File is Open");
            fstream.Seek(0, SeekOrigin.End);
            // write stream has to be created after seek due to .net wierdness
            // creating with 0 prevents buffering since we want tick data
            // to be available to consumers right away.
            fwriter = new System.IO.StreamWriter(fstream, System.Text.Encoding.UTF8, 1);
            // QUESTION:  How to tell when in Backtest mode so we
            //  can create the stream with a large buffer and turn off
            // auto flush to improve IO performance.
            Print("Fwriter is created");
            fwriter.AutoFlush = true;
            // with autoflush true will autocleanup
            // since we can not close since we may run forever
            Print("done onStart()");
        }

        protected double vol_weighted_price(cAlgo.API.Collections.IReadonlyList<cAlgo.API.MarketDepthEntry> mkentries)
        {
            double weightdiv = 0.0;
            double tsum = 0.0;
            for (int i = 0; i < mkentries.Count; i++)
            {
                var aent = mkentries[i];
                tsum += (double)aent.Price * (double)aent.Volume;
                weightdiv += (double)aent.Volume;
            }
            if (weightdiv == 0)
            {
                return 0;
            }
            else
            {
                return tsum / weightdiv;
            }
        }


        protected double vol_weighted_cnt(cAlgo.API.Collections.IReadonlyList<cAlgo.API.MarketDepthEntry> mkentries)
        {
            double weightdiv = 0.0;
            double tsum = 0.0;
            for (int i = 0; i < mkentries.Count; i++)
            {
                var aent = mkentries[i];
                tsum += (double)aent.Volume * (double)aent.Price;
                weightdiv += (double)aent.Price;
            }
            if (weightdiv == 0)
            {
                return 0;
            }
            else
            {
                return tsum / weightdiv;
            }
        }

        protected override void OnTick()
        {
            var sa = new System.Collections.Generic.List<string>();
            //var barTime = MarketSeries.OpenTime.LastValue;
            var barTime = Server.Time;
            var timestr = barTime.ToString("yyyy.MM.dd HH:mm:ss.fff");

            //MarketDepth mkdepth = MarketData.GetMarketDepth(Symbol.Code);
            var volAdjAsk = Symbol.Ask;
            var volAdjBid = Symbol.Bid;
            //var volAdjSpread = volAdjAsk - volAdjBid;
            //var volCntAsk = vol_weighted_cnt(mkdepth.AskEntries) / 100000.0;
            //var volCntBid = vol_weighted_cnt(mkdepth.BidEntries) / 100000.0;
            //var vonCntBuyVsSell = volCntAsk - volCntBid;

            //if ((volAdjAsk == 0.0) && (volAdjBid == 0.0))
            //{
            //    return;
            //}

            sa.Add(timestr);
            sa.Add(volAdjBid.ToString("F5").Replace(",", "."));
            sa.Add(volAdjAsk.ToString("F5").Replace(",", "."));
            sa.Add("1.0");
            sa.Add("1.0");


            var sout = string.Join(",", sa);
            //System.IO.File.AppendAllText(fiName, sout);
            fwriter.WriteLine(sout);
            fwriter.Flush();

        }


        protected override void OnStop()
        {
            Print("OnStop()");
            fwriter.Close();
            fstream.Close();
            // Put your deinitialization logic here
        }
    }
}
 

Dmitii

Интересующийся
Ребята можете написать подробнее что нужно для тестирования
Как-то давно хотел протестировать свой советник на реальных ТИКОВЫХ котировках в Metatrader 4, но в интернете не нашел, где можно их скачать. А тут на днях установил cTrader от IC Markets, и удивился что в нем есть реальные ТИКОВЫЕ котировки истории с ихнего сервера начиная с 2014 года. Осталось только выгрузить их в CSV и использовать скрипт CSV2FXT.

Код для выгрузки котировок:
C#:
using System;
using System.Linq;
using System.IO;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class DataExportTicks : Robot
    {
        // QUESTION:  How do I set default Source Timeframe to T1 (1 tick)
        //[Parameter("TimeFrame ")]
        //public TimeFrame TFrame { get; set; }

        // QUESTION: How do I get Backteset to default to 1 tick from server
        //  so don't have to manually reset?


        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Data Dir", DefaultValue = "c:\\download\\calgo")]
        public string DataDir { get; set; }


        private string fiName;
        private System.IO.FileStream fstream;
        private System.IO.StreamWriter fwriter;
        private string csvhead = "date,bid,ask,bidvol,askvol\n";


        protected override void OnStart()
        {
            var ticktype = MarketSeries.TimeFrame.ToString();
            fiName = DataDir + "\\" + "exp-" + Symbol.Code + "-ticks.csv";
            Print("fiName=" + fiName);

            if (System.IO.File.Exists(fiName) == false)
            {
                // generate new file with CSV header only if
                // one does not already exist.
                System.IO.File.WriteAllText(fiName, csvhead);
            }

            // had to open file this way to prevent .net from locking it and preventing
            // access by other processes when using to download live ticks.
            fstream = File.Open(fiName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
            // setup to append to end of file
            Print("File is Open");
            fstream.Seek(0, SeekOrigin.End);
            // write stream has to be created after seek due to .net wierdness
            // creating with 0 prevents buffering since we want tick data
            // to be available to consumers right away.
            fwriter = new System.IO.StreamWriter(fstream, System.Text.Encoding.UTF8, 1);
            // QUESTION:  How to tell when in Backtest mode so we
            //  can create the stream with a large buffer and turn off
            // auto flush to improve IO performance.
            Print("Fwriter is created");
            fwriter.AutoFlush = true;
            // with autoflush true will autocleanup
            // since we can not close since we may run forever
            Print("done onStart()");
        }

        protected double vol_weighted_price(cAlgo.API.Collections.IReadonlyList<cAlgo.API.MarketDepthEntry> mkentries)
        {
            double weightdiv = 0.0;
            double tsum = 0.0;
            for (int i = 0; i < mkentries.Count; i++)
            {
                var aent = mkentries[i];
                tsum += (double)aent.Price * (double)aent.Volume;
                weightdiv += (double)aent.Volume;
            }
            if (weightdiv == 0)
            {
                return 0;
            }
            else
            {
                return tsum / weightdiv;
            }
        }


        protected double vol_weighted_cnt(cAlgo.API.Collections.IReadonlyList<cAlgo.API.MarketDepthEntry> mkentries)
        {
            double weightdiv = 0.0;
            double tsum = 0.0;
            for (int i = 0; i < mkentries.Count; i++)
            {
                var aent = mkentries[i];
                tsum += (double)aent.Volume * (double)aent.Price;
                weightdiv += (double)aent.Price;
            }
            if (weightdiv == 0)
            {
                return 0;
            }
            else
            {
                return tsum / weightdiv;
            }
        }

        protected override void OnTick()
        {
            var sa = new System.Collections.Generic.List<string>();
            //var barTime = MarketSeries.OpenTime.LastValue;
            var barTime = Server.Time;
            var timestr = barTime.ToString("yyyy.MM.dd HH:mm:ss.fff");

            //MarketDepth mkdepth = MarketData.GetMarketDepth(Symbol.Code);
            var volAdjAsk = Symbol.Ask;
            var volAdjBid = Symbol.Bid;
            //var volAdjSpread = volAdjAsk - volAdjBid;
            //var volCntAsk = vol_weighted_cnt(mkdepth.AskEntries) / 100000.0;
            //var volCntBid = vol_weighted_cnt(mkdepth.BidEntries) / 100000.0;
            //var vonCntBuyVsSell = volCntAsk - volCntBid;

            //if ((volAdjAsk == 0.0) && (volAdjBid == 0.0))
            //{
            //    return;
            //}

            sa.Add(timestr);
            sa.Add(volAdjBid.ToString("F6").Replace(",", "."));
            sa.Add(volAdjAsk.ToString("F6").Replace(",", "."));
            sa.Add("1.0");
            sa.Add("1.0");


            var sout = string.Join(",", sa);
            //System.IO.File.AppendAllText(fiName, sout);
            fwriter.WriteLine(sout);
            fwriter.Flush();

        }


        protected override void OnStop()
        {
            Print("OnStop()");
            fwriter.Close();
            fstream.Close();
            // Put your deinitialization logic here
        }
    }
}

Если создал топик не в той теме, просьба модераторов не ругать сильно и перенести.
 
Верх