Skip to content

Commit

Permalink
added public futures websocket push data handling
Browse files Browse the repository at this point in the history
  • Loading branch information
samuael committed Dec 28, 2024
1 parent 4ec2a3f commit cad4e21
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 58 deletions.
29 changes: 3 additions & 26 deletions exchanges/poloniex/poloniex.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/thrasher-corp/gocryptotrader/common"
Expand Down Expand Up @@ -156,7 +157,7 @@ func (p *Poloniex) GetCandlesticks(ctx context.Context, symbol currency.Pair, in
if symbol.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
intervalString, err := intervalToString(interval)
intervalString, err := IntervalString(interval)
if err != nil {
return nil, err
} else if intervalString == "" {
Expand Down Expand Up @@ -505,30 +506,6 @@ func (p *Poloniex) NewCurrencyDepositAddress(ctx context.Context, ccy currency.C
return resp.Address, p.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, authResourceIntensiveEPL, http.MethodPost, "/wallets/address", nil, map[string]string{"currency": ccy.String()}, &resp)
}

func intervalToString(interval kline.Interval) (string, error) {
intervalMap := map[kline.Interval]string{
kline.OneMin: "MINUTE_1",
kline.FiveMin: "MINUTE_5",
kline.TenMin: "MINUTE_10",
kline.FifteenMin: "MINUTE_15",
kline.ThirtyMin: "MINUTE_30",
kline.OneHour: "HOUR_1",
kline.TwoHour: "HOUR_2",
kline.FourHour: "HOUR_4",
kline.SixHour: "HOUR_6",
kline.TwelveHour: "HOUR_12",
kline.OneDay: "DAY_1",
kline.ThreeDay: "DAY_3",
kline.SevenDay: "WEEK_1",
kline.OneMonth: "MONTH_1",
}
intervalString, okay := intervalMap[interval]
if okay {
return intervalString, nil
}
return "", kline.ErrUnsupportedInterval
}

func stringToInterval(interval string) (kline.Interval, error) {
intervalMap := map[string]kline.Interval{
"MINUTE_1": kline.OneMin,
Expand All @@ -546,7 +523,7 @@ func stringToInterval(interval string) (kline.Interval, error) {
"WEEK_1": kline.SevenDay,
"MONTH_1": kline.OneMonth,
}
intervalInstance, okay := intervalMap[interval]
intervalInstance, okay := intervalMap[strings.ToUpper(interval)]
if okay {
return intervalInstance, nil
}
Expand Down
60 changes: 60 additions & 0 deletions exchanges/poloniex/poloniex_futures_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ type FuturesV3Orderbook struct {
Bids [][]types.Number `json:"bids"`
Depth types.Number `json:"s"`
Timestamp types.Time `json:"ts"`

ID types.Number `json:"id"`
Symbol string `json:"symbol"`
CreationTime types.Time `json:"cT"`
}

// V3FuturesCandle represents a kline data for v3 futures instrument
Expand Down Expand Up @@ -270,11 +274,13 @@ type V3FuturesTickerDetail struct {
BestAskPrice types.Number `json:"aPx"`
BestAskSize types.Number `json:"aSz"`
MarkPrice types.Number `json:"mPx"`
Timestamp types.Time `json:"ts"`
}

// InstrumentIndexPrice represents a symbols index price
type InstrumentIndexPrice struct {
Symbol string `json:"symbol"`
Timestamp types.Time `json:"ts"`
IndexPrice types.Number `json:"iPx"`
}

Expand Down Expand Up @@ -310,6 +316,7 @@ func (v *V3FuturesIndexPriceData) UnmarshalJSON(data []byte) error {
type V3FuturesMarkPrice struct {
MarkPrice types.Number `json:"mPx"`
Symbol string `json:"symbol"`
Timestamp types.Time `json:"ts"`
}

// V3FuturesMarkPriceCandle represents a k-line data for mark price
Expand Down Expand Up @@ -364,6 +371,7 @@ type V3FuturesFundingRate struct {
FundingRateSettleTime types.Time `json:"fT"`
NextPredictedFundingRate types.Number `json:"nFR"`
NextFundingTime types.Time `json:"nFT"`
Timestamp types.Time `json:"ts"`
}

// OpenInterestData represents an open interest data
Expand All @@ -384,3 +392,55 @@ type RiskLimit struct {
NotionalCap types.Number `json:"notionalCap"`
Symbol string `json:"symbol"`
}

// WsFuturesCandlesctick represents a kline data for futures instrument
type WsFuturesCandlesctick struct {
Symbol string
LowestPrice types.Number
HighestPrice types.Number
OpenPrice types.Number
ClosePrice types.Number
Amount types.Number
Quantity types.Number
Trades types.Number
StartTime types.Time
EndTime types.Time
PushTime types.Time
}

// UnmarshalJSON deserializes byte data into futures candlesticks into *WsFuturesCandlesctick
func (o *WsFuturesCandlesctick) UnmarshalJSON(data []byte) error {
target := [11]any{&o.Symbol, &o.LowestPrice, &o.HighestPrice, &o.OpenPrice, &o.ClosePrice, &o.Amount, &o.Quantity, &o.Trades, &o.StartTime, &o.EndTime, &o.PushTime}
return json.Unmarshal(data, &target)
}

// FuturesTrades represents a futures trades detail
type FuturesTrades struct {
ID int `json:"id"`
Timestamp types.Time `json:"ts"`
Symbol string `json:"s"`
Price types.Number `json:"px"`
Quantity types.Number `json:"qty"`
Amount types.Number `json:"amt"`
Side string `json:"side"`
CreationTime types.Time `json:"cT"`
}

// V3WsFuturesMarkAndIndexPriceCandle represents a websocket k-line data for mark/index candlestick data
type V3WsFuturesMarkAndIndexPriceCandle struct {
OpeningPrice types.Number
HighestPrice types.Number
LowestPrice types.Number
ClosingPrice types.Number
StartTime types.Time
EndTime types.Time

Symbol string
PushTimestamp types.Time
}

// UnmarshalJSON deserializes byte data into V3WsFuturesMarkAndIndexPriceCandle instance
func (v *V3WsFuturesMarkAndIndexPriceCandle) UnmarshalJSON(data []byte) error {
target := [8]any{&v.Symbol, &v.LowestPrice, &v.HighestPrice, &v.OpeningPrice, &v.ClosingPrice, &v.StartTime, &v.EndTime, &v.PushTimestamp}
return json.Unmarshal(data, &target)
}
Loading

0 comments on commit cad4e21

Please sign in to comment.