且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-05-29 21:28:39

转载需注明: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.");
            }
        }
    }
}