import jqdata
import math
import numpy as np
import pandas as pd
from collections import deque
def initialize(context):
set_option('use_real_price', True)
set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
g.security = '000060.XSHE'
set_benchmark(g.security)
g.in_day = 20
g.out_day = 10
g.today_units = 0
g.current_units = 0
g.N=deque(maxlen=19)
g.current_N = 0
g.last_buy_price = 0
price = attribute_history(g.security, g.N.maxlen*2+1, '1d', ('high', 'low', 'close'))
for i in range(g.N.maxlen+1, g.N.maxlen*2+1):
li = []
for j in range(i-19,i+1):
a = price['high'][j]-price['low'][j]
b = abs(price['high'][j]-price['close'][j-1])
c = abs(price['low'][j]-price['close'][j-1])
li.append(max(a,b,c))
current_N = np.array(li).mean()
g.N.append(current_N)
def before_trading_start(context):
g.current_N = cal_N()
g.today_units = 0
def handle_data(context, data):
dt = context.current_dt
current_price = data[g.security].price #上一分钟价格
value = context.portfolio.total_value
cash = context.portfolio.available_cash
unit = math.floor(value * 0.01 / g.current_N)
if g.current_units == 0:
buy(current_price, cash, unit)
else:
if stop_loss(current_price):
return
if sell(current_price):
return
addin(current_price, cash, unit)
def cal_N():
# if len(g.N) < g.N.maxlen:
# price = attribute_history(g.security, g.N.maxlen+2, '1d', ('high', 'low', 'close'))
# li = []
# for i in range(1, g.N.maxlen+2):
# a = price['high'][i]-price['low'][i]
# b = abs(price['high'][i]-price['close'][i-1])
# c = abs(price['low'][i]-price['close'][i-1])
# li.append(max(a,b,c))
# current_N = np.array(li).mean()
# else:
price = attribute_history(g.security, 2, '1d', ('high', 'low', 'close'))
a = price['high'][1]-price['low'][1]
b = abs(price['high'][1]-price['close'][0])
c = abs(price['low'][1]-price['close'][0])
current_N = (max(a,b,c) + np.array(g.N).sum())/(g.N.maxlen+1)
g.N.append(current_N)
return current_N
def buy(current_price, cash, unit):
price = attribute_history(g.security, g.in_day, '1d', ('close',))
if current_price > max(price['close']):
shares = cash / current_price
if shares >= unit:
print("buying %d" % unit)
o = order(g.security, unit)
g.last_buy_price = o.price
g.current_units += 1
g.today_units += 1
return True
return False
def addin(current_price, cash, unit):
if current_price >= g.last_buy_price + 0.5 * g.current_N:
shares = cash / current_price
if shares >= unit:
print("adding %d" % unit)
o = order(g.security, unit)
g.last_buy_price = o.price
g.current_units += 1
g.today_units += 1
return True
return False
def sell(current_price):
price = attribute_history(g.security, g.out_day, '1d', ('close',))
if current_price < min(price['close']):
print("selling")
order_target(g.security, 0)
g.current_units = g.today_units
return True
return False
def stop_loss(current_price):
if current_price < g.last_buy_price - 2 * g.current_N:
print("stop loss")
order_target(g.security, 0)
g.current_units = g.today_units
return True
return False