用Python編程借助現有量化平臺編寫股票交易策略和回測分析
一、簡介
大家好,我是Snowball。今天給大家分享的內容是基于Python編程,實現股票交易相關功能開發,如果讀者對股票或金融衍生物交易不太了解,又比較感興趣的話可自行查詢相關資料。
接下來筆者會給大家介紹股票交易中的常見幾種交易策略實現思路和源碼編寫過程,如果大家聽說過量化交易這個詞語的話,對其中的交易策略或許了解過,大概意思就是在股票、加密貨幣或者金融衍生物在價格的波動過程中根據其交易策略進行不斷的買入和賣出,不斷的套利,降低持倉陳本,來達到收益最大化。
常見的交易策略有很多種,例如趨勢型,網格型,剝頭皮,概率法則,高頻交易等,今天主要給大家介紹2種低頻的交易策略,高拋低吸網格交易策略、日內做T策略。其他的交易策略較復雜,讀者可自行百度了解,筆者這里推薦一個量化交易網站,僅供參考,米筐量化:
https://www.ricequant.com/doc/quant/
二、需求分析&實現思路
每個交易日的股票都會上漲或者下跌,在這個過程中筆者們偶爾會想針對部分股票進行股價的漲跌幅進行監控,或者自動進行交易,在這個需求前提下,現有券商、股票分析軟件都會帶有機器人自動交易策略功能,大部分都需要收費或者部分策略不能滿足自己的需求,筆者這邊提供2種實現思路:
1、借助現有量化平臺編寫策略和回測分析,然后在券商軟件層面進行策略執行。
2、自己編寫功能代碼來監控估價,對股價波動進行特殊處理滿足特殊需求。
第一種實現成本較低,但功能受限于平臺;第二種實現成本毋庸置疑相對較高,但是邏輯可以自己控制。
三、借助現有量化平臺編寫策略和回測分析
這里利用米筐量化實現和分析自己的交易策略,需要先注冊個賬號,然后進入到平臺-筆者的策略中進行策略編寫,平臺的功能使用可以參考平臺文檔。
筆者這里貼出筆者自己寫的2種策略代碼,這個平臺只支持使用Python腳本編寫。
1)價差交易策略
平臺截圖:
部分代碼如下,詳細代碼可以自己手擼實現,也可以在文末進行獲取:
- # 你選擇的證券的數據更新將會觸發此段邏輯,例如日或分鐘歷史數據切片或者是實時數據切片更新
- def handle_bar(context, bar_dict):
- ...
- if newPrice >= context.nextSellPrice:
- logger.info("執行高拋交易,對應價格:{}".format(newPrice))
- amount = context.portfolio.positions[context.s1].quantity
- if amount >= context.tradeNumber:
- logger.info("執行高拋交易,對應數量:{}".format(context.tradeNumber))
- order_shares(context.s1, -context.tradeNumber)
- plot("S", newPrice)
- elif amount >= 100:
- logger.info("執行高拋交易,對應數量:{}".format(amount))
- order_shares(context.s1, -amount)
- plot("S", newPrice)
- calc_next_trade_price(context,newPrice)
- obj = {
- "nextSellPrice":context.nextSellPrice,
- "nextBuyPrice":context.nextBuyPrice,
- "curTradePrice":context.curTradePrice
- }
- context.buyTradeList.append(obj)
- if newPrice <= context.nextBuyPrice:
- logger.info("執行低吸交易,對應價格:{}".format(newPrice))
- amount = int(context.portfolio.cash / newPrice / 100.0) * 100
- if amount >= context.tradeNumber:
- logger.info("執行低吸交易,對應數量:{}".format(context.tradeNumber))
- order_shares(context.s1, context.tradeNumber)
- plot("B", newPrice)
- calc_next_trade_price(context,newPrice)
- obj = {
- "nextSellPrice":context.nextSellPrice,
- "nextBuyPrice":context.nextBuyPrice,
- "curTradePrice":context.curTradePrice
- }
- context.sellTradeList.append(obj)
選擇回測時間段,點擊右側平臺右側按鈕運行回測,結果頁面如下
從結果中可以看到,對招商銀行[600036]這只股票進行價差網格交易,其參數設置在上漲8%的時候賣出,下跌8%的時候買入,最大連續下跌買入次數為3次。
回測收益:13.628%
回測年化收益:17.096%
比基準年化收益-6%高出非常之大,這是在股價波動的過程中可以進行執行該策略來不斷的降低持倉成本。從交易詳情面板來看,這個策略可以通過參數調節交易頻率,在上漲下跌比率較大的情況下,其交易次數是能控制的相對較少,結果圖如下:
2)日內做T策略
同樣的,只貼部分代碼
- # 你選擇的證券的數據更新將會觸發此段邏輯,例如日或分鐘歷史數據切片或者是實時數據切片更新
- def handle_bar(context, bar_dict):
- ...
- newPrice = bar_dict[context.s1].last
- if newPrice >= context.nextSellPrice:
- context.lastTradeType = 1
- logger.info("執行高拋交易,對應價格:{}".format(newPrice))
- amount = context.portfolio.positions[context.s1].quantity
- #if amount - context.tradeNumber >= context.lockStockNumber:
- if amount - context.tradeNumber >= 0:
- logger.info("執行高拋交易,對應數量:{}".format(context.tradeNumber))
- order_shares(context.s1, -context.tradeNumber)
- plot("S", newPrice)
- else:
- logger.info("股票數量不足,無法執行高拋交易,對應數量:{}".format(amount))
- return
- calc_next_trade_price(context,newPrice)
- obj = {
- "nextSellPrice":context.nextSellPrice,
- "nextBuyPrice":context.nextBuyPrice,
- "curTradePrice":context.curTradePrice
- }
- context.buyTradeList.append(obj)
- if newPrice <= context.nextBuyPrice:
- context.lastTradeType = 0
- logger.info("執行低吸交易,對應價格:{}".format(newPrice))
- amount = int(context.portfolio.cash / newPrice / 100.0) * 100
- if amount >= context.tradeNumber:
- logger.info("執行低吸交易,對應數量:{}".format(context.tradeNumber))
- order_shares(context.s1, context.tradeNumber)
- plot("B", newPrice)
- else:
- logger.info("現金不足,無法執行低吸交易,對應數量:{}".format(amount))
- return
- calc_next_trade_price(context,newPrice)
- obj = {
- "nextSellPrice":context.nextSellPrice,
- "nextBuyPrice":context.nextBuyPrice,
- "curTradePrice":context.curTradePrice
- }
- context.sellTradeList.append(obj)
總體來說,代碼邏輯還是比較簡單,就是對價格的漲跌進行處理,其參數設置在日內上漲2%的時候賣出,下跌2%的時候買入,初始買入資金比例7成,鎖定最低倉位5成。然后運行回測,其結果如下
回測收益:5.501%
回測年化收益:6.839%
基準收益:19.26%
可以看到日內做T這種高頻交易,在長期來看收益可能并不高,適合在短期價格內運行。
四、總結
我是Snowball。這個量化平臺在筆者的熟悉情況下,它可以很方便的回測你的交易策略,但是在股價盯盤上,或者自定義邏輯上支持的不是很完善,很多功能也是需要收費才能使用。本文基于Python,借助現有量化平臺編寫策略和回測分析,希望對大家的學習有所幫助。