github.com/diadata-org/diadata@v1.4.593/pkg/model/stockQuotation.go (about) 1 package models 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "time" 7 8 clientInfluxdb "github.com/influxdata/influxdb1-client/v2" 9 ) 10 11 // SetStockQuotationInflux stores a stock quotation to an influx batch. 12 func (datastore *DB) SetStockQuotation(sq StockQuotation) error { 13 fields := map[string]interface{}{ 14 "priceAsk": sq.PriceAsk, 15 "priceBid": sq.PriceBid, 16 "sizeAsk": sq.SizeAskLot, 17 "sizeBid": sq.SizeBidLot, 18 "source": sq.Source, 19 } 20 tags := map[string]string{ 21 "symbol": sq.Symbol, 22 "name": sq.Name, 23 "isin": sq.ISIN, 24 } 25 pt, err := clientInfluxdb.NewPoint(influxDbStockQuotationsTable, tags, fields, sq.Time) 26 if err != nil { 27 log.Errorln("NewOptionInflux:", err) 28 } else { 29 datastore.addPoint(pt) 30 } 31 err = datastore.WriteBatchInflux() 32 if err != nil { 33 log.Errorln("Write influx batch: ", err) 34 } 35 36 return err 37 } 38 39 // GetStockQuotationInflux returns the last quotation of @symbol before @timestamp. 40 func (db *DB) GetStockQuotation(source string, symbol string, timeInit time.Time, timeFinal time.Time) ([]StockQuotation, error) { 41 stockQuotations := []StockQuotation{} 42 43 unixtimeInit := timeInit.UnixNano() 44 unixtimeFinal := timeFinal.UnixNano() 45 46 query := "SELECT priceAsk,priceBid,sizeAsk,sizeBid,source,\"isin\",\"name\" FROM %s WHERE source='%s' and \"symbol\"='%s' and time>%d and time<=%d order by time desc" 47 q := fmt.Sprintf(query, influxDbStockQuotationsTable, source, symbol, unixtimeInit, unixtimeFinal) 48 res, err := queryInfluxDB(db.influxClient, q) 49 if err != nil { 50 fmt.Println("Error querying influx") 51 return stockQuotations, err 52 } 53 54 if len(res) > 0 && len(res[0].Series) > 0 { 55 layout := "2006-01-02T15:04:05Z" 56 vals := res[0].Series[0].Values 57 58 for i := 0; i < len(vals); i++ { 59 var stockQuotation StockQuotation 60 stockQuotation.Time, err = time.Parse(layout, vals[i][0].(string)) 61 if err != nil { 62 log.Error(err) 63 } 64 stockQuotation.PriceAsk, err = vals[i][1].(json.Number).Float64() 65 if err != nil { 66 log.Error(err) 67 } 68 stockQuotation.PriceBid, err = vals[i][2].(json.Number).Float64() 69 if err != nil { 70 log.Error(err) 71 } 72 stockQuotation.SizeAskLot, err = vals[i][3].(json.Number).Float64() 73 if err != nil { 74 log.Error(err) 75 } 76 stockQuotation.SizeBidLot, err = vals[i][4].(json.Number).Float64() 77 if err != nil { 78 log.Error(err) 79 } 80 stockQuotation.Source = vals[i][5].(string) 81 stockQuotation.ISIN = vals[i][6].(string) 82 stockQuotation.Name = vals[i][7].(string) 83 stockQuotation.Symbol = symbol 84 85 stockQuotations = append(stockQuotations, stockQuotation) 86 } 87 return stockQuotations, nil 88 89 } 90 return stockQuotations, err 91 } 92 93 // GetStockSymbols returns all symbols available from @source. 94 func (db *DB) GetStockSymbols() (map[Stock]string, error) { 95 allStocks := make(map[Stock]string) 96 97 q := fmt.Sprintf("SELECT \"symbol\",\"name\",\"isin\",source FROM %s WHERE time>now()-7d", influxDbStockQuotationsTable) 98 res, err := queryInfluxDB(db.influxClient, q) 99 if err != nil { 100 log.Error("query stock symbols from influx: ", err) 101 return allStocks, err 102 } 103 104 if len(res) > 0 && len(res[0].Series) > 0 { 105 // make unique list of stocks - i.e. pairs (isin,source) should be unique. 106 vals := res[0].Series[0].Values 107 set := make(map[string]struct{}) 108 109 for _, val := range vals { 110 if _, ok := set[val[3].(string)+val[4].(string)]; !ok { 111 allStocks[Stock{ 112 Symbol: val[1].(string), 113 Name: val[2].(string), 114 ISIN: val[3].(string), 115 }] = val[4].(string) 116 set[val[3].(string)+val[4].(string)] = struct{}{} 117 } 118 } 119 120 } 121 122 return allStocks, nil 123 } 124 125 // func (db *DB) GetStockQuotfation(starttime time.Time, endtime time.Time, asset string, protocol string) ([]StockQuotation, error) { 126 // retval := []dia.DefiRate{} 127 // influxQuery := "SELECT \"asset\",borrowRate,lendingRate,\"protocol\" FROM %s WHERE time > %d and time < %d and asset = '%s' and protocol = '%s'" 128 // q := fmt.Sprintf(influxQuery, influxDbDefiRateTable, starttime.UnixNano(), endtime.UnixNano(), asset, protocol) 129 // fmt.Println("influx query: ", q) 130 // res, err := queryInfluxDB(db.influxClient, q) 131 // fmt.Println("res, err: ", res, err) 132 // if err != nil { 133 // return retval, err 134 // } 135 // if len(res) > 0 && len(res[0].Series) > 0 { 136 // for i := 0; i < len(res[0].Series[0].Values); i++ { 137 // currentRate := dia.DefiRate{} 138 // currentRate.Timestamp, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string)) 139 // if err != nil { 140 // return retval, err 141 // } 142 // currentRate.Asset = res[0].Series[0].Values[i][1].(string) 143 // if err != nil { 144 // return retval, err 145 // } 146 // currentRate.BorrowingRate, err = res[0].Series[0].Values[i][2].(json.Number).Float64() 147 // if err != nil { 148 // return retval, err 149 // } 150 // currentRate.LendingRate, err = res[0].Series[0].Values[i][3].(json.Number).Float64() 151 // if err != nil { 152 // return retval, err 153 // } 154 // currentRate.Protocol = protocol 155 // retval = append(retval, currentRate) 156 // } 157 // } else { 158 // return retval, errors.New("Error parsing Defi Lending Rate from Database") 159 // } 160 // return retval, nil 161 // }