Skip to content

Commit

Permalink
Feature #1113 - [pdr sim trader] prediction confidence threshold (#1115)
Browse files Browse the repository at this point in the history
* Add confidence_threshold to trader ss

* Check sim_confidence_threshold in sim engine
  • Loading branch information
trizin authored Jun 2, 2024
1 parent 32b4e84 commit 160c6db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pdr_backend/ppss/trader_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def tradetype(self) -> str:
def allowed_tradetypes(self) -> List[str]:
return ["livemock", "livereal"]

@property
def sim_confidence_threshold(self) -> float:
return self.d["sim_only"].get("confidence_threshold", 0.0)

# feed defined in base

# --------------------------------
Expand Down
14 changes: 8 additions & 6 deletions pdr_backend/sim/sim_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ def run_one_iter(self, test_i: int, mergedohlcv_df: pl.DataFrame):

# predict price direction
prob_up: float = model.predict_ptrue(X_test)[0] # in [0.0, 1.0]
pred_up: bool = prob_up > 0.5
prob_down: float = 1.0 - prob_up
conf_up = (prob_up - 0.5) * 2.0 # to range [0,1]
conf_down = (prob_down - 0.5) * 2.0 # to range [0,1]
conf_threshold = self.ppss.trader_ss.sim_confidence_threshold
pred_up: bool = prob_up > 0.5 and conf_up > conf_threshold
pred_down: bool = prob_up < 0.5 and conf_down > conf_threshold
st.probs_up.append(prob_up)

# predictoor: (simulate) submit predictions with stake
Expand All @@ -169,12 +174,9 @@ def run_one_iter(self, test_i: int, mergedohlcv_df: pl.DataFrame):
# trader: enter the trading position
usdcoin_holdings_before = st.holdings[self.usdcoin]
if pred_up: # buy; exit later by selling
conf_up = (prob_up - 0.5) * 2.0 # to range [0,1]
usdcoin_amt_send = trade_amt * conf_up
tokcoin_amt_recd = self._buy(curprice, usdcoin_amt_send)
else: # sell; exit later by buying
prob_down = 1.0 - prob_up
conf_down = (prob_down - 0.5) * 2.0 # to range [0,1]
elif pred_down: # sell; exit later by buying
target_usdcoin_amt_recd = trade_amt * conf_down
p = self.ppss.trader_ss.fee_percent
tokcoin_amt_send = target_usdcoin_amt_recd / curprice / (1 - p)
Expand Down Expand Up @@ -205,7 +207,7 @@ def run_one_iter(self, test_i: int, mergedohlcv_df: pl.DataFrame):
if pred_up:
# we'd bought; so now sell
self._sell(trueprice, tokcoin_amt_recd)
else:
elif pred_down:
# we'd sold, so buy back the same # tokcoins as we sold
# (do *not* buy back the same # usdcoins! Not the same thing!)
target_tokcoin_amt_recd = tokcoin_amt_send
Expand Down
1 change: 1 addition & 0 deletions ppss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ trader_ss:
sim_only:
buy_amt: 1000 USD # buy this amount in each epoch
fee_percent: 0.0 # simulated % fee
confidence_threshold: 0.02 # skip trade if confidence < this
init_holdings:
- 100000 USDT
- 2 BTC
Expand Down

0 comments on commit 160c6db

Please sign in to comment.