Example #1
0
async def support_resistance(market, candles):
    close_prices = [float(i['mid']['c']) for i in candles]
    data = np.array(close_prices)
    close_series = pd.Series(data)
    (minimaIdxs, pmin, mintrend,
     minwindows), (maximaIdxs, pmax, maxtrend,
                   maxwindows) = trendln.calc_support_resistance(close_series)
    fig = trendln.plot_support_resistance(close_series)
    # plt.savefig('suppres_{}_{}.svg'.format(market, str(datetime.now())), format='svg')
    # plt.show()
    plt.clf()
    return minimaIdxs, pmin, mintrend, minwindows, maximaIdxs, pmax, maxtrend, maxwindows
def drawChart(hist, update):
    mins, maxs = trendln.calc_support_resistance(hist[-1000:].close)
    minimaIdxs, pmin, mintrend, minwindows = trendln.calc_support_resistance(
        (hist[-1000:].low, None))  #support only
    mins, maxs = trendln.calc_support_resistance(
        (hist[-1000:].low, hist[-1000:].high))
    (minimaIdxs, pmin, mintrend, minwindows), (maximaIdxs, pmax, maxtrend,
                                               maxwindows) = mins, maxs
    minimaIdxs, maximaIdxs = trendln.get_extrema(hist[-1000:].close)
    maximaIdxs = trendln.get_extrema((None, hist[-1000:].high))  #maxima only
    minimaIdxs, maximaIdxs = trendln.get_extrema(
        (hist[-1000:].low, hist[-1000:].high))
    fig = trendln.plot_support_resistance(
        hist[-1000:].close)  # requires matplotlib - pip install matplotlib
    #if not update:
    #    plt.savefig('suppres.svg', format='svg')
    #    plt.show()
    #    plt.clf() #clear figure
    fig = trendln.plot_sup_res_date((hist[-1000:].low, hist[-1000:].high),
                                    hist[-1000:].index)  #requires pandas
    if not update:
        plt.show()
    curdir = '.'
 end = '2020-05-4'  #timeframe = '3mo'   #30d #1mo, 3mo, max, 1y
 hist = yf.download(
     t, start, end)  # tick.history(period=timeframe, rounding=True)
 mins, maxs = trendln.calc_support_resistance(hist[-1000:].Close)
 minimaIdxs, pmin, mintrend, minwindows = trendln.calc_support_resistance(
     (hist[-1000:].Low, None))  #support only
 mins, maxs = trendln.calc_support_resistance(
     (hist[-1000:].Low, hist[-1000:].High))
 (minimaIdxs, pmin, mintrend, minwindows), (maximaIdxs, pmax, maxtrend,
                                            maxwindows) = mins, maxs
 minimaIdxs, maximaIdxs = trendln.get_extrema(hist[-1000:].Close)
 maximaIdxs = trendln.get_extrema(
     (None, hist[-1000:].High))  #maxima only
 minimaIdxs, maximaIdxs = trendln.get_extrema(
     (hist[-1000:].Low, hist[-1000:].High))
 fig = trendln.plot_support_resistance(
     hist[-1000:].Close)  # requires matplotlib - pip install matplotlib
 t1 = t
 t = t + "_" + start + "_" + end + ".png"
 file = os.path.join(filepath, t)
 t1 = t1 + "_" + start + "_" + end + "_" + ".png"
 print("\n Volume ratio: latest day/previous day:",
       round(hist['Volume'][-1] / hist['Volume'][-2], 4), ",",
       round(hist['Volume'][-2] / hist['Volume'][-3], 4), ",",
       round(hist['Volume'][-3] / hist['Volume'][-4], 4), ",",
       round(hist['Volume'][-4] / hist['Volume'][-5], 4), ",",
       round(hist['Volume'][-5] / hist['Volume'][-6], 4))
 file1 = os.path.join(filepath, t1)
 plt.savefig(file1, format='png')
 print(t, t1)
 fig = trendln.plot_sup_res_date((hist[-1000:].Low, hist[-1000:].High),
                                 hist[-1000:].index)
import trendln
import yfinance as yf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import datetime
from pandas_datareader import data as pdr

yf.pdr_override()

stock = 'AAPL'

num_of_years = 1
start_date = datetime.datetime.now() - datetime.timedelta(
    int(365.25 * num_of_years))
end_date = datetime.datetime.now()

delta = start_date - end_date
x_data = np.arange(delta)

hist = pdr.get_data_yahoo(stock, start_date, end_date)
print(hist)

fig = trendln.plot_support_resistance(hist[-1000:].Close)
plt.show()