progress
This commit is contained in:
@@ -22,7 +22,7 @@ from cvttpy_trading.trading.trading_instructions import TargetPositionSignal
|
||||
from pairs_trading.lib.pt_strategy.model_data_policy import ModelDataPolicy
|
||||
from pairs_trading.lib.pt_strategy.pt_model import Prediction
|
||||
from pairs_trading.lib.pt_strategy.trading_pair import LiveTradingPair
|
||||
from pairs_trading.apps.pairs_trader import PairsTrader
|
||||
from pairs_trading.apps.pair_trader import PairTrader
|
||||
from pairs_trading.lib.pt_strategy.pt_market_data import LiveMarketData
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ class PtLiveStrategy(NamedObject):
|
||||
|
||||
trading_pair_: LiveTradingPair
|
||||
model_data_policy_: ModelDataPolicy
|
||||
pairs_trader_: PairsTrader
|
||||
pairs_trader_: PairTrader
|
||||
|
||||
# for presentation: history of prediction values and trading signals
|
||||
predictions_df_: pd.DataFrame
|
||||
@@ -46,22 +46,28 @@ class PtLiveStrategy(NamedObject):
|
||||
def __init__(
|
||||
self,
|
||||
config: Config,
|
||||
pairs_trader: PairsTrader,
|
||||
pairs_trader: PairTrader,
|
||||
):
|
||||
# import copy
|
||||
# self.config_ = Config(json_src=copy.deepcopy(config.data()))
|
||||
self.config_ = config
|
||||
|
||||
self.pairs_trader_ = pairs_trader
|
||||
self.trading_pair_ = LiveTradingPair(
|
||||
config=config,
|
||||
instruments=self.pairs_trader_.instruments_,
|
||||
)
|
||||
self.model_data_policy_ = ModelDataPolicy.create(
|
||||
self.config_,
|
||||
is_real_time=True,
|
||||
pair=self.trading_pair_,
|
||||
)
|
||||
assert (
|
||||
self.model_data_policy_ is not None
|
||||
), f"{self.fname()}: Unable to create ModelDataPolicy"
|
||||
|
||||
self.predictions_df_ = pd.DataFrame()
|
||||
self.trading_signals_df_ = pd.DataFrame()
|
||||
# self.book_ = book
|
||||
|
||||
import copy
|
||||
|
||||
# modified config must be passed to PtMarketData
|
||||
self.config_ = Config(json_src=copy.deepcopy(config.data()))
|
||||
|
||||
self.instruments_ = self.pairs_trader_.instruments_
|
||||
|
||||
@@ -71,25 +77,27 @@ class PtLiveStrategy(NamedObject):
|
||||
|
||||
async def _on_config(self) -> None:
|
||||
self.interval_sec_ = self.config_.get_value("interval_sec", 0)
|
||||
assert self.interval_sec_ > 0, "interval_sec cannot be 0"
|
||||
self.history_depth_sec_ = (
|
||||
self.config_.get_value("history_depth_hours", 0) * SecPerHour
|
||||
)
|
||||
assert self.history_depth_sec_ > 0, "history_depth_hours cannot be 0"
|
||||
|
||||
await self.pairs_trader_.subscribe_md()
|
||||
|
||||
self.open_threshold_ = self.config_.get_value(
|
||||
"dis-equilibrium_open_trshld", 0.0
|
||||
"model/disequilibrium/open_trshld", 0.0
|
||||
)
|
||||
self.close_threshold_ = self.config_.get_value(
|
||||
"dis-equilibrium_close_trshld", 0.0
|
||||
"model/disequilibrium/close_trshld", 0.0
|
||||
)
|
||||
|
||||
assert (
|
||||
self.open_threshold_ > 0
|
||||
), "dis-equilibrium_open_trshld must be greater than 0"
|
||||
), "disequilibrium/open_trshld must be greater than 0"
|
||||
assert (
|
||||
self.close_threshold_ > 0
|
||||
), "dis-equilibrium_close_trshld must be greater than 0"
|
||||
), "disequilibrium/close_trshld must be greater than 0"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"{self.classname()}: trading_pair={self.trading_pair_}, mdp={self.model_data_policy_.__class__.__name__}, "
|
||||
@@ -106,16 +114,8 @@ class PtLiveStrategy(NamedObject):
|
||||
return
|
||||
|
||||
self.trading_pair_.market_data_ = market_data_df
|
||||
self.model_data_policy_ = ModelDataPolicy.create(
|
||||
self.config_,
|
||||
is_real_time=True,
|
||||
pair=self.trading_pair_,
|
||||
mkt_data=market_data_df,
|
||||
)
|
||||
assert (
|
||||
self.model_data_policy_ is not None
|
||||
), f"{self.fname()}: Unable to create ModelDataPolicy"
|
||||
|
||||
Log.info(f"{self.fname()}: Running prediction for pair: {self.trading_pair_}")
|
||||
prediction = self.trading_pair_.run(
|
||||
market_data_df, self.model_data_policy_.advance()
|
||||
)
|
||||
@@ -132,13 +132,16 @@ class PtLiveStrategy(NamedObject):
|
||||
await self._send_trading_instructions(trading_instructions)
|
||||
|
||||
def _is_md_actual(self, hist_aggr: List[MdTradesAggregate]) -> bool:
|
||||
curr_ns = current_nanoseconds()
|
||||
LAG_THRESHOLD = 5 * NanoPerSec
|
||||
|
||||
if len(hist_aggr) == 0:
|
||||
Log.warning(f"{self.fname()} list of aggregates IS EMPTY")
|
||||
return False
|
||||
# MAYBE check market data length
|
||||
if current_nanoseconds() - hist_aggr[-1].time_ns_ > LAG_THRESHOLD:
|
||||
lag_ns = curr_ns - hist_aggr[-1].time_ns_
|
||||
if lag_ns > LAG_THRESHOLD:
|
||||
Log.warning(f"{self.fname()} {hist_aggr[-1].exch_inst_.details_short()} Lagging {int(lag_ns/NanoPerSec)} seconds")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user