-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
1272 lines (1229 loc) · 41 KB
/
main.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 dx // dx
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"io"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"
)
type Identity struct {
Username string `json:"username"`
Password string `json:"password"`
Server string `json:"vendor"`
AccountId int `json:"accountId"`
Cookies map[string]string
Csrf string
}
const (
ETHUSD = 3443
BTCUSD = 3425
XRPUSD = 3404
DOGEUSD = 3410
EURUSD = 3438
US30CASH = 3351
NEOUSD = 3370
AUDCAD = 3436
XAUUSD = 3406
DASHUSD = 3445
USD = 1201
CNY = 1301
XMRUSD = 3396
LTC = 1401
USDILS = 3405
XAUEUR = 3412
MAR = 1605
XPTUSD = 3444
USDSGD = 3421
ADA = 3367
USOILCASH = 3354
USDTRY = 3400
YHOO = 1515
MS = 1584
AUDNZD = 3446
CADJPY = 3391
EURCAD = 3399
EURRUB = 3394
USDHKD = 3441
VOD = 1597
GBPUSD = 3440
KRW = 801
THB = 501
USDCHF = 3390
GBPNZD = 3418
TRIP = 1627
DXF = 3374
ADAUSD = 3369
GBP = 751
USTN10F = 3376
DOGE = 1567
PLN = 551
USDNOK = 3417
NZD = 901
LTCUSD = 3409
GBPAUD = 3430
AAPL = 1570
US100CASH = 3352
ETFC = 1556
IBE = 3317
XAUAUD = 3449
AMZN = 1569
NZDUSD = 3398
INR = 1101
RL = 1501
GBPCHF = 3432
CNH = 1251
USDJPY = 3427
NWS = 1614
EURJPY = 3392
V = 3316
UKOILCASH = 3357
YELP = 1559
US500CASH = 3363
JPM = 1526
FRA40CASH = 3358
T = 1640
GBPJPY = 3397
EOS = 1600
AUD = 701
PEP = 1633
NVDA = 3318
NATGASF = 3377
EURPLN = 3401
BLK = 1631
USDRUB = 3429
MXN = 851
WHEAT = 4954
SEK = 401
NKE = 1523
BTC = 1351
HUF = 601
AUDCHF = 3395
EURNZD = 3414
USDSEK = 3423
CS = 1646
US2000CASH = 3356
HKD = 251
LVMH = 3324
XPT = 3308
TMUS = 3385
HK50CASH = 3362
YNDX = 1576
USDPLN = 3434
NZDCAD = 3428
UPS = 1518
QCOM = 3381
NFLX = 1649
TWTR = 1563
ADBE = 3383
TSLA = 1645
USDZAR = 3435
GBPCAD = 3403
DIS = 1527
USDMXN = 3439
XPDUSD = 3408
SBUX = 1642
COCOAC = 4956
DASH = 1529
XLM = 1508
RACE = 3311
ERBNF = 3378
PYPL = 3382
SOYBEAN = 4955
WFM = 1606
NEO = 3368
DOT = 3366
ATVI = 3380
BAYGN = 3313
SKK = 951
DPZ = 1598
EURGBP = 3419
PFE = 1534
JPY = 301
BCH = 1528
ABNB = 3386
JNJ = 1503
ETC = 1620
DB = 1594
XAGAUD = 3393
GER40CASH = 3365
PCG = 1654
CADCHF = 3424
RUB = 1151
ZM = 3320
EURCHF = 3426
DOTUSD = 3371
UK100CASH = 3355
EU50CASH = 3359
CHFJPY = 3407
VZ = 1544
EURHUF = 3402
CHF = 101
EURNOK = 3447
MU = 3384
USDCAD = 3433
USDCZK = 3448
SPN35CASH = 3360
INTC = 1562
VOWG_P = 3319
EXPE = 1647
COFFEEC = 4959
XAGEUR = 3416
XRP = 1521
EUR = 1
NZDJPY = 3415
AIRF = 3312
CZK = 151
ETH = 1451
XAGUSD = 3413
USDHUF = 3437
USDILSIS = 3379
DBKGN = 3314
SGD = 451
ILS = 1051
SOYBEANC = 4957
LVS = 1609
XMR = 1623
COCOA = 4951
AUDJPY = 3431
ALVG = 3323
NOK = 351
UAH = 1575
PM = 1545
COFFEE = 4952
HOG = 1564
TROW = 1512
XDG = 1603
RBAG = 3325
TRY = 651
CORN = 4953
BAT_TST = 1504
AUS200CASH = 3361
MSFT = 3310
USDT = 1610
CORNC = 4958
GE = 1644
META = 3373
BAC = 1524
BABA = 1542
WHEATC = 4960
CAD = 51
DEVX = 1516
NZDCHF = 3442
WMT = 1519
JP225CASH = 3364
DKK = 201
GOOG = 3315
AUDUSD = 3411
PG = 1593
EURCZK = 3420
EURAUD = 3422
TWC = 1557
ZAR = 1001
)
const (
BUY = iota
SELL
MARKET = -1
)
func (i *Identity) Login() {
url := "https://" + i.Server + "/api/auth/login"
method := "POST"
payload, err := json.Marshal(i)
if err != nil {
fmt.Println(err)
return
}
client := &http.Client{}
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("authority", "dxtrade."+i.Server+".com")
req.Header.Add("accept", "*/*")
req.Header.Add("accept-language", "en-US,en;q=0.9")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("content-type", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
_, err = io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
i.Cookies = make(map[string]string)
for _, cookie := range res.Cookies() {
i.Cookies[cookie.Name] = cookie.Value
}
i.EstablishHandshake()
}
func (i *Identity) GetTransactions() Positions {
inc_msg := strings.Split(i.EstablishHandshake("POSITIONS"), "|")
if len(inc_msg) < 2 {
return Positions{}
}
inc_msg2 := inc_msg[1]
var positions Positions
err := json.Unmarshal([]byte(inc_msg2), &positions)
if err != nil {
fmt.Println(err)
return Positions{}
}
return positions
}
func (i *Identity) EstablishHandshake(kill_msg ...string) string {
// Websocket
var kill string
if len(kill_msg) > 0 {
kill = kill_msg[0]
}
dialer := websocket.Dialer{}
headers := http.Header{}
headers.Add("Cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
conn, _, err := dialer.Dial("wss://"+i.Server+"/client/connector?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.3.2-javascript&X-Atmosphere-Transport=websocket&X-Atmosphere-TrackMessageSize=true&Content-Type=text/x-gwt-rpc;%20charset=UTF-8&X-atmo-protocol=true&sessionState=dx-new&guest-mode=false", headers)
if err != nil {
fmt.Println(err)
return ""
}
defer conn.Close()
// Send handshake
for {
_, message, err := conn.ReadMessage()
if err != nil {
fmt.Println(err)
return ""
}
if strings.Contains(string(message), kill) {
return string(message)
}
}
return ""
}
func (i *Identity) Buy(Quantity, Price, TakeProfit, StopLoss float64, symbol string, instrumentId int) {
i.ExecuteOrder(BUY, Quantity, Price, TakeProfit, StopLoss, symbol, instrumentId)
}
func (i *Identity) Sell(Quantity, Price, TakeProfit, StopLoss float64, symbol string, instrumentId int) {
i.ExecuteOrder(SELL, -Quantity, Price, TakeProfit, StopLoss, symbol, instrumentId)
}
type ExecutePayload struct {
DirectExchange bool `json:"directExchange"`
Legs []struct {
InstrumentId int `json:"instrumentId"`
PositionEffect string `json:"positionEffect"`
RatioQuantity int `json:"ratioQuantity"`
Symbol string `json:"symbol"`
} `json:"legs"`
LimitPrice float64 `json:"limitPrice"`
OrderSide string `json:"orderSide"`
OrderType string `json:"orderType"`
Quantity float64 `json:"quantity"`
RequestId string `json:"requestId"`
TimeInForce string `json:"timeInForce"`
StopLoss struct {
FixedOffset int `json:"fixedOffset"`
FixedPrice float64 `json:"fixedPrice"`
OrderType string `json:"orderType"`
PriceFixed bool `json:"priceFixed"`
QuantityForProtection float64 `json:"quantityForProtection"`
Removed bool `json:"removed"`
} `json:"stopLoss"`
TakeProfit struct {
FixedOffset int `json:"fixedOffset"`
FixedPrice float64 `json:"fixedPrice"`
OrderType string `json:"orderType"`
PriceFixed bool `json:"priceFixed"`
QuantityForProtection float64 `json:"quantityForProtection"`
Removed bool `json:"removed"`
} `json:"takeProfit"`
}
func (i *Identity) CloseAllPositions() {
positions := i.GetTransactions()
for _, position := range positions.Body {
i.ClosePosition(position.PositionKey.PositionCode, position.Quantity, 0, position.PositionKey.PositionCode, position.PositionKey.InstrumentId)
}
}
func (i *Identity) ClosePosition(PositionId string, Quantity float64, Price float64, symbol string, instrumentId int) {
url := "https://" + i.Server + "/api/positions/close"
method := "POST"
var payload ClosePosition
legs := make([]struct {
InstrumentId int `json:"instrumentId"`
PositionCode string `json:"positionCode"`
PositionEffect string `json:"positionEffect"`
RatioQuantity int `json:"ratioQuantity"`
Symbol string `json:"symbol"`
}, 1)
legs[0].InstrumentId = instrumentId
legs[0].PositionCode = PositionId
legs[0].PositionEffect = "CLOSING"
legs[0].RatioQuantity = 1
legs[0].Symbol = symbol
payload.Legs = legs
payload.LimitPrice = 0
payload.OrderType = "MARKET"
payload.Quantity = -Quantity
payload.TimeInForce = "GTC"
client := &http.Client{}
payloadJson, err := json.Marshal(payload)
if err != nil {
fmt.Println(err)
return
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(payloadJson))
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("content-type", "application/json; charset=UTF-8")
req.Header.Add("cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
req.Header.Add("x-csrf-token", i.FetchCSRF())
req.Header.Add("x-requested-with", "XMLHttpRequest")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
fmt.Println(res.Status)
}
type CloseStruct struct {
Legs []struct {
InstrumentId int `json:"instrumentId"`
PositionCode string `json:"positionCode"`
PositionEffect string `json:"positionEffect"`
RatioQuantity int `json:"ratioQuantity"`
Symbol string `json:"symbol"`
} `json:"legs"`
LimitPrice float64 `json:"limitPrice"`
OrderType string `json:"orderType"`
Quantity float64 `json:"quantity"`
TimeInForce string `json:"timeInForce"`
}
func (i *Identity) PartialClose(InstrumentId int, PositionCode, Symbol string, Quantity float64) error {
//https://dxtrade.ftmo.com/api/positions/close
url := "https://" + i.Server + "/api/positions/close"
method := "POST"
var payload CloseStruct
payload.Legs = make([]struct {
InstrumentId int `json:"instrumentId"`
PositionCode string `json:"positionCode"`
PositionEffect string `json:"positionEffect"`
RatioQuantity int `json:"ratioQuantity"`
Symbol string `json:"symbol"`
}, 1)
payload.Legs[0].InstrumentId = InstrumentId
payload.Legs[0].PositionCode = PositionCode
payload.Legs[0].PositionEffect = "CLOSING"
payload.Legs[0].RatioQuantity = 1
payload.Legs[0].Symbol = Symbol
payload.LimitPrice = 0
payload.OrderType = "MARKET"
payload.Quantity = Quantity // not auto
payload.TimeInForce = "GTC"
client := &http.Client{}
payloadJson, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(payloadJson))
req.Header.Add("content-type", "application/json; charset=UTF-8")
req.Header.Add("cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
req.Header.Add("x-csrf-token", i.FetchCSRF())
req.Header.Add("x-requested-with", "XMLHttpRequest")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (i *Identity) ExecuteOrder(Method int, Quantity, Price, TakeProfit, StopLoss float64, symbol string, instrumentId int) {
var executePayload ExecutePayload
executePayload.DirectExchange = false
executePayload.Legs = make([]struct {
InstrumentId int `json:"instrumentId"`
PositionEffect string `json:"positionEffect"`
RatioQuantity int `json:"ratioQuantity"`
Symbol string `json:"symbol"`
}, 1)
// The generated request ID gwt-uid-931-08b3a3e1-5e92-4db9-9b32-049777c03e17
executePayload.Legs[0].Symbol = symbol
executePayload.Legs[0].InstrumentId = instrumentId
executePayload.Legs[0].PositionEffect = "OPENING"
executePayload.Legs[0].RatioQuantity = 1
switch Price {
case -1:
executePayload.DirectExchange = false
executePayload.LimitPrice = 0
executePayload.OrderType = "MARKET"
default:
executePayload.DirectExchange = false
executePayload.LimitPrice = Price
executePayload.OrderType = "LIMIT"
}
switch Method {
case BUY:
executePayload.OrderSide = "BUY"
case SELL:
executePayload.OrderSide = "SELL"
}
if TakeProfit != 0 {
executePayload.TakeProfit.FixedOffset = 0
executePayload.TakeProfit.FixedPrice = TakeProfit
executePayload.TakeProfit.OrderType = "LIMIT"
executePayload.TakeProfit.PriceFixed = true
executePayload.TakeProfit.QuantityForProtection = Quantity
executePayload.TakeProfit.Removed = false
}
if StopLoss != 0 {
executePayload.StopLoss.FixedOffset = 0
executePayload.StopLoss.FixedPrice = StopLoss
executePayload.StopLoss.OrderType = "STOP"
executePayload.StopLoss.PriceFixed = true
executePayload.StopLoss.QuantityForProtection = Quantity
executePayload.StopLoss.Removed = false
}
executePayload.Quantity = Quantity
executePayload.TimeInForce = "GTC"
//931-08b3a3e1-5e92-4db9-9b32-049777c03e17
executePayload.RequestId = "gwt-uid-931-" + uuid.New().String()
url := "https://" + i.Server + "/api/orders/single"
method := "POST"
payload, err := json.Marshal(executePayload)
if StopLoss == 0 && TakeProfit == 0 {
payload = []byte(strings.ReplaceAll(string(payload), ",\"stopLoss\":{\"fixedOffset\":0,\"fixedPrice\":0,\"orderType\":\"\",\"priceFixed\":false,\"quantityForProtection\":0,\"removed\":false},\"takeProfit\":{\"fixedOffset\":0,\"fixedPrice\":0,\"orderType\":\"\",\"priceFixed\":false,\"quantityForProtection\":0,\"removed\":false}", ""))
}
client := &http.Client{}
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("content-type", "application/json; charset=UTF-8")
req.Header.Add("cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
req.Header.Add("x-csrf-token", i.FetchCSRF())
req.Header.Add("x-requested-with", "XMLHttpRequest")
// Fetch csrf document.querySelector('meta[name="csrf"]'))
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
if res.StatusCode != http.StatusOK {
fmt.Println(req.Header)
fmt.Println(string(payload), res.Status)
}
fmt.Println(string(payload), res.Status)
defer res.Body.Close()
}
func (i *Identity) FetchCSRF() string {
url := "https://" + i.Server + "/"
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return ""
}
req.Header.Add("authority", "dxtrade."+i.Server+".com")
req.Header.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
req.Header.Add("accept-language", "en-US,en;q=0.9")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return ""
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
// GET THIS <meta id="csrf-token" name="csrf" content="">
if err != nil {
fmt.Println(err)
return ""
}
if strings.Contains(string(body), "name=\"csrf\" content=\"") {
csrf := strings.Split(string(body), "name=\"csrf\" content=\"")
csrf = strings.Split(csrf[1], "\">")
return csrf[0]
}
return ""
}
func (i *Identity) GetOrders() *Order {
order_str := strings.Split(i.EstablishHandshake("ORDERS"), "|")[1]
var orders *Order
err := json.Unmarshal([]byte(order_str), &orders)
if err != nil {
fmt.Println(err)
return nil
}
return orders
}
func (i *Identity) GetInstruments() *GetInstruments {
instrument_str := strings.Split(i.EstablishHandshake("Euro vs United States Dollar"), "|")[1]
var instruments *GetInstruments
err := json.Unmarshal([]byte(instrument_str), &instruments)
if err != nil {
fmt.Println(err)
return nil
}
return instruments
}
func (i *Identity) GetAccountMetrix() *AccountData {
inc_msg := strings.Split(i.EstablishHandshake("ACCOUNT_METRICS"), "|")
if len(inc_msg) < 2 {
return nil
}
inc_msg2 := inc_msg[1]
var accountData *AccountData
err := json.Unmarshal([]byte(inc_msg2), &accountData)
if err != nil {
fmt.Println(err)
return nil
}
return accountData
}
func (i *Identity) PositionMetrix() *PositionMetrix {
inc_msg := strings.Split(i.EstablishHandshake("POSITION_METRICS"), "|")
if len(inc_msg) < 2 {
return nil
}
inc_msg2 := inc_msg[1]
var positionMetrix *PositionMetrix
err := json.Unmarshal([]byte(inc_msg2), &positionMetrix)
if err != nil {
fmt.Println(err)
return nil
}
return positionMetrix
}
func (i *Identity) CancelOrder(OrderId int) bool {
url := fmt.Sprintf("https://dxtrade.%s.com/api/orders/cancel?accountId=%d&orderChainId=%d", i.Server, i.AccountId, OrderId)
method := "DELETE"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return false
}
req.Header.Add("Content-Type", "application/json; charset=UTF-8")
req.Header.Add("Cookie", fmt.Sprintf("DXTFID=%s; JSESSIONID=%s", i.Cookies["DXTFID"], i.Cookies["JSESSIONID"]))
req.Header.Add("X-CSRF-Token", i.FetchCSRF())
req.Header.Add("X-Requested-With", "XMLHttpRequest")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return false
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return false
}
return true
}
func (i *Identity) CancelAllOrders() {
orders := i.GetOrders()
for _, order := range orders.Body {
i.CancelOrder(order.OrderId)
}
}
func getTodayTimestampMs() int64 {
now := time.Now()
timestampMs := now.UnixNano() / int64(time.Millisecond)
return timestampMs
}
func (i *Identity) TradeHistory() []TradeHistory {
i.Login()
timestampMs := strconv.Itoa(int(getTodayTimestampMs()))
fiveDaysAgoTimestampMs := strconv.Itoa(int(getTodayTimestampMs() - 5*24*60*60*1000))
url := "https://" + i.Server + "/api/history?from=" + fiveDaysAgoTimestampMs + "&to=" + timestampMs + "&orderId="
fmt.Println(url)
method := "POST"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return nil
}
req.Header.Add("content-type", "application/json; charset=UTF-8")
req.Header.Add("cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
req.Header.Add("x-csrf-token", i.FetchCSRF())
req.Header.Add("x-requested-with", "XMLHttpRequest")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return nil
}
defer res.Body.Close()
var tradeHistory []TradeHistory
err = json.NewDecoder(res.Body).Decode(&tradeHistory)
if err != nil {
fmt.Println(err)
return nil
}
return tradeHistory
}
func (i *Identity) CandleStickProcess(symbol string) (*CandleStickData, error) {
retry:
var dataStr string
timeStart := time.Now()
go func() {
dataStr = i.EstablishHandshake("BigChartComponentPresenter")
if strings.Contains(dataStr, "BigChartComponentPresenter") {
dataStr = strings.Split(dataStr, "|")[1]
} else {
dataStr = "err"
}
}()
i.EstablishHandshake("INSTRUMENT_METRICS")
url := "https://dxtrade.ftmo.com/api/instruments/subscribeInstrumentSymbols"
method := "PUT"
payload := strings.NewReader(`{"instruments":["` + symbol + `"]}`)
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return nil, err
}
req.Header.Add("accept", "*/*")
req.Header.Add("accept-language", "en-US,en;q=0.9")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("content-type", "application/json; charset=UTF-8")
req.Header.Add("cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
req.Header.Add("x-csrf-token", i.FetchCSRF())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return nil, err
}
defer res.Body.Close()
url = "https://dxtrade.ftmo.com/api/charts"
method = "PUT"
payload = strings.NewReader(`{"chartIds":[],"requests":[{"aggregationPeriodSeconds":300,"extendedSession":true,"forexPriceField":"bid","id":0,"maxBarsCount":3500,"range":345600,"studySubscription":[],"subtopic":"BigChartComponentPresenter-4","symbol":"` + symbol + `"}]}`)
client = &http.Client{}
req, err = http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return nil, err
}
req.Header.Add("accept", "*/*")
req.Header.Add("accept-language", "en-US,en;q=0.9")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("content-type", "application/json; charset=UTF-8")
req.Header.Add("cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
req.Header.Add("x-csrf-token", i.FetchCSRF())
req.Header.Add("x-requested-with", "XMLHttpRequest")
res, err = client.Do(req)
if err != nil {
fmt.Println(err)
return nil, err
}
defer res.Body.Close()
for {
if dataStr != "" {
if strings.Contains(dataStr, "BigChartComponentPresenter") {
var candleData *CandleStickData
err := json.Unmarshal([]byte(dataStr), &candleData)
if err != nil {
fmt.Println(err)
return nil, err
}
return candleData, nil
} else if dataStr == "err" {
return nil, errors.New("error fetching data")
}
break
}
if time.Since(timeStart) > 6500*time.Millisecond {
log.Println("TIMEOUT")
goto retry
}
time.Sleep(1 * time.Second)
}
return nil, errors.New("error fetching data")
}
func (i *Identity) GetCandleStickData(sym string) *CandleStickData {
var retrievedData *CandleStickData
for interval := 0; interval < 10; interval++ {
go func() {
data, err := i.CandleStickProcess(sym)
if err != nil {
fmt.Println(err)
} else {
retrievedData = data
}
}()
}
for {
if retrievedData != nil {
return retrievedData
}
time.Sleep(100 * time.Millisecond)
}
/*
Candlestick data is in a slice under the data key ranging from the oldest to the newest data available by candle
I'm not sure what the timeframe is
*/
}
// Identical to the one above but with a different return type
// Couldnt get it to work, some weird api update but if you want to give it a shot you need to be able to find the refOrderChainId
//
// func (i *Identity) ModifyOrder(orderId int, newLimit, newTp, newSl float64) bool {
// type Payload struct {
// DirectExchange bool `json:"directExchange"`
// Legs []struct {
// InstrumentId int `json:"instrumentId"`
// PositionEffect string `json:"positionEffect"`
// RatioQuantity int `json:"ratioQuantity"`
// Symbol string `json:"symbol"`
// } `json:"legs"`
// LimitPrice float64 `json:"limitPrice"`
// OrderSide string `json:"orderSide"`
// OrderType string `json:"orderType"`
// Quantity float64 `json:"quantity"`
// RefOrderChainId string `json:"refOrderChainId"`
// RequestId string `json:"requestId"`
// StopLoss struct {
// FixedOffset int `json:"fixedOffset"`
// FixedPrice float64 `json:"fixedPrice"`
// OrderType string `json:"orderType"`
// PriceFixed bool `json:"priceFixed"`
// QuantityForProtection float64 `json:"quantityForProtection"`
// RefOrderChainId string `json:"refOrderChainId"`
// Removed bool `json:"removed"`
// } `json:"stopLoss"`
// TakeProfit struct {
// FixedOffset int `json:"fixedOffset"`
// FixedPrice float64 `json:"fixedPrice"`
// OrderType string `json:"orderType"`
// PriceFixed bool `json:"priceFixed"`
// QuantityForProtection float64 `json:"quantityForProtection"`
// RefOrderChainId string `json:"refOrderChainId"`
// Removed bool `json:"removed"`
// } `json:"takeProfit"`
// TimeInForce string `json:"timeInForce"`
// }
// var x int
// for i, order := range i.GetOrders().Body {
// if order.OrderId == orderId {
// x = i
// }
// }
// myOrder := i.GetOrders().Body[x]
// var payload Payload
// payload.DirectExchange = false
// payload.Legs = make([]struct {
// InstrumentId int `json:"instrumentId"`
// PositionEffect string `json:"positionEffect"`
// RatioQuantity int `json:"ratioQuantity"`
// Symbol string `json:"symbol"`
// }, 1)
// fmt.Println(myOrder.Legs[0].PositionCode)
//
// payload.Legs[0].InstrumentId = myOrder.Legs[0].InstrumentId
// payload.Legs[0].PositionEffect = "OPENING"
// payload.Legs[0].RatioQuantity = 1
// var symbol string
// for key, value := range symbols {
// if value == myOrder.Legs[0].InstrumentId {
// symbol = key
// }
// }
// payload.Legs[0].Symbol = symbol
// switch newLimit {
// case -1:
// payload.LimitPrice = 0
// payload.OrderType = "MARKET"
// default:
// payload.LimitPrice = newLimit
// payload.OrderType = "LIMIT"
// }
// var orderSide string
// if myOrder.Quantity > 0 {
// orderSide = "BUY"
// } else {
// orderSide = "SELL"
// }
// payload.OrderSide = orderSide
// payload.Quantity = myOrder.Quantity
// payload.RefOrderChainId = myOrder.OrderChainId
// fmt.Println("ORDER IDS", myOrder.OrderChainId, myOrder.OrderId, myOrder.ParentOrderId, myOrder.RequestId, myOrder.ThenOrdersIds)
// if newTp != 0 {
// payload.TakeProfit.FixedOffset = 0
// payload.TakeProfit.FixedPrice = newTp
// payload.TakeProfit.OrderType = "LIMIT"
// payload.TakeProfit.PriceFixed = true
// payload.TakeProfit.QuantityForProtection = myOrder.Quantity
// payload.TakeProfit.RefOrderChainId = "3870852:20601"
// payload.TakeProfit.Removed = false
// }
// if newSl != 0 {
// payload.StopLoss.FixedOffset = 0
// payload.StopLoss.FixedPrice = newSl
// payload.StopLoss.OrderType = "STOP"
// payload.StopLoss.PriceFixed = true
// payload.StopLoss.QuantityForProtection = myOrder.Quantity
// payload.StopLoss.RefOrderChainId = "3870852:20602"
// payload.StopLoss.Removed = false
// }
// payload.TimeInForce = "GTC"
// payload.RequestId = "gwt-uid-2553-" + uuid.New().String()
// url := "https://"+i.Server+"/api/orders/single"
// var payloadJson []byte
// payloadJson, err := json.Marshal(payload)
// fmt.Println(string(payloadJson))
// if err != nil {
// fmt.Println(err)
// return false
// }
// client := &http.Client{}
// req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadJson))
//
// if err != nil {
// fmt.Println(err)
// return false
// }
// req.Header.Add("content-type", "application/json; charset=UTF-8")
// req.Header.Add("cookie", "DXTFID="+i.Cookies["DXTFID"]+"; JSESSIONID="+i.Cookies["JSESSIONID"])
// req.Header.Add("x-csrf-token", i.FetchCSRF())
// req.Header.Add("x-requested-with", "XMLHttpRequest")
//
// res, err := client.Do(req)
// if err != nil {
// fmt.Println(err)
// return false
// }
// defer res.Body.Close()
//
// body, err := ioutil.ReadAll(res.Body)
// if err != nil {
// fmt.Println(err)
// return false
// }
// fmt.Println(string(body))
// return true
// }
type PositionMetrix struct {
AccountId string `json:"accountId"`
Body []struct {
Uid string `json:"uid"`
AccountId string `json:"accountId"`
Margin float64 `json:"margin"`
PlOpen float64 `json:"plOpen"`
PlClosed int `json:"plClosed"`
TotalCommissions interface{} `json:"totalCommissions"`
TotalFinancing interface{} `json:"totalFinancing"`
PlRate float64 `json:"plRate"`
} `json:"body"`
Type string `json:"type"`
}
type AccountData struct {
AccountId string `json:"accountId"`
Body struct {
AccountId string `json:"accountId"`
AllMetrics struct {
AvailableFunds float64 `json:"availableFunds"`
MarginCallLevel interface{} `json:"marginCallLevel"`
RiskLevel float64 `json:"riskLevel"`
OpenPl float64 `json:"openPl"`
CashBalance float64 `json:"cashBalance"`
Equity float64 `json:"equity"`
ConversionRate int `json:"conversionRate"`
ReverseRiskLevel interface{} `json:"reverseRiskLevel"`
InitialMargin float64 `json:"initialMargin"`
} `json:"allMetrics"`
} `json:"body"`
Type string `json:"type"`
}
type Order struct {
AccountId string `json:"accountId"`
Body []struct {
OrderId int `json:"orderId"`
AccountId string `json:"accountId"`
OrderChainId string `json:"orderChainId"`
OcoGroupCode interface{} `json:"ocoGroupCode"`
BracketGroupCode interface{} `json:"bracketGroupCode"`
Status string `json:"status"`