#BTC Backtesting Performance Hands-On




current_balance = 100
max_investment_timeframe = 13
interval = '4hour'
symbol = mylib.BTC

BULL = 'BULL'
BEAR = 'BEAR'
ZIGZAG = 'ZIGZAG'

invest_data = []

def get_lower_interval(data):
 if data.interval == mylib.INTERVAL_1D:
 return mylib.INTERVAL_4HOURS
 if data.interval == mylib.INTERVAL_4HOURS:
 return mylib.INTERVAL_1HOUR

def get_history_close_to(date_0, dataframe):
  index = dataframe.loc[dataframe['Date_'] >= date_0].index.values[0]
 # print('|- index of start_date = ', index, ' / ', date_0)
  history = len(dataframe) - index
 # print('|- resulting history   = ', history, ' / ', dataframe['Date_'].iloc[index])
 return int(history)
 

all_data = mylib.load_data_using_gcp(symbol, interval, history=0)
all_data.all_data_unscaled['d_1'] = (all_data.all_data_unscaled['Close'] - all_data.all_data_unscaled['Close'].shift(1)) / all_data.all_data_unscaled['Close'].shift(1)
mean_change = all_data.all_data_unscaled['d_1'].mean()
print('MEAN CHANGE = ', mean_change)

#all_data_1 = mylib.load_data_using_gcp(symbol, get_lower_interval(all_data))
#print('LENS: ', len(all_data.all_data_unscaled), len(all_data_1.all_data_unscaled))


mf = mylib.ModelFactory_v2(all_data)
# mf_1 = mylib.ModelFactory_v2(all_data_1)

hp = mylib.HarryPlotter(all_data)
# hp_1 = mylib.HarryPlotter(all_data_1)

transactions = []
investment_history_x = []
investment_history_y = []

current_history = 500
current_balance = 100

TAKE_PROFIT_POINTS = {
    mylib.SMP + mylib.INTERVAL_4HOURS: 1.005,
    mylib.BTC + mylib.INTERVAL_4HOURS: 1.02
}

CURRENT_TAKE_PROFIT_POINT = TAKE_PROFIT_POINTS[all_data.symbol + all_data.interval] 


def do_not_invest(f_0):
 global current_history
 global current_balance
 print('do_not_investst(', f_0['last_date'], ')') 
  current_history -= 13


 

def go_long(f_0, forecast_scenario):
 global current_history
 global current_balance
 global mf_1
 global hp
  true_closings = f_0['true_closings']
  true_dates = f_0['true_dates']
  quote_0 = f_0['last_quote']
  date_0 = f_0['last_date']
 print('start of go long = ', date_0, ' / ', quote_0)

  current_point = 0
  discount_factor = 0

 
  maximum_exit_point = len(true_closings)

 for current_point in range(3, maximum_exit_point - 2):
    quote_1 = true_closings[current_point]
    date_1 = true_dates[current_point]
    loss_gain = quote_1 / quote_0
    factor = loss_gain
    current_balance_snapshot = current_balance * factor
 print('\t ', current_point, ' | loss / gain @ ', date_1, ' | ', quote_0, ' -> ', quote_1, ' = ', factor, ' | current balance = ', current_balance_snapshot, '\t EXIT @ ', (2 - CURRENT_TAKE_PROFIT_POINT))
    investment_history_x.append(date_1)
    investment_history_y.append(current_balance_snapshot)

    multiply = 1
 if all_data.symbol == mylib.BTC:
      multiply = 1.6 

 if factor > multiply * CURRENT_TAKE_PROFIT_POINT:
 print('\t\t\tBAM!')
      current_point += 2
 break
 elif factor < (2 - CURRENT_TAKE_PROFIT_POINT):
 print('\t\t\tSHIT!')
      current_point = maximum_exit_point - 3
 break


  current_balance = current_balance_snapshot  
  current_history -= current_point

 return



def go_short(f_0, forecast_scenario):
 global current_history
 global current_balance
 global mf
 global hp
 global all_data
  true_closings = f_0['true_closings']
  true_dates = f_0['true_dates']
  quote_0 = f_0['last_quote']
  date_0 = f_0['last_date']
 print('start of go long = ', date_0, ' / ', quote_0)

  current_point = 0
  discount_factor = 0

 
  maximum_exit_point = len(true_closings)

  start_of_test = 3
 if all_data.symbol == mylib.BTC:
    start_of_test = 7

 for current_point in range(start_of_test, maximum_exit_point - 1):
    quote_1 = true_closings[current_point]
    date_1 = true_dates[current_point]
    loss_gain = quote_1 / quote_0
    factor = 2 - loss_gain
    current_balance_snapshot = current_balance * factor
 print('\t ', current_point, ' | loss / gain @ ', date_1, ' | ', quote_0, ' -> ', quote_1, ' = ', factor, ' | current balance = ', current_balance_snapshot, '\t EXIT @ ', (2 - CURRENT_TAKE_PROFIT_POINT))
    investment_history_x.append(date_1)
    investment_history_y.append(current_balance_snapshot)

    multiply = 1
 if all_data.symbol == mylib.BTC:
      multiply = 1.6 

 if factor > multiply * CURRENT_TAKE_PROFIT_POINT:
 print('\t\t\tBAM!')
      current_point += 2
 break
 elif factor < (2 - CURRENT_TAKE_PROFIT_POINT):
 print('\t\t\tSHIT!')
      current_point = maximum_exit_point - 3
 break


  current_balance = current_balance_snapshot  
  current_history -= current_point

 return

def do_not_invest(f_0):
 pass


while current_history > 15:
  key = all_data.symbol + all_data.interval + str(current_history)
 if key in f_cache.keys():
    f_0 = f_cache[key]
 else:
    f_0 = mf.predict_next(history=current_history)
    f_cache[key] = f_0
  date_0 = f_0['last_date']
  quote_0 = f_0['last_quote']
  dates_1ff = f_0['true_dates']
  quotes_1ff = f_0['true_closings']
  forecast, forecast_text = mylib.describe_forecast_bbz(f_0)
 print(f_0['quotes_forecast_top'])
 print(f_0['quotes_forecast_bottom'])
 print('')
 print('')
 print('------------------------------------------- ', date_0, ' / ', quote_0, ' :: ', forecast_text)
  hp.plot_forecast(f_0, dark_mode=False)

 if (forecast[0] == BULL):
    go_long(f_0, forecast)

 elif (forecast[0] == BEAR and forecast[1] != BULL):
    go_short(f_0, forecast)

 elif (forecast[0] == BEAR and forecast[1] == BULL):
    go_long(f_0, forecast)

 else:
    do_not_invest(f_0)
 
  current_history -= 2

 print('__ current_history = ', current_history, '____________________________________________')



plt.plot(investment_history_x, investment_history_y)
plt.show()