public void ReadData(string url) { if (string.IsNullOrEmpty(url)) { return; } var fileManager = new DataFile(); var fileName = fileManager.GetFileName(url); fileName = fileName.Substring(0, fileName.Length - 4); using (var fs = new FileStream(url, FileMode.Open, FileAccess.Read)) { BinaryReader r = new BinaryReader(fs); var xd = new byte[32]; for (long i = 0; i < fs.Length; i += 32) { for (int j = 0; j < 32; j++) { var x = r.ReadByte(); xd[j] = Convert.ToByte(x); } var rec = new MarketData(xd); rec.Ticker = fileName; var l = rec.Ticker.Length; if (l > 6) { rec.Ticker = rec.Ticker.Substring(l - 6); } MarketDataList.Add(rec); } } }
void Delete() { if (CurrentMarketData != null && MarketDataList.Contains(CurrentMarketData)) { MarketDataList.Remove(CurrentMarketData); } }
void GenerateDistribute() { if (MarketDataList.Count == 0) { MessageBox.Show("Please load market data first."); return; } MarketDistributeList.Clear(); NormalDistributeList.Clear(); var nl = CommonLib.Distribute.GetNormalDistribute(0, 1, 3.5, 1d / CountStep); nl.ForEach(v => NormalDistributeList.Add(v)); da.StatisticStep = CountStep; da.ProcessMode = CurrentProcessType; var l = da.GetDistributeByTime(MarketDataList.ToList()); if (l.Count == 0) { return; } l.FirstOrDefault().Value.ForEach(v => MarketDistributeList.Add(v)); if (CalculateDataList == null) { CalculateDataList = new List <TimeValueObject>(); } CalculateDataList.Clear(); foreach (var v in da.GetDistributeTrendByTime(MarketDataList.ToList())) { if (v.Name == TargetObject.Ticker) { CalculateDataList.Add(new TimeValueObject() { Time = v.Time, Value = v.Value, Name = v.Name }); } } CurrentTime = EndTime; CurrentValue = da.GetDistanceByTime(MarketDataList.ToList(), Ticker); OnPropertyChanged("CurrentValue"); DistributeTrendList.Clear(); var trendList = da.GetDistributeTrendByTime(MarketDataList.ToList()); trendList.ForEach(v => { if (v.Name == TargetObject.Ticker) { DistributeTrendList.Add(new TimeValueObject() { Time = v.Time, Value = v.Value, Name = v.Name }); } }); }
void SaveToFile() { var dlg = new SaveFileDialog() { Filter = "Text File|*.txt |Csv File| *.csv | (*.*)|*.*" }; if (dlg.ShowDialog() == true) { var ep = new ReportCommonLib.ReportExportHelper(); ep.CreateCsvReport <IMarketData>(dlg.FileName, MarketDataList.ToList()); } }
void SaveToDB() { var dh = new DatabaseDataProvider(); var connectionStr = ConfigurationManager.ConnectionStrings["MarketDataConnectionString"].ConnectionString; dh.GetDatabaseConnection = () => { return(new SqlConnection(connectionStr)); }; dh.DeleteBatch(null, "Table_TradePrice", new List <string>() { "Ticker,Time" }, MarketDataList.Cast <object>().ToList()); dh.InsertBatch(null, "Table_TradePrice", MarketDataList.Cast <object>().ToList()); }
void LoadFromFile() { var dlg = new OpenFileDialog() { Filter = "Text File|*.txt |Csv File| *.csv | (*.*)|*.*" }; if (dlg.ShowDialog() == true) { var ep = new ReportCommonLib.ReportExportHelper(); var l = ep.LoadFromCsvFile <BackTestingCommonLib.MarketData>(dlg.FileName, Encoding.GetEncoding("GB18030"), GetSeperateChar(), null, preProcess); l.ForEach(v => MarketDataList.Add(v)); } }
public void LoadCurrentInstrumentMarketData(DateTime start, DateTime end) { if (CurrentInstrument == null) { return; } else { MarketDataList.Clear(); var ml = CurrentDataSource.GetDataList(new List <IInstrument>() { CurrentInstrument }, start, end, Grade); ml.ForEach(v => MarketDataList.Add(v)); } }
public void TransToCSV(string dirPath) { if (!string.IsNullOrEmpty(dirPath) && Directory.Exists(dirPath)) { var fileList = Directory.EnumerateFiles(dirPath); foreach (var f in fileList) { if (f.EndsWith("lc5")) { MarketDataList.Clear(); ReadData(f); WriteData(f + ".csv"); } } } }
public void WriteToDB(string connectionStr) { var dh = new DatabaseDataProvider(); dh.GetDatabaseConnection = () => CreateConnection(connectionStr); //dh.DeleteBatch(null, "marketminuteprice", "UID", MarketDataList.Cast<object>().ToList()); //dh.InsertBatch(null, "marketminuteprice", MarketDataList.Cast<object>().ToList()); var l = MarketDataList.Cast <object>().ToList(); if (LimitBeginTime != null) { l = MarketDataList.Where(v => v.Time > LimitBeginTime).Cast <object>().ToList(); } dh.DeleteBatch(null, "Table_TradePrice", new List <string>() { "Time", "Ticker" }, l); dh.InsertBatch(null, "Table_TradePrice", l); }
public void LoadMarketData(DateTime start, DateTime end, Func <IDataSource> ds, IInstrument instrument) { if (ds == null || ds() == null) { MessageBox.Show("please select valid data source"); return; } var dl = ds().GetDataList(new List <IInstrument> { instrument }, start, end, MarketDataGrade.FiveMinutes); SourceList.Clear(); dl.ForEach(v => SourceList.Add(v)); var ml = MarketData.SummaryMarketDataList(dl, CurrentPeriod); MarketDataList.Clear(); ml.ForEach(v => MarketDataList.Add(v)); OnPropertyChanged("IsLoaded"); }
void LoadFromDB() { var sql = "Select * from Table_TradePrice where Time>='" + StartTime.ToString() + "' and Time<='" + EndTime.ToString() + "'"; if (!string.IsNullOrEmpty(InstrumentTicker)) { sql = sql + " and Ticker='" + InstrumentTicker + "'"; } var connectionStr = ConfigurationManager.ConnectionStrings["MarketDataConnectionString"]; using (var conn = new SqlConnection(connectionStr.ConnectionString)) { conn.Open(); var rl = conn.Query <BackTestingCommonLib.MarketData>(sql, new { }, null, true, 60000).ToList(); foreach (var r in rl) { MarketDataList.Add(r); } } }
public override void Refresh() { CheckRunningStatus(); CommonProc.SynchroniseList <IMarketData>(TargetProject.MarketDataList, MarketDataList); //CommonProc.SynchroniseList<IIndicatorValue>(TargetProject.IndicatorDataList, IndicatorDataList); CommonProc.SynchroniseList <IOrder>(TargetProject.OrderList, OrderList); CommonProc.SynchroniseList <TimeValueObject>(TargetProject.CurrentValueList, CurrentValueList); CommonProc.SynchroniseList <TimeValueObject>(TargetProject.StandardValueList, StandardValueList); if (CurrentInstrument == null && InstrumentList.Count > 0) { CurrentInstrument = InstrumentList.FirstOrDefault(); } if (CurrentInstrument != null) { var l = MarketDataList.Where(v => v.InstrumentTicker == CurrentInstrument.Ticker); CommonProc.SynchroniseList <IMarketData>(l, CurrentInstrumentMarketDataList); var ol = OrderList.Where(v => v.Ticker == CurrentInstrument.Ticker); CommonProc.SynchroniseList <IOrder>(ol, CurrentInstrumentOrderList); } TestCurrentTime = TargetProject.TestCurrentTime; OnPropertyChanged("CurrentValue"); OnPropertyChanged("Pnl"); OnPropertyChanged("StatusPnl"); OnPropertyChanged("Efficiency"); OnPropertyChanged("MaxLost"); refreshChartAxis(); OnPropertyChanged("CanStart"); OnPropertyChanged("CanPause"); OnPropertyChanged("CanStop"); OnPropertyChanged("FinishPercent"); if (TargetSummaryVM != null) { TargetSummaryVM.Refresh(); } OnPropertyChanged("CanStart"); OnPropertyChanged("CanPause"); OnPropertyChanged("CanStop"); OnPropertyChanged("TestCurrentTimeInfo"); base.Refresh(); }
public void TransToDB(string dirPath, string connectionStr) { if (!string.IsNullOrEmpty(dirPath) && Directory.Exists(dirPath)) { var fileList = Directory.EnumerateFiles(dirPath); var count = fileList.Count(); var i = 0; foreach (var f in fileList) { var d = DateTime.Now; if (f.EndsWith(".lc5")) { MarketDataList.Clear(); ReadData(f); WriteToDB(connectionStr); } i++; Console.WriteLine("Finish " + (i / Convert.ToDouble(count) * 100d).ToString() + "%," + (DateTime.Now - d).TotalSeconds.ToString() + " s"); } } }
double GetPriceDis() { return(MarketDataList.Max(v => v.High) - MarketDataList.Min(v => v.Low)); }
double GetVolumeDis() { return(MarketDataList.Max(v => v.Volume) - MarketDataList.Min(v => v.Volume)); }
void New() { var obj = new BackTestingCommonLib.MarketData(); MarketDataList.Add(obj); }
void Clear() { MarketDataList.Clear(); }
public override void PrepareWork() { TargetPortfolio.PrepareWork(); MarketDataList.Clear(); CurrentDataSource.DataList.Clear(); CurrentValueList.Clear(); StandardValueList.Clear(); OrderList.Clear(); DateTime st = TestStartTime; if (TestStartTime > AnalyseStartTime) { st = AnalyseStartTime; } CurrentDataSource.CacheStartTime = st; CurrentDataSource.CacheEndTime = TestEndTime; TestCurrentTime = TestStartTime; analyseTime = MarketData.GetNextTime(TestCurrentTime, AnalyseGrade); _MaxLost = new Money() { FxCode = Pnl.FxCode, Number = 0 }; if (UseFirstMarketDataInit) { var fdl = new List <IMarketData>(); InstrumentList.ForEach(v => { var d = CurrentDataSource.GetFirstData(v, TestStartTime, TestEndTime, Grade); if (d != null) { fdl.Add(d); } }); TargetPortfolio.ProcessMarketData(fdl); TargetPortfolio.PositionList.ForEach(v => { var d = fdl.FirstOrDefault(m => m.InstrumentTicker == v.InstrumentTicker); if (d != null) { v.Cost = d.Close; } }); } if (IsUnlimited) { TargetPortfolio.IsUnlimited = true; } standardPortfolio = TargetPortfolio.Clone() as IPortfolio; testStartValue = TargetPortfolio.CurrentValue; CurrentDataSource.PrepareWork(); CurrentTradeGate.PrepareWork(); foreach (var condition in ConditionList) { condition.PrepareWork(); condition.GetInstrumentList = () => { return(InstrumentList); }; condition.AnalyseDataSource = CurrentDataSource; } TestStrategy.CurrentPortfolio = TargetPortfolio; TestStrategy.PrepareWork(); RiskPolicy.PrepareWork(); }
void TestStep(DateTime start, DateTime end) { TestStrategy.CurrentTime = end; AddInfo("test step, start:" + start.ToString() + ",end:" + end.ToString()); var dl = CurrentDataSource.GetDataList(InstrumentList, start, end, Grade); AddInfo("got market data ,count is:" + dl.Count.ToString()); if (dl == null || dl.Count == 0) { return; } TestStrategy.CurrentTime = dl.Max(d => d.Time); dl.ForEach(v => { var inst = InstrumentList.FirstOrDefault(i => i.Ticker == v.InstrumentTicker); if (inst != null) { AddInfo("update " + inst.Name + " price, value is:" + v.Close.ToString() + "(" + v.Time.ToString() + ")"); inst.CurrentPrice = v.Close; } }); MarketDataList.AddRange(dl); AddInfo("portfolio process market data!"); TargetPortfolio.ProcessMarketData(dl); AddInfo("standard portfolio process market data!"); standardPortfolio.ProcessMarketData(dl); AddInfo("strategy process market data!"); TestStrategy.ProcessMarketData(dl); if (analyseTime <= end) { AddInfo("prepare analyse, analyse time is:" + analyseTime.ToString()); AnalyseStep(); analyseTime = MarketData.GetNextTime(end, AnalyseGrade); var cl = new List <ISignal>(); foreach (var condition in ConditionList) { var rl = condition.GetResult(); if (rl != null && rl.Count > 0) { cl.AddRange(rl); } } if (cl.Count > 0) { AddInfo("got signal, count is " + cl.Count.ToString()); TestStrategy.ProcessSignal(cl); } } AddInfo("strategy process portfolio"); TestStrategy.ProcessPortfolio(); var ol = TestStrategy.GetOrderList(); if (ol.Count > 0) { AddInfo("strategy generate order, count is :" + ol.Count.ToString()); List <IOrder> col = new List <IOrder>(); foreach (var o in ol) { if (o != null && RiskPolicy.PredictOrder(o, TargetPortfolio)) { col.Add(o); } } OrderList.AddRange(col); AddInfo("trade gate process order"); CurrentTradeGate.ProcessorOrder(col); AddInfo("portfolio info before process order is" + GetPortfolioMemo(TargetPortfolio)); TargetPortfolio.ProcessOrderList(col); AddInfo("portfolio info after process order is" + GetPortfolioMemo(TargetPortfolio)); } if (!IsUnlimited)//adjust risk { AddInfo("adjust risk"); ol = RiskPolicy.AdjustRisk(TargetPortfolio); if (ol.Count > 0) { AddInfo("risk order generate, count is:" + ol.Count.ToString()); OrderList.AddRange(ol); List <IOrder> col = ol.Where(v => v != null).ToList(); CurrentTradeGate.ProcessorOrder(col); TargetPortfolio.ProcessOrderList(col); } } CurrentValueList.Add(new TimeValueObject() { Time = dl.Max(v => v.Time), Value = CurrentValue, Memo = GetPortfolioMemo(TargetPortfolio) }); StandardValueList.Add(new TimeValueObject() { Time = dl.Max(v => v.Time), Value = StandardValue, Memo = GetPortfolioMemo(standardPortfolio) }); if (_MaxLost.Number > Pnl.Number) { _MaxLost.Number = Pnl.Number; } if (TestStepDelayMS > 0) { Thread.Sleep(TestStepDelayMS); } }