I'm building a trading bot that looks through a df of prices and sells, buys, or passes depending on the price relative to the bounds. Every transaction uses all funds available, so the other constraint is that you must have the stock in the bank to execute a sale and vice versa. Finally I want to add the relevant trade details to a log when trades occur. This is intuitive with iterrows, but is there a more efficient way to do this? Particularly I'm struggling to track the two banks and update them after transactions so future transactions only occur when we have the right denomination (assume df is sorted by date ascending). Here is a snippet of the working but slow iterrows version.
stock_bank = 1000
usd_bank = 0
trade_log = pd.DataFrame()
prices = pd.DataFrame({'buy_price': [9, 11, 12, 13, 12],
'sell_price': [10, 10, 11, 12, 12],
'lower_bound': [10, 11, 11, 10, 9],
'upper_bound': [12, 13, 13, 12, 11]})
for (i, row) in prices.iterrows():
# Sell stock if own stock and sell price above upper bound
if row['sell_price'] > row['upper_bound'] and stock_bank != 0:
usd_bank = stock_bank * (row['sell_price'])
stock_bank = 0
# trade_entry = (relevant trade details)
# trade_log = trades.append(trade_entry)
# Repeat for Buys
# Buy stock if have USD and buy price below lower bound
# etc