saniales/golang-crypto-trading-bot

Is there an example for implementing websocket example?

vladblaj opened this issue · 14 comments

Where exactly do I need to handle the update function? Do you simply do it in binance.go for example in subscribeMarketSummaryFeed?

check examples folder. I would say no

yea onUpdate works well for interval but not sure where to handle onUpdate in websockets cause it is not called. Do you simply add you logic in subscribeMarketSummaryFeed?

for example I would like to send to telegram using a websocket strategy when onUpdate is called. Cause onUpdate by websocket strategy I handled sending messages in subscribeMarketSummary but i don't think that is reasonable
image

Nope, that's the wrong way to resolve this ;)

REST and Websocket make no difference in strategy object usage, you do not see it used because it is wrapped by Strategy.Apply

from here

if hasSetupFunc {
err = wss.Model.Setup(wrappers, markets)
if err != nil && hasErrorFunc {
wss.Model.OnError(err)
}
}

So your setup function must call FeedConnect() to enable websockets, after that just Use the normal Update func

reference on why it works can be extracted from following lines

if !wrapper.websocketOn {
orderbook, _, err := wrapper.orderbookFromREST(market)
if err != nil {
return nil, err
}
wrapper.orderbook.Set(market, orderbook)
return orderbook, nil
}

Like from the example

OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
// do something
return nil
},

Ok thanks, I tried again, the websocket handler gets called for sure in binance.go. But after that I can not see how that is linked to the model onUpdate. It gets here:
image
But will never touch the onUpdate:
image

Also as far as I see there is this comment:
image

Indeed If I do it this way:
image
AND also call onUpdate in websocket it will work though it is not printed every second and again it does not seem right.

The fact that is not printed every second may depend on your internet connection. Can you double check that ?

Also try removing the go func()

I solved the issue using channels. It is not printed every second because it is printed hundred of times per second because of the non blocking for. Could you please show me for websocket strategy where exactly is the onUpdate called?.

The secret is the websocket part, the o update is run in the Apply method of Strategy implementation

It is complex to explain, but if you have websockets the population of wrapped structures is done on the fly by the subscribe functions.

I think the question from @vladblaj is still valid.

@saniales in websocket.go, what do you mean by "update is handled by the developer externally" in:

// update is handled by the developer externally, here we just checked for existence.
	if !hasUpdateFunc {
		_err := errors.New("OnUpdate func cannot be empty")
		if hasErrorFunc {
			wss.Model.OnError(_err)
		} else {
			panic(_err)
		}
	}

Hello @hmedkouri

the update function is decided by the developer strategy, not by the bot itself (the bot only updates the data when the new information comes, but it is up to the strategy, hence the "OnUpdate" function to actually do something)

That is what I meant, do you think we can rephrase it better?

I don't know about your solutions, but I broke my brain.

Just show a working example how to subscribe to changes in summaries and orderbook cache or where I have to put my own for {}.

Because all my attempts always end up the same way:

  • no OnUpdate call
  • the bot just says "bye"

Sorry, but there is no crystal clean (for understanding) example of ws strategy.

you have to pass the OnUpdate function as you would do without websockets, nothing changes, you just add the ws connection before initializing the bot