Learn practical skills, build real-world projects, and advance your career
import pandas as pd
INPUT_PATH = "BTCUSDT4h_24months_06-12-2020T0452/Binance_Candles_BTCUSDT4h_24months_06-12-2020T0452.csv"
OUTPUT_PATH = "BTCUSDT4h_24months_06-12-2020T0452/ATR_Renko_withEMA_4h_24months_06-12-2020T0452.csv"

def calculate_renko_atr_with_ema (INPUT_PATH, OUTPUT_PATH, ATR, EMA_TYPE):
    
    # INPUT PATH gives us the binance candles price data
    
    EMA_SPAN = 20
    df = pd.read_csv(INPUT_PATH)
    df['close_0'] = df['close'].shift(periods=1)
    df['diff'] = df["high"] - df['close']
    df['abs_high'] = df["high"] - df['close_0'].abs()
    df['abs_low'] = df["low"] - df['close_0'].abs()
    
    # calculating The true range
    df["TR"] = df[['diff', 'abs_high', 'abs_low']].max(axis=1)
    
    # calculating the ATR (dynamic)
    df["ATR"] = df["TR"].ewm(alpha=1/14, adjust=False).mean()
    
    renko_price = 6300
    prev_renko = 2
    
    # Make Columns
    datetime_column = []
    renko_column = []
    atr_column = []
    tr_column = []
    close_price_column = []
    indicator_column = []

    df2 = pd.DataFrame()

    for i in range(1, len(df["ATR"])):
        cur_row = df.iloc[i]
        prev_row = df.iloc[i - 1]
        
        # either we set a fixed atr or we use the dynamically calculated
        if ATR == "Variable":
            atr = cur_row["ATR"]
        else:
            atr = ATR
        tr = cur_row["TR"]
        difference = cur_row['close'] - renko_price
        sign = difference / abs(difference)
        indicator = 0
        
        breakLoop = False
        
        # We enter the while loop to create multiple rows of a given record (each row with a datetime key) 
        # This is to replicate the binance data where you can have multilpe rows getting formed for the same datetime
        # It happens when the multiple renko blocks are formed in the same time window. Like taking an example of a candlestick data 
        # with a close price of 9000. if the previous renko block was formed at 10000$, then we will create 10 new Renko blocks 
        # in the same time window.
        
        while (abs(difference) >= atr and breakLoop is False):
            # This loop creates multiple records of the same datetime index

            # difference is either less than -100 (or whatever the ATR value is dynamic or fixed) or more than 100
            if difference >= atr:
                if prev_renko == 1:
                    renko_price += atr
                    prev_renko = 1
                    indicator = 1
                elif prev_renko == 2 and difference >= 2*atr:
                    renko_price += 2*atr
                    prev_renko = 1
                    indicator = 1
                else:
                    breakLoop = True

            # difference is either less than -100 (or whatever the ATR value is dynamic or fixed) or more than 100
            elif difference <= -atr:
                if prev_renko == 2:
                    renko_price -= atr
                    prev_renko = 2
                    indicator = 2
                elif prev_renko == 1 and difference <= -2*atr:
                    renko_price -= 2*atr
                    prev_renko = 2
                    indicator = 2
                else:
                    breakLoop = True

                
            # For the traditional Renko chart we append the indicator received from row depending on the Renko block formed (1 or 2) or
            # or 0 if no Renko block was formed. We append this decision to the indicator column
            if breakLoop is False:
                datetime_column.append(cur_row['datetime'])
                close_price_column.append(cur_row['close'])
                atr_column.append(atr)
                tr_column.append(tr)
                renko_column.append(renko_price)
                indicator_column.append(indicator)
                difference = cur_row['close'] - renko_price
            
            # exiting iteration of the while loop
    
    # After while loop completes execution
    df2['datetime'] = datetime_column
    df2['close'] = close_price_column
    df2['atr'] = atr_column
    df2['tr'] = tr_column
    df2['renko_price'] = renko_column
    df2['renko_indicator'] = indicator_column
    
    # Calculating the EMA line using the renko prices we have calculated over the entire dataset
    df2['ema'] = df2['renko_price'].ewm(span=EMA_SPAN, adjust=False).mean()
    
    # Creating indicator column on the basis of renko
    # df2['indicator'] = indicator_column
    
    # creating new indicators (if renko price is more than the EMA line) then buy else sell decision
    # Note: This line overwrites our previous renko chart based calculations for the indicators
    df2['indicator'] = df2.apply(lambda row: 1 if row[EMA_TYPE] > row['ema'] else 2, axis=1)
    
    df2.to_csv(OUTPUT_PATH)
    return df
# calculate_renko_atr_ema_on_renko(INPUT_PATH, OUTPUT_PATH)
import jovian
jovian.commit(project="BitBotBoom_Simulation_atr_renko_ema_on_renko_calculation", environment=None, filename="/home/ec2-user/SageMaker/Renko Simulation/atr_renko_ema_on_renko_calculation.ipynb")
[jovian] Attempting to save notebook.. [jovian] Creating a new project "aman/BitBotBoom_Simulation_atr_renko_ema_on_renko_calculation" [jovian] Uploading notebook.. [jovian] Committed successfully! https://jovian.ml/aman/bitbotboom-simulation-atr-renko-ema-on-renko-calculation