Debugging live strategy#

In this notebook, we show how to debug live trading strategy instance using Python and Jupyter notebook.

  • Downloading the trades from the live instance

  • Reading data to State object

  • Inspecting State in Python

Download live data#

  • Each strategy Docker instances offers a webhook that allows access the data of this strategy, include State object that is the flat file state of the all the strategy current and past decision making

  • Note that State class and any children classes in the state tree may have changes between versions and decoding might need you to use a specific version of trade-executor

[7]:
from tradeexecutor.monkeypatch.dataclasses_json import patch_dataclasses_json
from tradeexecutor.state.state import State
import requests

# Currently needed because unpatched dataclasses_json package issues
patch_dataclasses_json()

# Public internet endpoint as exposed by the trade executor Docker
webbhook_url = "https://pancake-eth-usd-sma.tradingstrategy.ai"

state_api = f"{webbhook_url}/state"

resp = requests.get(state_api)

state_blob = resp.content

print(f"Downloaded {len(state_blob):,} bytes state data")

state = State.from_json(state_blob)

print(f"trade-executor was launched at: {state.created_at}, we have {len(list(state.portfolio.get_all_trades()))} trades")
Downloaded 2,160,683 bytes state data
trade-executor was launched at: 2022-12-01 22:17:08.835950, we have 136 trades

Examining data#

You can check what individual persistent objects are made of.

In this example, we have one blockchain trade whose transactions were broadcasted to the blockchain, but for which we never got transaction receipts back to the blockchain.

[8]:
# Inspect a single position
position = next(iter(state.portfolio.closed_positions.values()))
print(position)

print("Value: ", position.get_value())
print("Quantity: ", position.get_quantity())
print("Successful trades:")
for t in position.get_successful_trades():
    print(f"     {t}")

print("Failed trades:")
for t in position.get_failed_trades():
    print(f"     {t}")

print("Any trades:")
for trade_id, t in position.trades.items():
    print(f"     {t}")
<Closed position #1 <Pair ETH-USDC at 0xea26b78255df2bbc31c1ebf60010d78670185bd0 (0.0000% fee) on exchange 0xca143ce32fe78f1f7019d7d551a6402fc5350c73> $349.14808504607964>
Value:  0.0
Quantity:  0E-18
Successful trades:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 9
      7 print("Successful trades:")
      8 for t in position.get_successful_trades():
----> 9     print("     " + t)
     11 print("Failed trades:")
     12 for t in position.get_failed_trades():

TypeError: can only concatenate str (not "TradeExecution") to str