Skip to content

Commit a204e29

Browse files
json
1 parent bbc24b4 commit a204e29

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

common/data/candlestick.go

+42-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package data
22

33
import (
4+
"encoding/json"
45
"sort"
56
"time"
7+
goErrors "errors"
68

79
"github.com/quick-trade/xoney/errors"
810
"github.com/quick-trade/xoney/internal"
@@ -92,12 +94,12 @@ func (t TimeStamp) Len() int { return len(t.Timestamp) }
9294
// encapsulating the open, high, low, close values and the volume of trading
9395
// over a particular time period, with TimeClose marking the end of that period.
9496
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"`
101103
}
102104

103105
func NewCandle(open, high, low, c, volume float64, timeClose time.Time) *Candle {
@@ -127,6 +129,40 @@ func NewInstrumentCandle(candle Candle, instrument Instrument) *InstrumentCandle
127129
}
128130
}
129131

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+
130166
// Chart represents a sequence of candlestick data points for a specific instrument.
131167
// It contains slices of open, high, low, close values and the trading volume,
132168
// along with the corresponding timestamps for each data point.

common/data/instrument.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type Exchange string
1414
// It contains the asset identifier (e.g., "BTC" for Bitcoin) and
1515
// the exchange where it is traded.
1616
type Currency struct {
17-
Asset string // Identifier of the financial asset.
18-
Exchange Exchange // Exchange on which the asset is traded.
17+
Asset string `json:"asset"` // Identifier of the financial asset.
18+
Exchange Exchange `json:"exchange"` // Exchange on which the asset is traded.
1919
}
2020

2121
// String returns the string representation of the Currency in the format

realtime/executor.go

+33
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,36 @@ func (e *Executor) stop() error {
100100

101101
return firstErr
102102
}
103+
104+
type DummySupplier struct {
105+
candles []data.InstrumentCandle
106+
}
107+
108+
func (s *DummySupplier) GetCharts(durations st.Durations) (data.ChartContainer, error) {
109+
return data.ChartContainer{}, nil
110+
}
111+
112+
func (s *DummySupplier) SetCandles(candles []data.InstrumentCandle) {
113+
s.candles = candles
114+
}
115+
116+
func (s *DummySupplier) SetCandlesFromJSON(json []byte) error {
117+
candles, err := data.CandlesFromJSON(json)
118+
if err != nil {
119+
return err
120+
}
121+
122+
s.candles = candles
123+
return nil
124+
}
125+
126+
func (s *DummySupplier) StreamCandles(ctx context.Context, instruments []data.Instrument) <-chan data.InstrumentCandle {
127+
ch := make(chan data.InstrumentCandle)
128+
go func() {
129+
defer close(ch)
130+
for _, candle := range s.candles {
131+
ch <- candle
132+
}
133+
}()
134+
return ch
135+
}

0 commit comments

Comments
 (0)