c9s / bbgo

The modern cryptocurrency trading bot framework written in Go.
https://bbgo.finance
GNU Affero General Public License v3.0
1.18k stars 283 forks source link

Strategy metric panel #643

Open c9s opened 2 years ago

c9s commented 2 years ago

Requirement

In this panel, we will render the metrics of each running strategy. There are some common metrics we are interested in:

  1. total net profit (gross profit - gross loss - trading fee)
  2. gross profit (total profit)
  3. gross loss (total loss)
  4. trading volume
  5. running time

Some panel could be different if the strategy is not a position-based strategy, for example, the grid strategy.

For grid strategy, we're interested in:

  1. How many grids are placed.
  2. Upper price and lower price. (the top of the grid and the bottom of the grid)
  3. The number of arbitrages.
  4. Total arbitrage profits

Implementation

Hence, we need to add a new interface for strategies to export their metrics.

type MetricsExporter interface {
    Metrics() interface{}
}

And each strategy can export their own metrics structs that could be marshalled into JSON format for the front-end application to render.

And the metrics could be fetched from an endpoint like:

https://localhost:3000/exchangeStrategies/metrics

And the response structure could be something like:

[
    { "instanceID": "....", "strategy": "grid", "symbol": "BTCUSDT", "stats": {....}, "status": "RUNNING" },
    { "instanceID": "....", "strategy": "bollmaker", "symbol": "BNBUSDT", "stats": {....}, "status": "STOPPED" },
]
jundesu commented 2 years ago

I’m interested in this task and will contribute the frontend part.

c9s commented 2 years ago

@jundesu thank you!

jundesu commented 2 years ago

Here’s my draft. Please review it. image

  1. The grid strategy algorithm is easier than other strategies, so I want to implement it first.
  2. I study other similar products. They all provide the information on my draft. I think the information is necessary for the grid strategy.

If you agree with the draft, I will design the desired json response. Thank you.

jundesu commented 2 years ago

@c9s According to my draft, could bbgo server provide the api response for grid strategy?

[
  {
    "id":"uuid",
    "instanceID": "....",
    "strategy": "grid",
    "symbol": "BTCUSDT",
    "metrics": {
      "oneDayArbs": 0,
      "totalArbs": 3,
      "investment": 100,
      "totalProfits": 5.6,
      "gridProfits": 2.5,
      "floatingPNL": 3.1, 
      "currentPrice": 29000,
      "lowestPrice": 25000,
      "highestPrice": 35000
    },
    "status": "RUNNING",
    "startTime": 1654938187102
  }
]
c9s commented 2 years ago

yes, It's something similar: (I renamed the metrics to stats)

[
  {
    "id":"uuid",
    "instanceID": "....",
    "strategy": "grid",
    "grid": {
         "symbol": "BTCUSDT",
         ..... other grid strategy settings
    },
    "stats": {
      "oneDayArbs": 0,
      "totalArbs": 3,
      "investment": 100,
      "totalProfits": 5.6,
      "gridProfits": 2.5,
      "floatingPNL": 3.1, 
      "currentPrice": 29000,
      "lowestPrice": 25000,
      "highestPrice": 35000
    },
    "status": "RUNNING",
    "startTime": 1654938187102
  }
]
c9s commented 2 years ago

our grid strategy config looks like:

exchangeStrategies:

- on: binance
  grid:
    symbol: BTCUSDT
    quantity: 0.001
    # scaleQuantity:
    #   byPrice:
    #     exp:
    #       domain: [20_000, 30_000]
    #       range: [0.2, 0.001]
    gridNumber: 20
    profitSpread: 1000.0  # The profit price spread that you want to add to your sell order when your buy order is executed
    upperPrice: 30_000.0
    lowerPrice: 28_000.0
    # long: true  # The sell order is submitted in the same order amount as the filled corresponding buy order, rather than the same quantity.