You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04 Strategy Library/02 Combining Mean Reversion and Momentum in Forex Market/03 Method.html
+71-68Lines changed: 71 additions & 68 deletions
Original file line number
Diff line number
Diff line change
@@ -4,31 +4,32 @@
4
4
5
5
<h3>Step 1: Request Historical Data</h3>
6
6
<p>
7
-
The first function takes two arguments: symbol and number of daily data points requested. This function requests historical <ahref="https://www.quantconnect.com/docs#Consolidating-Data-TradeBars-vs-QuoteBars">QuoteBars</a> and builds it into a pandas DataFrame. For more information about pandas DataFrame, please refer to the help documentation <ahref="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html">DataFrame</a>. The calculate_return function takes a DataFrame as an argument to calculate the mean and standard deviation of the log prices, and create new columns for the DataFrame (return, reversal factor and momentum) - it prepares the DataFrame for multiple linear regression.
7
+
The first function takes two arguments: symbol and number of daily data points requested. This function requests historical <ahref="https://www.quantconnect.com/docs#Consolidating-Data-TradeBars-vs-QuoteBars">QuoteBars</a> and builds it into a pandas DataFrame. For more information about pandas DataFrame, please refer to the help documentation <ahref="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html">DataFrame</a>. The <code>_calculate_return</code>function takes a DataFrame as an argument to calculate the mean and standard deviation of the log prices, and create new columns for the DataFrame (return, reversal factor and momentum) - it prepares the DataFrame for multiple linear regression.
The concat function requests history and joins the results into a single DataFrame. As \(\mu \) varies by country so we assign the mean and standard deviation to the symbol for each currency for future use. The OLS function takes the resulting DataFrame to conduct an OLS regression. We write it into a function because it's easier to change the formula here if we need.
49
+
The <code>_concat</code> function requests history and joins the results into a single DataFrame. As \(\mu \) varies by country so we assign the mean and standard deviation to the symbol for each currency for future use. The OLS function takes the resulting DataFrame to conduct an OLS regression. We write it into a function because it's easier to change the formula here if we need.
49
50
</p>
50
51
51
52
<divclass="section-example-container">
52
53
53
-
<preclass="python">def concat(self):
54
+
<preclass="python">def _concat(self):
54
55
# we requested as many daily tradebars as we can
55
-
his = self.get_history(self.quoted[0].Value,20*365)
56
+
his = self._get_history(self._quoted[0].value,20*365)
56
57
# get the clean DataFrame for linear regression
57
-
his = self.calculate_return(his)
58
+
his = self._calculate_return(his)
58
59
# add property to the symbol object for further use.
59
-
self.quoted[0].mean = his[1]
60
-
self.quoted[0].sd = his[2]
60
+
self._quoted[0].mean = his[1]
61
+
self._quoted[0].sd = his[2]
61
62
df = his[0]
62
63
# repeat the above procedure for each symbols, and concat the dataframes
63
-
for i in range(1,len(self.quoted)):
64
-
his = self.get_history(self.quoted[i].Value,20*365)
65
-
his = self.calculate_return(his)
66
-
self.quoted[i].mean = his[1]
67
-
self.quoted[i].sd = his[2]
64
+
for i in range(1,len(self._quoted)):
65
+
his = self._get_history(self._quoted[i].value,20*365)
66
+
his = self._calculate_return(his)
67
+
self._quoted[i].mean = his[1]
68
+
self._quoted[i].sd = his[2]
68
69
df = pd.concat([df,his[0]])
69
70
df = df.sort_index()
70
71
# remove outliers that outside the 99.9% confidence interval
The predict function uses the history for the last 3 months, merges it into a DataFrame and then calculates the updated factors. Using these updated factors (together with the model we built) we calculate the expected return.
82
+
The <code>_predict</code> function uses the history for the last 3 months, merges it into a DataFrame and then calculates the updated factors. Using these updated factors (together with the model we built) we calculate the expected return.
82
83
</p>
83
84
<divclass="section-example-container">
84
85
85
-
<preclass="python">def predict(self,symbol):
86
+
<preclass="python">def _predict(self,symbol):
86
87
# get current month in string
87
-
month = str(self.Time).split(' ')[0][5:7]
88
+
month = str(self.time).split(' ')[0][5:7]
88
89
# request the data in the last three months
89
-
res = self.get_history(symbol.Value,33*3)
90
+
res = self._get_history(symbol.value,33*3)
90
91
# pandas method to take the last datapoint of each month
91
-
res = res.resample('BM',how = lambda x: x[-1])
92
+
res = res.resample('BM').last()
92
93
# remove the data points in the current month
93
94
res = res[res.index.month != int(month)]
94
95
# calculate the variables
95
-
res = self.calculate_input(res,symbol.mean,symbol.sd)
96
-
res = res.ix[0]
96
+
res = self._calculate_input(res,symbol.mean,symbol.sd)
97
+
res = res.iloc[0]
97
98
# take the coefficient. The first one will not be used for sum-product because it's the intercept
98
-
params = self.formula.params[1:]
99
+
params = self._formula.params[1:]
99
100
# calculate the expected return
100
-
re = sum([a*b for a,b in zip(res[1:],params)]) + self.formula.params[0]
101
+
re = sum([a*b for a,b in zip(res[1:],params)]) + self._formula.params[0]
In the Initialize function we prepare the data and conduct a linear regression. The class property 'self.formula' is the result of the OLS regression. We will use this object each time we rebalance the portfolio.
127
+
In the <ahref='/docs/v2/writing-algorithms/initialization'>initialize</a>function we prepare the data and conduct a linear regression. The class property <code>self._formula</code> is the result of the OLS regression. We will use this object each time we rebalance the portfolio.
0 commit comments