Пример #1
0
def ema(ticker,
        rolling_windows=[20, 50],
        hide_price=False,
        graph_path=os.path.join(os.getcwd(), "ema")):
    data = stock_data.get_dataframe_by_ticker(ticker)
    exp_means = [
        data['Adj Close'].ewm(span=i, adjust=False).mean()
        for i in rolling_windows
    ]
    for i, exp_mean in enumerate(exp_means):
        plt.plot(
            data['Date'],
            exp_mean,
            label=f'{ticker} {rolling_windows[i]} Exponential Moving Average')
    if hide_price is False:
        plt.plot(data['Date'],
                 data['Adj Close'],
                 label=f'{ticker} {max(rolling_windows)} Price',
                 color='magenta')
    plt.legend(loc='upper left')
    try:
        plt.savefig(os.path.join(
            graph_path,
            f'{ticker}_{datetime.datetime.now().date().isoformat()}.png'),
                    format="png")
    except FileNotFoundError:
        os.makedirs(graph_path)
        plt.savefig(os.path.join(
            graph_path,
            f'{ticker}_{datetime.datetime.now().date().isoformat()}.png'),
                    format="png")
Пример #2
0
def graph_macd(ticker,
               windows=[12, 26],
               signal_line=9,
               graph_path=os.path.join(os.getcwd(), "macd")):
    data = stock_data.get_dataframe_by_ticker(ticker)
    if data is None or len(data) < min(windows):
        return None
    data[f'{windows[0]} ema'] = data['Adj Close'].ewm(span=windows[0],
                                                      adjust=False,
                                                      min_periods=1).mean()
    data[f'{windows[1]} ema'] = data['Adj Close'].ewm(span=windows[1],
                                                      adjust=False,
                                                      min_periods=1).mean()
    data['MACD'] = data[f'{windows[0]} ema'] - data[f'{windows[1]} ema']
    data[f'signal line'] = data['MACD'].ewm(span=signal_line,
                                            adjust=False).mean()
    fig, ax = plt.subplots(1)
    ax.plot(data['Date'][-30:], data['MACD'][-30:], label=f'MACD')
    ax.plot(data['Date'][-30:],
            data['signal line'][-30:],
            label=f'Signal Line')
    ax.axhline(0, linestyle='--')
    ax.legend(loc='upper left')
    try:
        plt.savefig(os.path.join(
            graph_path,
            f'{ticker}_{datetime.datetime.now().date().isoformat()}.png'),
                    format='png')
    except FileNotFoundError:
        os.makedirs(graph_path)
        plt.savefig(os.path.join(
            graph_path,
            f'{ticker}_{datetime.datetime.now().date().isoformat()}.png'),
                    format='png')
Пример #3
0
def get_rsi_dataframe(ticker,
                      start=two_years_from_today,
                      end=today,
                      time_window=14):
    df = stock_data.get_dataframe_by_ticker(ticker, start, end)
    if df is not None:
        df['RSI'] = compute_rsi(df['Adj Close'], time_window)
    return df
Пример #4
0
def get_macd_intersection(ticker,
                          windows=[12, 26],
                          signal_line=9,
                          look_back_time=5):
    data = stock_data.get_dataframe_by_ticker(ticker)
    if data is None or len(data) < min(windows):
        return None
    data[f'{windows[0]} ema'] = data['Adj Close'].ewm(span=windows[0],
                                                      adjust=False,
                                                      min_periods=1).mean()
    data[f'{windows[1]} ema'] = data['Adj Close'].ewm(span=windows[1],
                                                      adjust=False,
                                                      min_periods=1).mean()
    data['MACD'] = data[f'{windows[0]} ema'] - data[f'{windows[1]} ema']
    data[f'signal line'] = data['MACD'].ewm(span=signal_line,
                                            adjust=False).mean()
    intersections = {}
    for i in range(len(data['Date'][-look_back_time:])):
        if len(data['Date'][:]) - look_back_time + 1 + i < len(
                data['Date'][:]):
            y11 = data[f'MACD'][len(data['Date'][:]) - look_back_time +
                                i]  # macd y1
            y21 = data[f'signal line'][len(data['Date'][:]) - look_back_time +
                                       i]  # signal y1
            y12 = data[f'MACD'][len(data['Date'][:]) - look_back_time + 1 +
                                i]  #macd y2
            y22 = data[f'signal line'][len(data['Date'][:]) - look_back_time +
                                       1 + i]  # signal y2
            if y11 > y21 and y12 < y22:
                intersections = {
                    "ticker":
                    ticker,
                    "cross_date":
                    data['Date'][len(data['Date'][:]) - look_back_time + 1 +
                                 i],
                    "sentiment":
                    "Bearish",
                    "count":
                    -1
                }
            if y21 > y11 and y22 < y12:
                intersections = {
                    "ticker":
                    ticker,
                    "cross_date":
                    data['Date'][len(data['Date'][:]) - look_back_time + 1 +
                                 i],
                    "sentiment":
                    "Bullish",
                    "count":
                    1
                }
    return intersections
Пример #5
0
def get_sma_intersection(ticker, windows=[20, 50], look_back_time=5):
    data = stock_data.get_dataframe_by_ticker(ticker)
    if data is None or len(data) < min(windows):
        return None
    data[f'{windows[0]} SMA'] = data['Adj Close'].rolling(
        window=windows[0]).mean()
    data[f'{windows[1]} SMA'] = data['Adj Close'].rolling(
        window=windows[1]).mean()
    intersections = {}
    for i in range(len(data['Date'][-look_back_time:])):
        if len(data['Date'][:]) - look_back_time + 1 + i < len(
                data['Date'][:]):
            y11 = data[f'{windows[0]} SMA'][len(data['Date'][:]) -
                                            look_back_time +
                                            i]  # short window n day price
            y21 = data[f'{windows[1]} SMA'][len(data['Date'][:]) -
                                            look_back_time +
                                            i]  # long window n day price
            y12 = data[f'{windows[0]} SMA'][len(data['Date'][:]) -
                                            look_back_time + 1 +
                                            i]  # short window n + 1 price
            y22 = data[f'{windows[1]} SMA'][len(data['Date'][:]) -
                                            look_back_time + 1 +
                                            i]  # long window n + 1 price
            if y11 > y21 and y12 < y22:
                intersections = {
                    "ticker":
                    ticker,
                    "cross_date":
                    data['Date'][len(data['Date'][:]) - look_back_time + 1 +
                                 i],
                    "sentiment":
                    "Bearish",
                    "count":
                    -1
                }
            if y21 > y11 and y22 < y12:
                intersections = {
                    "ticker":
                    ticker,
                    "cross_date":
                    data['Date'][len(data['Date'][:]) - look_back_time + 1 +
                                 i],
                    "sentiment":
                    "Bullish",
                    "count":
                    1
                }
    return intersections
Пример #6
0
def sma(ticker,
        rolling_windows=[20, 50],
        hide_price=False,
        bollinger=False,
        graph_path=os.path.join(os.getcwd(), "sma")):
    data = stock_data.get_dataframe_by_ticker(ticker)[-2 *
                                                      (max(rolling_windows)):]
    rolling_means = [
        data['Adj Close'].rolling(window=i).mean() for i in rolling_windows
    ]
    for i, rolling_mean in enumerate(rolling_means):
        plt.plot(data['Date'],
                 rolling_mean,
                 label=f'{ticker} {rolling_windows[i]} Simple Moving Average')
    if bollinger is True:
        data = bollinger_bands(data, window=rolling_windows[0])
        plt.plot(data['Date'],
                 data['Lower Bollinger'],
                 label=f'{ticker} {rolling_windows[0]} Lower Bollinger Band')
        plt.plot(data['Date'],
                 data['Upper Bollinger'],
                 label=f'{ticker} {rolling_windows[0]} Upper Bollinger Band')
    if hide_price is False:
        plt.plot(data['Date'],
                 data['Adj Close'],
                 label=f'{ticker} {max(rolling_windows)} Price',
                 color='magenta')
    plt.legend(loc='upper left')
    try:
        plt.savefig(os.path.join(
            graph_path,
            f'{ticker}_{datetime.datetime.now().date().isoformat()}.png'),
                    format='png')
    except FileNotFoundError:
        os.makedirs(graph_path)
        plt.savefig(os.path.join(
            graph_path,
            f'{ticker}_{datetime.datetime.now().date().isoformat()}.png'),
                    format='png')
Пример #7
0
def get_rsi_plot(ticker,
                 start=two_years_from_today,
                 end=today,
                 time_window=14):
    df = stock_data.get_dataframe_by_ticker(ticker, start, end)
    df['RSI'] = compute_rsi(df['Adj Close'], time_window)
    fig, (ax1, ax2) = plt.subplots(2)
    fig.set_figwidth(15)
    fig.set_figheight(5)
    fig.tight_layout(pad=3.0)
    ax1.set_title("Time vs Price")
    ax1.set
    ax1.plot(df['Date'], df['Adj Close'])
    ax2.set_title('RSI chart')
    ax2.plot(df['Date'], df['RSI'])
    ax2.axhline(0, linestyle='--', alpha=0.1)
    ax2.axhline(20, linestyle='--', alpha=0.5)
    ax2.axhline(30, linestyle='--')
    ax2.axhline(70, linestyle='--')
    ax2.axhline(80, linestyle='--', alpha=0.5)
    ax2.axhline(100, linestyle='--', alpha=0.1)
    #plt.savefig(f'{ticker}_RSI.png')
    plt.show()
Пример #8
0
def get_volume(ticker, start=fourteen_days_ago, end=stock_data.today):
    data = stock_data.get_dataframe_by_ticker(ticker, start_time=fourteen_days_ago, end_time=stock_data.today)
    plt.plot(data['Date'], data['Volume'])
    plt.show()