Created
September 8, 2021 17:19
-
-
Save SajidLhessani/452b18dcfbb4af5ffd3f3dd9bf8a15ac to your computer and use it in GitHub Desktop.
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
| # Create a column by name, SMA and assign the SMA calculation to it | |
| df['SMA'] = df['Close'].shift(1).rolling(window=n).mean() | |
| # Create a column by name, Corr and assign the calculation of correlation to it | |
| df['Corr'] = df['Close'].shift(1).rolling(window=n).corr(df['SMA'].shift(1)) | |
| # Create a column by name, SAR and assign the SAR calculation to it | |
| df['SAR'] = ta.SAR(np.array(df['High'].shift(1)), np.array(df['Low'].shift(1)), | |
| 0.2, 0.2) | |
| # Create a column by name, ADX and assign the ADX calculation to it | |
| df['ADX'] = ta.ADX(np.array(df['High'].shift(1)), np.array(df['Low'].shift(1)), | |
| np.array(df['Open']), timeperiod=n) | |
| # Create columns high, low and close with previous minute's OHLC data | |
| df['Prev_High'] = df['High'].shift(1) | |
| df['Prev_Low'] = df['Low'].shift(1) | |
| df['Prev_Close'] = df['Close'].shift(1) | |
| # Create columns 'OO' with the difference between the current minute's open and last minute's open | |
| df['OO'] = df['Open']-df['Open'].shift(1) | |
| # Create columns 'OC' with the difference between the current minute's open and last minute's close | |
| df['OC'] = df['Open']-df['Prev_Close'] | |
| # Create a column 'Ret' with the calculation of returns | |
| df['Ret'] = (df['Open'].shift(-1)-df['Open'])/df['Open'] | |
| # Create n columns and assign | |
| for i in range(1, n): | |
| df['return%i' % i] = df['Ret'].shift(i) | |
| # Change the value of 'Corr' to -1 if it is less than -1 | |
| df.loc[df['Corr'] < -1, 'Corr'] = -1 | |
| # Change the value of 'Corr' to 1 if it is greater than 1 | |
| df.loc[df['Corr'] > 1, 'Corr'] = 1 | |
| # Drop the NaN values | |
| df = df.dropna() | |
| # Create a variable split that stores 80% of the length of the dataframe | |
| t = .8 | |
| split = int(t*len(df)) | |
| split | |
| # Ignore warnings | |
| import warnings | |
| warnings.filterwarnings("ignore") | |
| # Create a column by name, 'Signal' and initialize with 0 | |
| df['Signal'] = 0 | |
| # Assign a value of 1 to 'Signal' column for the quantile with the highest returns | |
| df.loc[df['Ret'] > df['Ret'][:split].quantile(q=0.66), 'Signal'] = 1 | |
| # Assign a value of -1 to 'Signal' column for the quantile with the lowest returns | |
| df.loc[df['Ret'] < df['Ret'][:split].quantile(q=0.34), 'Signal'] = -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment