Documentation ¶
Overview ¶
Package go-tradingview provides an interface to watch and action on updates of Stock symbols via the TradingView WebSocket interface.
Index ¶
- type Quote
- type TradingView
- func (tv *TradingView) Connect()
- func (tv *TradingView) ConnectAndStart()
- func (tv *TradingView) GetLastQuote(symbol string) (quote Quote, ok bool)
- func (tv *TradingView) GetQuote(symbol string, callback func(Quote))
- func (tv *TradingView) OnUpdate(symbol string, callback func(Quote) (shouldDelete bool))
- func (tv *TradingView) Start()
- func (tv *TradingView) Update(symbol string, quote Quote)
- func (tv *TradingView) Watch(symbol string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Quote ¶
type Quote struct { Symbol string `json:"short_name"` FullName string `json:"description"` CurrencyCode string `json:"currency_code"` IsTradable bool `json:"is_tradable"` Exchange string `json:"listed_exchange"` OriginalName string `json:"original_name"` ProName string `json:"pro_name"` CurrentSession string `json:"current_session"` LastPrice float64 `json:"lp"` Change float64 `json:"ch"` ChangePercentage float64 `json:"chp"` LivePrice float64 `json:"rtc"` LiveChange float64 `json:"rch"` LiveChangePercentage float64 `json:"rchp"` }
type TradingView ¶
type TradingView struct { OnConnected func(tv TradingView) OnConnectError func(err error, tv TradingView) OnDisconnected func(err error, tv TradingView) Watching map[string]Quote IsConnected bool // contains filtered or unexported fields }
func NewClient ¶
func NewClient() TradingView
Return a new instance of a TradingView.
Example ¶
package main import ( "github.com/elliottcarlson/tradingview" ) func main() { client := tradingview.NewClient() client.Connect() }
Output:
func (*TradingView) Connect ¶
func (tv *TradingView) Connect()
Connect the current instance of TradingView to the TradingView WebSocket.
Example ¶
package main import ( "github.com/elliottcarlson/tradingview" ) func main() { client := tradingview.NewClient() client.Connect() }
Output:
func (*TradingView) ConnectAndStart ¶
func (tv *TradingView) ConnectAndStart()
Connect and start the main event loop in one command.
func (*TradingView) GetLastQuote ¶
func (tv *TradingView) GetLastQuote(symbol string) (quote Quote, ok bool)
Retrieve the most recent quote for the specified symbol. If the symbol is already being watched, it will return the latest quote available, and a true value to indicate this request was successful. If the symbol is not watched, it will return an empty TradingViewQuote struct, and a false value to indicate the quote is not being watched.
Example ¶
package main import ( "fmt" "github.com/elliottcarlson/tradingview" ) func main() { client := tradingview.NewClient() client.Connect() client.Watch("AAPL") if quote, ok := client.GetLastQuote("AAPL"); ok { // ... Handle the received quote data. fmt.Println(quote) } client.Start() }
Output:
func (*TradingView) GetQuote ¶
func (tv *TradingView) GetQuote(symbol string, callback func(Quote))
Retrieve the specified symbols quote, with a callback for when the quote resolves. If the symbol is already being watched, it will call the provided callback with the last cached version of the symbol. If the symbol is not being watched yet, it will If the symbol is not in the existing watch list, it will add it, and queue up a response to the callback once it has a quote to provide. The callback should expect to be called once, with a TradingViewQuote containing the latest quote for the specified symbol.
Example ¶
package main import ( "fmt" "github.com/elliottcarlson/tradingview" ) func main() { client := tradingview.NewClient() client.Connect() client.GetQuote("AAPL", func(quote tradingview.Quote) { // .. Handle the received quote data. fmt.Printf("AAPL last price: %f\n", quote.LastPrice) }) client.Start() }
Output:
func (*TradingView) OnUpdate ¶
func (tv *TradingView) OnUpdate(symbol string, callback func(Quote) (shouldDelete bool))
Create a notification request for all updates to the specified symbol which will call the specified callback for each update event. The callback should expect a TradingViewQuote for each updated quote value it receives, until the callback returns true to indicate it should stop receiving updates. Returning false in the callback will indicate that it should continue to notify on updates for the specified symbol.
Example ¶
package main import ( "fmt" "github.com/elliottcarlson/tradingview" ) func main() { client := tradingview.NewClient() client.Connect() client.OnUpdate("AAPL", func(quote tradingview.Quote) (shouldDelete bool) { // ... Handle the received quote data. if quote.LastPrice > 500.00 { fmt.Println("AAPL is now over $500.00!") return true // Stop calling this callback for each update. } return false // Continue listening for future updates. }) client.Start() }
Output:
func (*TradingView) Start ¶
func (tv *TradingView) Start()
Start the main event loop after a connection is established.
func (*TradingView) Update ¶
func (tv *TradingView) Update(symbol string, quote Quote)
Send an updated quote for the specified symbol. Sends the provided TradingViewQuote to all callbacks listening for updates on the specified symbol. This method is useful for testing or replaying quotes, but does not provide functionality that changes the interaction with the TradingView WebSocket.
func (*TradingView) Watch ¶
func (tv *TradingView) Watch(symbol string)
Add the specified symbol to the TradingView watch list.
Example ¶
package main import ( "github.com/elliottcarlson/tradingview" ) func main() { client := tradingview.NewClient() client.Connect() client.Watch("IBM") client.Watch("MSFT") client.Watch("AAPL") client.Start() }
Output: