用5日均线和10日均线进行判断 --- 改进版
来源:https://uqer.io/community/share/561e3a65f9f06c4ca82fb5ec
1、 修改Adobe同学的代码
data=DataAPI.MktEqudGet(ticker="600030",beginDate="20131001")
a = data.closePrice
B = []
n = len(a)
for i in range(10, n):
x5 = a[i-5:i].mean()
x10 = a[i-10:i].mean()
B.append(x5 > x10)
import matplotlib.pyplot as plt
o = data.openPrice
m = len(B)
w = 0
cash = 1000000
amount = 0
PL = []
for i in range(1, m):
k = i + 10
if B[i-1] == 0 and B[i] == 1 and not amount:
amount = cash // o[k]
cash -= o[k] * amount
elif B[i-1] == 1 and B[i]==0 and amount:
cash += o[k] * amount
amount = 0
PL.append(cash + o[k] * amount)
print("利润:{}".format(PL[-1]))
plt.plot(PL,color="green",label="Profit and Loss")
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()
plt.plot(a[10:], color="red",label="Profit and Loss")
plt.show()
利润:1559132.78
2、 Uqer框架相同策略
import numpy as np
start = '2013-10-01'
end = '2015-10-13'
benchmark = 'SH50'
universe = ['600030.XSHG']
capital_base = 100000
commission = Commission(0.0,0.0)
window_short = 5
window_long = 10
def initialize(account):
account.fund = universe[0]
def handle_data(account):
cp_hist = account.get_attribute_history('closePrice', window_long)[account.fund]
short_mean = np.mean(cp_hist[-window_short:])
long_mean = np.mean(cp_hist[-window_long:])
if short_mean - long_mean > 0:
if account.fund not in account.valid_secpos:
approximationAmount = int(account.cash / account.referencePrice[account.fund] / 100) * 100
order(account.fund, approximationAmount)
else:
if account.fund in account.valid_secpos:
order_to(account.fund, 0)