美文网首页股票小工具系列
利用tushare判断用哪条均线买卖比较适合

利用tushare判断用哪条均线买卖比较适合

作者: 平凡啊菜 | 来源:发表于2017-06-24 12:06 被阅读93次

    见标题,不多说,上源码,需要的自取

    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)
    
    赞赏是最真诚的认可

    相关文章

      网友评论

        本文标题:利用tushare判断用哪条均线买卖比较适合

        本文链接:https://www.haomeiwen.com/subject/jlckcxtx.html