Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@terwey
Copy link
Collaborator

@terwey terwey commented Oct 21, 2025

feat(filltracker,engine,ws,storage)!: track fills and size TP from executed position; replace naive TP cancel

WHY:
Replicating a bot without tracking actual fills can over-order (planned ladder ≠ executed fills). The bot is unaware of our replication, so TP sized from the plan becomes wrong after any fill. We must trade the executed position, not the plan, and keep TP reduce-only to current net size.

Introduce filltracker.Service to aggregate executed/outstanding orders per deal and expose snapshots:

  • Rebuild(ctx) to hydrate from storage
  • UpdateStatus(md, wsOrder) to ingest HL updates
  • Snapshot(dealID) to read net qty/avg entry/active TP/open buys
  • CancelCompletedTakeProfits(ctx, submitter) to drop stale TP when all buys filled

Engine:

  • WithFillTracker; processDeal consults snapshot
  • adjustActionWithTracker: TP qty == net position, reduce_only=true; skip when flat
  • Idempotent: only change when different from exchange state

Main:

  • Wire tracker into ws and engine
  • Replace cancelTakeProfit with reconcileTakeProfits using tracker

WS:

  • New(ctx, store, tracker, wallet, url); forward order updates into tracker before persisting

Storage:

  • New(path, ...opts) with storage.WithLogger
  • ListMetadataForDeal(ctx, dealID) to support tracker rebuild

Tests:

  • filltracker/service_test.go and engine tests cover TP sizing and partial fills

BREAKING CHANGE:

  • storage.New(path, logger) → storage.New(path, storage.WithLogger(logger))
  • ws.New(ctx, store, wallet, url) → ws.New(ctx, store, tracker, wallet, url)
  • TP placement now always derived from executed position and marked reduce_only

@terwey terwey force-pushed the filltracker branch 2 times, most recently from e35d747 to e931398 Compare October 21, 2025 03:57
…ecuted position; replace naive TP cancel

WHY:
Replicating a bot without tracking actual fills can over-order (planned
ladder ≠ executed fills). The bot is unaware of our replication, so TP sized
from the plan becomes wrong after any fill. We must trade the *executed*
position, not the plan, and keep TP reduce-only to current net size.

Introduce filltracker.Service to aggregate executed/outstanding orders per
deal and expose snapshots:

* Rebuild(ctx) to hydrate from storage
* UpdateStatus(md, wsOrder) to ingest HL updates
* Snapshot(dealID) to read net qty/avg entry/active TP/open buys
* CancelCompletedTakeProfits(ctx, submitter) to drop stale TP when all buys filled

Engine:

* WithFillTracker; processDeal consults snapshot
* adjustActionWithTracker: TP qty == net position, reduce_only=true; skip when flat
* Idempotent: only change when different from exchange state

Main:

* Wire tracker into ws and engine
* Replace cancelTakeProfit with reconcileTakeProfits using tracker

WS:

* New(ctx, store, tracker, wallet, url); forward order updates into tracker before persisting

Storage:

* New(path, ...opts) with storage.WithLogger
* ListMetadataForDeal(ctx, dealID) to support tracker rebuild

Tests:

* filltracker/service_test.go and engine tests cover TP sizing and partial fills

BREAKING CHANGE:

* storage.New(path, logger) → storage.New(path, storage.WithLogger(logger))
* ws.New(ctx, store, wallet, url) → ws.New(ctx, store, tracker, wallet, url)
* TP placement now always derived from executed position and marked reduce_only
@terwey terwey merged commit 739fefc into main Oct 21, 2025
1 check passed
@terwey terwey deleted the filltracker branch October 31, 2025 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants