Skip to content

Commit

Permalink
Update trading_bot_cf.py
Browse files Browse the repository at this point in the history
Added in a check to the stock_diff function to see if all the stocks were different week to week which can potentially happen with some strategies
  • Loading branch information
robsalgado authored Dec 10, 2019
1 parent 31bfee1 commit ba77a53
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions trading_bot/trading_bot_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,8 @@ def sell_stocks(df, df_pf, sell_list, date):

# Get a list of all stocks to sell i.e. any not in the current df_buy and any diff in qty
def stock_diffs(df_sell, df_pf, df_buy):
# Select only the columns we need
df_stocks_held_prev = df_pf[['symbol', 'qty']]
df_stocks_held_curr = df_buy[['symbol', 'qty', 'date', 'close']]
df_stocks_held_curr = df_buy[['symbol', 'qty', 'close']]

# Inner merge to get the stocks that are the same week to week
df_stock_diff = pd.merge(
Expand All @@ -258,38 +257,43 @@ def stock_diffs(df_sell, df_pf, df_buy):
how='inner'
)

# Calculate any difference in positions based on the new pf
df_stock_diff['share_amt_change'] = df_stock_diff['qty_x'] - df_stock_diff['qty_y']
# Check to make sure not all of the stocks are different compared to what we have in the pf
if df_stock_diff.shape[0] > 0:
# Calculate any difference in positions based on the new pf
df_stock_diff['share_amt_change'] = df_stock_diff['qty_x'] - df_stock_diff['qty_y']

# Create df with the share difference and current closing price
df_stock_diff = df_stock_diff[[
'symbol',
'share_amt_change',
'close'
]]

# If there's less shares compared to last week for the stocks that
# are still in our portfolio, sell those shares
df_stock_diff_sale = df_stock_diff.loc[df_stock_diff['share_amt_change'] < 0]

# If there are stocks whose qty decreased,
# add the df with the stocks that dropped out of the pf
if df_stock_diff_sale.shape[0] > 0:
if df_sell is not None:
df_sell_final = pd.concat([df_sell, df_stock_diff_sale])
# Fill in NaNs in the share amount change column with
# the qty of the stocks no longer in the pf
df_sell_final['share_amt_change'] = df_sell_final['share_amt_change'].fillna(df_sell_final['qty'])
# Turn the negative numbers into positive for the order
df_sell_final['share_amt_change'] = np.abs(df_sell_final['share_amt_change'])
df_sell_final = df_sell_final.rename(columns={'share_amt_change': 'qty'})
# Create df with the share difference and current closing price
df_stock_diff = df_stock_diff[[
'symbol',
'share_amt_change',
'close'
]]

# If there's less shares compared to last week for the stocks that
# are still in our portfolio, sell those shares
df_stock_diff_sale = df_stock_diff.loc[df_stock_diff['share_amt_change'] < 0]

# If there are stocks whose qty decreased,
# add the df with the stocks that dropped out of the pf
if df_stock_diff_sale.shape[0] > 0:
if df_sell is not None:
df_sell_final = pd.concat([df_sell, df_stock_diff_sale], sort=True)
# Fill in NaNs in the share amount change column with
# the qty of the stocks no longer in the pf, then drop the qty columns
df_sell_final['share_amt_change'] = df_sell_final['share_amt_change'].fillna(df_sell_final['qty'])
df_sell_final = df_sell_final.drop(['qty'], 1)
# Turn the negative numbers into positive for the order
df_sell_final['share_amt_change'] = np.abs(df_sell_final['share_amt_change'])
df_sell_final.columns = df_sell_final.columns.str.replace('share_amt_change', 'qty')
else:
df_sell_final = df_stock_diff_sale
# Turn the negative numbers into positive for the order
df_sell_final['share_amt_change'] = np.abs(df_sell_final['share_amt_change'])
df_sell_final.columns = df_sell_final.columns.str.replace('share_amt_change', 'qty')
else:
df_sell_final = df_stock_diff_sale
# Turn the negative numbers into positive for the order
df_sell_final['share_amt_change'] = np.abs(df_sell_final['share_amt_change'])
df_sell_final = df_sell_final.rename(columns={'share_amt_change': 'qty'})
df_sell_final = None
else:
df_sell_final = None
df_sell_final = df_stocks_held_curr

return df_sell_final

Expand Down

0 comments on commit ba77a53

Please sign in to comment.