# 雙動力策略

``````import talib

def initialize(context):
# 初始化此策略
# 設置我們要操作的股票池, 這裏我們只操作一支股票
g.security = '600570.SS'
set_universe(g.security)
g.K1 = 0.4
g.K2 = 0.6

#當五日均線高於十日均線時買入，當五日均線低於十日均線時賣出
def handle_data(context, data):
security = g.security
# 獲取歷史價格，最高價
H = get_history(30, '1d', field = 'high', fq = 'pre')
# 獲取歷史價格，最低價
L = get_history(30, '1d', field = 'low', fq = 'pre')
# 獲取歷史價格，收盤價
C = get_history(30, '1d', field = 'close', fq = 'pre')
# 獲取歷史價格，開盤價
O = get_history(30, '1d', field = 'open', fq = 'pre')
# 最高價最高價
HH = H[security].max()
# 收盤價最低價
LC = C[security].min()
# 收盤價最高價
HC = C[security].max()
# 最低價最低價
LL = L[security].min()

# 用於計算入場指標
Range = max(HH - LC, HC - LL)

# 計算移動平均線
ma10 = talib.MA(C[security].values, 20)

JX = ma10[-1]
ZSX = min(ma10[-11:-1])

amount = context.portfolio.positions[security]['amount']
cash = context.portfolio.cash

if H[security][-1] > (O[security][-1] + g.K1 * Range) and C[security][-1] > JX and amount == 0:
# 全部的可用資金買入
order_target_value(security, cash)
# 記錄買入價格