如何预加载历史数据

The code below pre-loads five last days of 1min bars from historical data base to strategy bar series on startup. Then it creates and draws SMA indicator on this series.

以下代码在策略启动时加载了过去5天的1分钟Bar历史数据,然后基于此数据创建并绘出了SMA指标

In this scenario you don’t need to wait (128 minutes in this example) to run a strategy in live or paper trading mode. Indeed you should have historical data in the data base to use this technique. Note tha you can capture real time data into historical data base when you run a strategy live, see http://www.smartquant.com/forums/viewtopic.php?p=17031#17031

在这个场景(scenario)中,实盘(Live)或Paper模式下你不需要等待(这个示例中是128分钟)。在使用此技术时确保你数据库中已经有了历史数据。提示,你可以在实盘模式下接收实时行情并保存到数据库。参考:http://www.smartquant.com/forums/viewtopic.php?p=17031#17031

  1. using System;
  2. using System.Drawing;
  3.  
  4. using OpenQuant.API;
  5. using OpenQuant.API.Indicators;
  6.  
  7. public class MyStrategy : Strategy
  8. {
  9. SMA sma;
  10.  
  11. DateTime datetime1;
  12. DateTime datetime2;
  13.  
  14. public override void OnStrategyStart()
  15. {
  16. datetime2 = DateTime.Now;
  17. datetime1 = datetime2.AddDays(-5);
  18.  
  19. foreach (Bar bar in GetHistoricalBars(datetime1, datetime2, BarType.Time, 60))
  20. Bars.Add(bar);
  21.  
  22. sma = new SMA(Bars, 128);
  23.  
  24. Draw(sma);
  25. }
  26. }
This code gets historical bars from the local OpenQuant data base. Note that you can modify the code so that it will download historical data from a historical data provider instead (for example IB). 以上代码是从本地OpenQuant数据库中加载历史数据。你也可以修改代码通过历史数据提供商那下载历史数据(例如IB)
  1. GetHistoricalBars("IB", datetime1, datetime2, BarType.Time, 60);