-
Notifications
You must be signed in to change notification settings - Fork 17
/
poloniex.go
207 lines (149 loc) · 4.66 KB
/
poloniex.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/vevsatechnologies/External_Data_Feed_Processor/models"
// null "gopkg.in/nullbio/null.v6"
null "gopkg.in/nullbio/null.v6"
// null "gopkg.in/volatiletech/null.v6"
"github.com/vattle/sqlboiler/boil"
)
const (
poloniexBaseURL = "https://poloniex.com/public"
)
// Structure containing Poloniex client data
type Poloniex struct {
client *http.Client
}
//Structure containing Poloniex Historic Data
type poloniexData struct {
Result []struct {
GlobalTradeID null.String `json:"globalTradeID"`
TradeID null.String `json:"tradeID"`
Date null.Time `json:"date"`
Types null.String `json:"type"`
Rate null.String `json:"rate"`
Amount null.String `json:"amount"`
Total null.String `json:"total"`
}
}
// Structure containing Poloniex Chart Data
type chartData struct {
Result []struct {
Date null.Time `json:"date"`
High null.String `json:"high"`
Low null.String `json:"low"`
Open null.String `json:"open"`
Close null.String `json:"close"`
Volume null.String `json:"volume"`
QuoteVolume null.String `json:"quoteVolume"`
WeightedAverage null.String `json:"weightedAverage"`
}
}
// Get Poloniex Historic Data
// parameters : Currency pair, Start time , End time
func (p *Poloniex) getPoloniexData(currencyPair string, start string, end string) string {
db, err := sql.Open("postgres", "dbname=data_feed_processor user=postgres host=localhost password=alisha")
if err != nil {
panic(err.Error())
}
boil.SetDB(db)
//Get Url of Poloniex API
url := poloniexBaseURL
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err.Error())
}
//Append user provided parameters in the URL
q := req.URL.Query()
q.Add("command", "returnTradeHistory")
q.Add("currencyPair", currencyPair)
q.Add("start", start)
q.Add("end", end)
req.URL.RawQuery = q.Encode()
//Get Historic Data from the API
request, err := http.NewRequest("GET", req.URL.String(), nil)
res, _ := p.client.Do(request)
//Get response of the request as []byte
fmt.Println(res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
//Map the data to poloniexData struct and unmarshal the contents
var data poloniexData
json.Unmarshal(body, &data)
fmt.Printf("Results: %v\n", data)
for i := range data.Result {
var p1 models.HistoricDatum
// p1.Exchangeid = 0
p1.Globaltradeid = data.Result[i].GlobalTradeID
p1.Tradeid = data.Result[i].TradeID
// p1.timest = data.Result[i].Date
p1.Quantity = data.Result[i].Amount
p1.Price = data.Result[i].Rate
p1.Total = data.Result[i].Total
p1.OrderType = data.Result[i].Types
err := p1.Insert(db)
panic(err.Error())
}
return "Saved poloneix historic data!"
}
//Returns data from Poloniex exchange
//Parameters : currency pair , start time , end time
// func (p *Poloniex) fetchPoloniexData(date string) {
// err := models.HistoricDatum(qm.Where("timestamp=$1", date)).All()
// }
//Returns Poloniex Chart Data
//Parameters : Currency pair, Start time , End time
func (p *Poloniex) getChartData(currencyPair string, start string, end string) {
db, err := sql.Open("postgres", "dbname=data_feed_processor user=postgres host=localhost password=alisha")
if err != nil {
panic(err.Error())
}
boil.SetDB(db)
//Get the base URL
url := poloniexBaseURL
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err.Error())
}
//Append the user defined parameters to the url
q := req.URL.Query()
q.Add("command", "returnChartData")
q.Add("currencyPair", currencyPair)
q.Add("start", start)
q.Add("end", end)
req.URL.RawQuery = q.Encode()
request, err := http.NewRequest("GET", req.URL.String(), nil)
//Get the data from API and convert the data to byte array
res, _ := p.client.Do(request)
fmt.Println(res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
//Store the data to charData struct
var data chartData
json.Unmarshal(body, &data)
fmt.Printf("Results: %v\n", data)
//Loop over the entire data and store it in the table
for i := range data.Result {
var p2 models.ChartDatum
// p2.exchangeID = "0"
//p2.date = data.Result[i].Date
p2.High = data.Result[i].High
p2.Low = data.Result[i].Low
p2.Opening = data.Result[i].Open
p2.Closing = data.Result[i].Close
p2.Volume = data.Result[i].Volume
p2.Quotevolume = data.Result[i].QuoteVolume
// p2.Basevolume = data.Result[i].BaseVolume
p2.Weightedaverage = data.Result[i].WeightedAverage
err := p2.Insert(db)
panic(err.Error())
}
}