progress: stop signals
This commit is contained in:
@@ -1,9 +1,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import pandas as pd # type:ignore
|
||||
from statsmodels.tsa.vector_ar.vecm import VECM, VECMResults # type:ignore
|
||||
from statsmodels.tsa.vector_ar.vecm import VECM, VECMResults
|
||||
|
||||
class PairState(Enum):
|
||||
INITIAL = 1
|
||||
OPEN = 2
|
||||
CLOSE = 3
|
||||
CLOSE_POSITION = 4
|
||||
CLOSE_STOP_LOSS = 5
|
||||
CLOSE_STOP_PROFIT = 6
|
||||
|
||||
class CointegrationData:
|
||||
EG_PVALUE_THRESHOLD = 0.05
|
||||
@@ -288,13 +297,6 @@ class TradingPair:
|
||||
/ self.training_std_
|
||||
)
|
||||
|
||||
# print("*** PREDICTED DF")
|
||||
# print(predicted_df)
|
||||
# print("*" * 80)
|
||||
# print("*** SELF.PREDICTED_DF")
|
||||
# print(self.predicted_df_)
|
||||
# print("*" * 80)
|
||||
|
||||
predicted_df = predicted_df.reset_index(drop=True)
|
||||
if self.predicted_df_ is None:
|
||||
self.predicted_df_ = predicted_df
|
||||
@@ -343,6 +345,49 @@ class TradingPair:
|
||||
curr_training_start_idx += 1
|
||||
return result
|
||||
|
||||
def to_stop_close_conditions(self, predicted_row: pd.Series) -> bool:
|
||||
config = self.config_
|
||||
if ("stop_close_conditions" not in config or config["stop_close_conditions"] is None) :
|
||||
return False
|
||||
if "profit" in config["stop_close_conditions"]:
|
||||
current_return = self._current_return(predicted_row)
|
||||
#
|
||||
# print(f"time={predicted_row['tstamp']} current_return={current_return}")
|
||||
#
|
||||
if current_return >= config["stop_close_conditions"]["profit"]:
|
||||
self.user_data_["stop_close_state"] = PairState.CLOSE_STOP_PROFIT
|
||||
return True
|
||||
if "loss" in config["stop_close_conditions"]:
|
||||
if current_return <= config["stop_close_conditions"]["loss"]:
|
||||
self.user_data_["stop_close_state"] = PairState.CLOSE_STOP_LOSS
|
||||
return True
|
||||
return False
|
||||
|
||||
def on_open_trades(self, trades: pd.DataFrame) -> None:
|
||||
if "close_trades" in self.user_data_: del self.user_data_["close_trades"]
|
||||
self.user_data_["open_trades"] = trades
|
||||
|
||||
def on_close_trades(self, trades: pd.DataFrame) -> None:
|
||||
del self.user_data_["open_trades"]
|
||||
self.user_data_["close_trades"] = trades
|
||||
|
||||
def _current_return(self, predicted_row: pd.Series) -> float:
|
||||
if "open_trades" in self.user_data_:
|
||||
open_trades = self.user_data_["open_trades"]
|
||||
if len(open_trades) == 0:
|
||||
return 0.0
|
||||
def _stock_return(stock: str) -> float:
|
||||
stock_open_trades = open_trades[open_trades["symbol"] == stock]
|
||||
stock_sign = -1 if stock_open_trades["action"].iloc[0] == "SELL" else 1
|
||||
stock_price = predicted_row[f"{self.price_column_}_{stock}"]
|
||||
stock_return = stock_sign * (stock_price - stock_open_trades["price"].iloc[0]) / stock_open_trades["price"].iloc[0]
|
||||
return float(stock_return)
|
||||
|
||||
stock_a_return = _stock_return(self.symbol_a_)
|
||||
stock_b_return = _stock_return(self.symbol_b_)
|
||||
return (stock_a_return + stock_b_return) * 100.0
|
||||
return 0.0
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return self.name()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user