-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathticker.go
1141 lines (1048 loc) · 25.6 KB
/
ticker.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ibsync
import (
"errors"
"math"
"strconv"
"strings"
"sync"
"time"
)
// Ticker represents real-time market data for a financial contract.
//
// The Ticker struct captures comprehensive market information including:
// - Current price data (bid, ask, last price)
// - Historical price metrics (open, high, low, close)
// - Trading volume and statistics
// - Volatility indicators
// - Options-specific data like greeks
//
// Thread-safe access is provided through mutex-protected getter methods.
//
// Market Data Types:
// - Level 1 streaming ticks stored in 'ticks'
// - Level 2 market depth ticks stored in 'domTicks'
// - Order book (DOM) available in 'domBids' and 'domAsks'
// - Tick-by-tick data stored in 'tickByTicks'
//
// Options Greeks:
// - Bid, ask, and last greeks stored in 'bidGreeks', 'askGreeks', and 'lastGreeks'
// - Model-calculated greeks available in 'modelGreeks'
type Ticker struct {
mu sync.Mutex
contract *Contract
time time.Time
marketDataType int64
minTick float64
bid float64
bidSize Decimal
bidExchange string
ask float64
askSize Decimal
askExchange string
last float64
lastSize Decimal
lastExchange string
prevBid float64
prevBidSize Decimal
prevAsk float64
prevAskSize Decimal
prevLast float64
prevLastSize Decimal
volume Decimal
open float64
high float64
low float64
close float64
vwap float64
low13Week float64
high13Week float64
low26Week float64
high26Week float64
low52Week float64
high52Week float64
bidYield float64
askYield float64
lastYield float64
markPrice float64
halted float64
rtHistVolatility float64
rtVolume float64
rtTradeVolume float64
rtTime time.Time
avVolume Decimal
tradeCount float64
tradeRate float64
volumeRate float64
shortableShares Decimal
indexFuturePremium float64
futuresOpenInterest Decimal
putOpenInterest Decimal
callOpenInterest Decimal
putVolume Decimal
callVolume Decimal
avOptionVolume Decimal
histVolatility float64
impliedVolatility float64
dividends Dividends
fundamentalRatios FundamentalRatios
ticks []TickData
tickByTicks []TickByTick
domBids map[int64]DOMLevel
domAsks map[int64]DOMLevel
domTicks []MktDepthData
bidGreeks TickOptionComputation
askGreeks TickOptionComputation
lastGreeks TickOptionComputation
modelGreeks TickOptionComputation
auctionVolume Decimal
auctionPrice float64
auctionImbalance Decimal
regulatoryImbalance Decimal
bboExchange string
snapshotPermissions int64
}
// NewTicker creates a new Ticker instance for the given contract.
func NewTicker(contract *Contract) *Ticker {
return &Ticker{
contract: contract,
domBids: make(map[int64]DOMLevel),
domAsks: make(map[int64]DOMLevel),
}
}
// Contract returns the financial contract associated with this Ticker.
func (t *Ticker) Contract() *Contract {
t.mu.Lock()
defer t.mu.Unlock()
return t.contract
}
func (t *Ticker) Time() time.Time {
t.mu.Lock()
defer t.mu.Unlock()
return t.time
}
func (t *Ticker) MarketDataType() int64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.marketDataType
}
func (t *Ticker) setMarketDataType(marketDataType int64) {
t.mu.Lock()
defer t.mu.Unlock()
t.marketDataType = marketDataType
}
func (t *Ticker) MinTick() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.minTick
}
func (t *Ticker) Bid() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.bid
}
func (t *Ticker) BidSize() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.bidSize
}
func (t *Ticker) BidExchange() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.bidExchange
}
func (t *Ticker) Ask() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.ask
}
func (t *Ticker) AskSize() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.askSize
}
func (t *Ticker) AskExchange() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.askExchange
}
func (t *Ticker) Last() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.last
}
// Getters pour les autres champs
func (t *Ticker) LastSize() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.lastSize
}
func (t *Ticker) LastExchange() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.lastExchange
}
func (t *Ticker) PrevBid() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.prevBid
}
func (t *Ticker) PrevBidSize() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.prevBidSize
}
func (t *Ticker) PrevAsk() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.prevAsk
}
func (t *Ticker) PrevAskSize() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.prevAskSize
}
func (t *Ticker) PrevLast() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.prevLast
}
func (t *Ticker) PrevLastSize() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.prevLastSize
}
func (t *Ticker) Volume() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.volume
}
func (t *Ticker) Open() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.open
}
func (t *Ticker) High() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.high
}
func (t *Ticker) Low() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.low
}
func (t *Ticker) Close() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.close
}
func (t *Ticker) Vwap() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.vwap
}
func (t *Ticker) Low13Week() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.low13Week
}
func (t *Ticker) High13Week() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.high13Week
}
func (t *Ticker) Low26Week() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.low26Week
}
func (t *Ticker) High26Week() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.high26Week
}
func (t *Ticker) Low52Week() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.low52Week
}
func (t *Ticker) High52Week() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.high52Week
}
func (t *Ticker) BidYield() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.bidYield
}
func (t *Ticker) AskYield() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.askYield
}
func (t *Ticker) LastYield() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.lastYield
}
func (t *Ticker) MarkPrice() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.markPrice
}
func (t *Ticker) Halted() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.halted
}
func (t *Ticker) RtHistVolatility() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.rtHistVolatility
}
func (t *Ticker) RtVolume() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.rtVolume
}
func (t *Ticker) RtTradeVolume() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.rtTradeVolume
}
func (t *Ticker) RtTime() time.Time {
t.mu.Lock()
defer t.mu.Unlock()
return t.rtTime
}
func (t *Ticker) AvVolume() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.avVolume
}
func (t *Ticker) TradeCount() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.tradeCount
}
func (t *Ticker) TradeRate() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.tradeRate
}
func (t *Ticker) VolumeRate() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.volumeRate
}
func (t *Ticker) ShortableShares() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.shortableShares
}
func (t *Ticker) IndexFuturePremium() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.indexFuturePremium
}
func (t *Ticker) FuturesOpenInterest() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.futuresOpenInterest
}
func (t *Ticker) PutOpenInterest() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.putOpenInterest
}
func (t *Ticker) CallOpenInterest() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.callOpenInterest
}
func (t *Ticker) PutVolume() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.putVolume
}
func (t *Ticker) CallVolume() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.callVolume
}
func (t *Ticker) AvOptionVolume() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.avOptionVolume
}
func (t *Ticker) HistVolatility() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.histVolatility
}
func (t *Ticker) ImpliedVolatility() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.impliedVolatility
}
func (t *Ticker) Dividends() Dividends {
t.mu.Lock()
defer t.mu.Unlock()
return t.dividends
}
func (t *Ticker) FundamentalRatios() FundamentalRatios {
t.mu.Lock()
defer t.mu.Unlock()
return t.fundamentalRatios
}
func (t *Ticker) Ticks() []TickData {
t.mu.Lock()
defer t.mu.Unlock()
return t.ticks
}
func (t *Ticker) TickByTicks() []TickByTick {
t.mu.Lock()
defer t.mu.Unlock()
return t.tickByTicks
}
func (t *Ticker) DomBids() []DOMLevel {
t.mu.Lock()
defer t.mu.Unlock()
var dls []DOMLevel
for _, dl := range t.domBids {
dls = append(dls, dl)
}
return dls
}
func (t *Ticker) DomAsks() []DOMLevel {
t.mu.Lock()
defer t.mu.Unlock()
var dls []DOMLevel
for _, dl := range t.domAsks {
dls = append(dls, dl)
}
return dls
}
func (t *Ticker) DomTicks() []MktDepthData {
t.mu.Lock()
defer t.mu.Unlock()
return t.domTicks
}
func (t *Ticker) BidGreeks() TickOptionComputation {
t.mu.Lock()
defer t.mu.Unlock()
return t.bidGreeks
}
func (t *Ticker) AskGreeks() TickOptionComputation {
t.mu.Lock()
defer t.mu.Unlock()
return t.askGreeks
}
func (t *Ticker) midGreeks() TickOptionComputation {
if t.bidGreeks.Type() == 0 || t.askGreeks.Type() == 0 {
return TickOptionComputation{}
}
mg := TickOptionComputation{
ImpliedVol: (t.bidGreeks.ImpliedVol + t.askGreeks.ImpliedVol) / 2,
Delta: (t.bidGreeks.Delta + t.askGreeks.Delta) / 2,
OptPrice: (t.bidGreeks.OptPrice + t.askGreeks.OptPrice) / 2,
PvDividend: (t.bidGreeks.PvDividend + t.askGreeks.PvDividend) / 2,
Gamma: (t.bidGreeks.Gamma + t.askGreeks.Gamma) / 2,
Vega: (t.bidGreeks.Vega + t.askGreeks.Vega) / 2,
Theta: (t.bidGreeks.Theta + t.askGreeks.Theta) / 2,
UndPrice: (t.bidGreeks.UndPrice + t.askGreeks.UndPrice) / 2,
}
return mg
}
func (t *Ticker) MidGreeks() TickOptionComputation {
t.mu.Lock()
defer t.mu.Unlock()
return t.midGreeks()
}
func (t *Ticker) LastGreeks() TickOptionComputation {
t.mu.Lock()
defer t.mu.Unlock()
return t.lastGreeks
}
func (t *Ticker) ModelGreeks() TickOptionComputation {
t.mu.Lock()
defer t.mu.Unlock()
return t.modelGreeks
}
// Greeks returns the most representative option Greeks.
//
// Selection priority:
// 1. Midpoint Greeks (average of bid and ask Greeks)
// 2. Last trade Greeks
// 3. Model-calculated Greeks
func (t *Ticker) Greeks() TickOptionComputation {
t.mu.Lock()
defer t.mu.Unlock()
greeks := t.midGreeks()
if greeks != (TickOptionComputation{}) {
return greeks
}
if t.lastGreeks != (TickOptionComputation{}) {
return t.lastGreeks
}
if t.modelGreeks != (TickOptionComputation{}) {
return t.modelGreeks
}
return TickOptionComputation{}
}
func (t *Ticker) AuctionVolume() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.auctionVolume
}
func (t *Ticker) AuctionPrice() float64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.auctionPrice
}
func (t *Ticker) AuctionImbalance() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.auctionImbalance
}
func (t *Ticker) RegulatoryImbalance() Decimal {
t.mu.Lock()
defer t.mu.Unlock()
return t.regulatoryImbalance
}
func (t *Ticker) BboExchange() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.bboExchange
}
func (t *Ticker) SnapshotPermissions() int64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.snapshotPermissions
}
func (t *Ticker) hasBidAsk() bool {
return t.bid > 0 && t.ask > 0
}
func (t *Ticker) HasBidAsk() bool {
t.mu.Lock()
defer t.mu.Unlock()
return t.hasBidAsk()
}
// MidPoint calculates the average of the current bid and ask prices.
func (t *Ticker) MidPoint() float64 {
t.mu.Lock()
defer t.mu.Unlock()
if t.hasBidAsk() {
return (t.bid + t.ask) * 0.5
}
return math.NaN()
}
// MarketPrice determines the most appropriate current market price.
//
// Price selection priority:
// 1. If last price is within the bid-ask spread, use last price.
// 2. If no last price fits the spread, use midpoint (average of bid and ask).
// 3. If no bid-ask available, return last price.
func (t *Ticker) MarketPrice() float64 {
t.mu.Lock()
defer t.mu.Unlock()
if t.hasBidAsk() {
if t.bid <= t.last && t.last <= t.ask {
return t.last
}
return (t.bid + t.ask) * 0.5
}
return t.last
}
func (t *Ticker) SetTickPrice(tp TickPrice) {
t.mu.Lock()
defer t.mu.Unlock()
var size Decimal
switch tp.TickType {
case BID, DELAYED_BID:
if tp.Price == t.bid {
return
}
t.prevBid = t.bid
t.bid = tp.Price
case ASK, DELAYED_ASK:
if tp.Price == t.ask {
return
}
t.prevAsk = t.ask
t.ask = tp.Price
case LAST, DELAYED_LAST:
if tp.Price == t.last {
return
}
t.prevLast = t.last
t.last = tp.Price
case HIGH, DELAYED_HIGH:
t.high = tp.Price
case LOW, DELAYED_LOW:
t.low = tp.Price
case CLOSE, DELAYED_CLOSE:
t.close = tp.Price
case OPEN, DELAYED_OPEN:
t.open = tp.Price
case LOW_13_WEEK:
t.low13Week = tp.Price
case HIGH_13_WEEK:
t.high13Week = tp.Price
case LOW_26_WEEK:
t.low26Week = tp.Price
case HIGH_26_WEEK:
t.high26Week = tp.Price
case LOW_52_WEEK:
t.low52Week = tp.Price
case HIGH_52_WEEK:
t.high52Week = tp.Price
case AUCTION_PRICE:
t.auctionPrice = tp.Price
case MARK_PRICE:
t.markPrice = tp.Price
case BID_YIELD, DELAYED_YIELD_BID:
t.bidYield = tp.Price
case ASK_YIELD, DELAYED_YIELD_ASK:
t.askYield = tp.Price
case LAST_YIELD:
t.lastYield = tp.Price
default:
log.Warn().Err(errUnknownTickType).Int64("TickType", tp.TickType).Msg("SetTickPrice")
}
td := TickData{
Time: time.Now().UTC(),
TickType: tp.TickType,
Price: tp.Price,
Size: size,
}
t.ticks = append(t.ticks, td)
}
func (t *Ticker) SetTickSize(ts TickSize) {
t.mu.Lock()
defer t.mu.Unlock()
var price float64
switch ts.TickType {
case BID_SIZE, DELAYED_BID_SIZE:
if ts.Size == t.bidSize {
return
}
price = t.bid
t.prevBidSize = t.bidSize
t.bidSize = ts.Size
case ASK_SIZE, DELAYED_ASK_SIZE:
if ts.Size == t.askSize {
return
}
price = t.ask
t.prevAskSize = t.askSize
t.askSize = ts.Size
case LAST_SIZE, DELAYED_LAST_SIZE:
price = t.last
if price == 0 {
return
}
if ts.Size != t.lastSize {
t.prevLastSize = t.lastSize
t.lastSize = ts.Size
}
case VOLUME, DELAYED_VOLUME:
t.volume = ts.Size
case AVG_VOLUME:
t.avVolume = ts.Size
case OPTION_CALL_OPEN_INTEREST:
t.callOpenInterest = ts.Size
case OPTION_PUT_OPEN_INTEREST:
t.putOpenInterest = ts.Size
case OPTION_CALL_VOLUME:
t.callVolume = ts.Size
case OPTION_PUT_VOLUME:
t.putVolume = ts.Size
case AUCTION_VOLUME:
t.auctionVolume = ts.Size
case AUCTION_IMBALANCE:
t.auctionImbalance = ts.Size
case REGULATORY_IMBALANCE:
t.regulatoryImbalance = ts.Size
case FUTURES_OPEN_INTEREST:
t.futuresOpenInterest = ts.Size
case AVG_OPT_VOLUME:
t.avOptionVolume = ts.Size
case SHORTABLE_SHARES:
t.shortableShares = ts.Size
default:
log.Warn().Err(errUnknownTickType).Int64("TickType", ts.TickType).Msg("SetTickSize")
}
td := TickData{
Time: time.Now().UTC(),
TickType: ts.TickType,
Price: price,
Size: ts.Size,
}
t.ticks = append(t.ticks, td)
}
func (t *Ticker) SetTickOptionComputation(toc TickOptionComputation) {
t.mu.Lock()
defer t.mu.Unlock()
switch toc.TickType {
case BID_OPTION_COMPUTATION, DELAYED_BID_OPTION:
t.bidGreeks = toc
case ASK_OPTION_COMPUTATION, DELAYED_ASK_OPTION:
t.askGreeks = toc
case LAST_OPTION_COMPUTATION, DELAYED_LAST_OPTION:
t.lastGreeks = toc
case MODEL_OPTION, DELAYED_MODEL_OPTION:
t.modelGreeks = toc
default:
log.Warn().Err(errUnknownTickType).Int64("TickType", toc.TickType).Msg("SetTickOptionComputation")
}
}
func (t *Ticker) SetTickGeneric(tg TickGeneric) {
t.mu.Lock()
defer t.mu.Unlock()
switch tg.TickType {
case OPTION_HISTORICAL_VOL:
t.histVolatility = tg.Value
case OPTION_IMPLIED_VOL:
t.impliedVolatility = tg.Value
case INDEX_FUTURE_PREMIUM:
t.indexFuturePremium = tg.Value
case HALTED, DELAYED_HALTED:
t.halted = tg.Value
case TRADE_COUNT:
t.tradeCount = tg.Value
case TRADE_RATE:
t.tradeRate = tg.Value
case VOLUME_RATE:
t.volumeRate = tg.Value
case RT_HISTORICAL_VOL:
t.rtHistVolatility = tg.Value
default:
log.Warn().Err(errUnknownTickType).Int64("TickType", tg.TickType).Float64("TickValue", tg.Value).Msg("SetTickGeneric")
}
td := TickData{
Time: time.Now().UTC(),
TickType: tg.TickType,
Price: tg.Value,
Size: ZERO,
}
t.ticks = append(t.ticks, td)
}
func (t *Ticker) SetTickString(ts TickString) {
t.mu.Lock()
defer t.mu.Unlock()
switch ts.TickType {
case BID_EXCH:
t.bidExchange = ts.Value
case ASK_EXCH:
t.askExchange = ts.Value
case LAST_EXCH:
t.lastExchange = ts.Value
case FUNDAMENTAL_RATIOS:
d := make(FundamentalRatios)
for _, t := range strings.Split(ts.Value, ";") {
if t == "" {
continue
}
kv := strings.Split(t, "=")
if len(kv) == 2 {
k, v := kv[0], kv[1]
if v == "-99999.99" {
d[k] = UNSET_FLOAT
continue
}
f, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Warn().Err(errors.New("fundamental ratio error")).Str("key", k).Str("value", v).Msg("SetTickString")
continue
}
d[k] = f
}
}
t.fundamentalRatios = d
case RT_VOLUME, RT_TRD_VOLUME:
// RT Volume or RT Trade Volume value: " price;size;ms since epoch;total volume;VWAP;single trade"
split := strings.Split(ts.Value, ";")
if split[3] != "" {
f, err := strconv.ParseFloat(split[3], 64)
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
if ts.TickType == RT_VOLUME {
t.rtVolume = f
} else {
t.rtTradeVolume = f
}
}
if split[4] != "" {
f, err := strconv.ParseFloat(split[4], 64)
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
t.vwap = f
}
if split[2] != "" {
d, err := ParseIBTime(split[2])
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
t.rtTime = d
}
if split[0] != "" {
return
}
price, err := strconv.ParseFloat(split[0], 64)
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
if split[1] != "" {
size := StringToDecimal(split[1])
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
if t.prevLast != t.last {
t.prevLast = t.last
t.last = price
}
if t.prevLastSize != t.lastSize {
t.prevLastSize = t.lastSize
t.lastSize = size
}
td := TickData{
Time: time.Now().UTC(),
TickType: ts.TickType,
Price: price,
Size: size,
}
t.ticks = append(t.ticks, td)
}
case IB_DIVIDENDS:
// Dividend Value: "past12,next12,nextDate,nextAmount"
split := strings.Split(ts.Value, ",")
ds := Dividends{}
if split[0] != "" {
f, err := strconv.ParseFloat(split[0], 64)
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
ds.Past12Months = f
}
if split[1] != "" {
f, err := strconv.ParseFloat(split[1], 64)
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
ds.Next12Months = f
}
if split[2] != "" {
d, err := ParseIBTime(split[2])
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
ds.NextDate = d
}
if split[3] != "" {
f, err := strconv.ParseFloat(split[3], 64)
if err != nil {
log.Error().Err(err).Msg("<SetTickString>")
}
ds.NextAmount = f
}
t.dividends = ds
case DELAYED_LAST_TIMESTAMP:
default:
log.Warn().Err(errUnknownTickType).Int64("TickType", ts.TickType).Msg("SetTickString")
}
}
func (t *Ticker) SetTickEFP(te TickEFP) {
t.mu.Lock()
defer t.mu.Unlock()
// TODO
}
func (t *Ticker) SetTickByTickAllLast(tbt TickByTickAllLast) {
t.mu.Lock()
defer t.mu.Unlock()
if tbt.Price != t.last {
t.prevLast = t.last
t.last = tbt.Price
}
if tbt.Size != t.lastSize {
t.prevLastSize = t.lastSize
t.lastSize = tbt.Size
}
t.tickByTicks = append(t.tickByTicks, tbt)
}
func (t *Ticker) SetTickByTickBidAsk(tbt TickByTickBidAsk) {
t.mu.Lock()
defer t.mu.Unlock()
if tbt.BidPrice != t.bid {
t.prevBid = t.bid
t.bid = tbt.BidPrice
}
if tbt.BidSize != t.bidSize {
t.prevBidSize = t.bidSize
t.bidSize = tbt.BidSize
}
if tbt.AskPrice != t.ask {
t.prevAsk = t.ask
t.ask = tbt.AskPrice
}
if tbt.AskSize != t.askSize {
t.prevAskSize = t.askSize
t.askSize = tbt.AskSize
}
t.tickByTicks = append(t.tickByTicks, tbt)
}
func (t *Ticker) SetTickByTickMidPoint(tbt TickByTickMidPoint) {
t.mu.Lock()
defer t.mu.Unlock()
t.tickByTicks = append(t.tickByTicks, tbt)
}
func (t *Ticker) String() string {
return Stringify(struct {
Contract *Contract
Time time.Time
MarketDataType int64
MinTick float64