progress
This commit is contained in:
+186
-22
@@ -1,4 +1,5 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from enum import Enum
|
||||
import sys
|
||||
|
||||
from typing import Dict, Optional
|
||||
@@ -22,10 +23,10 @@ class PairsTradingStrategy(ABC):
|
||||
]
|
||||
@abstractmethod
|
||||
def run_pair(self, pair: TradingPair, bt_result: BacktestResult) -> Optional[pd.DataFrame]:
|
||||
...
|
||||
...
|
||||
|
||||
class StaticFitStrategy(PairsTradingStrategy):
|
||||
|
||||
|
||||
def run_pair(self, config: Dict, pair: TradingPair, bt_result: BacktestResult) -> Optional[pd.DataFrame]: # abstractmethod
|
||||
pair.get_datasets(training_minutes=config["training_minutes"])
|
||||
try:
|
||||
@@ -42,7 +43,7 @@ class StaticFitStrategy(PairsTradingStrategy):
|
||||
except Exception as e:
|
||||
print(f"{pair}: Prediction failed: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
pair_trades = self.create_trading_signals(pair=pair, config=config, result=bt_result)
|
||||
|
||||
return pair_trades
|
||||
@@ -199,42 +200,205 @@ class StaticFitStrategy(PairsTradingStrategy):
|
||||
columns=self.TRADES_COLUMNS,
|
||||
)
|
||||
|
||||
class PairState(Enum):
|
||||
INITIAL = 1
|
||||
OPEN = 2
|
||||
CLOSED = 3
|
||||
|
||||
class SlidingFitStrategy(PairsTradingStrategy):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.curr_training_start_idx_ = 0
|
||||
|
||||
def run_pair(self, config: Dict, pair: TradingPair, bt_result: BacktestResult) -> Optional[pd.DataFrame]:
|
||||
pair.user_data_['is_position_open'] = False
|
||||
self.curr_training_start_idx_ = 0
|
||||
|
||||
def run_pair(self, config: Dict, pair: TradingPair, bt_result: BacktestResult) -> Optional[pd.DataFrame]:
|
||||
print(f"***{pair}*** STARTING....")
|
||||
|
||||
pair.user_data_['state'] = PairState.INITIAL
|
||||
pair.user_data_["trades"] = pd.DataFrame(columns=self.TRADES_COLUMNS)
|
||||
pair.user_data_["is_cointegrated"] = False
|
||||
|
||||
open_threshold = config["dis-equilibrium_open_trshld"]
|
||||
close_threshold = config["dis-equilibrium_open_trshld"]
|
||||
|
||||
training_minutes = config["training_minutes"]
|
||||
while True:
|
||||
print(self.curr_training_start_idx_, end='\r')
|
||||
pair.get_datasets(
|
||||
training_minutes=training_minutes,
|
||||
training_start_index=self.curr_training_start_idx_,
|
||||
training_minutes=training_minutes,
|
||||
training_start_index=self.curr_training_start_idx_,
|
||||
testing_size=1
|
||||
)
|
||||
|
||||
|
||||
if len(pair.training_df_) < training_minutes:
|
||||
print(f"{pair}: Not enough training data. Completing the job.")
|
||||
print(f"{pair}: {self.curr_training_start_idx_} Not enough training data. Completing the job.")
|
||||
if pair.user_data_["state"] == PairState.OPEN:
|
||||
print(f"{pair}: {self.curr_training_start_idx_} Position is not closed.")
|
||||
# outstanding positions
|
||||
# last_row_index = self.curr_training_start_idx_ + training_minutes
|
||||
|
||||
bt_result.handle_outstanding_position(
|
||||
pair=pair,
|
||||
pair_result_df=pair.predicted_df_,
|
||||
last_row_index=0,
|
||||
open_side_a=pair.user_data_["open_side_a"],
|
||||
open_side_b=pair.user_data_["open_side_b"],
|
||||
open_px_a=pair.user_data_["open_px_a"],
|
||||
open_px_b=pair.user_data_["open_px_b"],
|
||||
open_tstamp=pair.user_data_["open_tstamp"],
|
||||
)
|
||||
break
|
||||
|
||||
|
||||
try:
|
||||
is_cointegrated = pair.train_pair()
|
||||
if not is_cointegrated:
|
||||
print(f"{pair} IS NOT COINTEGRATED")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"{pair}: Training failed: {str(e)}")
|
||||
return None
|
||||
raise Exception(f"{pair}: Training failed: {str(e)}") from e
|
||||
|
||||
if pair.user_data_["is_cointegrated"] != is_cointegrated:
|
||||
pair.user_data_["is_cointegrated"] = is_cointegrated
|
||||
if not is_cointegrated:
|
||||
if pair.user_data_["state"] == PairState.OPEN:
|
||||
print(f"{pair} {self.curr_training_start_idx_} LOST COINTEGRATION. Consider closing positions...")
|
||||
else:
|
||||
print(f"{pair} {self.curr_training_start_idx_} IS NOT COINTEGRATED. Moving on")
|
||||
else:
|
||||
print('*' * 80)
|
||||
print(f"Pair {pair} ({self.curr_training_start_idx_}) IS COINTEGRATED")
|
||||
print('*' * 80)
|
||||
if not is_cointegrated:
|
||||
self.curr_training_start_idx_ += 1
|
||||
continue
|
||||
|
||||
try:
|
||||
pair.predict()
|
||||
except Exception as e:
|
||||
print(f"{pair}: Prediction failed: {str(e)}")
|
||||
return None
|
||||
|
||||
raise Exception(f"{pair}: Prediction failed: {str(e)}") from e
|
||||
|
||||
if pair.user_data_["state"] == PairState.INITIAL:
|
||||
|
||||
open_trades = self._get_open_trades(pair, open_threshold=open_threshold)
|
||||
if open_trades is not None:
|
||||
pair.user_data_["trades"] = open_trades
|
||||
pair.user_data_["state"] = PairState.OPEN
|
||||
elif pair.user_data_["state"] == PairState.OPEN:
|
||||
close_trades = self._get_close_trades(pair, close_threshold=close_threshold)
|
||||
if close_trades is not None:
|
||||
pair.user_data_["trades"] = pd.concat([pair.user_data_["trades"], close_trades], ignore_index=True)
|
||||
pair.user_data_["state"] = PairState.CLOSED
|
||||
break
|
||||
|
||||
self.curr_training_start_idx_ += 1
|
||||
|
||||
print(f"***{pair}*** FINISHED ... {len(pair.user_data_['trades'])}")
|
||||
return pair.user_data_["trades"]
|
||||
|
||||
def _get_open_trades(self, pair: TradingPair, open_threshold: float) -> Optional[pd.DataFrame]:
|
||||
colname_a, colname_b = pair.colnames()
|
||||
|
||||
predicted_df = pair.predicted_df_
|
||||
|
||||
open_row = predicted_df.loc[0]
|
||||
open_tstamp = open_row["tstamp"]
|
||||
open_disequilibrium = open_row["disequilibrium"]
|
||||
open_scaled_disequilibrium = open_row["scaled_disequilibrium"]
|
||||
open_px_a = open_row[f"{colname_a}"]
|
||||
open_px_b = open_row[f"{colname_b}"]
|
||||
|
||||
if open_scaled_disequilibrium < open_threshold:
|
||||
return None
|
||||
|
||||
# creating the trades
|
||||
if open_disequilibrium > 0:
|
||||
open_side_a = "SELL"
|
||||
open_side_b = "BUY"
|
||||
close_side_a = "BUY"
|
||||
close_side_b = "SELL"
|
||||
else:
|
||||
open_side_a = "BUY"
|
||||
open_side_b = "SELL"
|
||||
close_side_a = "SELL"
|
||||
close_side_b = "BUY"
|
||||
|
||||
# save closing sides
|
||||
pair.user_data_["open_side_a"] = open_side_a
|
||||
pair.user_data_["open_side_b"] = open_side_b
|
||||
pair.user_data_["open_px_a"] = open_px_a
|
||||
pair.user_data_["open_px_b"] = open_px_b
|
||||
|
||||
pair.user_data_["open_tstamp"] = open_tstamp
|
||||
|
||||
pair.user_data_["close_side_a"] = close_side_a
|
||||
pair.user_data_["close_side_b"] = close_side_b
|
||||
|
||||
|
||||
# create opening trades
|
||||
trd_signal_tuples = [
|
||||
(
|
||||
open_tstamp,
|
||||
open_side_a,
|
||||
pair.symbol_a_,
|
||||
open_px_a,
|
||||
open_disequilibrium,
|
||||
open_scaled_disequilibrium,
|
||||
pair,
|
||||
),
|
||||
(
|
||||
open_tstamp,
|
||||
open_side_b,
|
||||
pair.symbol_b_,
|
||||
open_px_b,
|
||||
open_disequilibrium,
|
||||
open_scaled_disequilibrium,
|
||||
pair,
|
||||
),
|
||||
]
|
||||
return pd.DataFrame(
|
||||
trd_signal_tuples,
|
||||
columns=self.TRADES_COLUMNS,
|
||||
)
|
||||
|
||||
def _get_close_trades(self, pair: TradingPair, close_threshold: float) -> Optional[pd.DataFrame]:
|
||||
colname_a, colname_b = pair.colnames()
|
||||
|
||||
close_row = pair.predicted_df_.loc[0]
|
||||
close_tstamp = close_row["tstamp"]
|
||||
close_disequilibrium = close_row["disequilibrium"]
|
||||
close_scaled_disequilibrium = close_row["scaled_disequilibrium"]
|
||||
close_px_a = close_row[f"{colname_a}"]
|
||||
close_px_b = close_row[f"{colname_b}"]
|
||||
|
||||
close_side_a = pair.user_data_["close_side_a"]
|
||||
close_side_b = pair.user_data_["close_side_b"]
|
||||
|
||||
if close_scaled_disequilibrium > close_threshold:
|
||||
return None
|
||||
|
||||
trd_signal_tuples = [
|
||||
(
|
||||
close_tstamp,
|
||||
close_side_a,
|
||||
pair.symbol_a_,
|
||||
close_px_a,
|
||||
close_disequilibrium,
|
||||
close_scaled_disequilibrium,
|
||||
pair,
|
||||
),
|
||||
(
|
||||
close_tstamp,
|
||||
close_side_b,
|
||||
pair.symbol_b_,
|
||||
close_px_b,
|
||||
close_disequilibrium,
|
||||
close_scaled_disequilibrium,
|
||||
pair,
|
||||
),
|
||||
]
|
||||
|
||||
# Add tuples to data frame
|
||||
return pd.DataFrame(
|
||||
trd_signal_tuples,
|
||||
columns=self.TRADES_COLUMNS,
|
||||
)
|
||||
|
||||
|
||||
pair_trades = self.create_trading_signals(pair=pair, config=config, result=bt_result)
|
||||
|
||||
return pair_trades
|
||||
|
||||
|
||||
Reference in New Issue
Block a user