程序員的量化交易之路(33)--QuantConnect之案例1

轉載需註明:http://blog.csdn.net/minimicall ,http://cloudtrade.top/

分析Cointrader有一定層度了,發現它畢竟不是一個產品,沒有得到驗證。在架構、編碼等方面都非常的不規範。

想編寫一個雲交易平臺,任道而重遠。我們需要參照一些成熟的架構。

Quantopian的zipline不行,因爲我就是看到它不行,所以纔去分析Cointrader的。

現在這兩個,一個壓根就不是雲平臺,一個是不成熟,所以我只能去分析剩下的一個QuantConnect了。它的引擎是lean。

學習lean從學習使用開始,然後學習其架構、源碼。然後設計我們的架構、平臺。

我們首先來看一個例子。官方的最基本的案例。

代碼如下:

namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Full Basic Template:
    *
    *   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full algorithm can be found at:
    *   https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
    */
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        //Initialize the data and resolution you require for your strategy
        public override void Initialize()
        {
			
            //Start and End Date range for the backtest:
            SetStartDate(2013, 1, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1)); 
            
            //Cash allocation
            SetCash(25000);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
        }

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data) 
        {   
            // "TradeBars" object holds many "TradeBar" objects: it is a dictionary indexed by the symbol:
            // 
            //  e.g.  data["MSFT"] data["GOOG"]
            
            if (!Portfolio.HoldStock) 
            {
                int quantity = (int)Math.Floor(Portfolio.Cash / data["SPY"].Close);
                
                //Order function places trades: enter the string symbol and the quantity you want:
                Order("SPY",  quantity);
                
                //Debug sends messages to the user console: "Time" is the algorithm time keeper object 
                Debug("Purchased SPY on " + Time.ToShortDateString());
                
                //You can also use log to send longer messages to a file. You are capped to 10kb
                //Log("This is a longer message send to log.");
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章