金字塔Python網(wǎng)格交易策略源碼[金字塔模型]
相關(guān)標(biāo)簽:
?
? 發(fā)布一個(gè)Python網(wǎng)格交易策略
?
?
?
#老聽說(shuō)金字塔支持Python了,而且很牛X,一直想了解一下,就邊學(xué)邊玩,寫了一個(gè)網(wǎng)格交易的策略,經(jīng)過(guò)驗(yàn)證,還不錯(cuò),
#金字塔的Python版用于算法交易還是可以,從交易下單,到成交過(guò)程訂單狀態(tài)的回報(bào),都很準(zhǔn)確。
#相對(duì)于VBA使用對(duì)象的方式來(lái)調(diào)用,Python更直觀,簡(jiǎn)潔。
#
#需要增強(qiáng)的地方(可能我還沒(méi)學(xué)會(huì)):
#1、希望增加對(duì)INI文件讀寫的功能,我使用import ConfigParser不支持
#2、希望增加對(duì)界面方面的支持,Python有支持圖形的庫(kù)
#--------------------------------------------------------------
?
# 本Python代碼主要用于策略交易
# 可以自己import我們平臺(tái)支持的第三方python模塊,比如pandas、numpy等。
from PythonApi import *
import time
from decimal import Decimal
?
#? 參數(shù)定義區(qū),這里定義的參數(shù)可以直接在context對(duì)象中獲取。--(選擇實(shí)現(xiàn))
def parameter():
? ? settimer(GridTrade,200000)? ? ? ? #200秒執(zhí)行一次? ? ? ??
? ? input_par("lastbuy",0,0,9,1)? ? #末次買入開倉(cāng)單號(hào)
? ? input_par("lastsell",0,0,9,1)? ? #末次賣出開倉(cāng)單號(hào)
? ? input_par("lastbuyping",0,0,9,1)? ? #末次平多單單號(hào)
? ? input_par("lastsellping",0,0,9,1)? ? #末次平空單單號(hào)
? ? input_par("jiange",2,0,5,1)? ? #間隔
? ? input_par("vol",1,0,9,1)? ? #每檔手?jǐn)?shù)
? ? input_par("firstvol",1,0,9,1)? ? #初始手?jǐn)?shù)
? ? input_par("fx",1,1,2,1)? ? #交易方向 1-多 2-空
? ? input_par("maxvol",1,0,9,1)? ? #最大手?jǐn)?shù)
?
#? 在這個(gè)方法中編寫任何的初始化邏輯。context對(duì)象將會(huì)在你的算法策略的任何方法之間做傳遞。--(必須實(shí)現(xiàn))
?
def init(context):
? ? settimer(GridTrade,10000)? ? ? ? #10秒執(zhí)行一次
? ? # 在context中保存全局變量
? ? context.s1 = "SQFU00"? ?#交易代碼
? ? #settimer(GridTrade,20000)? ? ? ? #2秒執(zhí)行一次
? ? #引用PEL指標(biāo)公式"my_test"的ma5日均線指標(biāo)值。PEL指標(biāo)必須提前存在或者構(gòu)建。
? ? context.price = int( history_bars(context.s1,1,\'1m\',\'open\') )? ? ? ? ? ?
? ? context.priceO = int( history_bars(context.s1,1,\'1m\',\'open\') )? ? ? ? ? ?
? ? context.priceH = int( history_bars(context.s1,1,\'1m\',\'high\') )
? ? context.priceL = int( history_bars(context.s1,1,\'1m\',\'low\') )
? ? context.priceC = int( history_bars(context.s1,1,\'1m\',\'close\') )? ??
?
? ? print("T0 D+K/策略啟動(dòng)/"+context.s1+\'/參考價(jià)=\'+str(context.price)+\'/價(jià)O=\'+str(context.priceO)+\'/價(jià)H=\'+str(context.priceH)+\'/價(jià)L=\'+str(context.priceL)+\'/價(jià)C=\'+str(context.priceC))
? ? log_debug_info(\'C:\\T0DK.txt\',"T0 D+K/策略啟動(dòng)/"+context.s1+\'/參考價(jià)=\'+str(context.price)+\'/價(jià)O=\'+str(context.priceO)+\'/價(jià)H=\'+str(context.priceH)+\'/價(jià)L=\'+str(context.priceL)+\'/價(jià)C=\'+str(context.priceC))
? ? ? ??
def setorderid(context):
? ? settimer(GridTrade,20000)? ? ? ? #20秒執(zhí)行一次? ??
? ? #檢查未成交訂單,將單號(hào)賦值給全局變量,避免啟動(dòng)策略時(shí)變量的值為0
? ? #print("獲取未成交訂單編號(hào)")
? ? log_debug_info(\'C:\\T0DK.txt\',"獲取未成交訂單編號(hào)")
? ??
? ? Orders=get_orders(context.s1,0)? ? #取未成交單
? ? context.lastbuy=0
? ? context.lastbuyping=0
? ? context.lastsell=0
? ? context.lastsellping=0
? ? if not(Orders == None):
? ? ? ? for order in Orders:
? ? ? ? ? ? orderID=order.order_id? ? #委托單號(hào)
? ? ? ? ? ? sBuySell=order.side? ? ? ? ? ? ? ? #買賣
? ? ? ? ? ? sKp=order.position_effect? ? ? ? #開平
? ? ? ? ? ? sStatus=order.status? ? ? ? ? ? ?#狀態(tài)
? ? ? ? ? ??
? ? ? ? ? ? #print(str(orderID)+\',\'+sBuySell+\',\'+sKp+\',\'+sStatus)
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',str(orderID)+\',\'+sBuySell+\',\'+sKp+\',\'+sStatus)? ?
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? if context.lastbuy==0 and sBuySell=="buy" and sKp=="open" and sStatus=="submitted":
? ? ? ? ? ? ? ? context.lastbuy=order.order_id? ? #單號(hào)
? ? ? ? ? ? if context.lastsell==0 and sBuySell=="sell" and sKp=="open" and sStatus=="submitted":
? ? ? ? ? ? ? ? context.lastsell=order.order_id? ? #單號(hào)
? ? ? ? ? ? if context.lastbuyping==0 and sBuySell=="sell" and sKp=="close" and sStatus=="submitted":
? ? ? ? ? ? ? ? context.lastbuyping=order.order_id? ? #單號(hào)
? ? ? ? ? ? if context.lastsellping==0 and sBuySell=="buy" and sKp=="close" and sStatus=="submitted":
? ? ? ? ? ? ? ? context.lastsellping=order.order_id? ? #單號(hào)
? ? ? ? ? ??
? ? ? ? #print(\'開多:\'+str(context.lastbuy)+\',平多:\'+str(context.lastbuyping)+\',開空:\'+str(context.lastsell)+\',平空:\'+str(context.lastsellping))
? ? ? ? log_debug_info(\'C:\\T0DK.txt\',\'開多:\'+str(context.lastbuy)+\',平多:\'+str(context.lastbuyping)+\',開空:\'+str(context.lastsell)+\',平空:\'+str(context.lastsellping))
?
#網(wǎng)格交易主程序? ? ?
def GridTrade(context):
? ? settimer(GridTrade,60000)? ? ? ? #60秒執(zhí)行一次? ? ? ??
? ? nAmount0=get_account(53)? ? #帳戶有效檢查
? ? if nAmount0==False:
? ? ? ??
? ? ? ? #print(\'賬戶=\'+str(nAmount0)+\'登陸推出了,退出交易。\')
? ? ? ? log_debug_info(\'C:\\T0DK.txt\',\'賬戶\'+str(nAmount0)+\'登陸推出了,退出交易。\')
? ? ? ? return
? ? ? ??
? ? if istradertime(context.s1)==False:? ? #不在交易時(shí)間,不進(jìn)入交易段
? ? ? ??
? ? ? ? #print("不在交易時(shí)間,不進(jìn)入交易段")
? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"不在交易時(shí)間,不進(jìn)入交易段")
? ? ? ? return
? ??
? ? setorderid(context)? ? ? ? ? ?#獲取未成交訂單號(hào),保存到全局變量
? ??
? ? nAmount=get_account(19)? ? #可用資金
? ? if nAmount<=0:
? ? ? ? print(\'賬戶可用資金\'+str(nAmount)+\'低于0了/登陸推出了,退出交易。\')
? ? ? ? log_debug_info(\'C:\\T0DK.txt\',\'賬戶可用資金低于0了,退出交易。\')
? ? ? ? return
? ? ? ? ? ??
? ? portfolio=get_portfolio(context.s1,0)? ? #獲取持倉(cāng)量
? ? if context.fx==1:
? ? ? ? iDuoTotal=portfolio.buy_quantity
? ? ? ? DTnCurOrdPrice = round(context.priceH - ((iDuoTotal -context.firstvol) * context.jiange) / context.vol, 1)
?
? ? if context.fx==1:
? ? ? ? iKongTotal=portfolio.sell_quantity
? ? ? ? KTnCurOrdPrice = round(context.priceL - ((context.firstvol - iKongTotal) * context.jiange) / context.vol, 1)
? ? ? ??
? ? #print(\'檔位價(jià):\'+str(nCurOrdPrice)+\',開多:\'+str(context.lastbuy)+\',平多:\'+str(context.lastbuyping)+\',開空:\'+str(context.lastsell)+\',平空:\'+str(context.lastsellping))
? ? log_debug_info(\'C:\\T0DK.txt\',\'空檔位價(jià):\'+str(KTnCurOrdPrice)+\'多檔位價(jià):\'+str(DTnCurOrdPrice)+\',開多:\'+str(context.lastbuy)+\',平多:\'+str(context.lastbuyping)+\',開空:\'+str(context.lastsell)+\',平空:\'+str(context.lastsellping))? ? ? ? ? ??
?
? ? nPrice = get_dynainf (context.s1,7)? ? #獲取最新價(jià)
? ? if context.fx==1:? ? #做多
? ? ? ? if (context.lastbuy==0 and iDuoTotal<context.maxvol):
? ? ? ? ? ? DTnOrdPrice=DTnCurOrdPrice-context.jiange
? ? ? ? ? ? context.lastbuy=buy_open(context.s1,"Limit",DTnOrdPrice,context.vol)
? ? ? ? ? ? #print(\'檔位價(jià):\'+str(nCurOrdPrice)+\',委托價(jià):\'+str(nOrdPrice)+\',開多\')
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',\'檔位價(jià):\'+str(DTnCurOrdPrice)+\',委托價(jià):\'+str(DTnOrdPrice)+\',開多\')
? ? ? ? ? ??
? ? ? ? if (context.lastbuyping==0 and iDuoTotal>0):
? ? ? ? ? ? DTnOrdPrice=DTnCurOrdPrice+context.jiange
? ? ? ? ? ? context.lastbuyping=sell_close(context.s1,"Limit",DTnOrdPrice,context.vol)
? ? ? ? ? ? #print(\'檔位價(jià):\'+str(nCurOrdPrice)+\',委托價(jià):\'+str(nOrdPrice)+\',平多\')
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',\'檔位價(jià):\'+str(DTnCurOrdPrice)+\',委托價(jià):\'+str(DTnOrdPrice)+\',平多\')
? ? ? ? ? ??
? ? if context.fx==1: #and iKongTotal<context.maxvol:? ? #做空
? ? ? ? if (context.lastsell==0 and iKongTotal<context.maxvol):
? ? ? ? ? ? KTnOrdPrice=KTnCurOrdPrice+context.jiange
? ? ? ? ? ? context.lastsell=sell_open(context.s1,"Limit",KTnOrdPrice,context.vol)
? ? ? ? ? ? #print(\'檔位價(jià):\'+str(nCurOrdPrice)+\',委托價(jià):\'+str(nOrdPrice)+\',開空\(chéng)')
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',\'檔位價(jià):\'+str(KTnCurOrdPrice)+\',委托價(jià):\'+str(KTnOrdPrice)+\',開空\(chéng)')
? ? ? ? ? ??
? ? ? ? if (context.lastsellping==0 and iKongTotal>0):
? ? ? ? ? ? KTnOrdPrice=KTnCurOrdPrice-context.jiange
? ? ? ? ? ? context.lastsellping=buy_close(context.s1,"Limit",KTnOrdPrice,context.vol)
? ? ? ? ? ? #print(\'檔位價(jià):\'+str(nCurOrdPrice)+\',委托價(jià):\'+str(nOrdPrice)+\',平空\(chéng)')
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',\'檔位價(jià):\'+str(KTnCurOrdPrice)+\',委托價(jià):\'+str(KTnOrdPrice)+\',平空\(chéng)')
? ? ? ? ? ??
# before_trading此函數(shù)會(huì)在每天基準(zhǔn)合約的策略交易開始前被調(diào)用,當(dāng)天只會(huì)被調(diào)用一次。--(選擇實(shí)現(xiàn))
def before_trading(context):
? ? pass
?
?
# 你選擇的品種的數(shù)據(jù)更新將會(huì)觸發(fā)此段邏輯,例如日或分鐘歷史數(shù)據(jù)切片或者是實(shí)時(shí)數(shù)據(jù)切片更新。--(必須實(shí)現(xiàn))
def handle_bar(context):
? ? # 開始編寫你的主要的算法邏輯。
? ? pass
? ??
? ??
# after_trading函數(shù)會(huì)在每天交易結(jié)束后被調(diào)用,當(dāng)天只會(huì)被調(diào)用一次。 --(選擇實(shí)現(xiàn))
def after_trading(context):
? ? pass?
? ??
# order_status當(dāng)委托下單,成交,撤單等與下單有關(guān)的動(dòng)作時(shí),該方法就會(huì)被調(diào)用。---(選擇實(shí)現(xiàn))
def order_status(context,order):
? ? #print(\'訂單成交\')
? ? log_debug_info(\'C:\\T0DK.txt\',\'訂單成交\')
? ? #print(order.order_book_id)
? ? log_debug_info(\'C:\\T0DK.txt\',order.order_book_id)
? ? #print(str(order.order_id)+\',\'+order.status+\',\'+order.order_book_id)
? ? log_debug_info(\'C:\\T0DK.txt\',str(order.order_id)+\',\'+order.status+\',\'+order.order_book_id)
? ? #print(\'開多:\'+str(context.lastbuy)+\',平多:\'+str(context.lastbuyping)+\',開空:\'+str(context.lastsell)+\',平空:\'+str(context.lastsellping))
? ? log_debug_info(\'C:\\T0DK.txt\',\'開多:\'+str(context.lastbuy)+\',平多:\'+str(context.lastbuyping)+\',開空:\'+str(context.lastsell)+\',平空:\'+str(context.lastsellping))
? ? #如果是成交,將對(duì)應(yīng)的委托單撤銷
? ? log_debug_info(\'C:\\T0DK.txt\',"如果是成交,將對(duì)應(yīng)的委托單撤銷")
? ??
? ? if (order.status=="tradeing" and order.order_book_id==context.s1):
? ? ? ? #print(str(order.order_id)+\'全部成交\')
? ? ? ? log_debug_info(\'C:\\T0DK.txt\',str(order.order_id)+\'全部成交\')
? ? ? ??
? ? ? ? if order.order_id==context.lastbuy:? ? ? ? #買入成交
? ? ? ? ? ? if context.lastbuyping!=0:
? ? ? ? ? ? ? ? cancel_order (context.lastbuyping)
? ? ? ? ? ? ? ? #print("買入成交之后撤平倉(cāng)單,"+str(context.lastbuyping))
? ? ? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"買入成交之后撤平倉(cāng)單,"+str(context.lastbuyping))
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? context.lastbuyping=0
? ? ? ? if order.order_id==context.lastbuyping:? ? #平多成交
? ? ? ? ? ? if context.lastbuy!=0:
? ? ? ? ? ? ? ? cancel_order (context.lastbuy)
? ? ? ? ? ? ? ? #print("平多成交之后撤開倉(cāng)單,"+str(context.lastbuy))
? ? ? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"平多成交之后撤開倉(cāng)單,"+str(context.lastbuy))
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? context.lastbuy=0
? ? ? ? if order.order_id==context.lastsell:? ? ? ? #賣出成交
? ? ? ? ? ? if context.lastsellping!=0:
? ? ? ? ? ? ? ? cancel_order (context.lastsellping)
? ? ? ? ? ? ? ? #print("賣出成交之后撤平倉(cāng)單,"+str(context.lastsellping))
? ? ? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"賣出成交之后撤平倉(cāng)單,"+str(context.lastsellping))
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? context.lastsellping=0
? ? ? ? ? ? ? ??
? ? ? ? if order.order_id==context.lastsellping:? ? #平空成交
? ? ? ? ? ? if context.lastsell!=0:
? ? ? ? ? ? ? ? cancel_order (context.lastsell)
? ? ? ? ? ? ? ? #print("平空成交之后撤開倉(cāng)單,"+str(context.lastsell))
? ? ? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"平空成交之后撤開倉(cāng)單,"+str(context.lastsell))
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? context.lastsell=0
? ??
? ? #如果是撤單,將對(duì)應(yīng)的變量設(shè)置為0?
? ? log_debug_info(\'C:\\T0DK.txt\',"如果是撤單,將對(duì)應(yīng)的變量設(shè)置為0 ")? ? ? ? ? ? ? ?
?
? ? if (order.status=="cancelled" and order.order_book_id==context.s1):
? ? ? ? if order.order_id==context.lastbuy:? ? ? ? #買入撤單
? ? ? ? ? ? #print("買入開倉(cāng)撤單,"+str(context.lastbuy))
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"買入開倉(cāng)撤單,"+str(context.lastbuy))
? ? ? ? ? ??
? ? ? ? ? ? context.lastbuy=0
? ? ? ? if order.order_id==context.lastbuyping:? ? #平多撤單
? ? ? ? ? ? #print("平多單撤單,"+str(context.lastbuyping))
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"平多單撤單,"+str(context.lastbuyping))
? ? ? ? ? ??
? ? ? ? ? ? context.lastbuyping=0
? ? ? ? if order.order_id==context.lastsell:? ? ? ?#賣出撤單
? ? ? ? ? ? #print("賣出開倉(cāng)撤單,"+str(context.lastsell))
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"賣出開倉(cāng)撤單,"+str(context.lastsell))
? ? ? ? ? ??
? ? ? ? ? ? context.lastsell=0
? ? ? ? if order.order_id==context.lastsellping:? ?#平空撤單
? ? ? ? ? ? #print("平空單撤單,"+str(context.lastsellping))
? ? ? ? ? ? log_debug_info(\'C:\\T0DK.txt\',"平空單撤單,"+str(context.lastsellping))
? ? ? ? ? ??
? ? ? ? ? ? context.lastsellping=0
?
# order_action當(dāng)查詢交易接口信息時(shí)返回的通知---(選擇實(shí)現(xiàn))
#注意:該事件函數(shù)僅在融資融券、新股申購(gòu)操作刷新動(dòng)作時(shí)才會(huì)觸發(fā),一般賬戶無(wú)效。
#def order_action(context):
? ??
? ? #pass
?
# exit函數(shù)會(huì)在測(cè)評(píng)結(jié)束或者停止策略運(yùn)行時(shí)會(huì)被調(diào)用。---(選擇實(shí)現(xiàn))
def exit(context):
? ??
? ? #獲得品種的浮動(dòng)盈虧,(多空同時(shí)存在時(shí),為多空浮動(dòng)盈虧之和)
? ? zhan_bzj = get_account(28)
? ? #print(\'占用保證金總額\'+str(zhan_bzj))
? ? log_debug_info(\'C:\\T0DK.txt\', \'占用保證金總額\'+str(zhan_bzj))
? ??
? ? #獲得帳戶平倉(cāng)盈虧
? ? pingcang_win_long = get_account(30)
? ? #print(\'帳戶平倉(cāng)盈虧\'+str(pingcang_win_long))
? ? log_debug_info(\'C:\\T0DK.txt\', \'帳戶平倉(cāng)盈虧\'+str(pingcang_win_long))
? ??
? ? #獲得帳戶浮動(dòng)盈虧
? ? fudong_win_long = get_account(4)
? ? #print(\'帳戶浮動(dòng)盈虧\'+str(fudong_win_long))
? ? log_debug_info(\'C:\\T0DK.txt\', \'帳戶浮動(dòng)盈虧\'+str(fudong_win_long))
? ??
? ? #獲得帳戶手續(xù)費(fèi)
? ? shouxufei = get_account(31)
? ? #print(\'帳戶手續(xù)費(fèi)\'+str(shouxufei))
? ? log_debug_info(\'C:\\T0DK.txt\', \'帳戶手續(xù)費(fèi)\'+str(shouxufei))
? ??
? ? #盈虧率和成本比計(jì)算
? ? #fudong_ykl = get_account(4)/get_account(28)*100
? ? #chengbenbi = get_account(30)/get_account(31)
? ? #log_debug_info(\'C:\\T0DK.txt\', \'浮動(dòng)盈虧率\'+str(fudong_ykl)+\'/平倉(cāng)成本比\'+str(chengbenbi))
? ? ? ??
? ? killtimer(GridTrade)? ? #終止計(jì)時(shí)器
? ? print("終止計(jì)時(shí)器")
? ? log_debug_info(\'C:\\T0DK.txt\', \'終止計(jì)時(shí)器\')
? ? return
?
?
#type參數(shù) (get_account函數(shù))?
# type? ? 說(shuō)明
#1? ? 該函數(shù)返回常數(shù),返回當(dāng)前交易帳戶ID(該函數(shù)返回字符串類型數(shù)值)
#2? ? 賬戶類型,0 盈透 1 CTP 2 金仕達(dá)期貨 3FIX接口 4恒生期貨 5子賬戶 6其他柜臺(tái) 255 無(wú)效賬戶
#3? ? 現(xiàn)金余額
#5? ? 浮動(dòng)盈虧
#6? ? 當(dāng)前交易帳戶中的動(dòng)態(tài)權(quán)益/資產(chǎn)值
#19? ? 當(dāng)前可用資金
#20? ? 當(dāng)前流動(dòng)資產(chǎn)
#26? ? 上次結(jié)算準(zhǔn)備金/期初余額
#27? ? 結(jié)算準(zhǔn)備金/期初余額
#28? ? 占用保證金/證券市值
#29? ? 可取資金
#30? ? 平倉(cāng)盈虧數(shù)額/回報(bào)賣出金額/融券盈虧
#31? ? 手續(xù)費(fèi)
#32? ? 入金金額/利息積數(shù)/融資市值
#33? ? 出金金額/當(dāng)前余額
#34? ? 上次信用額度
#35? ? 上次質(zhì)壓
#36? ? 質(zhì)壓金額
#37? ? 信用額度
#38? ? 凍結(jié)保證金/禁取資產(chǎn)
#39? ? 凍結(jié)手續(xù)費(fèi)/回報(bào)買入金額/融資盈虧
#40? ? 保底資金
#41? ? 多頭保證金率(期貨專有)
#42? ? 空頭保證金率(期貨專有)
#43? ? 返回交易網(wǎng)關(guān)名稱,該函數(shù)返回字符串常數(shù)
#44? ? 融券市值
#45? ? 融券費(fèi)用
#46? ? 融券利息
#47? ? 融資余額
#48? ? 融券余額
#49? ? 可用保證金
#50? ? 已用融資額
#51? ? 已用融券額
#52? ? 融資負(fù)債
#53? ? 返回當(dāng)前交易賬戶是否處于有效狀態(tài)。建議對(duì)賬戶持倉(cāng)或資金進(jìn)行讀取時(shí)首先調(diào)用該函數(shù)對(duì)賬戶有效性進(jìn)行判斷,以免出現(xiàn)誤操作。(對(duì)IB外盤無(wú)效,僅限國(guó)內(nèi))
?
# 本Python代碼主要用于策略交易
# 可以自己import我們平臺(tái)支持的第三方python模塊,比如pandas、numpy等。
#from PythonApi import *
?
#? 參數(shù)定義區(qū),這里定義的參數(shù)可以直接在context對(duì)象中獲取。--(選擇實(shí)現(xiàn))
#def parameter():
#? ? input_par("myvalues1",5,1,20,1)
#? ? input_par("myvalues2",10,1,20,1)
?
?
#? 在這個(gè)方法中編寫任何的初始化邏輯。context對(duì)象將會(huì)在你的算法策略的任何方法之間做傳遞。--(必須實(shí)現(xiàn))
#def init(context):
? ? # 在context中保存全局變量
? ? #context.s1 = "SZ000001"? ?#平安銀行股票
? ??
? ? # print("策略啟動(dòng)") #調(diào)試打印輸出
? ??
?
# before_trading此函數(shù)會(huì)在每天基準(zhǔn)合約的策略交易開始前被調(diào)用,當(dāng)天只會(huì)被調(diào)用一次。--(選擇實(shí)現(xiàn))
#def before_trading(context):
#? ? pass
?
?
# 你選擇的品種的數(shù)據(jù)更新將會(huì)觸發(fā)此段邏輯,例如日或分鐘歷史數(shù)據(jù)切片或者是實(shí)時(shí)數(shù)據(jù)切片更新。--(必須實(shí)現(xiàn))
#def handle_bar(context):
? ? # 開始編寫你的主要的算法邏輯。
? ??
? ? #使用buy_open、sell_close等方法下單
? ? #下單示例:
? ? #buy_open(context.s1, "Market", volume = 100)? ? #? 市價(jià)開多
? ? #buy_open(context.s1, "Limit", 25.45, 100)? ? ? ?#? 限價(jià)開多
#? ? pass
? ??
? ??
# after_trading函數(shù)會(huì)在每天交易結(jié)束后被調(diào)用,當(dāng)天只會(huì)被調(diào)用一次。 --(選擇實(shí)現(xiàn))
#def after_trading(context):
#? ? pass
? ??
? ??
# order_status當(dāng)委托下單,成交,撤單等與下單有關(guān)的動(dòng)作時(shí),該方法就會(huì)被調(diào)用。---(選擇實(shí)現(xiàn))
#def order_status(context,order):
#? ? pass
?
# order_action當(dāng)查詢交易接口信息時(shí)返回的通知---(選擇實(shí)現(xiàn))
#def order_action(context,type, account, datas)
#? ? ? ?pass
?
# exit函數(shù)會(huì)在測(cè)評(píng)結(jié)束或者停止策略運(yùn)行時(shí)會(huì)被調(diào)用。---(選擇實(shí)現(xiàn))
#def exit(context):
#? ? pass
?
{別忘了將本網(wǎng)告訴您身邊的朋友,向朋友傳達(dá)有用資料,也是一種人情,你朋友會(huì)感謝你的。}
?
有思路,想編寫各種指標(biāo)公式,交易模型,選股公式,還原公式的朋友
可聯(lián)系技術(shù)人員 QQ: 262069696 或微信:cxhjy888 進(jìn)行 有償收費(fèi) 編寫!(注:由于人數(shù)限制,QQ或微信請(qǐng)選擇方便的一個(gè)聯(lián)系我們就行,加好友時(shí)請(qǐng)簡(jiǎn)單備注下您的需求,否則無(wú)法通過(guò)。謝謝您!)
(怎么收費(fèi),代編流程等詳情請(qǐng)點(diǎn)擊查閱!)
(注:由于人數(shù)限制,QQ或微信請(qǐng)選擇方便的一個(gè)聯(lián)系我們就行,加好友時(shí)請(qǐng)簡(jiǎn)單備注下您的需求,否則無(wú)法通過(guò)。謝謝您!)
相關(guān)文章
-
沒(méi)有相關(guān)內(nèi)容