golang-crypto-trading-bot icon indicating copy to clipboard operation
golang-crypto-trading-bot copied to clipboard

Is there an example for implementing websocket example?

Open vladblaj opened this issue 3 years ago • 14 comments

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

vladblaj avatar Jan 22 '22 14:01 vladblaj

check examples folder. I would say no

saniales avatar Jan 23 '22 15:01 saniales

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?

vladblaj avatar Jan 23 '22 15:01 vladblaj

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

vladblaj avatar Jan 23 '22 15:01 vladblaj

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

https://github.com/saniales/golang-crypto-trading-bot/blob/b0c20d4990023f4c81b9c4dee645ebff97f9cff0/strategies/websocket.go#L36-L41

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

https://github.com/saniales/golang-crypto-trading-bot/blob/b0c20d4990023f4c81b9c4dee645ebff97f9cff0/exchanges/binance.go#L84-L92

Like from the example

https://github.com/saniales/golang-crypto-trading-bot/blob/b0c20d4990023f4c81b9c4dee645ebff97f9cff0/examples/websocket.go#L39-L42

saniales avatar Jan 24 '22 00:01 saniales

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

vladblaj avatar Jan 25 '22 15:01 vladblaj

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

vladblaj avatar Jan 25 '22 15:01 vladblaj

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.

vladblaj avatar Jan 25 '22 15:01 vladblaj

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()

saniales avatar Jan 26 '22 10:01 saniales

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?.

vladblaj avatar Jan 26 '22 11:01 vladblaj

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.

saniales avatar Jan 26 '22 11:01 saniales

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)
		}
	}

hmedkouri avatar Apr 18 '22 16:04 hmedkouri

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?

saniales avatar Apr 21 '22 16:04 saniales

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.

vanodevium avatar May 16 '22 21:05 vanodevium

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

saniales avatar May 18 '22 08:05 saniales