Last active
September 1, 2024 12:07
-
-
Save yongghongg/ec89ada5709abe574ea6fe93a98c1a94 to your computer and use it in GitHub Desktop.
Revisions
-
yongghongg revised this gist
Jul 3, 2021 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -46,14 +46,14 @@ # buying window for long position: # 1. black cross becomes gray (the squeeze is released) long_cond1 = (df['squeeze_off'][-2] == False) & (df['squeeze_off'][-1] == True) # 2. bar value is positive => the bar is light green k long_cond2 = df['value'][-1] > 0 enter_long = long_cond1 and long_cond2 # buying window for short position: # 1. black cross becomes gray (the squeeze is released) short_cond1 = (df['squeeze_off'][-2] == False) & (df['squeeze_off'][-1] == True) # 2. bar value is negative => the bar is light red short_cond2 = df['value'][-1] < 0 enter_short = short_cond1 and short_cond2 -
yongghongg revised this gist
Jul 3, 2021 . 1 changed file with 10 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -45,8 +45,15 @@ df['squeeze_off'] = (df['lower_BB'] < df['lower_KC']) & (df['upper_BB'] > df['upper_KC']) # buying window for long position: # 1. black cross becomes gray (the squeeze is released) buy_cond1 = (df['squeeze_off'][-2] == False) & (df['squeeze_off'][-1] == True) # 2. bar value is positive => the bar is light green buy_cond2 = df['value'][-1] > 0 buy_long = buy_cond1 and buy_cond2 # buying window for short position: # 1. black cross becomes gray (the squeeze is released) buy_cond1 = (df['squeeze_off'][-2] == False) & (df['squeeze_off'][-1] == True) # 2. bar value is negative => the bar is light red buy_cond2 = df['value'][-1] < 0 buy_short = buy_cond1 and buy_cond2 -
yongghongg revised this gist
Jul 3, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import numpy as np import math # get stock prices df = yf.download('AAPL', start='2020-01-01', threads= False) # parameter setup -
yongghongg created this gist
Jul 3, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ # import required libraries import pandas as pd import yfinance as yf import numpy as np import math # calculation df = yf.download('AAPL', start='2020-01-01', threads= False) # parameter setup length = 20 mult = 2 length_KC = 20 mult_KC = 1.5 # calculate BB m_avg = df['Close'].rolling(window=length).mean() m_std = df['Close'].rolling(window=length).std(ddof=0) df['upper_BB'] = m_avg + mult * m_std df['lower_BB'] = m_avg - mult * m_std # calculate true range df['tr0'] = abs(df["High"] - df["Low"]) df['tr1'] = abs(df["High"] - df["Close"].shift()) df['tr2'] = abs(df["Low"] - df["Close"].shift()) df['tr'] = df[['tr0', 'tr1', 'tr2']].max(axis=1) # calculate KC range_ma = df['tr'].rolling(window=length_KC).mean() df['upper_KC'] = m_avg + range_ma * mult_KC df['lower_KC'] = m_avg - range_ma * mult_KC # calculate bar value highest = df['High'].rolling(window = length_KC).max() lowest = df['Low'].rolling(window = length_KC).min() m1 = (highest + lowest)/2 df['value'] = (df['Close'] - (m1 + m_avg)/2) fit_y = np.array(range(0,length_KC)) df['value'] = df['value'].rolling(window = length_KC).apply(lambda x: np.polyfit(fit_y, x, 1)[0] * (length_KC-1) + np.polyfit(fit_y, x, 1)[1], raw=True) # check for 'squeeze' df['squeeze_on'] = (df['lower_BB'] > df['lower_KC']) & (df['upper_BB'] < df['upper_KC']) df['squeeze_off'] = (df['lower_BB'] < df['lower_KC']) & (df['upper_BB'] > df['upper_KC']) # buying window for long position: # 1. black cross becomes gray buy_cond1 = (df['squeeze_off'][-2] == False) & (df['squeeze_off'][-1] == True) # 2. bar value is positive => the bar is light green buy_cond2 = df['value'][-1] >= 0 buy = buy_cond1 and buy_cond2