import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import datetime
from dateutil.relativedelta import relativedelta

daylist = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
stock = "010620.KS"

def current_price():
    s = datetime.datetime.today().strftime("%Y-%m-%d")
    e = datetime.datetime.today().strftime("%Y-%m-%d")
    #cur_gs = web.DataReader("006400.KS", "yahoo", s, e)
    cur_gs = web.DataReader(stock, "yahoo", '2018-08-30', '2018-08-30')
    new_cur_gs = cur_gs[cur_gs['Volume']!=0]
    print(new_cur_gs.loc[new_cur_gs.index.max()]['Close'])
    return new_cur_gs.loc[new_cur_gs.index.max()]['Close']

def monthly_momentum(year_month, month):
    start = year_month + "-" + str(1)
    end = year_month + "-" + str(daylist[month]-1)
    gs = web.DataReader(stock, "yahoo", start, end)
    new_gs = gs[gs['Volume']!=0]
    #print(new_gs)
    #print(new_gs.index)
    #print(new_gs.index.min())
    #print(new_gs.index.max())
    date_s = new_gs.index.min()
    date_e = new_gs.index.max()
    #print(year_month, " : open =", new_gs.loc[date_s]['Open'], "close = ", new_gs.loc[date_e]['Close'])
    print(year_month, " : ", "close = ", new_gs.loc[date_e]['Close'])
    return new_gs.loc[date_e]['Close']

cur_p = current_price()
avg_m = 0

for i in range(12):
    relative_time = datetime.datetime.today() - relativedelta(months=i+1)
    relative_time_str = relative_time.strftime("%Y-%m")
    #print(relative_time_str)
    month_str = relative_time.strftime("%m")
    #print(month_str)
    month_p = monthly_momentum(relative_time_str, int(month_str))
    if cur_p > month_p:
        avg_m = avg_m + 1;

print("average momentum = ", avg_m/12)

 

 

systrader79 님의 매매전략 중 하나인 평균모멘텀 전략을 구사하기 위해 필요한 종목의 12개월 평균 모멘텀을 구하는 코드 이다. (문제가 된다면 삭제 ^^)

 

'python & stock' 카테고리의 다른 글

4/19 수급특징주  (0) 2019.04.22
제로투세븐  (0) 2019.04.21
엔브이에이치코리아  (0) 2019.04.19
[사업보고서] 이마트  (0) 2019.04.14
종목 코드 가져오기  (0) 2018.08.31
Posted by 황승곤
,