mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #459 from zenixls2/feature/paper_trade
add binance paper trade endpoint
This commit is contained in:
commit
e18ab13abe
|
@ -173,6 +173,15 @@ bbgo pnl --exchange binance --asset BTC --since "2019-01-01"
|
||||||
|
|
||||||
## Advanced Configuration
|
## Advanced Configuration
|
||||||
|
|
||||||
|
### Testnet (Paper Trading)
|
||||||
|
|
||||||
|
Currently only supports binance testnet.
|
||||||
|
To run bbgo in testnet, apply new API keys from [Binance Test Network](https://testnet.binance.vision), and set the following env before you start bbgo:
|
||||||
|
```bash
|
||||||
|
export PAPER_TRADE=1
|
||||||
|
export DISABLE_MARKET_CACHE=1 # the symbols supported in testnet is far less than the mainnet
|
||||||
|
```
|
||||||
|
|
||||||
### Notification
|
### Notification
|
||||||
|
|
||||||
- [Setting up Telegram notification](./doc/configuration/telegram.md)
|
- [Setting up Telegram notification](./doc/configuration/telegram.md)
|
||||||
|
|
|
@ -24,14 +24,19 @@ import (
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
"github.com/c9s/bbgo/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const BNB = "BNB"
|
const BNB = "BNB"
|
||||||
|
|
||||||
const BinanceUSBaseURL = "https://api.binance.us"
|
const BinanceUSBaseURL = "https://api.binance.us"
|
||||||
|
const BinanceTestBaseURL = "https://testnet.binance.vision"
|
||||||
const BinanceUSWebSocketURL = "wss://stream.binance.us:9443"
|
const BinanceUSWebSocketURL = "wss://stream.binance.us:9443"
|
||||||
const WebSocketURL = "wss://stream.binance.com:9443"
|
const WebSocketURL = "wss://stream.binance.com:9443"
|
||||||
|
const WebSocketTestURL = "wss://testnet.binance.vision"
|
||||||
|
const FutureTestBaseURL = "https://testnet.binancefuture.com"
|
||||||
const FuturesWebSocketURL = "wss://fstream.binance.com"
|
const FuturesWebSocketURL = "wss://fstream.binance.com"
|
||||||
|
const FuturesWebSocketTestURL = "wss://stream.binancefuture.com"
|
||||||
|
|
||||||
// 5 per second and a 2 initial bucket
|
// 5 per second and a 2 initial bucket
|
||||||
var orderLimiter = rate.NewLimiter(5, 2)
|
var orderLimiter = rate.NewLimiter(5, 2)
|
||||||
|
@ -56,6 +61,11 @@ func isBinanceUs() bool {
|
||||||
return err == nil && v
|
return err == nil && v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func paperTrade() bool {
|
||||||
|
v, ok := util.GetEnvVarBool("PAPER_TRADE")
|
||||||
|
return ok && v
|
||||||
|
}
|
||||||
|
|
||||||
type Exchange struct {
|
type Exchange struct {
|
||||||
types.MarginSettings
|
types.MarginSettings
|
||||||
types.FuturesSettings
|
types.FuturesSettings
|
||||||
|
@ -80,6 +90,11 @@ func New(key, secret string) *Exchange {
|
||||||
client.BaseURL = BinanceUSBaseURL
|
client.BaseURL = BinanceUSBaseURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if paperTrade() {
|
||||||
|
client.BaseURL = BinanceTestBaseURL
|
||||||
|
futuresClient.BaseURL = FutureTestBaseURL
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if len(key) > 0 && len(secret) > 0 {
|
if len(key) > 0 && len(secret) > 0 {
|
||||||
timeSetter.Do(func() {
|
timeSetter.Do(func() {
|
||||||
|
|
|
@ -40,7 +40,6 @@ func (s *OrderService) Sync(ctx context.Context, exchange types.Exchange, symbol
|
||||||
symbol = futuresSettings.IsolatedFuturesSymbol
|
symbol = futuresSettings.IsolatedFuturesSymbol
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
records, err := s.QueryLast(exchange.Name(), symbol, isMargin, isFutures, isIsolated, 50)
|
records, err := s.QueryLast(exchange.Name(), symbol, isMargin, isFutures, isIsolated, 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
"github.com/c9s/bbgo/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrNotImplemented = errors.New("not implemented")
|
var ErrNotImplemented = errors.New("not implemented")
|
||||||
|
@ -23,6 +24,11 @@ type SyncService struct {
|
||||||
DepositService *DepositService
|
DepositService *DepositService
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func paperTrade() bool {
|
||||||
|
v, ok := util.GetEnvVarBool("PAPER_TRADE")
|
||||||
|
return ok && v
|
||||||
|
}
|
||||||
|
|
||||||
// SyncSessionSymbols syncs the trades from the given exchange session
|
// SyncSessionSymbols syncs the trades from the given exchange session
|
||||||
func (s *SyncService) SyncSessionSymbols(ctx context.Context, exchange types.Exchange, startTime time.Time, symbols ...string) error {
|
func (s *SyncService) SyncSessionSymbols(ctx context.Context, exchange types.Exchange, startTime time.Time, symbols ...string) error {
|
||||||
markets, err := cache.LoadExchangeMarketsWithCache(ctx, exchange)
|
markets, err := cache.LoadExchangeMarketsWithCache(ctx, exchange)
|
||||||
|
@ -44,6 +50,10 @@ func (s *SyncService) SyncSessionSymbols(ctx context.Context, exchange types.Exc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if paperTrade() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
log.Infof("syncing %s deposit records...", exchange.Name())
|
log.Infof("syncing %s deposit records...", exchange.Name())
|
||||||
if err := s.DepositService.Sync(ctx, exchange); err != nil {
|
if err := s.DepositService.Sync(ctx, exchange); err != nil {
|
||||||
if err != ErrNotImplemented {
|
if err != ErrNotImplemented {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user