-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsource-code.txt
1248 lines (1029 loc) · 42.6 KB
/
source-code.txt
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
//-----------------------------------------------------------------------------
// Sample Web TradingApp
//
// This sample illustrates the ability for a web browser to interact with
// EasyLanguage using hashtags in the url and javascript function calls.
//-----------------------------------------------------------------------------
using elsystem.windows.forms;
using elsystem.drawing;
using elsystem.collections;
using platform;
vars:
String url("http://frankts.github.io/tradestation-web-tradingapp-sample/v95-u14/index.html"),
bool isWebPageLoaded(false),
Form mainForm(null),
WebBrowser web(null),
ProgressBar progress(null),
bool displayDebugInfo(false),
SymbolListDialog slDialog(null),
SymbolLinking symbolLink(null),
intrabarpersist string currentSymbol(""),
string JS_AP_CALLBACK("TradingAppAccountsDelegate"),
string JS_OT_CALLBACK("TradingAppOrdersTicketDelegate"),
string JS_OP_CALLBACK("TradingAppOrdersDelegate"),
string JS_PP_CALLBACK("TradingAppPositionsDelegate"),
string JS_SL_CALLBACK("TradingAppSymbolListDelegate"),
string JS_SC_CALLBACK("TradingAppSymbolLinkDelegate");
method void AnalysisTechnique_Initialized( elsystem.Object sender, elsystem.InitializedEventArgs args )
var: int x, Dictionary dict;
begin
currentSymbol = symbol;
symbolLink = SymbolLinking.Create();
symbolLink.SetContext += symbolLink_SetContext;
symbolLink.GetContext += symbolLink_GetContext;
web = WebBrowser.Create(100, 100);
web.Dock = DockStyle.fill;
progress = ProgressBar.Create(1,8);
progress.Dock = DockStyle.bottom;
progress.Maximum = 0;
progress.Maximum = 100;
progress.Value = 100;
mainForm = Form.Create("TradingAppJSForm", 100, 100);
mainForm.Dock = DockStyle.right;
mainForm.AddControl(progress);
mainForm.AddControl(web);
web.AllowWebBrowserDrop = false;
web.ScriptErrorsSuppressed = false;
web.DocumentCompleted += web_DocumentCompleted;
web.ProgressChanged += web_ProgressChanged;
web.Navigating += web_Navigating;
AccountsProvider1.statechanged += AccountsProvider1_StateChanged;
AccountsProvider1.updated += AccountsProvider1_Updated;
web.Navigate(url);
mainForm.Show();
progress.Visible = false;
end;
method void web_Navigating( elsystem.Object sender, elsystem.windows.forms.WebBrowserNavigatingEventArgs args )
var: int hashLocation, int startLocation;
begin
if displayDebugInfo then
print("URL -> " + args.Url);
progress.Visible = true;
isWebPageLoaded = true;
if IsHashUrl(args.Url) then
EvaluateURL(args.Url);
end;
method void web_ProgressChanged ( elsystem.Object sender, elsystem.EventArgs args )
var:
elsystem.windows.forms.WebBrowserProgressChangedEventArgs progArgs;
begin
progArgs = args astype elsystem.windows.forms.WebBrowserProgressChangedEventArgs;
progress.Minimum = 0;
progress.Maximum = progArgs.MaximumProgress;
if progArgs.CurrentProgress >= 0 then
progress.Value = progArgs.CurrentProgress;
If progress.Minimum = 0 and progress.Maximum = 0 then
progress.Visible = false
Else
progress.Visible = true;
end;
method void web_DocumentCompleted (elsystem.Object sender, elsystem.windows.forms.WebBrowserDocumentCompletedEventArgs args )
begin
progress.Visible = false;
end;
method void symbolLink_GetContext ( elsystem.Object sender, platform.SymbolLinkingEventArgs args )
begin
// You will need to manage your own symbol if using SymbolLinking and referring to a symbol
// as your current symbol that is different than the keyword "symbol". Keep track of your symbol
// by creating your own variable (intrabar persist) and assigning that variable a expressed here.
args.Symbol = currentSymbol;
args.Recalculate = false;
end;
method void symbolLink_SetContext ( elsystem.Object sender, platform.SymbolLinkingEventArgs args )
begin
currentSymbol = args.Symbol;
web.Document.InvokeScript(JS_SC_CALLBACK + "Changed", args.Symbol);
web.Document.InvokeScript(JS_SC_CALLBACK + "Next");
args.Recalculate = false;
end;
//--------------------------------------------------------------------------------
// Check for specific hashtags
//--------------------------------------------------------------------------------
method bool IsHashUrl(string url)
begin
//-------------------------------------
// Hashtag prefixes
//-------------------------------------
// #OT = OrderTicket
// #OP = OrdersProvider
// #PP = PositionsProvider
// #AP = AccountsProvider
// #FN = Function Call
return (instr( URL, "#SC:") > 0) or
(instr( URL, "#OT:") > 0) or
(instr( URL, "#OP:") > 0) or
(instr( URL, "#PP:") > 0) or
(instr( URL, "#AP:") > 0) or
(instr( URL, "#SL:") > 0) or
(instr( URL, "#FN:") > 0);
end;
//--------------------------------------------------------------------------------
// Inspect the URL for
//--------------------------------------------------------------------------------
method bool EvaluateURL(string url)
vars: string fields, bool res;
begin
if IsHashUrl(url) then
begin
fields = ExtractFieldsFromURL(url, "#SC:");
if (strlen(fields) > 0) then
begin
JSSymbolLink(fields);
return true;
end;
fields = ExtractFieldsFromURL(url, "#AP:");
if (strlen(fields) > 0) then
begin
JSAccountsProvider(fields);
return true;
end;
fields = ExtractFieldsFromURL(url, "#OT:");
if (strlen(fields) > 0) then
begin
JSOrderTicket(fields);
return true;
end;
fields = ExtractFieldsFromURL(url, "#OP:");
if (strlen(fields) > 0) then
begin
JSOrdersProvider(fields);
return true;
end;
fields = ExtractFieldsFromURL(url, "#PP:");
if (strlen(fields) > 0) then
begin
JSPositionsProvider(fields);
return true;
end;
fields = ExtractFieldsFromURL(url, "#SL:");
if (strlen(fields) > 0) then
begin
JSSymbolList(fields);
return true;
end;
end;
return false;
end;
method void JSSymbolLink(string fields)
var: string field, Vector jsArr, SymbolContext symbolCtx;
begin
field = FindField(fields, "AN=", ";");
if strlen(field) > 0 and upperstr(field) = "SYMBOL" then
begin
web.Document.InvokeScript(JS_SC_CALLBACK + "Changed", symbol);
web.Document.InvokeScript(JS_SC_CALLBACK + "Next");
end;
field = FindField(fields, "BROADCAST=", ";");
if strlen(field) > 0 then
begin
symbolCtx = SymbolContext.Create();
symbolCtx.Symbol = field;
symbolLink.SetSymbolContext(symbolCtx);
web.Document.InvokeScript(JS_SC_CALLBACK + "Next");
end;
end;
method void JSSymbolList(string fields)
var: string field, Vector jsArr;
begin
field = FindField(fields, "AN=", ";");
if strlen(field) > 0 and upperstr(field) = "DISPLAY" then
begin
slDialog = SymbolListDialog.Create();
slDialog.StatusChanged += OnSymbolList_StatusChanged;
slDialog.Show();
end;
end;
method void OnSymbolList_StatusChanged( elsystem.Object sender, DialogStatusChangedEventArgs args )
vars: int x, Vector jsArr;
begin
jsArr = Vector.Create();
For x = 0 to slDialog.Count - 1
begin
jsArr.push_back(slDialog[x]);
end;
web.Document.InvokeScript(JS_SL_CALLBACK + "Add", VectorToJSArray(jsArr));
web.Document.InvokeScript(JS_SL_CALLBACK + "Next");
end;
method void JSAccountsProvider(string fields)
var: string field, int x, int y, Vector jsArr;
begin
field = FindField(fields, "AN=", ";");
if strlen(field) > 0 and upperstr(field) = "ITEMS" then
begin
UpdateAccountsJS("items", "");
end;
web.Document.InvokeScript(JS_AP_CALLBACK + "Next");
end;
method void JSOrderTicket(string fields)
vars: string field;
begin
field = FindField(fields, "AN=", ";");
if strlen(field) > 0 and upperstr(field) = "LOAD" then
begin
LoadOrderTicketJS(OrderTicket1);
end
else if strlen(field) > 0 and upperstr(field) = "SEND" then
begin
field = FindField(fields, "TK=", ";");
if strlen(field) > 0 then
SendOrderTicketJS(OrderTicket1, field);
end;
web.Document.InvokeScript(JS_OT_CALLBACK + "Next");
end;
method void JSOrdersProvider(string fields)
vars: string field, tsdata.trading.Order order;
begin
field = FindField(fields, "AN=", ";");
if strlen(field) > 0 and upperstr(field) = "LOAD" then
begin
UpdateOrdersJS("load");
end
else if strlen(field) > 0 and upperstr(field) = "CANCEL" then
begin
field = FindField(fields, "ID=", ";");
if strlen(field) > 0 then
begin
order = OrdersProvider1.Order[field];
if order <> null then
begin
order.Cancel();
web.Document.InvokeScript(JS_OP_CALLBACK + "Update", "cancel");
end;
end;
end
else if strlen(field) > 0 and upperstr(field) = "REPLACE" then
begin
field = FindField(fields, "ID=", ";");
if strlen(field) > 0 then
begin
order = OrdersProvider1.Order[field];
if order <> null then
begin
field = FindField(fields, "TK=", ";");
if strlen(field) > 0 then
begin
order.Replace(DictionaryToReplaceTicket(JSONToDictionary(field), tsdata.trading.ReplaceTicket.Create()));
web.Document.InvokeScript(JS_OP_CALLBACK + "Update", "replace");
end;
end;
end;
end;
web.Document.InvokeScript(JS_OP_CALLBACK + "Next");
end;
method void JSPositionsProvider(string fields)
vars: string field, tsdata.trading.Position posObj, string posSymbol, string posAccount, int location;
begin
field = FindField(fields, "AN=", ";");
if strlen(field) > 0 and upperstr(field) = "LOAD" then
begin
UpdatePositionsJS("load");
end
else if strlen(field) > 0 and upperstr(field) = "CLOSE" then
begin
field = FindField(fields, "ID=", ";");
if strlen(field) > 0 then
begin
location = instr(field, "_");
if location > 0 then
begin
posAccount = leftstr(field, location - 1);
posSymbol = rightstr(field, strlen(field)- location);
end;
posObj = PositionsProvider1.Position[posSymbol, posAccount];
if posObj <> null then
begin
posObj.Close();
web.Document.InvokeScript(JS_PP_CALLBACK + "Update", "close");
end;
end;
end;
web.Document.InvokeScript(JS_PP_CALLBACK + "Next");
end;
//#############################################################################
// PositionsProvider
//#############################################################################
method string IDForPosition(tsdata.trading.Position posObj)
begin
return posObj.AccountID + "_" + posObj.Symbol;
end;
method void UpdatePositionsJS(string reason)
vars: int x, int y, Vector jsArr;
begin
web.Document.InvokeScript(JS_PP_CALLBACK + "Clear");
For x = 0 to PositionsProvider1.Count - 1
begin
UpdatePositionJS( PositionsProvider1.Position[x], false);
end;
web.Document.InvokeScript(JS_PP_CALLBACK + "Update", "reason");
end;
method void UpdatePositionJS(tsdata.trading.Position posObj, bool extendOnly)
begin
web.Document.InvokeScript(JS_PP_CALLBACK + "Add", IDForPosition(posObj), DictionaryToJSON(PositionToDictionary(posObj)));
end;
method Dictionary PositionToDictionary(tsdata.trading.Position posObj)
vars: Dictionary dict;
begin
dict = Dictionary.Create();
dict.Add("PositionID", IDForPosition(posObj));
dict.Add("AccountID", posObj.AccountID);
dict.Add("AveragePrice", posObj.AveragePrice);
dict.Add("BigPointValue", posObj.BigPointValue);
dict.Add("ContractExpirationDate", posObj.ContractExpirationDate.Value);
dict.Add("InitialMargin", posObj.InitialMargin);
dict.Add("MaintenanceMargin", posObj.MaintenanceMargin);
dict.Add("MarketValue", posObj.MarketValue);
dict.Add("OpenPL", posObj.OpenPL);
dict.Add("OriginalCost", posObj.OriginalCost);
dict.Add("PLPerQuantity", posObj.PLPerQuantity);
dict.Add("PercentPL", posObj.PercentPL);
dict.Add("Quantity", posObj.Quantity);
dict.Add("RequiredMargin", posObj.RequiredMargin);
dict.Add("Symbol", posObj.Symbol);
dict.Add("SymbolType", posObj.SymbolType.ToString());
dict.Add("TotalCost", posObj.TotalCost);
return dict;
end;
// Method called on PositionsProvider1 Updated event.
// The sender parameter identifies the object that fires the event.
// The args parameter contains additional information about the event.
// NOTE: Do not modify the method name, return type, or input parameters.
method void PositionsProvider1_Updated( elsystem.Object sender, tsdata.trading.PositionUpdatedEventArgs args )
begin
if args.Position <> null then
begin
switch ( args.Reason )
begin
case tsdata.trading.PositionUpdateReason.added:
UpdatePositionJS( args.Position, false);
case tsdata.trading.PositionUpdateReason.initialupdate:
UpdatePositionJS( args.Position, false);
case tsdata.trading.PositionUpdateReason.realtimeupdate:
return;
case tsdata.trading.PositionUpdateReason.removed:
web.Document.InvokeScript(JS_PP_CALLBACK + "Remove", IDForPosition(args.Position));
default:
UpdatePositionJS( args.Position, true);
end;
web.Document.InvokeScript(JS_PP_CALLBACK + "Update", args.Reason.ToString(), IDForPosition(args.Position));
web.Document.InvokeScript(JS_PP_CALLBACK + "Next");
end;
end;
//#############################################################################
// OrderProvider
//#############################################################################
//--------------------------------------------------------------------------------
// Loop through all orders and send them to JS
//--------------------------------------------------------------------------------
method void UpdateOrdersJS(string reason)
vars: int x, int y, Vector jsArr;
begin
web.Document.InvokeScript(JS_OP_CALLBACK + "Clear");
For x = 0 to OrdersProvider1.Count - 1
begin
UpdateOrderJS( OrdersProvider1.Order[x], false);
end;
web.Document.InvokeScript(JS_OP_CALLBACK + "Update", reason, "");
end;
method void UpdateOrderJS(tsdata.trading.Order order, bool extendOnly)
begin
web.Document.InvokeScript(JS_OP_CALLBACK + "Add", order.OrderID, DictionaryToJSON(OrderToDictionary(order)));
end;
method Dictionary OrderToDictionary(tsdata.trading.Order order)
vars: Dictionary dict;
begin
dict = Dictionary.Create();
dict.Add("OrderID", order.OrderID.ToString());
dict.Add("AccountID", order.AccountID);
dict.Add("Action", order.Action.ToString());
dict.Add("AdvanceOptions", order.AdvanceOptions);
dict.Add("AllOrNone", order.AllOrNone.ToString());
dict.Add("AvgFilledPrice", order.AvgFilledPrice);
dict.Add("BuyMinusSellPlus", order.BuyMinusSellPlus.ToString());
dict.Add("Commission", order.Commission);
dict.Add("Discretionary", order.Discretionary.ToString());
dict.Add("DiscretionaryAmount", order.DiscretionaryAmount);
dict.Add("Duration", order.Duration);
dict.Add("DurationDate", order.DurationDate.Value);
dict.Add("ECNSweep", order.ECNSweep.ToString());
dict.Add("EnteredQuantity", order.EnteredQuantity);
dict.Add("EnteredTime", order.EnteredTime.Value);
dict.Add("FilledQuantity", order.FilledQuantity);
dict.Add("FilledTime", order.FilledTime.Value);
dict.Add("GeneratingApplication", order.GeneratingApplication.ToString());
dict.Add("IfTouched", order.IfTouched.ToString());
dict.Add("IfTouchedPrice", order.IfTouchedPrice);
dict.Add("IfTouchedPriceOffset", order.IfTouchedPriceOffset);
dict.Add("IfTouchedPriceStyle", order.IfTouchedPriceStyle.ToString());
dict.Add("LeftQuantity", order.LeftQuantity);
dict.Add("LimitPrice", order.LimitPrice);
dict.Add("LimitPriceOffset", order.LimitPriceOffset);
dict.Add("LimitPriceStyle", order.LimitPriceStyle.ToString());
dict.Add("LotSize", order.LotSize);
dict.Add("NonDisplay", order.NonDisplay.ToString());
dict.Add("OCOGroupID", order.OCOGroupID);
dict.Add("OSOParentID", order.OSOParentID);
dict.Add("Originator", order.Originator);
dict.Add("Peg", order.Peg.ToString());
dict.Add("Route", order.Route);
dict.Add("ShowOnly", order.ShowOnly.ToString());
dict.Add("ShowOnlyQuantity", order.ShowOnlyQuantity);
dict.Add("SpreadName", order.SpreadName);
dict.Add("State", order.State.ToString());
dict.Add("StateDetail", order.StateDetail.ToString());
dict.Add("StopPrice", order.StopPrice);
dict.Add("StopPriceOffset", order.StopPriceOffset);
dict.Add("StopPriceStyle", order.StopPriceStyle.ToString());
dict.Add("Symbol", order.Symbol);
dict.Add("SymbolExtension", order.SymbolExtension);
dict.Add("TrailingStop", order.TrailingStop.ToString());
dict.Add("TrailingStopAmount", order.TrailingStopAmount);
dict.Add("TrailingStopValue", order.TrailingStopValue);
dict.Add("Type", order.Type.ToString());
return dict;
end;
// Method called on OrdersProvider1 StateChanged event.
// The sender parameter identifies the object that fires the event.
// The args parameter contains additional information about the event.
// NOTE: Do not modify the method name, return type, or input parameters.
method void OrdersProvider1_StateChanged( elsystem.Object sender, tsdata.common.StateChangedEventArgs args )
begin
{ Insert your EasyLanguage statements below }
end;
// Method called on OrdersProvider1 Updated event.
// The sender parameter identifies the object that fires the event.
// The args parameter contains additional information about the event.
// NOTE: Do not modify the method name, return type, or input parameters.
method void OrdersProvider1_Updated( elsystem.Object sender, tsdata.trading.OrderUpdatedEventArgs args )
begin
if strlen(args.OrderID) > 0 then
begin
if displayDebugInfo then
print(args.Reason.ToString());
switch ( args.Reason )
begin
case tsdata.trading.OrderUpdateReason.added:
UpdateOrderJS( args.Order, false);
case tsdata.trading.OrderUpdateReason.statechanged:
UpdateOrderJS( args.Order, false);
case tsdata.trading.OrderUpdateReason.removed:
web.Document.InvokeScript(JS_OP_CALLBACK + "Remove", args.Order.OrderID);
default:
UpdateOrderJS( args.Order, true);
end;
web.Document.InvokeScript(JS_OP_CALLBACK + "Update", args.Reason.ToString(), args.OrderID);
web.Document.InvokeScript(JS_OP_CALLBACK + "Next");
end;
end;
//#############################################################################
// OrderTicket
//#############################################################################
method void SendOrderTicketJS(tsdata.trading.OrderTicket ot, string json)
vars: Dictionary dict;
begin
dict = JSONToDictionary(json);
DictionaryToOrderTicket(dict, ot);
ot.Send();
web.Document.InvokeScript(JS_OT_CALLBACK + "Send");
end;
method void LoadOrderTicketJS(tsdata.trading.OrderTicket ot)
begin
web.Document.InvokeScript(JS_OT_CALLBACK + "Extend", DictionaryToJSON(OrderTicketToDictionary(ot)));
web.Document.InvokeScript(JS_OT_CALLBACK + "Load");
end;
method tsdata.trading.ReplaceTicket DictionaryToReplaceTicket(Dictionary dict, tsdata.trading.ReplaceTicket rt)
vars: string field;
begin
rt.IfTouched = DictionaryValueToBool(dict, "IfTouched", rt.IfTouched);
rt.IfTouchedPrice = DictionaryValueToDouble(dict, "IfTouchedPrice", rt.IfTouchedPrice);
rt.IfTouchedPriceOffset = DictionaryValueToInt(dict, "IfTouchedPriceOffset", rt.IfTouchedPriceOffset);
rt.IfTouchedPriceStyle = DictionaryValueToPriceStyle(dict, "IfTouchedPriceStyle", rt.IfTouchedPriceStyle);
rt.LimitPrice = DictionaryValueToDouble(dict, "LimitPrice", rt.LimitPrice);
rt.LimitPriceOffset = DictionaryValueToInt(dict, "LimitPriceOffset", rt.LimitPriceOffset);
rt.LimitPriceStyle = DictionaryValueToPriceStyle(dict, "LimitPriceStyle", rt.LimitPriceStyle);
rt.Quantity = DictionaryValueToInt(dict, "Quantity", rt.Quantity);
rt.StopPrice = DictionaryValueToDouble(dict, "StopPrice", rt.StopPrice);
rt.StopPriceOffset = DictionaryValueToInt(dict, "StopPriceOffset", rt.StopPriceOffset);
rt.StopPriceStyle = DictionaryValueToPriceStyle(dict, "StopPriceStyle", rt.StopPriceStyle);
rt.TrailingStop = DictionaryValueToTrailingStopBehavior(dict, "TrailingStop", rt.TrailingStop);
rt.TrailingStopAmount = DictionaryValueToDouble(dict, "TrailingStopAmount", rt.TrailingStopAmount);
rt.Type = DictionaryValueToOrderType( dict, "Type", rt.Type);
return rt;
end;
method tsdata.trading.OrderTicket DictionaryToOrderTicket(Dictionary dict, tsdata.trading.OrderTicket ot)
vars: string field;
begin
ot.Account = DictionaryValueToString(dict, "Account", ot.Account);
ot.Action = DictionaryValueToOrderAction(dict, "Action", ot.Action);
ot.AllOrNone = DictionaryValueToBool(dict, "AllOrNone", ot.AllOrNone);
ot.BuyMinusSellPlus = DictionaryValueToBool(dict, "BuyMinusSellPlus", ot.BuyMinusSellPlus);
ot.ConvertInvalidStopToMarket = DictionaryValueToBool(dict, "ConvertInvalidStopToMarket", ot.ConvertInvalidStopToMarket);
ot.Discretionary = DictionaryValueToBool(dict, "Discretionary", ot.Discretionary);
ot.DiscretionaryAmount = DictionaryValueToDouble(dict, "DiscretionaryAmount", ot.DiscretionaryAmount);
ot.Duration = DictionaryValueToString(dict, "Duration", ot.Duration);
ot.DurationDate = DictionaryValueToDateTime(dict, "DurationDate", ot.DurationDate);
ot.ECNSweep = DictionaryValueToBool(dict, "ECNSweep", ot.ECNSweep);
ot.IfTouched = DictionaryValueToBool(dict, "IfTouched", ot.IfTouched);
ot.IfTouchedPrice = DictionaryValueToDouble(dict, "IfTouchedPrice", ot.IfTouchedPrice);
ot.IfTouchedPriceOffset = DictionaryValueToInt(dict, "IfTouchedPriceOffset", ot.IfTouchedPriceOffset);
ot.IfTouchedPriceStyle = DictionaryValueToPriceStyle(dict, "IfTouchedPriceStyle", ot.IfTouchedPriceStyle);
ot.LimitPrice = DictionaryValueToDouble(dict, "LimitPrice", ot.LimitPrice);
ot.LimitPriceOffset = DictionaryValueToInt(dict, "LimitPriceOffset", ot.LimitPriceOffset);
ot.LimitPriceStyle = DictionaryValueToPriceStyle(dict, "LimitPriceStyle", ot.LimitPriceStyle);
ot.NonDisplay = DictionaryValueToBool(dict, "NonDisplay", ot.NonDisplay);
ot.Peg = DictionaryValueToPegBehavior(dict, "Peg", ot.Peg);
ot.Quantity = DictionaryValueToInt(dict, "Quantity", ot.Quantity);
ot.Route = DictionaryValueToString(dict, "Route", ot.Route);
ot.ShowOnly = DictionaryValueToBool(dict, "ShowOnly", ot.ShowOnly);
ot.ShowOnlyQuantity = DictionaryValueToInt(dict, "ShowOnlyQuantity", ot.ShowOnlyQuantity);
ot.StopPrice = DictionaryValueToDouble(dict, "StopPrice", ot.StopPrice);
ot.StopPriceOffset = DictionaryValueToInt(dict, "StopPriceOffset", ot.StopPriceOffset);
ot.StopPriceStyle = DictionaryValueToPriceStyle(dict, "StopPriceStyle", ot.StopPriceStyle);
ot.Symbol = DictionaryValueToString(dict, "Symbol", ot.Symbol);
ot.SymbolType = DictionaryValueToSecurityType(dict, "SymbolType", ot.SymbolType);
ot.TrailingStop = DictionaryValueToTrailingStopBehavior(dict, "TrailingStop", ot.TrailingStop);
ot.TrailingStopAmount = DictionaryValueToDouble(dict, "TrailingStopAmount", ot.TrailingStopAmount);
ot.Type = DictionaryValueToOrderType( dict, "Type", ot.Type);
return ot;
end;
method Dictionary OrderTicketToDictionary(tsdata.trading.OrderTicket ot)
vars: Dictionary dict;
begin
dict = Dictionary.Create();
dict.Add("TicketType", ot.TicketType.ToString());
dict.Add("OrderPlacementEnabled", ot.OrderPlacementEnabled.ToString());
dict.Add("Account", ot.Account);
dict.Add("Action", ot.Action.ToString());
// dict.Add("ActivationRules", ot.ActivationRules.ToString());
dict.Add("AllOrNone", ot.AllOrNone.ToString());
dict.Add("BuyMinusSellPlus", ot.BuyMinusSellPlus.ToString());
dict.Add("ConvertInvalidStopToMarket", ot.ConvertInvalidStopToMarket.ToString());
dict.Add("Discretionary", ot.Discretionary.ToString());
dict.Add("DiscretionaryAmount", ot.DiscretionaryAmount);
dict.Add("Duration", ot.Duration.ToString());
dict.Add("DurationDate", ot.DurationDate.Value);
dict.Add("ECNSweep", ot.ECNSweep.ToString());
dict.Add("IfTouched", ot.IfTouched.ToString());
dict.Add("IfTouchedPrice", ot.IfTouchedPrice);
dict.Add("IfTouchedPriceOffset", ot.IfTouchedPriceOffset);
dict.Add("IfTouchedPriceStyle", ot.IfTouchedPriceStyle.ToString());
dict.Add("LimitPrice", ot.LimitPrice);
dict.Add("LimitPriceOffset", ot.LimitPriceOffset);
dict.Add("LimitPriceStyle", ot.LimitPriceStyle.ToString());
dict.Add("NonDisplay", ot.NonDisplay.ToString());
dict.Add("Peg", ot.Peg.ToString());
dict.Add("Quantity", ot.Quantity);
dict.Add("Route", ot.Route);
dict.Add("ShowOnly", ot.ShowOnly.ToString());
dict.Add("ShowOnlyQuantity", ot.ShowOnlyQuantity);
dict.Add("StopPrice", ot.StopPrice);
dict.Add("StopPriceOffset", ot.StopPriceOffset);
dict.Add("StopPriceStyle", ot.StopPriceStyle.ToString());
dict.Add("Symbol", ot.Symbol);
dict.Add("SymbolType", ot.SymbolType.ToString());
dict.Add("TrailingStop", ot.TrailingStop.ToString());
dict.Add("TrailingStopAmount", ot.TrailingStopAmount);
dict.Add("Type", ot.Type.ToString());
return dict;
end;
//#############################################################################
// AccountsProvider
//#############################################################################
//--------------------------------------------------------------------------------
// Update or Add a specific account using JS
//--------------------------------------------------------------------------------
method void UpdateAccountJS(tsdata.trading.Account account, bool extendOnly)
begin
web.Document.InvokeScript(JS_AP_CALLBACK + "Add", account.AccountID, DictionaryToJSON(AccountToDictionary(account)));
end;
//--------------------------------------------------------------------------------
// Loop through all accounts and send them to JS
//--------------------------------------------------------------------------------
method void UpdateAccountsJS(string reason, string accountID)
vars: int x, int y, Vector jsArr;
begin
web.Document.InvokeScript(JS_AP_CALLBACK + "Clear");
For x = 0 to AccountsProvider1.Count - 1
begin
UpdateAccountJS( AccountsProvider1.Account[x], false);
end;
web.Document.InvokeScript(JS_AP_CALLBACK + "Update", reason, accountID);
end;
method Dictionary AccountToDictionary(tsdata.trading.Account acct)
vars: Dictionary dict;
begin
dict = Dictionary.Create();
dict.Add("AccountID", acct.AccountID);
dict.Add("BDAccountEquity", acct.BDAccountEquity);
dict.Add("BDAccountNetWorth", acct.BDAccountNetWorth);
dict.Add("BDCashBalance", acct.BDCashBalance);
dict.Add("BDDayTradingBuyingPower", acct.BDDayTradingBuyingPower);
dict.Add("BDOptionBuyingPower", acct.BDOptionBuyingPower);
dict.Add("BDOptionLiquidationValue", acct.BDOptionLiquidationValue);
dict.Add("BDOvernightBuyingPower", acct.BDOvernightBuyingPower);
dict.Add("BDUnrealizedPL", acct.BDUnrealizedPL);
dict.Add("CanDayTrade", acct.CanDayTrade.ToString());
dict.Add("FourDaysTradeCount", acct.FourDaysTradeCount);
dict.Add("IsDayTrader", acct.IsDayTrader.ToString());
dict.Add("Name", acct.Name);
dict.Add("OptionsApprovalLevel", acct.OptionsApprovalLevel);
dict.Add("RTAccountEquity", acct.RTAccountEquity);
dict.Add("RTAccountNetWorth", acct.RTAccountNetWorth);
dict.Add("RTCashBalance", acct.RTCashBalance);
dict.Add("RTCostOfPositions", acct.RTCostOfPositions);
dict.Add("RTDayTradingBuyingPower", acct.RTDayTradingBuyingPower);
dict.Add("RTInitialMargin", acct.RTInitialMargin);
dict.Add("RTMaintenanceMargin", acct.RTMaintenanceMargin);
dict.Add("RTOptionBuyingPower", acct.RTOptionBuyingPower);
dict.Add("RTOvernightBuyingPower", acct.RTOvernightBuyingPower);
dict.Add("RTPurchasingPower", acct.RTPurchasingPower);
dict.Add("RTRealizedPL", acct.RTRealizedPL);
dict.Add("RTUnrealizedPL", acct.RTUnrealizedPL);
dict.Add("Status", acct.Status);
dict.Add("TodaysRTTradeEquity", acct.TodaysRTTradeEquity);
dict.Add("Type", acct.Type.ToString());
dict.Add("UnclearedDeposits", acct.UnclearedDeposits);
dict.Add("UnsettledFund", acct.UnsettledFund);
return dict;
end;
method void AccountsProvider1_StateChanged( elsystem.Object sender, tsdata.common.StateChangedEventArgs args )
var: Dictionary dict;
begin
// web.Document.InvokeScript(JS_AP_CALLBACK + "Status", args.NewState.ToString(), args.OldState.ToString());
end;
method void AccountsProvider1_Updated( elsystem.Object sender, tsdata.trading.AccountUpdatedEventArgs args )
begin
if args.Reason <> tsdata.trading.AccountUpdateReason.realtimeupdate and isWebPageLoaded <> false and args.Account <> null then
begin
if displayDebugInfo then
print(args.Reason.ToString());
UpdateAccountJS(args.Account, false);
web.Document.InvokeScript(JS_AP_CALLBACK + "Update", args.Reason.ToString(), args.AccountID);
web.Document.InvokeScript(JS_AP_CALLBACK + "Next");
end;
end;
//#############################################################################
// Helper methods
//#############################################################################
//--------------------------------------------------------------------------------
// FindField will extract text from a string given an identifier and a delimiter
//--------------------------------------------------------------------------------
method string FindField(string fields, string identifier, string delimiter)
var: string field, int fieldLocation, int delimiterLocation;
begin
fieldLocation = instr( fields, identifier );
if fieldLocation > 0 then
begin
field = rightstr( fields, strlen(fields) - (fieldLocation + strlen(identifier) - 1));
delimiterLocation = instr( field, delimiter );
if delimiterLocation > 0 then
begin
field = leftstr(field, delimiterLocation - 1);
end;
end;
return field;
end;
//--------------------------------------------------------------------------------
// ExtractFieldsFromURL
//--------------------------------------------------------------------------------
method string ExtractFieldsFromURL(string url, string token)
vars: string fields, int location, int rightLocation;
begin
location = instr( URL, token);
if location > 0 then
begin
rightLocation = strlen(url) - (location + (strlen(token) - 1));
if rightLocation > 0 then
begin
fields = rightstr(url, rightLocation);
end;
end;
return fields;
end;
//--------------------------------------------------------------------------------
// Given a Dictionary, this function will return a JSON string and will
// recursively look for Dictionaries nested within Dictionaries and expose its
// content.
//--------------------------------------------------------------------------------
method string DictionaryToJSON( Dictionary dict )
vars: string jsonResult, int x, string itemName, elsystem.Object itemValue;
begin
jsonResult = "{";
For x = 0 to dict.Keys.Count - 1
begin
If x > 0 then
jsonResult += ",";
itemName = dict.Keys[x].ToString();
itemValue = dict.Items[itemName];
if itemValue istype Dictionary then
begin
jsonResult += doublequote + itemName + doublequote + ":" + DictionaryToJSON(itemValue astype Dictionary);
end
else if itemValue istype int or itemValue istype double or itemValue istype float then
begin
jsonResult += doublequote + itemName + doublequote + ":" + itemValue.ToString();
end
else
begin
jsonResult += doublequote + itemName + doublequote + ":" + doublequote + itemValue.ToString() + doublequote;
end;
end;
jsonResult += "}";
return jsonResult;
end;
//--------------------------------------------------------------------------------
// Given a JSON string, this function will return a Dictionary and will
// recursively look for embedded JSON objects nested within the string and expose
// its content as an embedded Dictionary.
//--------------------------------------------------------------------------------
method Dictionary JSONToDictionary(string json)
vars: int lastLocation, int firstLocation, int jsObjectLocation, int endOffset, bool isNumber, bool isDecimal,
Dictionary dict, string decoration, string modifiedJSON, string pair, string key, elsystem.Object value, string strValue;
begin
dict = Dictionary.Create();
if instr(json, "'") > 0 then
decoration = "'"
else
decoration = doublequote;
lastLocation = instr(json, "{");
modifiedJSON = rightstr(json, strlen(json) - lastLocation);
while lastLocation <> 0 begin
firstLocation = instr(modifiedJSON, decoration);
lastLocation = instr(modifiedJSON, "," + decoration);
jsObjectLocation = instr(modifiedJSON, ":{");
endOffset = 0;
// sometimes you will have an embedded json object as part of a field
// if that happens, skip the , + delimiter search are look for the
// end bracket }
if lastLocation = 0 or ((jsObjectLocation > 0) and (jsObjectLocation < lastLocation)) then
begin
endOffset = 1;
lastLocation = instr(modifiedJSON, "}");
end;
if firstLocation <> 0 and lastLocation <> 0 then
begin
isNumber = false;
isDecimal = false;
// extract the pair from the current string
pair = midstr(modifiedJSON, firstLocation, (lastLocation - firstLocation) + endOffset);
// separate the pair including surrounding decorations
key = midstr(pair, 1, instr(pair, decoration + ":"));
strValue = rightstr(pair, strlen(pair) - (strlen(key) + 1));
// remove decorations and extract text
key = midstr(key, 2, strlen(key) - 2);
// if the value includes a pair pattern, treat it like another json string
if instr(strValue, "{") = 1 then
value = JSONToDictionary(strValue)
else
begin
// otherwise, check if we've reached the end
if instr(strValue, "}") = strlen(strValue) then
strValue = leftstr(strValue, strlen(strValue) - 1);
// strip the surrounding decorations
if instr(strValue, decoration) = 1 then
strValue = midstr(strValue, 2, strlen(strValue) - 2)
else
begin
// if there are no decorations, then we have a number
isNumber = true;
if instr(strValue, ".") <> 0 then
isDecimal = true;
end;
value = strValue;
end;
// make sure to case the number correctly so that we have a valid number
// in the dictionary
if isNumber = True then
begin
if isDecimal = true then
dict.Add(key, strtonum(strValue) astype double)
else
dict.Add(key, strtonum(strValue) astype int);
end
else
dict.Add(key, value);
end;
// trim the string an continue
modifiedJSON = rightstr(modifiedJSON, strlen(modifiedJSON) - (lastLocation + endOffset));
end;
return dict;
end;
//--------------------------------------------------------------------------------
// Converts strings and primitive types to a javascript array
//--------------------------------------------------------------------------------
method string VectorToJSArray(Vector arr)
var: string jsResult, int x, bool isString;
begin
isString = ((arr.Count > 0) and (arr[0] istype String));
jsResult = "[";
For x = 0 to arr.Count - 1
begin
If x > 0 then
jsResult += ",";
if isString = true then
jsResult += doublequote + arr[x].ToString() + doublequote
else