Skip to main content

大神

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tushare as ts


df = ts.get_hist_data('000001')
df.to_csv("./day/000001.csv")

df = pd.read_csv("./day/000001.csv",index_col="date",parse_dates=['date'])[['open','close','low','high']]
df.info()
df = df.sort_values(by='date', ascending=True)

df['ma5'] =np.nan
df['ma30'] =np.nan
df['shift'] =np.nan

df['ma5'] = df['close'].rolling(2).mean()
df['ma30'] = df['close'].rolling(30).mean()
df['shift'] = df['close'].shift(1)

# df.head(40)
df = df[30:1000]
df[['close','ma5','ma30']].plot()


s1 = df['ma5'] < df['ma30']
s2 = df['ma5'] >= df['ma30']


death= df[s1 & s2.shift(1)].index
golden = df[ -(s1 | s2.shift(1))].index



first_money = 10000
money = first_money
hold = 0
sr1 = pd.Series(1,index=golden)
sr2 = pd.Series(0,index=death)
sr = sr1.append(sr2).sort_index()


for i in range(0,len(sr)):
p = df['open'][sr.index[i]]
if sr.iloc[i] == 1:
buy = (money // (100* p))
hold += buy * 100
money -= buy *100 * p
else :
money += hold *p
hold = 0
p = df['open'][-1]
now_money = hold * p + money
print(now_money - first_money)