-
Notifications
You must be signed in to change notification settings - Fork 13
/
market.go
148 lines (131 loc) · 5.78 KB
/
market.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package igmarkets
import (
"bytes"
"context"
"fmt"
"net/http"
)
// MarketData - Subset of OTCWorkingOrder
type MarketData struct {
Bid float64 `json:"bid"`
DelayTime int `json:"delayTime"`
Epic string `json:"epic"`
ExchangeID string `json:"exchangeId"`
Expiry string `json:"expiry"`
High float64 `json:"high"`
InstrumentName string `json:"instrumentName"`
InstrumentType string `json:"instrumentType"`
LotSize float64 `json:"lotSize"`
Low float64 `json:"low"`
MarketStatus string `json:"marketStatus"`
NetChange float64 `json:"netChange"`
Offer float64 `json:"offer"`
PercentageChange float64 `json:"percentageChange"`
ScalingFactor int `json:"scalingFactor"`
StreamingPricesAvailable bool `json:"streamingPricesAvailable"`
UpdateTime string `json:"updateTime"`
UpdateTimeUTC string `json:"updateTimeUTC"`
}
// MarketSearchResponse - Contains the response data for MarketSearch()
type MarketSearchResponse struct {
Markets []MarketData `json:"markets"`
}
// MarketsResponse - Market response for /markets/{epic}
type MarketsResponse struct {
DealingRules DealingRules `json:"dealingRules"`
Instrument Instrument `json:"instrument"`
Snapshot Snapshot `json:"snapshot"`
}
// DealingRules - Part of MarketsResponse
type DealingRules struct {
MarketOrderPreference string `json:"marketOrderPreference"`
TrailingStopsPreference string `json:"trailingStopsPreference"`
MaxStopOrLimitDistance UnitValueFloat `json:"maxStopOrLimitDistance"`
MinControlledRiskStopDistance UnitValueFloat `json:"minControlledRiskStopDistance"`
MinDealSize UnitValueFloat `json:"minDealSize"`
MinNormalStopOrLimitDistance UnitValueFloat `json:"minNormalStopOrLimitDistance"`
MinStepDistance UnitValueFloat `json:"minStepDistance"`
}
// Currency - Part of MarketsResponse
type Currency struct {
BaseExchangeRate float64 `json:"baseExchangeRate"`
Code string `json:"code"`
ExchangeRate float64 `json:"exchangeRate"`
IsDefault bool `json:"isDefault"`
Symbol string `json:"symbol"`
}
// UnitValueFloat - Part of MarketsResponse
type UnitValueFloat struct {
Unit string `json:"unit"`
Value float64 `json:"value"`
}
// Instrument - Part of MarketsResponse
type Instrument struct {
ChartCode string `json:"chartCode"`
ControlledRiskAllowed bool `json:"controlledRiskAllowed"`
Country string `json:"country"`
Currencies []Currency `json:"currencies"`
Epic string `json:"epic"`
Expiry string `json:"expiry"`
StreamingPricesAvailable bool `json:"streamingPricesAvailable"`
ForceOpenAllowed bool `json:"forceOpenAllowed"`
Unit string `json:"unit"`
Type string `json:"type"`
MarketID string `json:"marketID"`
LotSize float64 `json:"lotSize"`
MarginFactor float64 `json:"marginFactor"`
MarginFactorUnit string `json:"marginFactorUnit"`
SlippageFactor UnitValueFloat `json:"slippageFactor"`
LimitedRiskPremium UnitValueFloat `json:"limitedRiskPremium"`
NewsCode string `json:"newsCode"`
ValueOfOnePip string `json:"valueOfOnePip"`
OnePipMeans string `json:"onePipMeans"`
ContractSize string `json:"contractSize"`
SpecialInfo []string `json:"specialInfo"`
}
// Snapshot - Part of MarketsResponse
type Snapshot struct {
MarketStatus string `json:"marketStatus"`
NetChange float64 `json:"netChange"`
PercentageChange float64 `json:"percentageChange"`
UpdateTime string `json:"updateTime"`
DelayTime float64 `json:"delayTime"`
Bid float64 `json:"bid"`
Offer float64 `json:"offer"`
High float64 `json:"high"`
Low float64 `json:"low"`
DecimalPlacesFactor float64 `json:"decimalPlacesFactor"`
ScalingFactor float64 `json:"scalingFactor"`
ControlledRiskExtraSpread float64 `json:"controlledRiskExtraSpread"`
}
// MarketSearch - Search for ISIN or share names to get the epic.
func (ig *IGMarkets) MarketSearch(ctx context.Context, term string) (*MarketSearchResponse, error) {
bodyReq := new(bytes.Buffer)
// E.g. https://demo-api.ig.com/gateway/deal/markets?searchTerm=DE0005008007
url := fmt.Sprintf("%s/gateway/deal/markets?searchTerm=%s", ig.APIURL, term)
req, err := http.NewRequest("GET", url, bodyReq)
if err != nil {
return nil, fmt.Errorf("igmarkets: unable to get markets data: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 1, MarketSearchResponse{})
if err != nil {
return nil, err
}
igResponse, _ := igResponseInterface.(*MarketSearchResponse)
return igResponse, err
}
// GetMarkets - Return markets information for given epic
func (ig *IGMarkets) GetMarkets(ctx context.Context, epic string) (*MarketsResponse, error) {
bodyReq := new(bytes.Buffer)
req, err := http.NewRequest("GET", fmt.Sprintf("%s/gateway/deal/markets/%s",
ig.APIURL, epic), bodyReq)
if err != nil {
return nil, fmt.Errorf("igmarkets: unable to get markets data: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 3, MarketsResponse{})
if err != nil {
return nil, err
}
igResponse, _ := igResponseInterface.(*MarketsResponse)
return igResponse, err
}