macross
来源:https://uqer.io/community/share/55a92cdcf9f06c57a11b53be
from matplotlib import pylab
import numpy as np
import pandas as pd
import DataAPI
import seaborn as sns
sns.set_style('white')
start = datetime(2008, 1, 1)
end = datetime(2015, 4, 23)
benchmark = 'SH50'
universe = ['510050.XSHG']
capital_base = 100000
commission = Commission(0.0,0.0)
window_short = 20
window_long = 300
longest_history = window_long
SD = 0.05
def initialize(account):
account.fund = universe[0]
account.SD = SD
account.window_short = window_short
account.window_long = window_long
def handle_data(account):
hist = account.get_history(longest_history)
fund = account.fund
short_mean = np.mean(hist[fund]['closePrice'][-account.window_short:])
long_mean = np.mean(hist[fund]['closePrice'][-account.window_long:])
now_price = hist[fund]['closePrice'][-1:]
flag = True if (now_price - short_mean) > account.SD * short_mean else False
if flag:
if account.position.secpos.get(fund, 0) == 0:
approximationAmount = int(account.cash / hist[fund]['closePrice'][-1]/100.0) * 100
order(fund, approximationAmount)
else:
if account.position.secpos.get(fund, 0) >= 0:
order_to(fund, 0)