Learn practical skills, build real-world projects, and advance your career
import pandas as pd

INPUT_PATH = "BTCUSDT_Strategy_8h_24months_0604/ATR_Renko_BTCUSDT8h_24months_06-04-2020T0143.csv"
OUTPUT_PATH = "BTCUSDT_Strategy_8h_24months_0604/TradingSimulation_BTCUSDT8h_24months_06-04-2020T0143.csv"

class Simulation:
    def __init__ (self, INPUT_PATH, OUTPUT_PATH):
        self.input = INPUT_PATH
        self.output = OUTPUT_PATH
    
    def round_down(self, value, decimals):
        factor = 1 / (10 ** decimals)
        return (value // factor) * factor

    def trading(self, test, money, bitcoin, bnb):
        treasure = pd.DataFrame()
        record = {}

        A = []
        B = []
        C = []
        D = []
        E = []
        F = []
        G = []
        H = []
        I = []
        J = []
        K = []
        L = []
        
        commission_rate = 0.00075
        test.reset_index(drop=True, inplace=True)
        commission = 0
        EXCHANGE_PRICE = 'close'  # close
        
        for i in range(len(test)):
            # set the transaction rule
            # set the rule of buy bitcoin
            if (test['indicator'][i] == 1 and money > 0):
                commission = money * commission_rate
                bnb -= (money * commission_rate) / test['bnb_price'][i]
                bitcoin_exchange = self.round_down((money - commission)/ test[EXCHANGE_PRICE][i], 8)
                bitcoin += bitcoin_exchange
                money -= bitcoin_exchange * test[EXCHANGE_PRICE][i] + commission
                record[test['datetime'][i]] = 1
                
            # set the rule of sell bitcoin
            elif (test['indicator'][i] == 2 and bitcoin > 0):
                # each extraction opertion the system would charge commission fee
                commission = bitcoin * test[EXCHANGE_PRICE][i] * commission_rate
                bnb -= (bitcoin * test[EXCHANGE_PRICE][i] * commission_rate) / test['bnb_price'][i]
                money += bitcoin * test[EXCHANGE_PRICE][i] - commission
                bitcoin = 0
                record[test['datetime'][i]] = 2
            
            A.append(test['datetime'][i]);
            B.append(money)
            C.append(bitcoin)
            D.append(bitcoin * test[EXCHANGE_PRICE][i] + money - commission)
            E.append(test['indicator'][i])
            F.append(test[EXCHANGE_PRICE][i])
            G.append(bnb)
            H.append(test["atr"][i])
            I.append(test["tr"][i])
            J.append(test["ema"][i])
            K.append(test["renko_indicator"][i])
            L.append(commission)

        treasure['datetime'] = A
        treasure['renko'] = test['renko_price']
        treasure['close'] = F
        treasure['indicator'] = E
        treasure['bitcoin'] = C
        treasure['money'] = B
        treasure['total'] = D
        treasure['bnb'] = G
        treasure['commission'] = L
        treasure['atr'] = H
        treasure['tr'] = I
        treasure['ema'] = J
        treasure['renko_indicator'] = K        
        return treasure

    def run_bot(self):
        money = 10000
        bitcoin = 0
        bnb = 0

        initial_data = pd.read_csv(self.input)
        trading_record = self.trading(initial_data, money, bitcoin, bnb)
        trading_record.to_csv(self.output)

        return trading_record
# s = Simulation(INPUT_PATH, OUTPUT_PATH)
# s.run_bot()
import jovian
jovian.commit(project="BitBotBoom_Simulation_Using_csv", environment=None, filename="/home/ec2-user/SageMaker/Renko Simulation/simulation_using_csv.ipynb", message="Start sim with $10k and commission charged on outside investment")
[jovian] Attempting to save notebook.. [jovian] Updating notebook "aman/bitbotboom-simulation-using-csv" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Committed successfully! https://jovian.ml/aman/bitbotboom-simulation-using-csv