-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic Pair Trading Study Using Linear Regression #6
base: main
Are you sure you want to change the base?
Basic Pair Trading Study Using Linear Regression #6
Conversation
As a general comment, we should upload every file and replace absolute paths with local paths when reading data and importing functions. I've just uploaded data files to main branch so please try to replace absolute paths. I've uploaded historical data as well, so we should fit Linear Regression with that data. |
Ok I have uploaded all the changes, there are some details to fix, I have marked them down via comments in Caps. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work here, just a few minor comments. Also, could you please reset my doc comments? :)
streamPair.q
Outdated
// We calculate spreads for linear regression | ||
// WE SHOULD IMPLEMENT HERE RATIO OF RETURN SO WE CAN CALCULATE EWMA | ||
s: priceY[.streamPair.i][`bid] - ((priceX[.streamPair.i][`bid] * betaF[px;py])-alphaF[px;py]); // I NEED TO CALCULATE BETA AND ALPHA AGAIN I THINK IT HAS TO DO WITH THE LOCAL SCOPE MINOR DETAIL | ||
resSpread: enlist `dateTime`spread`mean`up`low`operation!("p"$(priceX[.streamPair.i][`dateTime]);"f"$(s);"f"$(0);"f"$(0);"f"$(0);"f"$(0)); // MEAN AND STD FROM streamPair.i#.streamPair.spreads ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mean, up, low and operation are always 0, why? I think you left something here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didnt know if the mean is the average of the already calculated spreads or from the historical data, same with standard deviation, and operation I need to implement it yet (I guess we use the same logic from python)
df['buy'] = df['spread'][((df['spread'] < df['lower']) & (df['spread'].shift(1) > df['lower']) | (df['spread'] < df['mean']) & (df['spread'].shift(1) > df['mean']))]
df['sell'] = df['spread'][((df['spread'] > df['upper']) & (df['spread'].shift(1) < df['upper']) | (df['spread'] > df['mean']) & (df['spread'].shift(1) < df['mean']))]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I have some doubts here. However, I believe trading signals should also be calculated using historical data. So, you would calculate spreads over the historical dataset (using the same linear regression) and then calculate the mean and standard deviation for those historical spreads. Does that make sense?
https://github.com/hablapps/pairstrading into 2-basic-study-of-pair-trading-using-linear-regression
Fixed EWMA values, now Spreads table has up and up2 columns refered to static std and moving std respectively, and also added an ewma column for ewma deviations values
Take away some innecesary lines
Overview
This pull request addresses the implementation of a basic study on pair trading using linear regression to identify and evaluate potential trading pairs.
Solves: /issues/2
Changes Made
linear_regression.q
that implements linear regression analysis on historical price data to identify correlated stock pairs.