/// <summary> /// Loads, filters and saves all data files. /// </summary> private void LoadSaveData() { var files = Directory.GetFiles(Data.OfflineDataDir, "*.csv"); foreach (var file in files) { var symbol = GetSymbolFromFileName(file); var 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> int LoadInstrument(bool useResource) { string symbol; DataPeriods dataPeriod; Cursor = Cursors.WaitCursor; // Takes the instrument symbol and period symbol = tscbSymbol.Text; dataPeriod = (DataPeriods)Enum.GetValues(typeof(DataPeriods)).GetValue(tscbPeriod.SelectedIndex); Instrument_Properties instrProperties = Instruments.InstrumentList[symbol].Clone(); // Makes an instance of class Instrument Instrument instrument = new Instrument(instrProperties, (int)dataPeriod); instrument.DataDir = Data.OfflineDataDir; instrument.FormatDate = DateFormat.Unknown; instrument.MaxBars = Configs.MaxBars; instrument.StartYear = Configs.StartYear; instrument.StartMonth = Configs.StartMonth; instrument.StartDay = Configs.StartDay; instrument.EndYear = Configs.EndYear; instrument.EndMonth = Configs.EndMonth; instrument.EndDay = Configs.EndDay; instrument.UseStartDate = Configs.UseStartDate; instrument.UseEndDate = Configs.UseEndDate; // Loads the data int iLoadDataResult = 0; if (useResource) { iLoadDataResult = instrument.LoadResourceData(); } else { iLoadDataResult = instrument.LoadData(); } if (instrument.Bars > 0 && iLoadDataResult == 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(); infpnlMarketStatistics.Update(Data.MarketStatsParam, Data.MarketStatsValue, Data.MarketStatsFlag, Language.T("Market Statistics")); infpnlAccountStatistics.Update(Backtester.AccountStatsParam, Backtester.AccountStatsValue, Backtester.AccountStatsFlags, Language.T("Account Statistics")); } else if (iLoadDataResult == -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); }
/// <summary> /// LoadInstrument /// </summary> int LoadInstrument(bool useResource) { string symbol; DataPeriods dataPeriod; Cursor = Cursors.WaitCursor; // Takes the instrument symbol and period symbol = tscbSymbol.Text; dataPeriod = (DataPeriods)Enum.GetValues(typeof(DataPeriods)).GetValue(tscbPeriod.SelectedIndex); Instrument_Properties instrProperties = Instruments.InstrumentList[symbol].Clone(); // Makes an instance of class Instrument Instrument instrument = new Instrument(instrProperties, (int)dataPeriod); instrument.DataDir = Data.OfflineDataDir; instrument.FormatDate = DateFormat.Unknown; instrument.MaxBars = Configs.MaxBars; instrument.StartYear = Configs.StartYear; instrument.StartMonth = Configs.StartMonth; instrument.StartDay = Configs.StartDay; instrument.EndYear = Configs.EndYear; instrument.EndMonth = Configs.EndMonth; instrument.EndDay = Configs.EndDay; instrument.UseStartDate = Configs.UseStartDate; instrument.UseEndDate = Configs.UseEndDate; // Loads the data int iLoadDataResult = 0; if (useResource) iLoadDataResult = instrument.LoadResourceData(); else iLoadDataResult = instrument.LoadData(); if (instrument.Bars > 0 && iLoadDataResult == 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(); infpnlMarketStatistics.Update(Data.MarketStatsParam, Data.MarketStatsValue, Data.MarketStatsFlag, Language.T("Market Statistics")); infpnlAccountStatistics.Update(Backtester.AccountStatsParam, Backtester.AccountStatsValue, Backtester.AccountStatsFlags, Language.T("Account Statistics")); } else if (iLoadDataResult == -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; }
/// <summary> /// Loads the Intrabar data. /// </summary> private int LoadIntrabarData(DataPeriods 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 != DataPeriods.week) { 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() { var files = Directory.GetFiles(Data.OfflineDataDir, "*.csv"); foreach (var file in files) { var symbol = GetSymbolFromFileName(file); var 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> /// Loads the Intrabar data. /// </summary> int LoadIntrabarData(DataPeriods period) { Instrument instrument = new Instrument(Data.InstrProperties.Clone(), (int)period); instrument.DataDir = Data.OfflineDataDir; instrument.FormatDate = DateFormat.Unknown; instrument.MaxBars = Configs.MAX_INTRA_BARS; // Loads the data int loadingResult = instrument.LoadData(); int loadedIntrabars = instrument.Bars; if (loadingResult == 0 && loadedIntrabars > 0) { if (Data.Period != DataPeriods.week) { 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(); } } int iStartBigBar = 0; for (iStartBigBar = 0; iStartBigBar < Data.Bars; iStartBigBar++) { if (Data.Time[iStartBigBar] >= instrument.Time(0)) { break; } } int iStopBigBar = 0; for (iStopBigBar = iStartBigBar; iStopBigBar < Data.Bars; iStopBigBar++) { if (Data.IntraBarsPeriods[iStopBigBar] != Data.Period) { break; } } // Seek for the place int iReachedBar = 0; for (int bar = iStartBigBar; bar < iStopBigBar; bar++) { Data.IntraBarData[bar] = new Bar[(int)Data.Period / (int)period]; DateTime endTime = Data.Time[bar] + new TimeSpan(0, (int)Data.Period, 0); int iCurrentBar = 0; for (int intrabar = iReachedBar; intrabar < loadedIntrabars && instrument.Time(intrabar) < endTime; intrabar++) { if (instrument.Time(intrabar) >= Data.Time[bar]) { Data.IntraBarData[bar][iCurrentBar].Time = instrument.Time(intrabar); Data.IntraBarData[bar][iCurrentBar].Open = instrument.Open(intrabar); Data.IntraBarData[bar][iCurrentBar].High = instrument.High(intrabar); Data.IntraBarData[bar][iCurrentBar].Low = instrument.Low(intrabar); Data.IntraBarData[bar][iCurrentBar].Close = instrument.Close(intrabar); Data.IntraBarsPeriods[bar] = period; Data.IntraBarBars[bar]++; iCurrentBar++; iReachedBar = intrabar; } } } } return(loadedIntrabars); }
/// <summary> /// Loads the Intrabar data. /// </summary> int LoadIntrabarData(DataPeriods period) { Instrument instrument = new Instrument(Data.InstrProperties.Clone(), (int)period); instrument.DataDir = Data.OfflineDataDir; instrument.FormatDate = DateFormat.Unknown; instrument.MaxBars = Configs.MAX_INTRA_BARS; // Loads the data int loadingResult = instrument.LoadData(); int loadedIntrabars = instrument.Bars; if (loadingResult == 0 && loadedIntrabars > 0) { if (Data.Period != DataPeriods.week) { 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(); } int iStartBigBar = 0; for (iStartBigBar = 0; iStartBigBar < Data.Bars; iStartBigBar++) if (Data.Time[iStartBigBar] >= instrument.Time(0)) break; int iStopBigBar = 0; for (iStopBigBar = iStartBigBar; iStopBigBar < Data.Bars; iStopBigBar++) if (Data.IntraBarsPeriods[iStopBigBar] != Data.Period) break; // Seek for the place int iReachedBar = 0; for (int bar = iStartBigBar; bar < iStopBigBar; bar++) { Data.IntraBarData[bar] = new Bar[(int)Data.Period/(int)period]; DateTime endTime = Data.Time[bar] + new TimeSpan(0, (int)Data.Period, 0); int iCurrentBar = 0; for (int intrabar = iReachedBar; intrabar < loadedIntrabars && instrument.Time(intrabar) < endTime; intrabar++) { if (instrument.Time(intrabar) >= Data.Time[bar]) { Data.IntraBarData[bar][iCurrentBar].Time = instrument.Time(intrabar); Data.IntraBarData[bar][iCurrentBar].Open = instrument.Open(intrabar); Data.IntraBarData[bar][iCurrentBar].High = instrument.High(intrabar); Data.IntraBarData[bar][iCurrentBar].Low = instrument.Low(intrabar); Data.IntraBarData[bar][iCurrentBar].Close = instrument.Close(intrabar); Data.IntraBarsPeriods[bar] = period; Data.IntraBarBars[bar]++; iCurrentBar++; iReachedBar = intrabar; } } } } return loadedIntrabars; }
/// <summary> /// Loads the Intrabar data. /// </summary> private int LoadIntrabarData(DataPeriods 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 != DataPeriods.week) { 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; }