Code Monkey home page Code Monkey logo

tradingview-scraper's Introduction

tradingview-scraper

Get any market data in real time from the TradingView socket :) Ready to use in your Golang projects!

Inspired by https://github.com/imxeno/tradingview-scraper, I decided to create my own implementation of the TradingView socket using Go

Installation

go get github.com/marcos-gonalons/tradingview-scraper/v2@latest

How to use

Call the Connect() function passing 2 callback functions; one callback for when new market data is read from the socket, and another one used if an error happens while the connection is active

import socket "github.com/marcos-gonalons/tradingview-scraper/v2"

func main() {
    tradingviewsocket, err := socket.Connect(
        func(symbol string, data *socket.QuoteData) {
            fmt.Printf("%#v", symbol)
            fmt.Printf("%#v", data)
        },
        func(err error, context string) {
            fmt.Printf("%#v", "error -> "+err.Error())
            fmt.Printf("%#v", "context -> "+context)
        },
    )
    if err != nil {
        panic("Error while initializing the trading view socket -> " + err.Error())
    }
}

How to add / remove symbols

The implementation allows you to listen for any market data changes, in real time, for any market available in TradingView. In order to tell the socket the symbols (markets) that we want to get the data from, we need to call socket.AddSymbol(), after the connection is stablished.

tradingviewsocket.AddSymbol("OANDA:EURUSD")
tradingviewsocket.AddSymbol("BITSTAMP:BTCUSD")
// etc etc

The syntax for the symbol needs to be broker or exchange name:market. Everytime the socket receives new data from those markets, it will call your callback function.

If you want to stop receiving updates from a particular market, just call RemoveSymbol()

   tradingviewsocket.RemoveSymbol("OANDA:EURUSD")

Callback function

The callback function has 2 parameters; the symbol (market) name, and the data. The data is a struct with these parameters: Price, Volume, Bid, Ask

callbackFn := func(symbol string, data *socket.QuoteData) {
    fmt.Printf("%#v", symbol)
    fmt.Printf("%#v", data)
    if data.Price != nil {
        fmt.Printf("%#v", "Price has changed")
    }
    if data.Volume != nil {
        fmt.Printf("%#v", "Volume has changed")
    }
    if data.Bid != nil {
        fmt.Printf("%#v", "Bid has changed")
    }
    if data.Ask != nil {
        fmt.Printf("%#v", "Ask has changed")
    }
}

Everytime new data is received from the socket, it will call your callback function. This means that not always all the parameters will be available; sometimes, only the bid changes, or only the price changes, or only the volume, or a combination of any of those. The ones that did not change will be nil, since all of them are pointers to float64.

Buy me a coffee?

If you found this repository useful for your needs, please consider sending a donation :) I highly appreciate it

  • Bitcoin: 33qUftxYZfSsinWsFRBGx29EawPPpqCnnu
  • Algorand: KKVNSSPLZVTSSNYNMDYAAFAJP2ELIQRBRIPDIQEYLKKDU6B5IKW4H4PTW4
  • ADA: addr1q8uuve52wjde69mwg5q6y8ghd24r4gtlrtlw93v7w9d34alrzr27g03klu862usxqsru794d03gzkk8n86ta34n85z0s55uund
  • Dogecoin: DLcJGsAmLC8cnGtsDhxvdJPi53geVXBeAS
  • ShibaInu: 0x5314f12a590b4662577af6e645b8ef44f8cf2497
  • Solana: AZDfsdFfESE8HJfD5neCCGHFY2F4hKcxJCHW3nyyH79J
  • Monero: 88g1yVAA6ot7maGyq4Vtz6RfdS3Dwm5VMMHqexH9FsNaR7yU3hi8GT5Sxr3A7YUyoNY5sEhwHRBpSHg9rFUTtuqUJoAbSxk

Or paypal: https://www.paypal.com/paypalme/mgonalonscamps

tradingview-scraper's People

Contributors

marcos-gonalons avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tradingview-scraper's Issues

Panic: concurrent write to websocket connection

Hi,

Here my simple app

package main

import (
	"log"

	socket "github.com/marcos-gonalons/tradingview-scraper/v2"
)

func main() {
    tradingviewsocket, err := socket.Connect(
        func(symbol string, data *socket.QuoteData) {
            log.Printf("%#v", symbol)
            log.Printf("%#v", data)
        },
        func(err error, context string) {
            log.Printf("%#v", "error -> "+err.Error())
            log.Printf("%#v", "context -> "+context)
        },
    )
    if err != nil {
        panic("Error while initializing the trading view socket -> " + err.Error())
    }
	log.Printf(`Open websocket: %+v`, tradingviewsocket)
	ch := make(chan int)
	<- ch
}

Here fail:

.\trading-view.exe
2022/11/11 14:42:08 socket: &{OnReceiveMarketDataCallback:0x1197a00 OnErrorCallback:0x1197ae0 conn:0xc0001de160 isClosed:false sessionID:qs_zIEtTavgxWra}
panic: concurrent write to websocket connection

goroutine 12799 [running]:
github.com/gorilla/websocket.(*messageWriter).flushFrame(0xc00018c120, 0x1, {0x0?, 0x0?, 0x0?})
        C:/Users/mche/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:610 +0x52b
github.com/gorilla/websocket.(*messageWriter).Close(0x0?)
        C:/Users/mche/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:724 +0x45
github.com/gorilla/websocket.(*Conn).beginMessage(0xc0001de160, 0xc000078960, 0x1)
        C:/Users/mche/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:473 +0x42
github.com/gorilla/websocket.(*Conn).NextWriter(0xc0001de160, 0x1)
        C:/Users/mche/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:513 +0x45
github.com/gorilla/websocket.(*Conn).WriteMessage(0x0?, 0x7e?, {0xc00006a000, 0xf, 0x200})
        C:/Users/mche/go/pkg/mod/github.com/gorilla/[email protected]/conn.go:766 +0x152
github.com/marcos-gonalons/tradingview-scraper/v2.(*Socket).connectionLoop.func1(0x1, {0xc00006a000, 0xf, 0x200})
        C:/Users/mche/go/pkg/mod/github.com/marcos-gonalons/tradingview-scraper/[email protected]/socket.go:166 +0x112
created by github.com/marcos-gonalons/tradingview-scraper/v2.(*Socket).connectionLoop
        C:/Users/mche/go/pkg/mod/github.com/marcos-gonalons/tradingview-scraper/[email protected]/socket.go:160 +0x45

I have go1.18.3

What is my mistake? Thank you.

Panic : invalid_parameters "quote_add_symbols"

hi,recntly I got this error :

"error -> Error -> {"m":"critical_error","p":["qs_wfAfBhBePuuw","invalid_parameters","quote_add_symbols"]}""context -> JSON message has an error message""error -> read tcp 192.168.0.112:58532->209.58.146.115:443: use of closed network connection""context -> Error while reading new messages through the socket connection"

code :
`import (
"fmt"
"sync"

socket "github.com/marcos-gonalons/tradingview-scraper/v2"

)

func main() {
var wg sync.WaitGroup
wg.Add(1)
tradingviewsocket, err := socket.Connect(
func(symbol string, data *socket.QuoteData) {
fmt.Printf("%#v", symbol)
fmt.Printf("%#v", data)
},
func(err error, context string) {
fmt.Printf("%#v", "error -> "+err.Error())
fmt.Printf("%#v", "context -> "+context)
},
)
if err != nil {
panic("Error while initializing the trading view socket -> " + err.Error())
}
tradingviewsocket.AddSymbol("BITSTAMP:BTCUSD")
wg.Wait()
}`

could you please help to fix it
best regard

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.