Skip to content

Commit

Permalink
fixes for 429
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekonan committed Nov 19, 2021
1 parent 88cd064 commit e770a99
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ make build
"secret": "",
"ccxt_config": {
"enableRateLimit": false,
"timeout": 60000,
"urls": {
"api": {
"public": "http://127.0.0.1:8080/kucoin",
Expand All @@ -33,14 +34,17 @@ make build
}
},
"ccxt_async_config": {
"enableRateLimit": false
"enableRateLimit": false,
"timeout": 60000
}
}
}
```

### Docker (suggested way)

###### Use different tags for different platforms e.g. - main-amd64, main-arm-v6, main-arm-v7, main-arm64

```
docker run --restart=always -p 127.0.0.1:8080:8080 --name freqtrade-proxy -d mikekonan/freqtrade-proxy:main-amd64
```
Expand All @@ -55,6 +59,7 @@ docker run --restart=always -p 127.0.0.1:8080:8080 --name freqtrade-proxy -d mik
"secret": "",
"ccxt_config": {
"enableRateLimit": false,
"timeout": 60000,
"urls": {
"api": {
"public": "http://127.0.0.1:8080/kucoin",
Expand All @@ -63,13 +68,15 @@ docker run --restart=always -p 127.0.0.1:8080:8080 --name freqtrade-proxy -d mik
}
},
"ccxt_async_config": {
"enableRateLimit": false
"enableRateLimit": false,
"timeout": 60000
}
}
}
```

### Docker-compose (best way)

###### Use different tags for different platforms e.g. - main-amd64, main-arm-v6, main-arm-v7, main-arm64

See example - [docker-compose.yml](docker-compose.yml)
Expand All @@ -91,6 +98,7 @@ See example - [docker-compose.yml](docker-compose.yml)
"secret": "",
"ccxt_config": {
"enableRateLimit": false,
"timeout": 60000,
"urls": {
"api": {
"public": "http://freqtrade-proxy:8080/kucoin",
Expand All @@ -99,7 +107,8 @@ See example - [docker-compose.yml](docker-compose.yml)
}
},
"ccxt_async_config": {
"enableRateLimit": false
"enableRateLimit": false,
"timeout": 60000
}
}
}
Expand Down
43 changes: 27 additions & 16 deletions proxy/kucoin/kucoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ func New(s *store.Store) *kucoin {
instance := &kucoin{
client: fasthttp.Client{},
store: s,
subscriptionManager: &subscriptionManager{clients: nil, rl: ratelimit.New(10), l: new(sync.Mutex)},
subscriptionManager: &subscriptionManager{clients: nil, rl: ratelimit.New(9), l: new(sync.Mutex)},
}

svc := sdk.NewApiService(sdk.ApiKeyVersionOption(sdk.ApiKeyVersionV2))
instance.svc = svc

instance.rl = ratelimit.New(20)
instance.rl = ratelimit.New(15)

return instance
}
Expand Down Expand Up @@ -157,6 +157,14 @@ func (ws *ws) serveFor(store *store.Store) {
}
}

func (kucoin *kucoin) kucoinCodeToHttpCode(str string) int {
if len(str) < 3 {
return 200
}

return cast.ToInt(str[:3])
}

func (kucoin *kucoin) getKlines(pair string, timeframe string, startAt int64, endAt int64, retryCount int) (sdk.KLinesModel, *sdk.ApiResponse, error) {
var (
resp *sdk.ApiResponse
Expand All @@ -170,11 +178,12 @@ func (kucoin *kucoin) getKlines(pair string, timeframe string, startAt int64, en
break
}

if i == retryCount || resp.Code != "429000" { //This type of Candlestick is currently not provided
logrus.Warn(i)
if i == retryCount {
return sdk.KLinesModel{}, resp, err
}

time.Sleep(time.Millisecond * 100)
time.Sleep(time.Second)
}

candlesModel := sdk.KLinesModel{}
Expand Down Expand Up @@ -251,14 +260,12 @@ func (kucoin *kucoin) Start(port int) {
candles := kucoin.store.Get("kucoin", pair, timeframe, startTruncated, endTruncated, kucoin.timeframeToDuration(timeframe))

if len(candles) == 0 {
candlesModel, resp, err := kucoin.getKlines(pair, timeframe, startTruncated.Unix(), endAt, 3)
if err != nil {
logrus.Warn(err)
candlesModel, resp, err := kucoin.getKlines(pair, timeframe, startTruncated.Unix(), endAt, 15)
c.Response.SetStatusCode(kucoin.kucoinCodeToHttpCode(resp.Code))
c.Response.SetBody((&apiResp{Code: resp.Code, RawData: resp.RawData, Message: resp.Message}).json())

c.Response.SetStatusCode(200)
c.Response.SetBody((&apiResp{Code: resp.Code, RawData: resp.RawData, Message: resp.Message}).json())

return nil
if len(candlesModel) == 0 {
logrus.Warnf("there is no candle data from kucoin for - '%s'", c.Request.RequestURI())
}

for _, c := range candlesModel {
Expand All @@ -267,11 +274,15 @@ func (kucoin *kucoin) Start(port int) {
kucoin.store.Store(pc)
}

kucoin.subscriptionManager.Subscribe(
kucoin.svc,
sdk.NewSubscribeMessage(fmt.Sprintf("/market/candles:%s_%s", pair, timeframe), false),
kucoin.store,
)
if err == nil {
go kucoin.subscriptionManager.Subscribe(
kucoin.svc,
sdk.NewSubscribeMessage(fmt.Sprintf("/market/candles:%s_%s", pair, timeframe), false),
kucoin.store,
)
}

return nil
}

_, err := c.Write(candles.KucoinRespJSON())
Expand Down

0 comments on commit e770a99

Please sign in to comment.