def data_frame(data, size, period1, period2):
    df = pd.DataFrame(data)
    df['date'], df['time'] = df['date'].dt.date, df['date'].dt.strftime('%H:%M:%S')
    df = Renko(df)
    df.brick_size = size
    data = df.get_bricks()
    data['MA4'] = data['close'].rolling(window=period1).mean()
    data['MA10'] = data['close'].rolling(window=period2).mean()
    return data
Beispiel #2
0
def GetRenko(DF, n):
    df = DF.copy()
    df.reset_index(inplace=True)
    df = df.iloc[:, [0, 1, 2, 3, 5, 6]]
    df.columns = ["date", "open", "high", "low", "close", "volume"]

    renko_df = Renko(df)
    renko_df.brick_size = round(ATR(DF, 120)['ATR'][-1], 0)

    df2 = renko_df.get_bricks()

    return df2
Beispiel #3
0
def renko_DF(DF):
    "function to convert ohlc data into renko bricks"
    df = DF.copy()
    df.reset_index(inplace=True)
    df = df.iloc[:, [0, 1, 2, 3, 5, 6]]
    df.rename(columns={"Date": "date", "High": "high", "Low": "low",
                       "Open": "open", "Adj Close": "close", "Volume": "volume"}, inplace=True)
    df2 = Renko(df)
    df2.brick_size = round(ATR(DF, 120)["ATR"][-1], 0)
    # if get_bricks() does not work try using get_ohlc_data() instead
    renko_df = df2.get_bricks()
    #renko_df = df2.get_ohlc_data()
    return renko_df
Beispiel #4
0
def renko_DF(DF):
    "function to convert ohlc data into renko bricks"
    df = DF.copy()
    df.reset_index(inplace=True)
    df = df.iloc[:, [0, 1, 2, 3, 4, 5]]
    df.columns = ["date", "open", "high", "low", "close", "volume"]
    df2 = Renko(df)
    df2.brick_size = max(0.5, round(ATR(DF, 120)["ATR"][-1], 0))
    renko_df = df2.get_bricks()
    renko_df["bar_num"] = np.where(renko_df["uptrend"] == True, 1, np.where(renko_df["uptrend"] == False, -1, 0))
    for i in range(1, len(renko_df["bar_num"])):
        if renko_df["bar_num"][i] > 0 and renko_df["bar_num"][i - 1] > 0:
            renko_df["bar_num"][i] += renko_df["bar_num"][i - 1]
        elif renko_df["bar_num"][i] < 0 and renko_df["bar_num"][i - 1] < 0:
            renko_df["bar_num"][i] += renko_df["bar_num"][i - 1]
    renko_df.drop_duplicates(subset="date", keep="last", inplace=True)
    return renko_df
Beispiel #5
0
 def renko_bars(self, DF, n, instrument):
     df = DF.copy()
     df2 = Renko(df)
     real = talib.ATR(df["high"], df["low"], df["close"], timeperiod = n)
     brick_sizing = round(real[len(real) - 1], 5)
     print("BRICK_SIZE: ", brick_sizing)
     df2.brick_size = brick_sizing
     renko_df = df2.get_bricks()
     renko_df["bar_num"] = np\
         .where(renko_df["uptrend"]==True,1,np\
             .where(renko_df["uptrend"]==False,-1,0))
     for i in range(1,len(renko_df["bar_num"])):
         if renko_df["bar_num"][i]>0 and renko_df["bar_num"][i-1]>0:
             renko_df["bar_num"][i]+=renko_df["bar_num"][i-1]
         elif renko_df["bar_num"][i]<0 and renko_df["bar_num"][i-1]<0:
             renko_df["bar_num"][i]+=renko_df["bar_num"][i-1]
     renko_df.drop_duplicates(subset="date",keep="last",inplace=True)
     return renko_df, brick_sizing
Beispiel #6
0
def renko_DF(DF):
    df = DF.copy()
    df2 = Renko(df)
    "Using ATR as renko brick size"
    real = talib.ATR(df["high"], df["low"], df["close"], timeperiod=14)
    brick_sizing = round(real[len(real) - 1], 6)
    df2.brick_size = brick_sizing
    renko_df = df2.get_bricks()
    renko_df["bar_num"] = np\
        .where(renko_df["uptrend"]==True,1,np\
            .where(renko_df["uptrend"]==False,-1,0))
    for i in range(1, len(renko_df["bar_num"])):
        if renko_df["bar_num"][i] > 0 and renko_df["bar_num"][i - 1] > 0:
            renko_df["bar_num"][i] += renko_df["bar_num"][i - 1]
        elif renko_df["bar_num"][i] < 0 and renko_df["bar_num"][i - 1] < 0:
            renko_df["bar_num"][i] += renko_df["bar_num"][i - 1]
    renko_df.drop_duplicates(subset="date", keep="last", inplace=True)
    return renko_df
def renko(o, h, l, c, v):
    '''
    Computes Ranko values for the given price series
    
    Parameters
    ==========
    o: pandas.Series(): Open price
    h: pandas.Series(): High price
    l: pandas.Series(): Low  price
    c: pandas.Series(): Closeprice
    v: pandas.Series(): Volume
    
    Returns
    =======
    renko_df: pandas.DataFrame()
    '''
    df = pd.concat([
        copy.deepcopy(o),
        copy.deepcopy(h),
        copy.deepcopy(l),
        copy.deepcopy(c),
        copy.deepcopy(v)
    ],
                   axis=1)
    df.reset_index(inplace=True)
    df.columns = ['date', 'open', 'high', 'low', 'close', 'volume']
    df2 = Renko(df)
    renko_df = df2.get_bricks()
    renko_df['bar_num'] = np.where(
        renko_df['uptrend'] == True, 1,
        np.where(renko_df['uptrend'] == False, -1, 0))
    for i in range(1, len(renko_df['bar_num'])):
        if renko_df['bar_num'][i] > 0 & renko_df['bar_num'][i - 1] > 0:
            renko_df['bar_num'][i] += renko_df['bar_num'][i - 1]
        elif renko_df['bar_num'][i] < 0 & renko_df['bar_num'][i - 1] < 0:
            renko_df['bar_num'][i] += renko_df['bar_num'][i - 1]
    renko_df.drop_duplicates(subset='Date', keep='last', inplace=True)
    return renko_df
Beispiel #8
0
import numpy as np
import pandas as pd
import nsepy
from stocktrends import Renko


if len(sys.argv) > 1:
    fname = sys.argv[1]
    print('Reading local file {}'.format(fname))
    df = pd.read_csv(sys.argv[1])
else:
    symbol='SBIN'
    print('Downloading {} data from nsepy'.format(symbol))
    df = nsepy.get_history(
        symbol=symbol,
        start=dt.date(2017,1,1),
        end=dt.date(2018,1,19)
    )
    if df.empty:
        print('No data is received from nsepy.')
        sys.exit()

debug = True
# print(df.tail(20))

renko = Renko(df)
renko.brick_size = 4
r = renko.get_bricks()
print(r.tail(33))