# 本Python代碼主要用于策略交易
?
?
# 可以自己import我們平臺(tái)支持的第三方python模塊,比如pandas、numpy等。
from PythonApi import *
import numpy as np
import talib
import math
#? 在這個(gè)方法中編寫任何的初始化邏輯。context對(duì)象將會(huì)在你的算法策略的任何方法之間做傳遞。--(必須實(shí)現(xiàn))
def init(context):
??? #入場(chǎng)周期
??? context.X = 20
??? #出場(chǎng)周期
??? context.Y = 10
??? #記錄建倉的atr
??? context.entry = 0
??? #記錄交易次數(shù)
??? context.num = 0
??? #交易標(biāo)的
??? context.s = context.run_info.base_book_id
??? #記錄上次開倉價(jià)
??? context.enterprice = 0
# 你選擇的品種的數(shù)據(jù)更新將會(huì)觸發(fā)此段邏輯,例如日或分鐘歷史數(shù)據(jù)切片或者是實(shí)時(shí)數(shù)據(jù)切片更新。--(必須實(shí)現(xiàn))
def handle_bar(context):
??? close = history_bars(context.s,context.X+2,\'self\',\'close\',include_now=True)
??? high = history_bars(context.s,context.X+2,\'self\',\'high\',include_now=True)?
??? low = history_bars(context.s,context.X+2,\'self\',\'low\',include_now=True)??
??? if len(close) == context.X+2:
??????? #atr的計(jì)算參考這個(gè)帖子http://www.weistock.com/bbs/dispbbs.asp?boardid=10&Id=173300
??????? tr = talib.TRANGE(high,low,close)
??????? atr = talib.SMA(tr[1:],context.X)
??????? unit = int((get_account(6)*0.01) / (atr[-2] * get_dynainf(context.s,209)))
??????? #X天的高低點(diǎn)(不包含當(dāng)天)
??????? X周期高點(diǎn) = high[:-1].max()
??????? X周期低點(diǎn) = low[:-1].min()
???????
??????? #建立頭寸,根據(jù)唐奇安通道創(chuàng)新高入場(chǎng),關(guān)鍵點(diǎn)就是利用波動(dòng)atr計(jì)算倉位數(shù)量,portfolio用來進(jìn)行倉位的控制
??????? portfolio=get_portfolio (context.s, 2)
??????? if high[-1]>=X周期高點(diǎn) and portfolio.buy_quantity==0 and portfolio.sell_quantity==0:
??????????? buy_open(context.s, "Market",0 ,unit,serial_id = 1)
??????????? context.entry = atr[-2]
??????????? context.num = 1
??????????? context.enterprice = close[-1]
??????? if low[-1]<=X周期低點(diǎn) and portfolio.sell_quantity==0 and portfolio.buy_quantity==0:
??????????? sell_open(context.s, "Market",0 ,unit,serial_id = 2)
??????????? context.entry = atr[-2]
??????????? context.num = 1
??????????? context.enterprice = close[-1]
???????????
??????? #加倉,最高價(jià)比上次開倉價(jià)多0.5個(gè)atr(盈利加倉)
??????? if portfolio.sell_quantity ==0 and portfolio.buy_quantity>0 and high[-1]>context.enterprice + 0.5*context.entry and context.num<4:
??????????? buy_open(context.s, "Market",0 ,unit,serial_id = 3)
??????????? context.num+=1
??????????? context.enterprice = close[-1]
??????? if portfolio.buy_quantity==0 and portfolio.sell_quantity>0 and low[-1]<context.enterprice - 0.5*context.entry and context.num<4:
??????????? sell_open(context.s, "Market",0 ,unit,serial_id = 4)
??????????? context.num+=1
??????????? context.enterprice = close[-1]
???????????
??????? #出場(chǎng),跌破短周期低點(diǎn)平多
??????? Y周期高點(diǎn) = high[-context.Y-1:-1].max()
??????? Y周期低點(diǎn) = low[-context.Y-1:-1].min()
??????? if portfolio.buy_quantity>0 and low[-1] < Y周期低點(diǎn):
??????????? sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 5)
??????? if portfolio.sell_quantity>0 and high[-1] > Y周期高點(diǎn):
??????????? buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 6)
???????????
??????? #止損,虧損幅度超過開倉2個(gè)atr幅度止損
??????? if portfolio.buy_quantity>0 and low[-1] < context.enterprice - 2*context.entry:
??????????? sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 7)
??????? if portfolio.sell_quantity>0 and high[-1] > context.enterprice + 2*context.entry:
??????????? buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 8)