outstanding positions bug fix
This commit is contained in:
@@ -276,25 +276,27 @@ class SlidingFit(PairsTradingFitMethod):
|
||||
|
||||
if len(pair.training_df_) < training_minutes:
|
||||
print(
|
||||
f"{pair}: {self.curr_training_start_idx_} Not enough training data. Completing the job."
|
||||
f"{pair}: current offset={self.curr_training_start_idx_}"
|
||||
f" * Training data length={len(pair.training_df_)} < {training_minutes}"
|
||||
" * 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
|
||||
if pair.predicted_df_ is not None:
|
||||
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"],
|
||||
)
|
||||
# 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
|
||||
# if pair.predicted_df_ is not None:
|
||||
# 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:
|
||||
@@ -367,6 +369,25 @@ class SlidingFit(PairsTradingFitMethod):
|
||||
pair.add_trades(close_trades)
|
||||
pair.user_data_["state"] = PairState.CLOSED
|
||||
|
||||
# Outstanding positions
|
||||
if pair.user_data_["state"] == PairState.OPEN:
|
||||
print(
|
||||
f"{pair}: *** Position is NOT CLOSED. ***"
|
||||
)
|
||||
# outstanding positions
|
||||
# last_row_index = self.curr_training_start_idx_ + training_minutes
|
||||
if pair.predicted_df_ is not None:
|
||||
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"],
|
||||
)
|
||||
|
||||
def _get_open_trades(
|
||||
self, pair: TradingPair, row: pd.Series, open_threshold: float
|
||||
) -> Optional[pd.DataFrame]:
|
||||
@@ -386,15 +407,15 @@ class SlidingFit(PairsTradingFitMethod):
|
||||
open_px_a = open_row[f"{colname_a}"]
|
||||
open_px_b = open_row[f"{colname_b}"]
|
||||
# Ensure scalars for handle_outstanding_position
|
||||
if isinstance(open_px_a, pd.Series):
|
||||
open_px_a = open_px_a.iloc[0]
|
||||
if isinstance(open_px_b, pd.Series):
|
||||
open_px_b = open_px_b.iloc[0]
|
||||
if isinstance(open_tstamp, pd.Series):
|
||||
open_tstamp = open_tstamp.iloc[0]
|
||||
open_px_a = float(open_px_a)
|
||||
open_px_b = float(open_px_b)
|
||||
open_tstamp = pd.Timestamp(open_tstamp)
|
||||
# if isinstance(open_px_a, pd.Series):
|
||||
# open_px_a = open_px_a.iloc[0]
|
||||
# if isinstance(open_px_b, pd.Series):
|
||||
# open_px_b = open_px_b.iloc[0]
|
||||
# if isinstance(open_tstamp, pd.Series):
|
||||
# open_tstamp = open_tstamp.iloc[0]
|
||||
# open_px_a = float(open_px_a)
|
||||
# open_px_b = float(open_px_b)
|
||||
# open_tstamp = pd.Timestamp(open_tstamp)
|
||||
|
||||
if open_scaled_disequilibrium < open_threshold:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user