From f9f8e5e58faaebc879ea768798cafd361fb13d0f Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:18:12 +0800 Subject: [PATCH 01/13] bump appVersion to v1.21.0 --- charts/bbgo/Chart.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/bbgo/Chart.yaml b/charts/bbgo/Chart.yaml index e94e9e706..5574462e8 100644 --- a/charts/bbgo/Chart.yaml +++ b/charts/bbgo/Chart.yaml @@ -20,4 +20,4 @@ version: 0.2.0 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. -appVersion: 1.15.0 +appVersion: 1.21.0 From 7e51265bc7fe31f55e08ca5102befae5cf952e05 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:18:28 +0800 Subject: [PATCH 02/13] bump chart version to 0.3.0 --- charts/bbgo/Chart.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/bbgo/Chart.yaml b/charts/bbgo/Chart.yaml index 5574462e8..85c81631f 100644 --- a/charts/bbgo/Chart.yaml +++ b/charts/bbgo/Chart.yaml @@ -15,7 +15,7 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.2.0 +version: 0.3.0 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to From dbaf61d6626a51db4fb4e9f2b7a9643aa47c6c48 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:24:35 +0800 Subject: [PATCH 03/13] chart: add podmonitor --- charts/bbgo/templates/podmonitor.yaml | 15 +++++++++++++++ charts/bbgo/values.yaml | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 charts/bbgo/templates/podmonitor.yaml diff --git a/charts/bbgo/templates/podmonitor.yaml b/charts/bbgo/templates/podmonitor.yaml new file mode 100644 index 000000000..ce96fb638 --- /dev/null +++ b/charts/bbgo/templates/podmonitor.yaml @@ -0,0 +1,15 @@ +{{- if .Values.metrics.enabled }} +--- +apiVersion: monitoring.coreos.com/v1 +kind: PodMonitor +metadata: + name: {{ include "bbgo.fullname" . }} + labels: + {{- include "bbgo.labels" . | nindent 4 }} +spec: + selector: + matchLabels: + {{- include "bbgo.selectorLabels" . | nindent 6 }} + podMetricsEndpoints: + - port: metrics +{{- end }} \ No newline at end of file diff --git a/charts/bbgo/values.yaml b/charts/bbgo/values.yaml index aaec58c3e..2d9e55345 100644 --- a/charts/bbgo/values.yaml +++ b/charts/bbgo/values.yaml @@ -64,6 +64,9 @@ ingress: # hosts: # - chart-example.local +metrics: + enabled: false + resources: # We usually recommend not to specify default resources and to leave this as a conscious # choice for the user. This also increases chances charts run on environments with little From 382449ec49f6409c0e40771aa0b51a854f76fa6e Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:42:05 +0800 Subject: [PATCH 04/13] chart: add webserver option and metrics option --- charts/bbgo/templates/deployment.yaml | 22 ++++++++++++++++++---- charts/bbgo/values.yaml | 4 ++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/charts/bbgo/templates/deployment.yaml b/charts/bbgo/templates/deployment.yaml index 32874f64e..a01149c91 100644 --- a/charts/bbgo/templates/deployment.yaml +++ b/charts/bbgo/templates/deployment.yaml @@ -33,10 +33,24 @@ spec: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} - # ports: - # - name: http - # containerPort: 80 - # protocol: TCP + args: + {{- if .Values.metrics.enabled }} + - "--metrics" + - "--metrics-port" + - {{ .Values.metrics.port | quote }} + {{- end }} + - "run" + - "--config" + - "/config/bbgo.yaml" + - "--no-compile" + {{- if .Values.webserver.enabled }} + - "--enable-webserver" + {{- end }} + + ports: + - name: metrics + containerPort: 9090 + protocol: TCP # livenessProbe: # httpGet: # path: / diff --git a/charts/bbgo/values.yaml b/charts/bbgo/values.yaml index 2d9e55345..75c9f03db 100644 --- a/charts/bbgo/values.yaml +++ b/charts/bbgo/values.yaml @@ -64,8 +64,12 @@ ingress: # hosts: # - chart-example.local +webserver: + enabled: false + metrics: enabled: false + port: 9090 resources: # We usually recommend not to specify default resources and to leave this as a conscious From 71225c3374c80a06103d27ecb3a6fc548988a086 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:43:50 +0800 Subject: [PATCH 05/13] add changelog util --- utils/changelog.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 utils/changelog.sh diff --git a/utils/changelog.sh b/utils/changelog.sh new file mode 100644 index 000000000..e0969c16c --- /dev/null +++ b/utils/changelog.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# Generate a Markdown change log of pull requests from commits between two tags +# Author: Russell Heimlich +# URL: https://gist.github.com/kingkool68/09a201a35c83e43af08fcbacee5c315a + +# HOW TO USE +# Copy this script to a directory under Git version control +# Make the script executable i.e. chmod +x changelog.sh +# Run it! ./changelog.sh +# Check CHANGELOG.md to see your results + +# Repo URL to base links off of +REPOSITORY_URL=https://github.com/wp-cli/wp-cli + +# Get a list of all tags in reverse order +# Assumes the tags are in version format like v1.2.3 +GIT_TAGS=$(git tag -l --sort=-version:refname) + +# Make the tags an array +TAGS=($GIT_TAGS) +LATEST_TAG=${TAGS[0]} +PREVIOUS_TAG=${TAGS[1]} + +# If you want to specify your own two tags to compare, uncomment and enter them below +# LATEST_TAG=v0.23.1 +# PREVIOUS_TAG=v0.22.0 + +# Get a log of commits that occured between two tags +# We only get the commit hash so we don't have to deal with a bunch of ugly parsing +# See Pretty format placeholders at https://git-scm.com/docs/pretty-formats +COMMITS=$(git log $PREVIOUS_TAG..$LATEST_TAG --pretty=format:"%H") + +# Store our changelog in a variable to be saved to a file at the end +MARKDOWN="[Full Changelog]($REPOSITORY_URL/compare/$PREVIOUS_TAG...$LATEST_TAG)" +MARKDOWN+='\n' + +# Loop over each commit and look for merged pull requests +for COMMIT in $COMMITS; do + # Get the subject of the current commit + SUBJECT=$(git log -1 ${COMMIT} --pretty=format:"%s") + + # If the subject contains "Merge pull request #xxxxx" then it is deemed a pull request + PULL_REQUEST=$( grep -Eo "Merge pull request #[[:digit:]]+" <<< "$SUBJECT" ) + if [[ $PULL_REQUEST ]]; then + # Perform a substring operation so we're left with just the digits of the pull request + PULL_NUM=${PULL_REQUEST#"Merge pull request #"} + # AUTHOR_NAME=$(git log -1 ${COMMIT} --pretty=format:"%an") + # AUTHOR_EMAIL=$(git log -1 ${COMMIT} --pretty=format:"%ae") + + # Get the body of the commit + BODY=$(git log -1 ${COMMIT} --pretty=format:"%b") + MARKDOWN+='\n' + MARKDOWN+=" - [#$PULL_NUM]($REPOSITORY_URL/pull/$PULL_NUM): $BODY" + fi +done + +# Save our markdown to a file +echo -e $MARKDOWN From 7eb290c5cbdfecbd5a5fea9622e2452b170dff1d Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:44:30 +0800 Subject: [PATCH 06/13] changelog.sh: fix repo link --- utils/changelog.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/changelog.sh b/utils/changelog.sh index e0969c16c..33a5c1449 100644 --- a/utils/changelog.sh +++ b/utils/changelog.sh @@ -10,7 +10,7 @@ # Check CHANGELOG.md to see your results # Repo URL to base links off of -REPOSITORY_URL=https://github.com/wp-cli/wp-cli +REPOSITORY_URL=https://github.com/c9s/bbgo # Get a list of all tags in reverse order # Assumes the tags are in version format like v1.2.3 From a8cdc4fb61fa949793e885ee0dd74b3555df2ae9 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:50:20 +0800 Subject: [PATCH 07/13] add livenessProbe --- charts/bbgo/templates/deployment.yaml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/charts/bbgo/templates/deployment.yaml b/charts/bbgo/templates/deployment.yaml index a01149c91..7f3164b02 100644 --- a/charts/bbgo/templates/deployment.yaml +++ b/charts/bbgo/templates/deployment.yaml @@ -48,13 +48,15 @@ spec: {{- end }} ports: + {{- if .Values.metrics.enabled }} - name: metrics containerPort: 9090 protocol: TCP - # livenessProbe: - # httpGet: - # path: / - # port: http + livenessProbe: + httpGet: + path: /metrics + port: metrics + {{- end }} # readinessProbe: # httpGet: # path: / From 80419018751249f5d3e59df8cc6a75dc6ea3619b Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:51:55 +0800 Subject: [PATCH 08/13] doc: update feature list --- README.md | 5 ++++- doc/deployment/helm-chart.md | 0 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 doc/deployment/helm-chart.md diff --git a/README.md b/README.md index 34bb1218d..59117da7c 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,10 @@ A trading bot framework written in Go. The name bbgo comes from the BB8 bot in t - Many built-in strategies. - Multi-exchange session support: you can connect to more than 2 exchanges with different accounts or subaccounts. - Standard indicators, e.g., SMA, EMA, BOLL, VMA, MACD... -- React-powered Web Dashboard +- React-powered Web Dashboard. +- Docker image ready. +- Kubernetes support. +- Helm chart ready. ## Screenshots diff --git a/doc/deployment/helm-chart.md b/doc/deployment/helm-chart.md new file mode 100644 index 000000000..e69de29bb From 5e8c1775be86b763470d555ee96b3e334f79d501 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 27 Dec 2021 23:54:23 +0800 Subject: [PATCH 09/13] ignore .deploy dir --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 0f259b69c..726c25d4a 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ bbgo.sqlite3 node_modules otp*png + +/.deploy From e1c02967056627175fe509b8fcc3846440b10183 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 28 Dec 2021 00:09:48 +0800 Subject: [PATCH 10/13] move helm chart deployment process doc --- README.md | 73 -------------------------------- charts/bbgo/values.yaml | 3 ++ doc/deployment/helm-chart.md | 81 ++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 59117da7c..095b6980f 100644 --- a/README.md +++ b/README.md @@ -481,79 +481,6 @@ streambook.BindStream(stream) (TBD) -## Helm Chart - -If you need redis: - -```sh -helm repo add bitnami https://charts.bitnami.com/bitnami -helm install redis bitnami/redis -``` - -To get the dynamically generated redis password, you can use the following command: - -```sh -export REDIS_PASSWORD=$(kubectl get secret --namespace bbgo redis -o jsonpath="{.data.redis-password}" | base64 --decode) -``` - -Prepare your docker image locally (you can also use the docker image from docker hub): - -```sh -make docker DOCKER_TAG=1.16.0 -``` - -The docker tag version number is from the file [Chart.yaml](charts/bbgo/Chart.yaml) - -Choose your instance name: - -```sh -export INSTANCE=grid -``` - -Prepare your secret: - -```sh -kubectl create secret generic bbgo-$INSTANCE --from-env-file .env.local -``` - -Configure your config file, the chart defaults to read config/bbgo.yaml to create a configmap: - -```sh -cp config/grid.yaml bbgo-$INSTANCE.yaml -vim bbgo-$INSTANCE.yaml -``` - -Prepare your configmap: - -```sh -kubectl create configmap bbgo-$INSTANCE --from-file=bbgo.yaml=bbgo-$INSTANCE.yaml -``` - -Install chart with the preferred release name, the release name maps to the previous secret we just created, that -is, `bbgo-grid`: - -```sh -helm install --set existingConfigmap=bbgo-$INSTANCE bbgo-$INSTANCE ./charts/bbgo -``` - -To use the latest version: - -```sh -helm install --set existingConfigmap=bbgo-$INSTANCE --set image.tag=latest bbgo-$INSTANCE ./charts/bbgo -``` - -To upgrade: - -```sh -helm upgrade bbgo-$INSTANCE ./charts/bbgo -helm upgrade --set image.tag=1.15.2 bbgo-$INSTANCE ./charts/bbgo -``` - -Delete chart: - -```sh -helm delete bbgo-$INSTANCE -``` ## Development diff --git a/charts/bbgo/values.yaml b/charts/bbgo/values.yaml index 75c9f03db..b749c9527 100644 --- a/charts/bbgo/values.yaml +++ b/charts/bbgo/values.yaml @@ -16,12 +16,15 @@ nameOverride: "" fullnameOverride: "" # existingConfigMap is used for loading the existing configmap +# optional, if you have a configmap rather than the default configmap name existingConfigMap: # configmap could be a string pointing to the local file in the chart # or a object with inlined bbgo yaml config configmap: +# dotenv defines environment variables, which are loaded from a k8s secret. +# if not defined, then release name will be used to load the secret. dotenv: secret: null diff --git a/doc/deployment/helm-chart.md b/doc/deployment/helm-chart.md index e69de29bb..bf048d62b 100644 --- a/doc/deployment/helm-chart.md +++ b/doc/deployment/helm-chart.md @@ -0,0 +1,81 @@ +## Helm Chart + +If you need redis: + +```sh +helm repo add bitnami https://charts.bitnami.com/bitnami +helm install redis bitnami/redis +``` + +To get the dynamically generated redis password, you can use the following command: + +```sh +export REDIS_PASSWORD=$(kubectl get secret --namespace bbgo redis -o jsonpath="{.data.redis-password}" | base64 --decode) +``` + +Prepare your docker image locally (you can also use the docker image from docker hub): + +```sh +make docker DOCKER_TAG=1.16.0 +``` + +The docker tag version number is from the file [Chart.yaml](charts/bbgo/Chart.yaml) + +Choose your instance name: + +```sh +export INSTANCE=grid +``` + +Prepare your secret: + +```sh +kubectl create secret generic bbgo-$INSTANCE --from-env-file .env.local +``` + +Configure your config file, the chart defaults to read config/bbgo.yaml to create a configmap: + +```sh +cp config/grid.yaml bbgo-$INSTANCE.yaml +vim bbgo-$INSTANCE.yaml +``` + +Prepare your configmap: + +```sh +kubectl create configmap bbgo-$INSTANCE --from-file=bbgo.yaml=bbgo-$INSTANCE.yaml +``` + +Install chart with the preferred release name, the release name maps to the previous secret we just created, that +is, `bbgo-grid`: + +```sh +helm install bbgo-$INSTANCE ./charts/bbgo +``` + +By default, the helm chart uses configmap and dotenv secret by the release name, +if you have an existing configmap that is not named `bbgo-$INSTANCE`, you can specify the configmap via +the `existingConfigmap` option: + +```sh +helm install --set existingConfigmap=bbgo-$INSTANCE bbgo-$INSTANCE ./charts/bbgo +``` + +To use the latest version: + +```sh +helm install --set existingConfigmap=bbgo-$INSTANCE --set image.tag=latest bbgo-$INSTANCE ./charts/bbgo +``` + +To upgrade: + +```sh +helm upgrade bbgo-$INSTANCE ./charts/bbgo +helm upgrade --set image.tag=1.15.2 bbgo-$INSTANCE ./charts/bbgo +``` + +Delete chart: + +```sh +helm delete bbgo-$INSTANCE +``` From 07dfae19bbb51cfaa33da5dc760e91b795852b42 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 28 Dec 2021 00:18:37 +0800 Subject: [PATCH 11/13] rearrange doc --- README.md | 69 ++----------------- doc/deployment/helm-chart.md | 11 ++- ...new-exchange.md => adding-new-exchange.md} | 0 doc/development/migration.md | 60 ++++++++++++++++ 4 files changed, 74 insertions(+), 66 deletions(-) rename doc/development/{new-exchange.md => adding-new-exchange.md} (100%) create mode 100644 doc/development/migration.md diff --git a/README.md b/README.md index 095b6980f..a861dcde2 100644 --- a/README.md +++ b/README.md @@ -477,15 +477,15 @@ streambook := types.NewStreamBook(symbol) streambook.BindStream(stream) ``` -## How To Add A New Exchange - -(TBD) +## Deployment +- [Helm Chart](./doc/deployment/helm-chart.md) +- Baremetal machine or a VPS ## Development -The overview function flow at bbgo -![image info](./assets/overview.svg) +- [Adding New Exchange](./doc/development/adding-new-exchange.md) +- [Migration](./doc/development/migration.md) ### Setting up your local repository @@ -497,65 +497,6 @@ The overview function flow at bbgo 6. Push your changes to your fork. 7. Send a pull request. -### Adding new migration - -1. The project used rockerhopper for db migration. -https://github.com/c9s/rockhopper - - -2. Create migration files - -```sh -rockhopper --config rockhopper_sqlite.yaml create --type sql add_pnl_column -rockhopper --config rockhopper_mysql.yaml create --type sql add_pnl_column -``` - -or you can use the util script: - -``` -bash utils/generate-new-migration.sh add_pnl_column -``` - -Be sure to edit both sqlite3 and mysql migration files. ( [Sample](migrations/mysql/20210531234123_add_kline_taker_buy_columns.sql) ) - - -To test the drivers, you have to update the rockhopper_mysql.yaml file to connect your database, -then do: - -```sh -rockhopper --config rockhopper_sqlite.yaml up -rockhopper --config rockhopper_mysql.yaml up -``` - -Then run the following command to compile the migration files into go files: - -```shell -make migrations -``` - -or - -```shell - rockhopper compile --config rockhopper_mysql.yaml --output pkg/migrations/mysql - rockhopper compile --config rockhopper_sqlite.yaml --output pkg/migrations/sqlite3 - git add -v pkg/migrations && git commit -m "compile and update migration package" pkg/migrations || true -``` - - -If you want to override the DSN and the Driver defined in the YAML config file, you can add some env vars in your dotenv file like this: - -```shell -ROCKHOPPER_DRIVER=mysql -ROCKHOPPER_DIALECT=mysql -ROCKHOPPER_DSN="root:123123@unix(/opt/local/var/run/mysql57/mysqld.sock)/bbgo" -``` - -And then, run: - -```shell -dotenv -f .env.local -- rockhopper --config rockhopper_mysql.yaml up -``` - ### Setup frontend development environment ```sh diff --git a/doc/deployment/helm-chart.md b/doc/deployment/helm-chart.md index bf048d62b..f8f855e49 100644 --- a/doc/deployment/helm-chart.md +++ b/doc/deployment/helm-chart.md @@ -1,4 +1,11 @@ -## Helm Chart +# Helm Chart + +## Requirement + +- redis (optional, if you need persistence) +- docker image (you can use the image from docker hub or build one by yourself) + +## Install If you need redis: @@ -74,7 +81,7 @@ helm upgrade bbgo-$INSTANCE ./charts/bbgo helm upgrade --set image.tag=1.15.2 bbgo-$INSTANCE ./charts/bbgo ``` -Delete chart: +## Delete an installed chart ```sh helm delete bbgo-$INSTANCE diff --git a/doc/development/new-exchange.md b/doc/development/adding-new-exchange.md similarity index 100% rename from doc/development/new-exchange.md rename to doc/development/adding-new-exchange.md diff --git a/doc/development/migration.md b/doc/development/migration.md new file mode 100644 index 000000000..77cf0c11a --- /dev/null +++ b/doc/development/migration.md @@ -0,0 +1,60 @@ +# Migration + +### Adding new migration + +1. The project used rockerhopper for db migration. + https://github.com/c9s/rockhopper + + +2. Create migration files + +```sh +rockhopper --config rockhopper_sqlite.yaml create --type sql add_pnl_column +rockhopper --config rockhopper_mysql.yaml create --type sql add_pnl_column +``` + +or you can use the util script: + +``` +bash utils/generate-new-migration.sh add_pnl_column +``` + +Be sure to edit both sqlite3 and mysql migration files. ( [Sample](migrations/mysql/20210531234123_add_kline_taker_buy_columns.sql) ) + + +To test the drivers, you have to update the rockhopper_mysql.yaml file to connect your database, +then do: + +```sh +rockhopper --config rockhopper_sqlite.yaml up +rockhopper --config rockhopper_mysql.yaml up +``` + +Then run the following command to compile the migration files into go files: + +```shell +make migrations +``` + +or + +```shell + rockhopper compile --config rockhopper_mysql.yaml --output pkg/migrations/mysql + rockhopper compile --config rockhopper_sqlite.yaml --output pkg/migrations/sqlite3 + git add -v pkg/migrations && git commit -m "compile and update migration package" pkg/migrations || true +``` + + +If you want to override the DSN and the Driver defined in the YAML config file, you can add some env vars in your dotenv file like this: + +```shell +ROCKHOPPER_DRIVER=mysql +ROCKHOPPER_DIALECT=mysql +ROCKHOPPER_DSN="root:123123@unix(/opt/local/var/run/mysql57/mysqld.sock)/bbgo" +``` + +And then, run: + +```shell +dotenv -f .env.local -- rockhopper --config rockhopper_mysql.yaml up +``` From 97bddc99824c5ab736d75103677d4ecbafc664ad Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 28 Dec 2021 00:21:07 +0800 Subject: [PATCH 12/13] doc: update new exchange checklist --- doc/development/adding-new-exchange.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/doc/development/adding-new-exchange.md b/doc/development/adding-new-exchange.md index 447cfcb22..b95235d6b 100644 --- a/doc/development/adding-new-exchange.md +++ b/doc/development/adding-new-exchange.md @@ -8,14 +8,23 @@ You should send multiple small pull request to implement them. ## Checklist -Exchange Interface (minimum) +Exchange Interface - minimum requirement for trading - [ ] QueryMarkets -- [ ] QueryKLines - [ ] QueryTickers -- [ ] QueryOrders -- [ ] QueryTrades +- [ ] QueryOpenOrders - [ ] SubmitOrders +- [ ] CancelOrders +- [ ] NewStream + +Trading History Service Interface - used for syncing user trading data + +- [ ] QueryClosedOrders +- [ ] QueryTrades + +Back-testing service - kline data is used for back-testing + +- [ ] QueryKLines Convert functions: From bb9ef7202872424748a4949966042f4899cb8a36 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 28 Dec 2021 00:49:56 +0800 Subject: [PATCH 13/13] update metricsConnectionStatus metrics --- pkg/bbgo/session.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 5546dd7ac..90c10bc55 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -810,7 +810,7 @@ func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) { "symbol": trade.Symbol, "liquidity": trade.Liquidity(), } - metricsTradingVolume.With(labels).Add(trade.Quantity) + metricsTradingVolume.With(labels).Add(trade.Quantity * trade.Price) metricsTradesTotal.With(labels).Inc() } @@ -818,4 +818,18 @@ func (session *ExchangeSession) bindUserDataStreamMetrics(stream types.Stream) { stream.OnBalanceUpdate(session.metricsBalancesUpdater) stream.OnBalanceSnapshot(session.metricsBalancesUpdater) stream.OnTradeUpdate(session.metricsTradeUpdater) + stream.OnDisconnect(func() { + metricsConnectionStatus.With(prometheus.Labels{ + "exchange": session.ExchangeName.String(), + "margin": session.MarginType(), + "symbol": session.IsolatedMarginSymbol, + }).Set(0.0) + }) + stream.OnConnect(func() { + metricsConnectionStatus.With(prometheus.Labels{ + "exchange": session.ExchangeName.String(), + "margin": session.MarginType(), + "symbol": session.IsolatedMarginSymbol, + }).Set(1.0) + }) }