Created
July 10, 2020 09:49
-
-
Save jericbas/115662754636d6401a7589776bf2a84e to your computer and use it in GitHub Desktop.
Chandelier Exit in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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