移动平均选股策略

# coding=UTF-8
# encoding:UTF-8
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
'''
s1 = pd.Series()
s2 =pd.Series([1,2,3,4],\
              index =[12,13,14,15])
s3 = pd.Series({'a':1,'b':2,'c':3})
s4 = pd.Series([1,2,3,4])

s6= pd.Series(np.arange(2,6))

print s1
print s2
print s2.values
print s2.index
print s3
print s4
print s4.head(2)

print s6.tail(1)
print s6.take([1])

'''
import tushare as ts
#tock_list = ts.get_stock_basics()
#rint stock_list
#stock_finance_report = ts.get_report_data(2018,3)
#print stock_finance_report
HS300_list = pd.Series()
HS300_list_temp = pd.Series()
HS300_list_temp = ts.get_zz500s() #沪深300成分股
HS300_list = HS300_list_temp.ix[:,'code':'name']
print HS300_list_temp
print HS300_list
HS300_list_temp.to_csv('/home/zhou/eclipse-workspace/stock/HS300_list.csv',encoding='gbk')

#for i in range(0,len(HS300_list)):
#    Stock_MA5_MA20_Analysis(HS300_list.code[i])


def Stock_MA5_MA20_Analysis(stock_list):
    for i in range (0, len(stock_list)):
#        print stock_list.code[i]
        try:
            stock_price_daily = ts.get_hist_data(stock_list.code[i])
#        print stock_price_daily
#        print stock_price_daily.close[:5]
#    stock_price_daily = stock_price_daily_temp.sort_index(ascending = True)
#        try:
            stock_price_close_temp = stock_price_daily.close[:2]
#        except AttributeError:
#            print stock_list.code[i]
#            continue
#        else:
            stock_price_close = stock_price_close_temp.sort_index(ascending = True)
#        print stock_price_close[0]
#        print stock_price_close[1]
#        print stock_price_close[2]
#        print stock_price_close[0:2]
            stock_price_ma5_temp = stock_price_daily.ma5[:2]
            stock_price_ma5 = stock_price_ma5_temp.sort_index(ascending = True)
            stock_price_ma10_temp = stock_price_daily.ma10[:2]
            stock_price_ma10 = stock_price_ma10_temp.sort_index(ascending = True)
            stock_price_ma20_temp = stock_price_daily.ma20[:2]
            stock_price_ma20 = stock_price_ma20_temp.sort_index(ascending = True)
            stock_buy_signal = False
            for j in range(1, len(stock_price_close)):
                if stock_price_ma5[j-1]-stock_price_ma20[j-1] < 0.00  and stock_price_ma5[j] - stock_price_ma20[j] > 0.00:
                    stock_buy_signal = True
            if stock_buy_signal == True:
                 print stock_list.code[i]
        except:
#            print stock_list.code[i]
#            i = i +1
            continue

       # elif stock_price_ma5[i]-stock_price_ma20[i] < 0.00  and stock_price_ma5[i-1] - stock_price_ma20[i-1] > 0.00:
       #    stock_signal_ma[i] = -1        

#for i in range(0,len(HS300_list)):
#    print HS300_list.code[i]
Stock_MA5_MA20_Analysis(HS300_list)


#Stock_MA5_MA20_Analysis('000858')
'''
#istorical_price = ts.get_hist_data('600848')
#rint Historical_price
stock_price_daily_temp = ts.get_hist_data('000858')
stock_price_daily = stock_price_daily_temp.sort_index(ascending = True)

#print stock_price_daily_temp
print stock_price_daily
#
#stock close price
#stock_price_daily_close = stock_price_daily.iloc[:,0:4]
stock_price_close = stock_price_daily.close[:]
#stock_price_close_sort = stock_price_close.sort_index(ascending = True)
print stock_price_close

# stock price ma5
stock_price_ma5 = stock_price_daily.ma5[:]
#stock_price_ma5_sort = stock_price_ma5.sort_index(ascending = True)
print stock_price_ma5

# stock price ma10
stock_price_ma10 = stock_price_daily.ma10[:]
#stock_price_ma10_sort =stock_price_ma10.sort_index(ascending = True)
print stock_price_ma10

# stock price ma20
stock_price_ma20 = stock_price_daily.ma20[:]
#stock_price_ma20_sort = stock_price_ma20.sort_index(ascending = True)
print stock_price_ma20
'''
'''
plt.plot(stock_price_close_sort, label = 'close')
plt.plot(stock_price_ma5_sort, label = 'ma5')
plt.plot(stock_price_ma10_sort, label = 'ma10')
plt.plot(stock_price_ma20_sort, label = 'ma20')

plt.legend()
plt.show()
#print stock_price_daily
#print stock_price_ma5
#print stock_price_ma10

stock_signal_ma = pd.Series(0,index = stock_price_daily.index)
print stock_signal_ma
for i in range(0, len(stock_price_daily)):
    if stock_price_ma5[i]-stock_price_ma20[i] > 0.00  and stock_price_ma5[i-1] - stock_price_ma20[i-1] < 0.00:
        stock_signal_ma[i] = 1
    elif stock_price_ma5[i]-stock_price_ma20[i] < 0.00  and stock_price_ma5[i-1] - stock_price_ma20[i-1] > 0.00:
        stock_signal_ma[i] = -1        
print stock_signal_ma
#print stock_signal_short_term.index
for i in range (0, len(stock_signal_short_term)):
    if stock_signal_short_term[i] == '1'
        print stock_signal_short_term[i].index

stock_price_ma5_ma20_deduce = pd.Series(0, index = stock_price_daily.index)
print stock_price_ma5_ma20_deduce

stock_price_ma5_ma20_deduce = stock_price_daily.ma5-stock_price_daily.ma10
'''

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值