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