tradingview

package module
v3.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

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/fabdotnet/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/fabdotnet/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: 3Ets7kos4fG4aiEmrBEmKKpKogPyJJsk9A
  • Ethereum: 0x4E73fcd8847bf456789b5D34Ed483C0474426271
  • ADA: addr1q8x0482rvp7wzjsladlfs89z5rg5pe4s8effvxan66ap8vnrl8psznmjw6cawc6qp89mp98fl4p63eknl8y545wdh4xqpjl08w
  • USDC (ERC-20): 0xfe770Bdf12ecF2036695402F5Af9e646b8A316e3

Documentation

Index

Constants

View Source
const ConnectionSetupMessagesErrorContext = "Sending connection setup messages"

ConnectionSetupMessagesErrorContext ...

View Source
const DecodeFirstMessageErrorContext = "Decoding the first message after stablishing the connection"

DecodeFirstMessageErrorContext ...

View Source
const DecodeMessageErrorContext = "Decoding the JSON message"

DecodeMessageErrorContext ...

View Source
const DecodedMessageDoesNotIncludePayloadErrorContext = "JSON message does not include the payload"

DecodedMessageDoesNotIncludePayloadErrorContext ...

View Source
const DecodedMessageHasErrorPropertyErrorContext = "JSON message has an error message"

DecodedMessageHasErrorPropertyErrorContext ...

View Source
const FinalPayloadCantBeParsedErrorContext = "The final JSON payload of the socket message couldn't be parsed"

FinalPayloadCantBeParsedErrorContext ...

View Source
const FinalPayloadHasMissingPropertiesErrorContext = "The final JSON payload doesn't have the expected data"

FinalPayloadHasMissingPropertiesErrorContext ...

View Source
const FirstMessageWithoutSessionIdErrorContext = "Does not have 'session_id' property"

FirstMessageWithoutSessionIdErrorContext ...

View Source
const GetPayloadLengthErrorContext = "Getting the payload length"

GetPayloadLengthErrorContext ...

View Source
const InitErrorContext = "Initializing the connection"

InitErrorContext ...

View Source
const PayloadCantBeParsedErrorContext = "JSON payload couldn't be parsed"

PayloadCantBeParsedErrorContext ...

View Source
const ReadFirstMessageErrorContext = "Reading the first message after stablishing the connection"

ReadFirstMessageErrorContext

View Source
const ReadMessageErrorContext = "Error while reading new messages through the socket connection"

ReadMessageErrorContext ...

View Source
const SendKeepAliveMessageErrorContext = "Sending the keep alive message"

SendKeepAliveMessageErrorContext ...

View Source
const SendMessageErrorContext = "Sending a message"

SendMessageErrorContext ...

Variables

This section is empty.

Functions

func GetRandomString

func GetRandomString(length int) string

GetRandomString ...

func GetStringRepresentation

func GetStringRepresentation(data interface{}) string

GetStringRepresentation ...

Types

type Flags

type Flags struct {
	Flags []string `json:"flags"`
}

Flags ...

type OnErrorCallback

type OnErrorCallback func(err error, context string)

OnErrorCallback ...

type OnReceiveDataCallback

type OnReceiveDataCallback func(symbol string, data *QuoteData)

OnReceiveDataCallback ...

type QuoteData

type QuoteData struct {
	Price         *float64 `mapstructure:"lp"`
	Volume        *float64 `mapstructure:"volume"`
	Bid           *float64 `mapstructure:"bid"`
	Ask           *float64 `mapstructure:"ask"`
	High          *float64 `mapstructure:"high_price"`
	Low           *float64 `mapstructure:"low_price"`
	Open          *float64 `mapstructure:"open_price"`
	PreviousClose *float64 `mapstructure:"prev_close_price"`
}

QuoteData ...

type QuoteMessage

type QuoteMessage struct {
	Symbol string     `mapstructure:"n"`
	Status string     `mapstructure:"s"`
	Data   *QuoteData `mapstructure:"v"`
}

QuoteMessage ...

type Socket

type Socket struct {
	OnReceiveMarketDataCallback OnReceiveDataCallback
	OnErrorCallback             OnErrorCallback
	// contains filtered or unexported fields
}

Socket ...

func (*Socket) AddSymbol

func (s *Socket) AddSymbol(symbol string) (err error)

AddSymbol ...

func (*Socket) Close

func (s *Socket) Close() (err error)

Close ...

func (*Socket) Init

func (s *Socket) Init() (err error)

Init connects to the tradingview web socket

func (*Socket) RemoveSymbol

func (s *Socket) RemoveSymbol(symbol string) (err error)

RemoveSymbol ...

type SocketInterface

type SocketInterface interface {
	AddSymbol(symbol string) error
	RemoveSymbol(symbol string) error
	Init() error
	Close() error
}

SocketInterface ...

func Connect

func Connect(
	onReceiveMarketDataCallback OnReceiveDataCallback,
	onErrorCallback OnErrorCallback,
) (socket SocketInterface, err error)

Connect - Connects and returns the trading view socket object

type SocketMessage

type SocketMessage struct {
	Message string      `json:"m"`
	Payload interface{} `json:"p"`
}

SocketMessage ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL