Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Created Aug 17, 2021
Embed
What would you like to do?
def get_buy_sell_points(data):
# iterate the dataset, check for cross above upper and below lower bb
trades = {
"buy_indices": [],
"sell_indices": [],
"sell_prices": [],
"buy_prices": []
}
for idx, row in data.iterrows():
if idx == 0:
continue
# above upper band and previously below upper band
if row["upper"] < row["Close"] and row["upper"] > data.loc[idx-1]["Close"]:
# no short selling, just sell the shares previously purchased
if len(trades["buy_indices"]) == 0:
continue # we don't own any shares here
elif len(trades["sell_indices"]) > 0 and trades["buy_indices"][len(trades["buy_indices"]) - 1] < trades["sell_indices"][len(trades["sell_indices"]) - 1]:
# have we bought since the last sell?
continue
trades["sell_indices"].append(idx) # indicate a sell at this point
trades["sell_prices"].append(row["Close"]) # record the price
# below lower band
if row["lower"] > row["Close"] and data.loc[idx-1]["lower"] < data.loc[idx-1]["Close"]:
trades["buy_indices"].append(idx) # buy at this point
trades["buy_prices"].append(row["Close"]) # record the price
return trades
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment