Skip to content

Instantly share code, notes, and snippets.

@sam-thecoder
Created August 30, 2018 05:58
Show Gist options
  • Save sam-thecoder/241cd6e4ee7009906445c8c8e6436a05 to your computer and use it in GitHub Desktop.
Save sam-thecoder/241cd6e4ee7009906445c8c8e6436a05 to your computer and use it in GitHub Desktop.
def player_3(start_date=None, sleep=False, sleep_time=1, plot=False, output=True, miss_output=False, miss_plot=False, kind='line'):
user_key, day_info, amount, invested, profit, loss, game_active = stock_game(start_date=start_date)
miss_colors = ['g']
while game_active == False:
predict = clf.predict(day_info[['Value USD', 'Max 7', 'Min 7', 'Change', 'Mean Change 7', 'Drop 7', 'Up 7']].values.reshape(1, -1))[0]
state = abs(day_info['Predict'])
if state == 0:
state = 1
if state == 1:
state /= 8
else:
state /= 4
if invested == 0:
user_key, day_info, amount, invested, profit, loss, game_active = stock_game(user_key=user_key, invest_amount=amount/2)
elif predict == 1:
user_key, day_info, amount, invested, profit, loss, game_active = stock_game(user_key=user_key, invest_amount=amount*state)
else:
user_key, day_info, amount, invested, profit, loss, game_active = stock_game(user_key=user_key, withdraw_amount=invested*state)
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]))
if miss_plot:
low_ball = day_info['Predict'] < 0
high_ball = day_info['Predict'] > 0
if low_ball and profit > 0 or high_ball and loss > 0:
miss_colors.append('r')
else:
miss_colors.append('g')
if miss_output:
low_ball = day_info['Predict'] < 0
high_ball = day_info['Predict'] > 0
if low_ball and profit > 0:
print('profit day: {0} profit: {1} loss: {2} amount: {3} invested: {4} predict: {5}'.format(day_info[0], profit, loss, amount, invested, day_info[-1]))
elif high_ball and loss > 0:
print('loss 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:
if start_date:
result = df[df['Date'] == start_date].index.tolist()
if result:
start_index = result[0]
else:
start_index = 0
else:
start_index = 0
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)]
miss_colors.append('g')
if miss_plot:
#make plot bigger
plt.rcParams['figure.figsize'] = (40,20)
index = []
count = 0
for color in miss_colors:
if color == 'r':
index.append(count)
count+=1
#colors = ['r']*miss_colors.count('r')
#df_copy.plot(kind='scatter', x=df_copy.index.tolist(), y='amount', figsize=(20, 20), colormap=miss_colors)
#plt.plot(x=df_copy.index, y=df_copy['Value USD'])
plt.scatter(x=df_copy.iloc[index].index, y=df_copy.iloc[index]['Value USD'], c='r', s=80)
plt.plot(df_copy.index, df_copy['Value USD'], 'g')
plt.show()
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
if kind == 'scatter':
df_copy.reset_index().plot(kind=kind, x='index', y=['investment', 'amount', 'Value USD'], figsize=(20, 20))
else:
df_copy.plot(kind=kind, 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