|
1 | 1 | package data
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "sort"
|
5 | 6 | "time"
|
| 7 | + goErrors "errors" |
6 | 8 |
|
7 | 9 | "github.com/quick-trade/xoney/errors"
|
8 | 10 | "github.com/quick-trade/xoney/internal"
|
@@ -92,12 +94,12 @@ func (t TimeStamp) Len() int { return len(t.Timestamp) }
|
92 | 94 | // encapsulating the open, high, low, close values and the volume of trading
|
93 | 95 | // over a particular time period, with TimeClose marking the end of that period.
|
94 | 96 | type Candle struct {
|
95 |
| - Open float64 |
96 |
| - High float64 |
97 |
| - Low float64 |
98 |
| - Close float64 |
99 |
| - Volume float64 |
100 |
| - TimeClose time.Time |
| 97 | + Open float64 `json:"open"` |
| 98 | + High float64 `json:"high"` |
| 99 | + Low float64 `json:"low"` |
| 100 | + Close float64 `json:"close"` |
| 101 | + Volume float64 `json:"volume"` |
| 102 | + TimeClose time.Time `json:"time_close"` |
101 | 103 | }
|
102 | 104 |
|
103 | 105 | func NewCandle(open, high, low, c, volume float64, timeClose time.Time) *Candle {
|
@@ -127,6 +129,40 @@ func NewInstrumentCandle(candle Candle, instrument Instrument) *InstrumentCandle
|
127 | 129 | }
|
128 | 130 | }
|
129 | 131 |
|
| 132 | + |
| 133 | +// creates an InstrumentCandle from a JSON-encoded string. |
| 134 | +func NewInstrumentCandleFromJSON(data []byte) (*InstrumentCandle, error) { |
| 135 | + var icj InstrumentCandle |
| 136 | + if err := json.Unmarshal(data, &icj); err != nil { |
| 137 | + return nil, goErrors.New("failed to unmarshal instrument candle JSON: " + err.Error()) |
| 138 | + } |
| 139 | + |
| 140 | + // Create a Candle from the unmarshaled CandleJSON |
| 141 | + candle := Candle{ |
| 142 | + Open: icj.Candle.Open, |
| 143 | + High: icj.Candle.High, |
| 144 | + Low: icj.Candle.Low, |
| 145 | + Close: icj.Candle.Close, |
| 146 | + Volume: icj.Candle.Volume, |
| 147 | + TimeClose: icj.Candle.TimeClose, |
| 148 | + } |
| 149 | + |
| 150 | + // Create and return the InstrumentCandle |
| 151 | + return &InstrumentCandle{ |
| 152 | + Candle: candle, |
| 153 | + Instrument: icj.Instrument, |
| 154 | + }, nil |
| 155 | +} |
| 156 | + |
| 157 | +func CandlesFromJSON(data []byte) ([]InstrumentCandle, error) { |
| 158 | + var icj []InstrumentCandle |
| 159 | + if err := json.Unmarshal(data, &icj); err != nil { |
| 160 | + return nil, goErrors.New("failed to unmarshal instrument candle JSON: " + err.Error()) |
| 161 | + } |
| 162 | + |
| 163 | + return icj, nil |
| 164 | +} |
| 165 | + |
130 | 166 | // Chart represents a sequence of candlestick data points for a specific instrument.
|
131 | 167 | // It contains slices of open, high, low, close values and the trading volume,
|
132 | 168 | // along with the corresponding timestamps for each data point.
|
|
0 commit comments