This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplus500api.go
76 lines (64 loc) · 1.67 KB
/
plus500api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// TODO change package to plus500api
package main
import "fmt"
import "encoding/json"
import "net/http"
import "io/ioutil"
// Instrument information.
// TODO: Update with readable names
// I: Instrument ID
// B: Buying price
// S: Selling price
// P: Unknown for the moment
type InstrumentInfo struct {
I int
B float64
S float64
P float64
}
// Object to represent the response from Plus500 call
type FeedResponse struct {
Feeds []InstrumentInfo
}
// Retrieve instrument info by ID
func getLastInstrumentInfo(instrumentId int) InstrumentInfo {
url := fmt.Sprintf("http://marketools.plus500.com/Feeds/UpdateTable?instsIds=%d", instrumentId)
resp, err := http.Get(url)
defer resp.Body.Close()
if err != nil {
// handle error
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
var output FeedResponse
if err := json.Unmarshal(body, &output); err != nil {
panic(err)
}
return output.Feeds[0]
}
// Get last buying price given the instrument ID
func getLastBuyPrice(instrumentId int) float64 {
info := getLastInstrumentInfo(instrumentId)
return info.B
}
// Get last selling price given the instrument ID
func getLastSellPrice(instrumentId int) float64 {
info := getLastInstrumentInfo(instrumentId)
return info.S
}
// Main method.
// This only contains some example calls
func main() {
// Show full info
var info = getLastInstrumentInfo(19)
fmt.Println("Full info:")
fmt.Println(info)
// Show last buy price
var lastBuyPrice = getLastBuyPrice(19)
fmt.Println("Last buy price:")
fmt.Println(lastBuyPrice)
// Show last sell price
var lastSellPrice = getLastSellPrice(19)
fmt.Println("Last sellprice:")
fmt.Println(lastSellPrice)
}