Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Created August 17, 2021 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonymorast/e4cc6ae8706f67bd0419b2cf34eb1733 to your computer and use it in GitHub Desktop.
Save anthonymorast/e4cc6ae8706f67bd0419b2cf34eb1733 to your computer and use it in GitHub Desktop.
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