progress, sliding model - buggy
This commit is contained in:
parent
85c9d2ab93
commit
48f18f7b4f
@ -20,6 +20,7 @@
|
||||
"training_minutes": 120,
|
||||
"funding_per_pair": 2000.0,
|
||||
"fit_method_class": "pt_trading.fit_methods.SlidingFit",
|
||||
# "fit_method_class": "pt_trading.fit_methods.StaticFit",
|
||||
"exclude_instruments": ["CAN"]
|
||||
|
||||
}
|
||||
@ -31,6 +31,7 @@ class TradingPair:
|
||||
|
||||
|
||||
self.user_data_ = {}
|
||||
self.predicted_df_ = pd.DataFrame()
|
||||
|
||||
def _transform_dataframe(self, df: pd.DataFrame) -> pd.DataFrame:
|
||||
# Select only the columns we need
|
||||
@ -80,7 +81,7 @@ class TradingPair:
|
||||
|
||||
testing_start_index = training_start_index + training_minutes
|
||||
self.training_df_ = self.market_data_.iloc[
|
||||
training_start_index:testing_start_index, :
|
||||
training_start_index:testing_start_index, : training_minutes
|
||||
].copy()
|
||||
assert self.training_df_ is not None
|
||||
self.training_df_ = self.training_df_.dropna().reset_index(drop=True)
|
||||
@ -101,7 +102,7 @@ class TradingPair:
|
||||
f"{self.price_column_}_{self.symbol_b_}",
|
||||
]
|
||||
|
||||
def fit_VECM(self):
|
||||
def fit_VECM(self) -> None:
|
||||
assert self.training_df_ is not None
|
||||
vecm_df = self.training_df_[self.colnames()].reset_index(drop=True)
|
||||
vecm_model = VECM(vecm_df, coint_rank=1)
|
||||
@ -120,7 +121,7 @@ class TradingPair:
|
||||
# print(f"{self}: {self.vecm_fit_.summary()}")
|
||||
pass
|
||||
|
||||
def check_cointegration_johansen(self):
|
||||
def check_cointegration_johansen(self) -> bool:
|
||||
assert self.training_df_ is not None
|
||||
from statsmodels.tsa.vector_ar.vecm import coint_johansen
|
||||
|
||||
@ -129,11 +130,11 @@ class TradingPair:
|
||||
print(
|
||||
f"{self}: lr1={result.lr1[0]} > cvt={result.cvt[0, 1]}? {result.lr1[0] > result.cvt[0, 1]}"
|
||||
)
|
||||
is_cointegrated = result.lr1[0] > result.cvt[0, 1]
|
||||
is_cointegrated: bool = bool(result.lr1[0] > result.cvt[0, 1])
|
||||
|
||||
return is_cointegrated
|
||||
|
||||
def check_cointegration_engle_granger(self):
|
||||
def check_cointegration_engle_granger(self) -> bool:
|
||||
from statsmodels.tsa.stattools import coint
|
||||
|
||||
col1, col2 = self.colnames()
|
||||
@ -144,7 +145,7 @@ class TradingPair:
|
||||
# Run Engle-Granger cointegration test
|
||||
pvalue = coint(series1, series2)[1]
|
||||
# Define cointegration if p-value < 0.05 (i.e., reject null of no cointegration)
|
||||
is_cointegrated = pvalue < 0.05
|
||||
is_cointegrated: bool = bool(pvalue < 0.05)
|
||||
print(f"{self}: is_cointegrated={is_cointegrated} pvalue={pvalue}")
|
||||
return is_cointegrated
|
||||
|
||||
@ -179,9 +180,30 @@ class TradingPair:
|
||||
predicted_prices = self.vecm_fit_.predict(steps=len(self.testing_df_))
|
||||
|
||||
# Convert prediction to a DataFrame for readability
|
||||
# predicted_df =
|
||||
predicted_df = pd.DataFrame(
|
||||
predicted_prices, columns=pd.Index(self.colnames()), dtype=float
|
||||
)
|
||||
|
||||
self.predicted_df_ = pd.merge(
|
||||
# self.predicted_df_ = pd.merge(
|
||||
# self.testing_df_.reset_index(drop=True),
|
||||
# pd.DataFrame(
|
||||
# predicted_prices, columns=pd.Index(self.colnames()), dtype=float
|
||||
# ),
|
||||
# left_index=True,
|
||||
# right_index=True,
|
||||
# suffixes=("", "_pred"),
|
||||
# ).dropna()
|
||||
|
||||
# self.predicted_df_["disequilibrium"] = (
|
||||
# self.predicted_df_[self.colnames()] @ self.vecm_fit_.beta
|
||||
# )
|
||||
|
||||
# self.predicted_df_["scaled_disequilibrium"] = (
|
||||
# abs(self.predicted_df_["disequilibrium"] - self.training_mu_)
|
||||
# / self.training_std_
|
||||
# )
|
||||
|
||||
predicted_df = pd.merge(
|
||||
self.testing_df_.reset_index(drop=True),
|
||||
pd.DataFrame(
|
||||
predicted_prices, columns=pd.Index(self.colnames()), dtype=float
|
||||
@ -191,16 +213,25 @@ class TradingPair:
|
||||
suffixes=("", "_pred"),
|
||||
).dropna()
|
||||
|
||||
self.predicted_df_["disequilibrium"] = (
|
||||
self.predicted_df_[self.colnames()] @ self.vecm_fit_.beta
|
||||
predicted_df["disequilibrium"] = (
|
||||
predicted_df[self.colnames()] @ self.vecm_fit_.beta
|
||||
)
|
||||
|
||||
self.predicted_df_["scaled_disequilibrium"] = (
|
||||
abs(self.predicted_df_["disequilibrium"] - self.training_mu_)
|
||||
predicted_df["scaled_disequilibrium"] = (
|
||||
abs(predicted_df["disequilibrium"] - self.training_mu_)
|
||||
/ self.training_std_
|
||||
)
|
||||
|
||||
print("*** PREDICTED DF")
|
||||
print(predicted_df)
|
||||
print("*" * 80)
|
||||
print("*** SELF.PREDICTED_DF")
|
||||
print(self.predicted_df_)
|
||||
print("*" * 80)
|
||||
|
||||
# Reset index to ensure proper indexing
|
||||
predicted_df = predicted_df.reset_index(drop=True)
|
||||
self.predicted_df_ = pd.concat([self.predicted_df_, predicted_df], ignore_index=True)
|
||||
# Reset index to ensure proper indexing
|
||||
self.predicted_df_ = self.predicted_df_.reset_index()
|
||||
return self.predicted_df_
|
||||
|
||||
|
||||
@ -44,6 +44,7 @@ mypy>=0.942
|
||||
mypy-extensions>=0.4.3
|
||||
netaddr>=0.8.0
|
||||
######### netifaces>=0.11.0
|
||||
numpy>=1.26.4,<2.3.0
|
||||
oauthlib>=3.2.0
|
||||
packaging>=23.1
|
||||
pathspec>=0.11.1
|
||||
|
||||
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user