Beispiel #1
0
        /// <summary>
        ///     Loads, filters and saves all data files.
        /// </summary>
        private void LoadSaveData()
        {
            string[] files = Directory.GetFiles(Data.OfflineDataDir, "*.csv");
            foreach (string file in files)
            {
                string symbol = GetSymbolFromFileName(file);
                int    period = GetPeriodFromFileName(file);
                if (string.IsNullOrEmpty(symbol) || period == 0)
                {
                    continue;
                }

                InstrumentProperties instrProperties = Instruments.InstrumentList[symbol].Clone();
                var instrument = new Instrument(instrProperties, period)
                {
                    DataDir      = Data.OfflineDataDir,
                    MaxBars      = Configs.MaxBars,
                    StartTime    = Configs.DataStartTime,
                    EndTime      = Configs.DataEndTime,
                    UseStartTime = Configs.UseStartTime,
                    UseEndTime   = Configs.UseEndTime
                };

                int loadDataResult = instrument.LoadData();

                if (instrument.Bars > 0 && loadDataResult == 0)
                {
                    var stringBuilder = new StringBuilder(instrument.Bars);
                    for (int bar = 0; bar < instrument.Bars; bar++)
                    {
                        stringBuilder.AppendLine(
                            instrument.Time(bar).ToString("yyyy-MM-dd") + "\t" +
                            instrument.Time(bar).ToString("HH:mm") + "\t" +
                            instrument.Open(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.High(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Low(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Close(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Volume(bar).ToString(CultureInfo.InvariantCulture)
                            );
                    }
                    try
                    {
                        var sw = new StreamWriter(file);
                        sw.Write(stringBuilder.ToString());
                        sw.Close();

                        TbxOutput.Text += symbol + period + " bars: " + instrument.Bars + Environment.NewLine;
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
                }
            }
        }
        /// <summary>
        ///     LoadInstrument
        /// </summary>
        private int LoadInstrument(bool useResource)
        {
            Cursor = Cursors.WaitCursor;

            //  Takes the instrument symbol and period
            string symbol = ComboBoxSymbol.Text;
            var dataPeriod = (DataPeriod) Enum.GetValues(typeof (DataPeriod)).GetValue(ComboBoxPeriod.SelectedIndex);
            InstrumentProperties instrProperties = Instruments.InstrumentList[symbol].Clone();

            //  Makes an instance of class Instrument
            var instrument = new Instrument(instrProperties, (int) dataPeriod)
                {
                    DataDir = Data.OfflineDataDir,
                    MaxBars = Configs.MaxBars,
                    StartTime = Configs.DataStartTime,
                    EndTime = Configs.DataEndTime,
                    UseStartTime = Configs.UseStartTime,
                    UseEndTime = Configs.UseEndTime
                };

            // Loads the data
            int loadDataResult = useResource
                                     ? instrument.LoadResourceData(Resources.EURUSD1440, DataPeriod.D1)
                                     : instrument.LoadData();

            if (instrument.Bars > 0 && loadDataResult == 0)
            {
                Data.InstrProperties = instrProperties.Clone();

                Data.Bars = instrument.Bars;
                Data.Period = dataPeriod;
                Data.Update = instrument.Update;

                Data.Time = new DateTime[Data.Bars];
                Data.Open = new double[Data.Bars];
                Data.High = new double[Data.Bars];
                Data.Low = new double[Data.Bars];
                Data.Close = new double[Data.Bars];
                Data.Volume = new int[Data.Bars];

                for (int bar = 0; bar < Data.Bars; bar++)
                {
                    Data.Open[bar] = instrument.Open(bar);
                    Data.High[bar] = instrument.High(bar);
                    Data.Low[bar] = instrument.Low(bar);
                    Data.Close[bar] = instrument.Close(bar);
                    Data.Time[bar] = instrument.Time(bar);
                    Data.Volume[bar] = instrument.Volume(bar);
                }

                Data.MinPrice = instrument.MinPrice;
                Data.MaxPrice = instrument.MaxPrice;
                Data.DaysOff = instrument.DaysOff;
                Data.AverageGap = instrument.AverageGap;
                Data.MaxGap = instrument.MaxGap;
                Data.AverageHighLow = instrument.AverageHighLow;
                Data.MaxHighLow = instrument.MaxHighLow;
                Data.AverageCloseOpen = instrument.AverageCloseOpen;
                Data.MaxCloseOpen = instrument.MaxCloseOpen;
                Data.DataCut = instrument.Cut;
                Data.IsIntrabarData = false;
                Data.IsTickData = false;
                Data.IsData = true;
                Data.IsResult = false;

                // Configs.SetAccountExchangeRate();

                CheckLoadedData();
                Data.GenerateMarketStats();
                InfoPanelMarketStatistics.Update(Data.MarketStatsParam, Data.MarketStatsValue,
                                                 Data.MarketStatsFlag, Language.T("Market Statistics"));
                InfoPanelAccountStatistics.Update(Backtester.AccountStatsParam, Backtester.AccountStatsValue,
                                                  Backtester.AccountStatsFlags, Language.T("Account Statistics"));
            }
            else if (loadDataResult == -1)
            {
                MessageBox.Show(Language.T("Error in the data file!"), Language.T("Data file loading"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                Cursor = Cursors.Default;
                return 1;
            }
            else
            {
                MessageBox.Show(
                    Language.T("There is no data for") + " " + symbol + " " + Data.DataPeriodToString(dataPeriod) + " " +
                    Language.T("in folder") + " " + Data.OfflineDataDir + Environment.NewLine + Environment.NewLine +
                    Language.T("Check the offline data directory path (Menu Market -> Data Directory)"),
                    Language.T("Data File Loading"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Cursor = Cursors.Default;
                return 1;
            }

            Cursor = Cursors.Default;

            return 0;
        }
Beispiel #3
0
        /// <summary>
        ///     Loads the Intrabar data.
        /// </summary>
        private int LoadIntrabarData(DataPeriod period)
        {
            var instrument = new Instrument(Data.InstrProperties.Clone(), (int)period)
            {
                DataDir = Data.OfflineDataDir,
                MaxBars = Configs.MaxIntraBars
            };


            // Loads the data
            int loadingResult   = instrument.LoadData();
            int loadedIntrabars = instrument.Bars;

            if (loadingResult == 0 && loadedIntrabars > 0)
            {
                if (Data.Period != DataPeriod.W1)
                {
                    if (instrument.DaysOff > 5)
                    {
                        warningMessage += Environment.NewLine + Language.T("Data for:") + " " + Data.Symbol + " " +
                                          Data.DataPeriodToString(period) + " - " + Language.T("Maximum days off:") +
                                          " " + instrument.DaysOff;
                    }
                    if (Data.Update - instrument.Update > new TimeSpan(24, 0, 0))
                    {
                        warningMessage += Environment.NewLine + Language.T("Data for:") + " " + Data.Symbol + " " +
                                          Data.DataPeriodToString(period) + " - " + Language.T("Updated on:") + " " +
                                          instrument.Update.ToString(CultureInfo.InvariantCulture);
                    }
                }

                int startBigBar;
                for (startBigBar = 0; startBigBar < Data.Bars; startBigBar++)
                {
                    if (Data.Time[startBigBar] >= instrument.Time(0))
                    {
                        break;
                    }
                }

                int stopBigBar;
                for (stopBigBar = startBigBar; stopBigBar < Data.Bars; stopBigBar++)
                {
                    if (Data.IntraBarsPeriods[stopBigBar] != Data.Period)
                    {
                        break;
                    }
                }

                // Seek for a place to put the intrabars.
                int lastIntraBar = 0;
                for (int bar = startBigBar; bar < stopBigBar; bar++)
                {
                    Data.IntraBarData[bar] = new Bar[(int)Data.Period / (int)period];
                    DateTime endTime  = Data.Time[bar] + new TimeSpan(0, (int)Data.Period, 0);
                    int      indexBar = 0;
                    for (int intrabar = lastIntraBar;
                         intrabar < loadedIntrabars && instrument.Time(intrabar) < endTime;
                         intrabar++)
                    {
                        if (instrument.Time(intrabar) >= Data.Time[bar])
                        {
                            Data.IntraBarData[bar][indexBar].Time  = instrument.Time(intrabar);
                            Data.IntraBarData[bar][indexBar].Open  = instrument.Open(intrabar);
                            Data.IntraBarData[bar][indexBar].High  = instrument.High(intrabar);
                            Data.IntraBarData[bar][indexBar].Low   = instrument.Low(intrabar);
                            Data.IntraBarData[bar][indexBar].Close = instrument.Close(intrabar);
                            Data.IntraBarsPeriods[bar]             = period;
                            Data.IntraBarBars[bar]++;
                            indexBar++;
                            lastIntraBar = intrabar;
                        }
                    }
                }
            }

            return(loadedIntrabars);
        }
        /// <summary>
        ///     Loads the Intrabar data.
        /// </summary>
        private int LoadIntrabarData(DataPeriod period)
        {
            var instrument = new Instrument(Data.InstrProperties.Clone(), (int) period)
                {
                    DataDir = Data.OfflineDataDir,
                    MaxBars = Configs.MaxIntraBars
                };

            // Loads the data
            int loadingResult = instrument.LoadData();
            int loadedIntrabars = instrument.Bars;

            if (loadingResult == 0 && loadedIntrabars > 0)
            {
                if (Data.Period != DataPeriod.W1)
                {
                    if (instrument.DaysOff > 5)
                        warningMessage += Environment.NewLine + Language.T("Data for:") + " " + Data.Symbol + " " +
                                          Data.DataPeriodToString(period) + " - " + Language.T("Maximum days off:") +
                                          " " + instrument.DaysOff;
                    if (Data.Update - instrument.Update > new TimeSpan(24, 0, 0))
                        warningMessage += Environment.NewLine + Language.T("Data for:") + " " + Data.Symbol + " " +
                                          Data.DataPeriodToString(period) + " - " + Language.T("Updated on:") + " " +
                                          instrument.Update.ToString(CultureInfo.InvariantCulture);
                }

                int startBigBar;
                for (startBigBar = 0; startBigBar < Data.Bars; startBigBar++)
                    if (Data.Time[startBigBar] >= instrument.Time(0))
                        break;

                int stopBigBar;
                for (stopBigBar = startBigBar; stopBigBar < Data.Bars; stopBigBar++)
                    if (Data.IntraBarsPeriods[stopBigBar] != Data.Period)
                        break;

                // Seek for a place to put the intrabars.
                int lastIntraBar = 0;
                for (int bar = startBigBar; bar < stopBigBar; bar++)
                {
                    Data.IntraBarData[bar] = new Bar[(int) Data.Period/(int) period];
                    DateTime endTime = Data.Time[bar] + new TimeSpan(0, (int) Data.Period, 0);
                    int indexBar = 0;
                    for (int intrabar = lastIntraBar;
                         intrabar < loadedIntrabars && instrument.Time(intrabar) < endTime;
                         intrabar++)
                    {
                        if (instrument.Time(intrabar) >= Data.Time[bar])
                        {
                            Data.IntraBarData[bar][indexBar].Time = instrument.Time(intrabar);
                            Data.IntraBarData[bar][indexBar].Open = instrument.Open(intrabar);
                            Data.IntraBarData[bar][indexBar].High = instrument.High(intrabar);
                            Data.IntraBarData[bar][indexBar].Low = instrument.Low(intrabar);
                            Data.IntraBarData[bar][indexBar].Close = instrument.Close(intrabar);
                            Data.IntraBarsPeriods[bar] = period;
                            Data.IntraBarBars[bar]++;
                            indexBar++;
                            lastIntraBar = intrabar;
                        }
                    }
                }
            }

            return loadedIntrabars;
        }
        /// <summary>
        ///     Loads, filters and saves all data files.
        /// </summary>
        private void LoadSaveData()
        {
            string[] files = Directory.GetFiles(Data.OfflineDataDir, "*.csv");
            foreach (string file in files)
            {
                string symbol = GetSymbolFromFileName(file);
                int period = GetPeriodFromFileName(file);
                if (string.IsNullOrEmpty(symbol) || period == 0)
                    continue;

                InstrumentProperties instrProperties = Instruments.InstrumentList[symbol].Clone();
                var instrument = new Instrument(instrProperties, period)
                    {
                        DataDir = Data.OfflineDataDir,
                        MaxBars = Configs.MaxBars,
                        StartTime = Configs.DataStartTime,
                        EndTime = Configs.DataEndTime,
                        UseStartTime = Configs.UseStartTime,
                        UseEndTime = Configs.UseEndTime
                    };

                int loadDataResult = instrument.LoadData();

                if (instrument.Bars > 0 && loadDataResult == 0)
                {
                    var stringBuilder = new StringBuilder(instrument.Bars);
                    for (int bar = 0; bar < instrument.Bars; bar++)
                    {
                        stringBuilder.AppendLine(
                            instrument.Time(bar).ToString("yyyy-MM-dd") + "\t" +
                            instrument.Time(bar).ToString("HH:mm") + "\t" +
                            instrument.Open(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.High(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Low(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Close(bar).ToString(CultureInfo.InvariantCulture) + "\t" +
                            instrument.Volume(bar).ToString(CultureInfo.InvariantCulture)
                            );
                    }
                    try
                    {
                        var sw = new StreamWriter(file);
                        sw.Write(stringBuilder.ToString());
                        sw.Close();

                        TbxOutput.Text += symbol + period + " bars: " + instrument.Bars + Environment.NewLine;
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
                }
            }
        }