见标题,不多说,上源码,需要的自取
import tushare as ts
def parse(code_list,maday):
'''process stock'''
is_buy = 0
buy_val = []
buy_date = []
sell_val = []
sell_date = []
df = ts.get_hist_data(code_list,start=START_DATE,end=END_DATE)
ma = df[u'ma%s'%maday]
# print(ma)
close = df[u'close']
# print(close)
rate = 1.0
idx = len(ma)
while idx > 0:
idx -= 1
close_val = close[idx]
ma_val = ma[idx]
if close_val > ma_val:
if is_buy == 0:
is_buy = 1
buy_val.append(close_val)
buy_date.append(close.keys()[idx])
elif close_val < ma_val:
if is_buy == 1:
is_buy = 0
sell_val.append(close_val)
sell_date.append(close.keys()[idx])
print("stock number: %s" %code_list)
print("buy count : %d" %len(buy_val))
print("sell count : %d" %len(sell_val))
for i in range(len(sell_val)):
rate = rate * (sell_val[i] * (1 - 0.002) / buy_val[i])
print("buy date : %s, buy price : %.2f" %(buy_date[i], buy_val[i]))
print("sell date: %s, sell price: %.2f" %(sell_date[i], sell_val[i]))
print('%s日线买卖的策略'%maday)
print("收益率: %.2f %% \n\n" % ((rate-1)*100))
if __name__ == '__main__':
STOCK = '300100'
START_DATE = '2017-01-01'
END_DATE = '2017-04-05'
parse(STOCK,5)
#parse('hs300',5)
parse(STOCK,10)
#parse('hs300',10)
parse(STOCK,20)
#parse('hs300',20)
非常好的模式,让我今年收益翻倍的文章,强烈推荐分享下:
2020年4月至6月净利润断层实战小结-真香~