Skip to content

Instantly share code, notes, and snippets.

@hackhowtofaq
Forked from jericbas/file.py
Created May 23, 2022 14:53
Show Gist options
  • Save hackhowtofaq/c48768d63ab2760c6915562560fc96d8 to your computer and use it in GitHub Desktop.
Save hackhowtofaq/c48768d63ab2760c6915562560fc96d8 to your computer and use it in GitHub Desktop.
Chandelier Exit in Python
def avg_true_range(self, df):
ind = range(0,len(df))
indexlist = list(ind)
df.index = indexlist
for index, row in df.iterrows():
if index != 0:
tr1 = row["High"] - row["Low"]
tr2 = abs(row["High"] - df.iloc[index-1]["Close"])
tr3 = abs(row["Low"] - df.iloc[index-1]["Close"])
true_range = max(tr1, tr2, tr3)
df.set_value(index,"True Range", true_range)
df["Avg TR"] = df["True Range"].rolling(min_periods=14, window=14, center=False).mean()
return df
def chandelier_exit(self, df): # default period is 22
df_tr = self.avg_true_range(df)
rolling_high = df_tr["High"][-22:].max()
rolling_low = df_tr["Low"][-22:].max()
chandelier_long = rolling_high - df_tr.iloc[-1]["Avg TR"] * 3
chandelier_short = rolling_low - df_tr.iloc[-1]["Avg TR"] * 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment