drew2323 / v2trading

V2Trading platform - live trading engine, backtesting and research tool.
https://trading.mujdenik.eu
9 stars 3 forks source link

Multi accounts trading #227

Open drew2323 opened 4 months ago

drew2323 commented 4 months ago

Support two Accounts for trading within one strategy. This would allow taking both long/short positions simultaneously, taking advantage of hedging.

Currently only one account is used for trading, thus long and short position within the same time is not possible.

Idea:

Currently the signal within a strategy defintiion is governed by signal section in stratvars TOML followed by name of the signal - in the case below main. In the root section there are various attributtes (window open and close, stoplosses etc.) in condition section there are specific condition for LONG or SHORT positon. Account should probably be new attribute within root signal section.

It could also be suffixed by _long or _short string to allow multiple accounts for each direction within I one signal section.

[stratvars.signals.main_acc1]
    signal_only_on_confirmed = false
    next_signal_offset_from_last_exit = 15
    window_open = 7
    window_close = 360
    profit = 0.07
    account = "ACCOUNT1" #fallback account for long and short
    account_long = "ACCOUNT2" #long override
    account_short = "ACCOUNT1" #short override
[stratvars.signals.main_acc1.conditions]
    #preconditions 

    #SHORT ENTRY
    #signal.go_short_if_equals = -1
    signal_conf_short.go_short_if_equals = 1

[stratvars.signals.main_acc2]
    signal_only_on_confirmed = false
    next_signal_offset_from_last_exit = 15
    window_open = 7
    window_close = 360
    profit = 0.07
    account = "ACCOUNT2" #fallback, applies for all directions
[stratvars.signals.main_acc2.conditions]
    #preconditions 

    #SHORT ENTRY
    #signal.go_short_if_equals = -1
    signal_conf_short.go_short_if_equals = 1

TBD - more details usage, cross referencing etc.

Impacted areas:

  1. Signal definition in stratvars
  2. Account storage (current Keys class works just with single keys, refactor to enable different keyTypes (alpaca-trading-keys being one of them) and also support multiple keys for trading probably by introduction "account-name" key).
  3. Trade Inventory
    • pendings
    • positions storage
    • position fetching
  4. Trade management
    • trade actions, trade/tradeUpdate entities (new attribute Account)
    • trade callabacks, including OrderUpdate thread (probably new thread+its lifecycle hooks)
    • storage of trades
  5. Live Trading Interfaces
    • self.interface.buy (account, side, size)
    • self.interface.pos(account)
  6. Backtesting interfaces and backtesting support
  7. Strategy Output
    • metrics (per account, joined) - equity curves, etc.
    • vizualizations (trades, metrics) on chart and chart details and/or reports
drew2323 commented 3 months ago

Currently the ACCOUNT that is used as only interface is set in RunReq dialog when running strategy (ACCOUNT1) AS change - in stratvars under stratvars.signals there can be additional ACCOUNTS that can be used for trades.

During intiialization in base.py there is unique set calculated from above and set of unique accounts is stored and those values are the used as keys to

LiveOrderUpdatesStreamer - pro každou instanci strategie a každý ACCOUNT je vytvořena vlastní instance. TBD dořešit způsob distribuce, každý streamer dostává všechny - nyní se distribuuje do dané strategie na základě symbolu. Zvážit použití nějakého clientID atp. OMEZENÍ: nelze více bežících strategií na stejný symbol

todo: