Skip to content

Instantly share code, notes, and snippets.

@sam-thecoder
Created August 7, 2018 09:37
Show Gist options
  • Save sam-thecoder/0fd67534c7cdd38d5c9e6766735669ee to your computer and use it in GitHub Desktop.
Save sam-thecoder/0fd67534c7cdd38d5c9e6766735669ee to your computer and use it in GitHub Desktop.
def player_2(start_index=0, sleep=False, sleep_time=1, plot=False, output=True, miss_output=False, miss_plot=False, show_exception=True):
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(start_index=start_index)
peak_price = day_info[1]
min_price = day_info[4]
max_price = day_info[5]
mean_price = day_info[6]
invest = None
max_profit = invested
total_profit = 0
max_profit = 0
week_counter = 0
week_predictions = [day_info[-1]]
dys_with_loss = 0
dys_with_profit = 0
hold = True
withdrawn = False
while game_active == False:
if invest == None:
invest = initial_investment = amount/2
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, invest_amount=invest)
if game_active == True:
break
elif hold == True:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key)
if game_active == True:
break
elif hold == False:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, invest_amount=amount/4)
if game_active == True:
break
if day_info[1] > peak_price:
peak_price = day_info[1]
if profit > 0:
dys_with_profit += 1
dys_with_loss = 0
elif loss > 0:
dys_with_profit = 0
dys_with_loss += 1
if invested > max_profit:
max_profit = invested
amount_lost = 0
hold = False
else:
amount_lost = max_profit - invested
hold = True
total_profit += (profit - loss)
if total_profit > max_profit:
max_profit = total_profit
count_dips = len([x for x in week_predictions if x < 0])
count_ups = len([x for x in week_predictions if x >= 0])
loss_25 = amount_lost > (initial_investment/4)
loss_10 = amount_lost > (initial_investment/10)
if loss_25:
#withdraw 25%
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, withdraw_amount=invested/4)
if game_active == True:
break
max_profit = invested
withdrawn = True
elif withdrawn and loss_10 and dys_with_profit > 3:
#invest 25%
withdrawn = False
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, invest_amount=amount/4)
if game_active == True:
break
if count_ups > count_dips and dys_with_profit > 3:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, invest_amount=amount/4)
if game_active == True:
break
elif count_ups > count_dips and dys_with_profit > 0:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, invest_amount=amount/10)
if game_active == True:
break
elif count_dips < count_ups and dys_with_loss > 3:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, withdraw_amount=invested/4)
if game_active == True:
break
elif count_dips < count_ups and dys_with_loss > 0:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, withdraw_amount=invested/10)
if game_active == True:
break
elif count_dips == count_ups and dys_with_profit > 3:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, invest_amount=amount/4)
if game_active == True:
break
elif count_ups == count_dips and dys_with_profit >0:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, invest_amount=amount/10)
if game_active == True:
break
elif count_ups == count_dips and dys_with_profit == 0 and dys_with_loss == 0:
pass
elif count_ups > count_dips and dys_with_profit == 0 and dys_with_loss == 0:
pass
elif count_ups < count_dips and dys_with_profit > 0:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, invest_amount=amount/10)
if game_active == True:
break
elif count_ups == count_dips and dys_with_loss > 0:
pass
elif count_ups < count_dips and dys_with_loss > 0:
user_key, day_info, amount, invested, profit, loss, game_active = bitcoin_game(user_key=user_key, withdraw_amount=invested/10)
if game_active == True:
break
elif show_exception: #this is an exception that needs to be classified
print('Ups in Week', count_ups, 'Downs in week', count_dips, 'Days with Profit', dys_with_profit, 'Days with Loss', dys_with_loss)
week_counter += 1
if week_counter == 7:
week_counter = 0
week_predictions = []
else:
#add last prediction to week predictions
week_predictions.append(day_info[-1])
if output:
print('day: {0} profit: {1} loss: {2} amount: {3} invested: {4} predict: {5}'.format(day_info[0], profit, loss, amount, invested, day_info[-1]))
#so I can easily assess what's going on
if sleep:
time.sleep(sleep_time)
if not output: #only one output
print('day: {0} profit: {1} loss: {2} amount: {3} invested: {4}'.format(day_info[0], profit, loss, amount, invested))
if plot or miss_plot:
df_copy = df[df.index >= start_index].copy()
df_copy['time'] = pd.to_datetime(df_copy['Date'], format='%m/%d/%Y')
player_record = game_record['player_{0}'.format(user_key)]
if plot:
try:
#the len seems to be off by 3
extended_invest = player_record['invest_history']
extended_invest.extend([extended_invest[-1]]*3)
extended_amount = player_record['amount_history']
extended_amount.extend([extended_amount[-1]]*2)
df_copy['investment'] = extended_invest
df_copy['amount'] = extended_amount
df_copy.plot(x='time', y=['investment', 'amount', 'Value USD'], figsize=(20, 20))
except Exception as e:
print(str(e))
print('df copy shape', df_copy.shape)
print('investment len', len(extended_invest))
print('amount len', len(extended_amount))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment