-
Notifications
You must be signed in to change notification settings - Fork 1
/
FXCM-To-Oanda-Trade-Copier.lua
1192 lines (1006 loc) · 58.5 KB
/
FXCM-To-Oanda-Trade-Copier.lua
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
---------------------------------------- Overview ------------------------------------------
-- Name: FXCM To Oanda Trade Copier
-- Notes: Copyright (c) 2017 Jeremy Gulickson
-- Version: 2.1.05112017
-- Format: major.minor.mmddyyyy
--
-- Usage: Designed to copy postion(s) from an FXCM account to an Oanda account.
-- FXCM postions are sourced from FXTS directly and Oanda positions verified
-- and modified via RESTful API queries.
--
-- Software Requirements: FXTS (FXCM Trading Station)
-- http_lua.dll
-- JSON.lua -> http://regex.info/blog/lua/json
--
-- Account Requirements: FXCM: N/A.
-- Oanda: Account must be type v20.
--
-- FXCM Documentation: http://www.fxcodebase.com/bin/products/IndicoreSDK/3.1.0/help/Lua/web-content.html
-- Oanda Documentation: http://developer.oanda.com/rest-live-v20/introduction/
--
-------------------------------------- Version History -------------------------------------
-- v1.0.01302017: -> Initial release; proof of concept.
--
-- v1.1.02022017: -> Performance improvements and cosmetic code updates.
-- -> Added ParseResponse() function to parse LUA tables for eventual
-- validation of responses from Oanda.
-- -> Added CreatePriceBounds() function to use in CreateOrder() to
-- support Oanda's priceBounds parameter (FXCM's market
-- range equivalent).
-- -> Added PositionCheck() function; currently not functional.
-- -> Updated log method for improved troubleshooting. Went from
-- 'Host:trace' to a custom 'WriteToLog:debug' funtion.
--
-- v1.2.02032017: -> Completed PositionCheck() function, now functional.
--
-- v1.3.02272017: -> Added email notifications.
--
-- v1.4.03192017: -> Cosmetic clean up; removed hardcoded values to make Github ready.
--
-- v2.0.04122017: -> Added email notifications for order execution.
-- -> Updated select default values.
--
-- v2.1.05112017: -> Removed hardcoded values in AsyncOperationFinished() and
-- updated to FXCM.SymbolToTrack.
--
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
-- Global Variable Setup
--------------------------------------------------------------------------------------------
-- Require LUA extension to support HTTPS protocol
require("http_lua");
-- Global strategy variables
local Host = nil;
local StopStrategy = false;
-- Global Oanda variables
local Oanda = {};
Oanda.DemoSubdomain = "api-fxpractice.oanda.com";
Oanda.RealSubdomain = "api-fxtrade.oanda.com";
Oanda.Subdomain = nil;
Oanda.APIToken = nil;
Oanda.AccountType = nil;
Oanda.AccountID = nil;
Oanda.AllowedSlippageInPips = nil;
Oanda.MaxOrderAttempts = nil;
Oanda.MaxPostionCheckAttempts= nil;
-- Global FXCM variables
local FXCM = {};
FXCM.AccountID = nil;
FXCM.SendEmailOrderExecution = nil;
FXCM.SendEmailStrategyFailure = nil;
FXCM.EmailAddress = nil;
FXCM.SymbolToTrack = nil;
-- Global Timer variables
local Timer = {};
Timer.HealthCheck = nil;
Timer.SendRequest = nil;
Timer.PositionCheckInterval = nil;
--------------------------------------------------------------------------------------------
-- Initialize Function
--------------------------------------------------------------------------------------------
function Init()
strategy:name("FXCM To Oanda Trade Copier");
strategy:type(core.Both);
strategy.parameters:addGroup("FXCM Related");
strategy.parameters:addString("FXCMAccountID", "Account ID", "Enter the FXCM account id to monitor.", "");
strategy.parameters:setFlag("FXCMAccountID", core.FLAG_ACCOUNT);
strategy.parameters:addGroup("Oanda Related");
strategy.parameters:addString("AccountType", "Account Type", "Select the Oanda account type.", "Demo")
strategy.parameters:addStringAlternative("AccountType", "Demo", "", "Demo");
strategy.parameters:addStringAlternative("AccountType", "Real", "", "Real");
strategy.parameters:addString("APIToken", "API Token", "Enter the Oanda API token.", "");
strategy.parameters:addString("OandaAccountID", "Account ID", "Enter the Oanda account id to trade.", "");
strategy.parameters:addDouble("AllowedSlippageInPips", "Allowed Slippage Range", "Enter allowed slippage in pips.", 5, 0, 100);
strategy.parameters:addInteger("MaxOrderAttempts", "Maximum Order Attempts", "Enter the maximum attempts to resend an order before stopping the stratey.", 5, 1, 10);
strategy.parameters:addInteger("MaxPostionCheckAttempts", "Maximum Position Check Attempts", "Enter the maximum position check attempts before stopping the stratey.", 5, 1, 10);
strategy.parameters:addGroup("Notification Related");
strategy.parameters:addBoolean("SendEmailOrderExecution", "Send Email On Order Execution", "Send an email on order execution.", true);
strategy.parameters:addBoolean("SendEmailStrategyFailure", "Send Email On Failure", "Send an email if strategy fails.", true);
strategy.parameters:addString("EmailAddress", "Email Address", "Enter the recipient address to email.", "");
strategy.parameters:setFlag("EmailAddress", core.FLAG_EMAIL);
strategy.parameters:addGroup("Risk Related");
strategy.parameters:addBoolean("RunHealthCheck", "Health Check", "Perform a health check on startup.", true);
strategy.parameters:addBoolean("PositionCheck", "Position Check", "Perform a postion check between brokers at set intervals.", true);
strategy.parameters:addInteger("PositionCheckInterval", "Positions Check Interval", "Enter time between positions checks in minutes.", 30, 5, 120);
strategy.parameters:addGroup("Log Related");
strategy.parameters:addInteger("LogLevel", "Log Level", "Determines what informaton to write to an external log.", 0);
strategy.parameters:addIntegerAlternative("LogLevel", "Off", "", 0);
strategy.parameters:addIntegerAlternative ("LogLevel", "Error", "", 1);
strategy.parameters:addIntegerAlternative("LogLevel", "Info", "", 2);
strategy.parameters:addIntegerAlternative("LogLevel", "Debug", "", 3);
end
--------------------------------------------------------------------------------------------
-- Prepare Function
--------------------------------------------------------------------------------------------
function Prepare()
Host = core.host;
Oanda.AccountType = instance.parameters.AccountType;
Oanda.APIToken = instance.parameters.APIToken;
Oanda.AccountID = instance.parameters.OandaAccountID;
FXCM.AccountID = instance.parameters.FXCMAccountID;
if Oanda.AccountType == "Real" then
Oanda.Subdomain = Oanda.RealSubdomain;
else
Oanda.Subdomain = Oanda.DemoSubdomain;
end
Oanda.AllowedSlippageInPips = instance.parameters.AllowedSlippageInPips;
Oanda.MaxOrderAttempts = instance.parameters.MaxOrderAttempts;
Oanda.MaxPostionCheckAttempts = instance.parameters.MaxPostionCheckAttempts;
FXCM.SendEmailOrderExecution = instance.parameters.SendEmailOrderExecution;
FXCM.SendEmailStrategyFailure = instance.parameters.SendEmailStrategyFailure;
FXCM.EmailAddress = instance.parameters.EmailAddress;
FXCM.SymbolToTrack = instance.bid:instrument();
instance:name("FXCM (" .. FXCM.AccountID .. ") to Oanda (" .. Oanda.AccountID .. ")");
WriteToLog = Logger:create("Marketscope-Log", instance.parameters.LogLevel);
if instance.parameters.RunHealthCheck then Timer.HealthCheck = Host:execute("setTimer", 100, 2) end
if instance.parameters.PositionCheck then Timer.PositionCheckInterval = Host:execute("setTimer", 200, instance.parameters.PositionCheckInterval * 60) end
Host:execute("subscribeTradeEvents", 300, "trades");
WriteToLog:debug("Prepare() <<<<< FINISHED <<<<< FINISHED <<<<<");
end
--------------------------------------------------------------------------------------------
-- Update Function
--------------------------------------------------------------------------------------------
function Update()
--Not Employed
end
--------------------------------------------------------------------------------------------
-- Health Check Function
--------------------------------------------------------------------------------------------
function HealthCheck(oInstrument)
WriteToLog:info("HealthCheck() >>>>> STARTING >>>>> STARTING >>>>>");
local zURL = nil;
local zRequestReponse = nil;
local zPass = true;
WriteToLog:debug("FXCM Account ID: ".. FXCM.AccountID);
WriteToLog:debug("Oanda API Token: " .. Oanda.APIToken);
WriteToLog:debug("Oanda Account ID: " .. Oanda.AccountID);
-- Get Accounts
zURL = CreateURLSyntax("Account", "GET_Accounts", Oanda.Subdomain, Oanda.AccountID, nil, nil, nil);
zRequestReponse = SendRequest("GET", zURL, nil, Oanda.APIToken);
if string.match(zRequestReponse, "accounts") ~= nil and string.match(zRequestReponse, "tags") ~= nil then
WriteToLog:debug("HealthCheck() | GET_Accounts Passed");
else
WriteToLog:error("HealthCheck() | GET_Accounts Failed");
zPass = false;
end
ParseResponse("GET_Accounts", DecodeResponse(zRequestReponse));
-- Get Account
zURL = CreateURLSyntax("Account", "GET_Account", Oanda.Subdomain, Oanda.AccountID, nil, nil, nil);
zRequestReponse = SendRequest("GET", zURL, nil, Oanda.APIToken);
if string.match(zRequestReponse, "account") ~= nil and string.match(zRequestReponse, "pendingOrderCount") ~= nil then
WriteToLog:debug("HealthCheck() | GET_Account Passed");
else
WriteToLog:error("HealthCheck() | GET_Account Failed");
zPass = false;
end
ParseResponse("GET_Account", DecodeResponse(zRequestReponse));
-- Get Open Positions
zURL = CreateURLSyntax("Positions", "GET_OpenPositions", Oanda.Subdomain, Oanda.AccountID, nil, nil, nil);
zRequestReponse = SendRequest("GET", zURL, nil, Oanda.APIToken);
if string.match(zRequestReponse, "positions") ~= nil and string.match(zRequestReponse, "lastTransactionID") ~= nil then
WriteToLog:debug("HealthCheck() | GET_OpenPositions Passed");
else
WriteToLog:error("HealthCheck() | GET_OpenPositions Failed");
zPass = false;
end
ParseResponse("GET_OpenPositions", DecodeResponse(zRequestReponse));
-- Get Pricing
zURL = CreateURLSyntax("Pricing", "GET_Pricing", Oanda.Subdomain, Oanda.AccountID, nil, nil, oInstrument);
zRequestReponse = SendRequest("GET", zURL, nil, Oanda.APIToken);
if string.match(zRequestReponse, "prices") ~= nil and string.match(zRequestReponse, "quoteHomeConversionFactors") ~= nil then
WriteToLog:debug("HealthCheck() | GET_Pricing Passed");
else
WriteToLog:error("HealthCheck() | GET_Pricing Failed");
zPass = false;
end
ParseResponse("GET_Pricing", DecodeResponse(zRequestReponse));
-- Get Instruments
zURL = CreateURLSyntax("Account", "GET_Instruments", Oanda.Subdomain, Oanda.AccountID, nil, nil, oInstrument);
zRequestReponse = SendRequest("GET", zURL, nil, Oanda.APIToken);
if string.match(zRequestReponse, "displayPrecision") ~= nil and string.match(zRequestReponse, "minimumTrailingStopDistance") ~= nil then
WriteToLog:debug("HealthCheck() | GET_Instruments Passed");
else
WriteToLog:error("HealthCheck() | GET_Instruments Failed");
zPass = false;
end
ParseResponse("GET_Instruments", DecodeResponse(zRequestReponse));
if zPass then
WriteToLog:debug("HealthCheck() <<<<< FINISHED <<<<< FINISHED <<<<<");
SendNotification("Trace", oInstrument, 0, "HealthCheck() Successful | Strategy OK", core.now());
SendNotification("StartEmail", oInstrument, 0, "HealthCheck() Successful | Strategy OK", core.now());
else
WriteToLog:error("HealthCheck() Failed");
SendNotification("All", oInstrument, 0, "HealthCheck() Failed | Strategy Stopped", core.now());
StopStrategy = true;
end
end
--------------------------------------------------------------------------------------------
-- Position Check Function
--------------------------------------------------------------------------------------------
function PositionCheck(oInstrument)
WriteToLog:info("PositionCheck() >>>>> STARTING >>>>> STARTING >>>>>");
local zFXCM = {};
local zOanda = {};
local zRequestReponse = nil;
-- Get FXCM Positions
zFXCM.OfferID = Host:findTable("offers"):find("Instrument", oInstrument).OfferID;
zFXCM.Table = Host:findTable("trades"):findAll("OfferID", zFXCM.OfferID);
zFXCM.Row = zFXCM.Table:next();
zFXCM.Units = 0;
while zFXCM.Row ~= nil do
if zFXCM.Row.OfferID == zFXCM.OfferID then
if zFXCM.Row.BS == "B" then
zFXCM.Units = zFXCM.Units + zFXCM.Row.Lot
elseif zFXCM.Row.BS == "S" then
zFXCM.Units = zFXCM.Units + (zFXCM.Row.Lot * -1)
else
WriteToLog:error("PositionCheck() Failed | In Else Clause; zFXCM.Row.BS Format Is Invalid");
end
end
zFXCM.Row = zFXCM.Table:next();
end
-- Get Oanda Positions
zOanda.AttemptNumber = 1;
while zOanda.AttemptNumber <= Oanda.MaxPostionCheckAttempts do
zURL = CreateURLSyntax("Positions", "GET_OpenPositions", Oanda.Subdomain, Oanda.AccountID, nil, nil, FormatInstrument(oInstrument));
zRequestReponse = SendRequest("GET", zURL, nil, Oanda.APIToken);
zOanda.Units = nil;
zOanda.LongUnits = nil;
zOanda.ShortUnits = nil;
if tonumber(ParseResponse("GET_OpenPositions", DecodeResponse(zRequestReponse)).Long.Units) ~= nil then
zOanda.LongUnits = tonumber(ParseResponse("GET_OpenPositions", DecodeResponse(zRequestReponse)).Long.Units)
else
zOanda.LongUnits = 0;
end
if tonumber(ParseResponse("GET_OpenPositions", DecodeResponse(zRequestReponse)).Short.Units) ~= nil then
zOanda.ShortUnits = tonumber(ParseResponse("GET_OpenPositions", DecodeResponse(zRequestReponse)).Short.Units)
else
zOanda.ShortUnits = 0;
end
zOanda.Units = zOanda.LongUnits + zOanda.ShortUnits;
zOanda.AttemptNumber = zOanda.AttemptNumber + 1;
end
-- Review to ensure FXCM units and Oanda units match
if zOanda.Units ~= nil and zFXCM.Units == zOanda.Units then
WriteToLog:debug("PositionCheck() Finished | FXCM Units: " .. zFXCM.Units .. " Oanda Units: " .. zOanda.Units .. ")");
SendNotification("Trace", oInstrument, 0, "PositionCheck() Successful | Strategy OK", core.now());
else
WriteToLog:error("PositionCheck() Failed | FXCM Units: " .. zFXCM.Units .. " Oanda Units: " .. zOanda.Units .. ")");
SendNotification("All", oInstrument, 0, "PositionCheck() Failed | Strategy Stopped", core.now());
StopStrategy = true;
end
WriteToLog:info("PositionCheck() <<<<< FINISHED <<<<< FINISHED <<<<<");
end
--------------------------------------------------------------------------------------------
-- Find Trade Function
--------------------------------------------------------------------------------------------
function FindTrade(oMessage, oMessage1, oMessage2)
WriteToLog:info("FindTrade() >>>>> STARTING >>>>> STARTING >>>>>");
local zTrade = {};
local zPass = false;
-- http://www.fxcodebase.com/bin/products/IndicoreSDK/3.1.0/help/Lua/TTTrades.html
if Host:findTable("trades"):find("OpenOrderID", oMessage1) ~= nil then
zTrade.Row = Host:findTable("trades"):find("OpenOrderID", oMessage1);
WriteToLog:debug("FindTrade() Finished | Ticket: " .. oMessage .. "; Order: " .. oMessage1 .. "; Direction: " .. zTrade.Row.BS .. "; Symbol: " .. zTrade.Row.Instrument .. "; Units: " .. zTrade.Row.Lot);
CreateOrder(zTrade.Row.Instrument, zTrade.Row.Lot, zTrade.Row.BS);
zPass = true;
-- http://www.fxcodebase.com/bin/products/IndicoreSDK/3.1.0/help/Lua/TTClosedTrades.html
elseif Host:findTable("closed trades"):find("CloseOrderID", oMessage1) ~= nil then
zTrade.Row = Host:findTable("closed trades"):find("CloseOrderID", oMessage1);
WriteToLog:debug("FindTrade() Finished | Ticket: " .. oMessage .. "; Order: " .. oMessage1 .. "; Direction: " .. zTrade.Row.BS .. "; Symbol: " .. zTrade.Row.Instrument .. "; Units: " .. zTrade.Row.Lot);
CreateOrder(zTrade.Row.Instrument, zTrade.Row.Lot, FlipDirection(zTrade.Row.BS));
zPass = true;
end
if zPass then
WriteToLog:debug("FindTrade() Finished | Called CreateOrder()");
WriteToLog:info("FindTrade() <<<<< FINISHED <<<<< FINISHED <<<<<");
SendNotification("Trace", zTrade.Row.Instrument, 0, "FindTrade() Successful | Strategy OK", core.now());
else
WriteToLog:error("FindTrade() failed via else clause; OrderID is not found in trades or closed trades tables");
SendNotification("All", zTrade.Row.Instrument, 0, "FindTrade() Failed | Strategy Stopped", core.now());
StopStrategy = true;
end
end
--------------------------------------------------------------------------------------------
-- Create Order Function
--------------------------------------------------------------------------------------------
function CreateOrder(oInstrument, oSize, oDirection)
WriteToLog:info("CreateOrder() >>>>> STARTING >>>>> STARTING >>>>>");
local zOrder = {};
local zURL = nil;
local zOrderSyntax = nil;
local zRequestReponse = nil;
local zPass = false;
-- Default Values
zOrder.TimeInForce = "FOK"
zOrder.OrderType = "MARKET"
zOrder.PositionFill = "REDUCE_FIRST"
zOrder.AttemptNumber = 1;
while zOrder.AttemptNumber <= Oanda.MaxOrderAttempts and zPass == false do
zOrder.PriceBound = CreatePriceBounds(oInstrument, oDirection, Oanda.AllowedSlippageInPips);
zURL = CreateURLSyntax("Orders", "POST_Order", Oanda.Subdomain, Oanda.AccountID, nil, nil, oInstrument);
zOrderSyntax = CreateOrderSyntax(FormatDirection(oDirection, oSize), FormatInstrument(oInstrument), zOrder.TimeInForce , zOrder.PriceBound, zOrder.OrderType, zOrder.PositionFill);
zRequestReponse = SendRequest("POST", zURL, zOrderSyntax, Oanda.APIToken);
if string.match(zRequestReponse, "orderCreateTransaction") ~= nil and string.match(zRequestReponse, "orderFillTransaction") ~= nil then
WriteToLog:error("CreateOrder() Successful | Attempt Number: " .. zOrder.AttemptNumber);
zPass = true;
else
WriteToLog:error("CreateOrder() Failed | Attempt Number: " .. zOrder.AttemptNumber);
SendNotification("Trace", oInstrument, 0, "CreateOrder() Failed | Attempt Number: " .. zOrder.AttemptNumber, core.now());
zPass = false;
end
zOrder.AttemptNumber = zOrder.AttemptNumber + 1;
end
if zPass then
WriteToLog:debug("CreateOrder() Finished | " .. zRequestReponse);
WriteToLog:info("CreateOrder() <<<<< FINISHED <<<<< FINISHED <<<<<");
SendNotification("Trace", oInstrument, 0, "CreateOrder() Successful | Strategy OK", core.now());
SendNotification("OrderExecution", oInstrument, 0, oDirection .. " " .. Format_Thousands(oSize, 0) .. " " .. oInstrument .. " order succesfully opened on ", core.now());
else
WriteToLog:error("CreateOrder() Failed");
SendNotification("All", oInstrument, 0, "CreateOrder() Failed | Strategy Stopped", core.now());
StopStrategy = true;
end
PositionCheck(oInstrument);
end
--------------------------------------------------------------------------------------------
-- Create Price Bounds Function
--------------------------------------------------------------------------------------------
function CreatePriceBounds(oInstrument, oDirection, oPointsRange)
WriteToLog:info("CreatePriceBounds() >>>>> STARTING >>>>> STARTING >>>>>");
local zPrice = {};
local zURL = nil;
local zRequestReponse = nil;
zURL = CreateURLSyntax("Account", "GET_Instruments", Oanda.Subdomain, Oanda.AccountID, nil, nil, FormatInstrument(oInstrument));
zRequestReponse = SendRequest("GET", zURL, nil, Oanda.APIToken);
zPrice.PipLocation = tonumber(ParseResponse("GET_Instruments", DecodeResponse(zRequestReponse)).Instruments.PipLocation);
zPrice.DisplayPrecision = ParseResponse("GET_Instruments", DecodeResponse(zRequestReponse)).Instruments.DisplayPrecision;
zPrice.AllowedSlippageInDecimals = oPointsRange * FormatPips(zPrice.PipLocation)
WriteToLog:debug("CreatePriceBounds() | Pip location: " .. zPrice.PipLocation);
WriteToLog:debug("CreatePriceBounds() | Display precision: " .. zPrice.DisplayPrecision);
WriteToLog:debug("CreatePriceBounds() | Allowed slippage in decimal form: " .. zPrice.AllowedSlippageInDecimals);
zURL = CreateURLSyntax("Pricing", "GET_Pricing", Oanda.Subdomain, Oanda.AccountID, nil, nil, oInstrument);
zRequestReponse = SendRequest("GET", zURL, nil, Oanda.APIToken);
if oDirection == "B" then
zPrice.BestAskPrice = ParseResponse("GET_Pricing", DecodeResponse(zRequestReponse)).Asks.Best.Price;
zPrice.PriceBound = FormatPrecision(zPrice.BestAskPrice + zPrice.AllowedSlippageInDecimals, zPrice.DisplayPrecision);
WriteToLog:debug("CreatePriceBounds() | Best ask price: " .. zPrice.BestAskPrice);
elseif oDirection == "S" then
zPrice.BestBidPrice = ParseResponse("GET_Pricing", DecodeResponse(zRequestReponse)).Bids.Best.Price;
zPrice.PriceBound = FormatPrecision(zPrice.BestBidPrice - zPrice.AllowedSlippageInDecimals, zPrice.DisplayPrecision);
WriteToLog:debug("CreatePriceBounds() | Best bid price: " .. zPrice.BestBidPrice);
else
WriteToLog:error("CreatePriceBounds() Failed | In Else Clause");
end
WriteToLog:debug("CreatePriceBounds() Finished | " .. zPrice.PriceBound);
WriteToLog:info("CreatePriceBounds() <<<<< FINISHED <<<<< FINISHED <<<<<");
return zPriceBound;
end
--------------------------------------------------------------------------------------------
-- Create URL Function
--------------------------------------------------------------------------------------------
function CreateURLSyntax(oEndpoint, oEndpointType, oSubdomain, oAccountID, oOrderID, oTradeID, oInstrument)
WriteToLog:info("CreateURLSyntax() >>>>> STARTING >>>>> STARTING >>>>>");
local zCompleteURL = nil;
local zBaseURL = nil;
local zBaseURL = "https://" .. oSubdomain .. "/v3/accounts/" .. oAccountID
-- Acount Endpoints
-- http://developer.oanda.com/rest-live-v20/account-ep/
if oEndpoint == "Account" then
if oEndpointType == "GET_Accounts" then zCompleteURL = "https://" .. oSubdomain .. "/v3/accounts";
elseif oEndpointType == "GET_Account" then zCompleteURL = zBaseURL;
elseif oEndpointType == "GET_Summary" then zCompleteURL = zBaseURL .. "/summary";
elseif oEndpointType == "GET_Instruments" then zCompleteURL = zBaseURL .. "/instruments?instruments=" .. FormatInstrument(oInstrument);
elseif oEndpointType == "PATCH_Configuration" then zCompleteURL = zBaseURL .. "/configuration";
elseif oEndpointType == "GET_Changes" then zCompleteURL = zBaseURL .. "/changes";
WriteToLog:error("CreateURLSyntax() Failed | In Sub Else Clause");
end
-- Orders Endpoints
-- http://developer.oanda.com/rest-live-v20/orders-ep/
elseif oEndpoint == "Orders" then
if oEndpointType == "POST_Order" then zCompleteURL = zBaseURL .. "/orders";
elseif oEndpointType == "GET_Orders" then zCompleteURL = zBaseURL .. "/orders";
elseif oEndpointType == "GET_PendingOrders" then zCompleteURL = zBaseURL .. "/pendingOrders";
elseif oEndpointType == "GET_SingleOrder" then zCompleteURL = zBaseURL .. "/orders/" .. oOrderID;
elseif oEndpointType == "PUT_ReplaceOrder" then zCompleteURL = zBaseURL .. "/orders/" .. oOrderID;
elseif oEndpointType == "PUT_CancelOrder" then zCompleteURL = zBaseURL .. "/orders" .. oOrderID .. "/cancel";
elseif oEndpointType == "PUT_UpdateExtension" then zCompleteURL = zBaseURL .. "/orders" .. oOrderID .."/clientExtensions";
WriteToLog:error("CreateURLSyntax() Failed | In Sub Else Clause");
end
-- Trades Endpoints
-- http://developer.oanda.com/rest-live-v20/trades-ep/
elseif oEndpoint == "Trades" then
if oEndpointType == "GET_Trades" then zCompleteURL = zBaseURL .. "/trades";
elseif oEndpointType == "GET_OpenTrade" then zCompleteURL = zBaseURL .. "/openTrades";
elseif oEndpointType == "GET_SingleTrade" then zCompleteURL = zBaseURL .. "/trades/" .. oTradeID;
elseif oEndpointType == "PUT_CloseTrade" then zCompleteURL = zBaseURL .. "/trades/" .. oTradeID .. "/close";
elseif oEndpointType == "PUT_UpdateExtension" then zCompleteURL = zBaseURL .. "/trades/" .. oTradeID .. "/clientExtensions";
elseif oEndpointType == "PUT_ModifyLinkedOrder" then zCompleteURL = zBaseURL .. "/trades/" .. oTradeID .. "/orders";
WriteToLog:error("CreateURLSyntax() Failed | In Sub Else Clause");
end
-- Positions Endpoints
-- http://developer.oanda.com/rest-live-v20/positions-ep/
elseif oEndpoint == "Positions" then
if oEndpointType == "GET_Postions" then zCompleteURL = zBaseURL .. "/positions";
elseif oEndpointType == "GET_OpenPositions" then zCompleteURL = zBaseURL .. "/openPositions";
elseif oEndpointType == "GET_InstrumentPosition" then zCompleteURL = zBaseURL .. "/positions/" .. oInstrument;
elseif oEndpointType == "PUT_ClosePosition" then zCompleteURL = zBaseURL .. "/positions/" .. oInstrument .. "/close";
WriteToLog:error("CreateURLSyntax() Failed | In Sub Else Clause");
end
-- Pricing Endpoints
-- http://developer.oanda.com/rest-live-v20/pricing-ep/
elseif oEndpoint == "Pricing" then
if oEndpointType == "GET_Pricing" then zCompleteURL = zBaseURL .. "/pricing?instruments=" .. FormatInstrument(oInstrument);
elseif oEndpointType == "GET_PricingStream" then zCompleteURL = zBaseURL .. "/pricing/stream";
WriteToLog:error("CreateURLSyntax() Failed | In Sub Else Clause");
end
else
WriteToLog:error("CreateURLSyntax() Failed | In Else Clause");
end
WriteToLog:debug("CreateURLSyntax() Finished | " .. zCompleteURL);
WriteToLog:info("CreateURLSyntax() <<<<< FINISHED <<<<< FINISHED <<<<<");
return zCompleteURL;
end
--------------------------------------------------------------------------------------------
-- Create Order Syntax Function
--------------------------------------------------------------------------------------------
function CreateOrderSyntax(oUnits, oInstrument, oTimeInForce, oPriceBound, oOrderType, oPositionFill)
WriteToLog:info("CreateOrderSyntax() >>>>> STARTING >>>>> STARTING >>>>>");
local zOrderTable = {};
zOrderTable["order"] = nil;
local zJSONString = nil;
zOrderTable["order"] = {units=oUnits, instrument=oInstrument, timeInForce=oTimeInForce, priceBound=oPriceBound, type=oOrderType, positionFill=oPositionFill};
zJSONString = EncodeResponse(zOrderTable);
WriteToLog:debug("CreateOrderSyntax() Finished | " .. tostring(zJSONString));
WriteToLog:info("CreateOrderSyntax() <<<<< FINISHED <<<<< FINISHED <<<<<");
return zJSONString;
end
--------------------------------------------------------------------------------------------
-- Send Request Function
--------------------------------------------------------------------------------------------
function SendRequest(oHTTPType, oCompleteURL, oOrderSyntax, oAPIToken)
WriteToLog:info("SendRequest() >>>>> STARTING >>>>> STARTING >>>>>");
local zRequest = {};
zRequest.URL = nil;
zRequest.HeadersAuthorization = nil;
zRequest.HeadersContentType = "application/json";
zRequest.Content = nil;
zRequest.Object = nil;
zRequest.Response = nil;
-- Create HTTP Object
zRequest.Object = http_lua.createRequest();
-- Set Up Variables
zRequest.URL = oCompleteURL;
zRequest.HeadersAuthorization = "Bearer " .. oAPIToken;
zRequest.Object:setRequestHeader("Authorization", zRequest.HeadersAuthorization);
-- Send HTTP Object
if oHTTPType == "GET" then
zRequest.Object:start(zRequest.URL, "GET");
elseif oHTTPType == "POST" then
zRequest.Object:setRequestHeader("Content-Type", zRequest.HeadersContentType);
zRequest.Content = oOrderSyntax;
zRequest.Object:start(zRequest.URL, "POST", zRequest.Content);
elseif oHTTPType == "PUT" then
WriteToLog:error("SendRequest() Failed | In Else Clause | Attempted With PUT HTTP Type");
elseif oHTTPType == "PATCH" then
WriteToLog:error("SendRequest() Failed | In Else Clause | Attempted With PATCH HTTP Type");
else
WriteToLog:error("SendRequest() Failed | In Else Clause");
end
-- Pause For Response; Up To 10 seconds
Timer.SendRequest = Host:execute("setTimer", 400, 10);
while zRequest.Object:loading() do
end
Host:execute("killTimer", Timer.SendRequest);
-- Error Catching Routine
if not(zRequest.Object:success()) then
WriteToLog:error("SendRequest() Failed | HTTP Status " .. zRequest.Object:httpStatus());
end
if zRequest.Object:httpStatus() ~= 200 and zRequest.Object:httpStatus() ~= 201 then
WriteToLog:info("SendRequest() Returned | HTTP Status " .. zRequest.Object:httpStatus());
end
-- Process Response
zRequest.Response = zRequest.Object:response();
WriteToLog:debug("SendRequest() Finished | " .. zRequest.Response);
WriteToLog:info("SendRequest() <<<<< FINISHED <<<<< FINISHED <<<<<");
return zRequest.Response;
end
--------------------------------------------------------------------------------------------
-- Encode Response Function
--------------------------------------------------------------------------------------------
function EncodeResponse(oRequest)
WriteToLog:info("EncodeResponse() >>>>> STARTING >>>>> STARTING >>>>>");
JSON = assert(loadfile "JSON.lua")();
local zLUATable = {};
zLUATable = JSON:encode(oRequest);
WriteToLog:debug("EncodeResponse() Finished | " .. tostring(zLUATable));
WriteToLog:info("EncodeResponse() <<<<< FINISHED <<<<< FINISHED <<<<<");
return zLUATable;
end
--------------------------------------------------------------------------------------------
-- Decode Response Function
--------------------------------------------------------------------------------------------
function DecodeResponse(oResponse)
WriteToLog:info("DecodeResponse() >>>>> STARTING >>>>> STARTING >>>>>");
JSON = assert(loadfile "JSON.lua")();
local zLUATable = {};
zLUATable = JSON:decode(oResponse);
WriteToLog:debug("DecodeResponse() Finished | " .. tostring(zLUATable));
WriteToLog:info("DecodeResponse() <<<<< FINISHED <<<<< FINISHED <<<<<");
return zLUATable;
end
--------------------------------------------------------------------------------------------
-- Parse Response Function
--------------------------------------------------------------------------------------------
function ParseResponse(oType, oResponse)
WriteToLog:info("ParseResponse() >>>>> STARTING >>>>> STARTING >>>>>");
-- http://developer.oanda.com/rest-live-v20/account-ep/
if oType == "GET_Accounts" then
local Accounts = {};
if pcall(function () Accounts.ID = tostring(oResponse["accounts"][1]["id"]); end) then
Accounts.ID = tostring(oResponse["accounts"][1]["id"]);
else Accounts.ID = tostring(nil); end
if pcall(function () Accounts.Tags = tostring(oResponse["accounts"][1]["tags"][1]); end) then
Accounts.Tags = tostring(oResponse["accounts"][1]["tags"][1]);
else Accounts.Tags = tostring(nil); end
WriteToLog:debug("ParseResponse() | Accounts.ID: " .. Accounts.ID);
WriteToLog:debug("ParseResponse() | Accounts.Tags: " .. Accounts.Tags);
return Accounts;
-- http://developer.oanda.com/rest-live-v20/account-ep/
elseif oType == "GET_Account" then
local Account = {};
-- ADD SUPPORT FOR THIS RESPONSE TYPE
-- ADD SUPPORT FOR THIS RESPONSE TYPE
-- ADD SUPPORT FOR THIS RESPONSE TYPE
return Accounts;
-- http://developer.oanda.com/rest-live-v20/instrument-ep/
elseif oType == "GET_Instruments" then
local Account = {};
Account.Instruments = {};
if pcall(function () Account.Instruments.DisplayName = tostring(oResponse["instruments"][1]["displayName"]); end) then
Account.Instruments.DisplayName = tostring(oResponse["instruments"][1]["displayName"]);
else Account.Instruments.DisplayName = tostring(nil); end
if pcall(function () Account.Instruments.DisplayPrecision = tostring(oResponse["instruments"][1]["displayPrecision"]); end) then
Account.Instruments.DisplayPrecision = tostring(oResponse["instruments"][1]["displayPrecision"]);
else Account.Instruments.DisplayPrecision = tostring(nil); end
if pcall(function () Account.Instruments.MarginRate = tostring(oResponse["instruments"][1]["marginRate"]); end) then
Account.Instruments.MarginRate = tostring(oResponse["instruments"][1]["marginRate"]);
else Account.Instruments.MarginRate = tostring(nil); end
if pcall(function () Account.Instruments.MaximumOrderUnits = tostring(oResponse["instruments"][1]["maximumOrderUnits"]); end) then
Account.Instruments.MaximumOrderUnits = tostring(oResponse["instruments"][1]["maximumOrderUnits"]);
else Account.Instruments.MaximumOrderUnits = tostring(nil); end
if pcall(function () Account.Instruments.MaximumPositionSize = tostring(oResponse["instruments"][1]["maximumPositionSize"]); end) then
Account.Instruments.MaximumPositionSize = tostring(oResponse["instruments"][1]["maximumPositionSize"]);
else Account.Instruments.MaximumPositionSize = tostring(nil); end
if pcall(function () Account.Instruments.MaximumTrailingStopDistance = tostring(oResponse["instruments"][1]["maximumTrailingStopDistance"]); end) then
Account.Instruments.MaximumTrailingStopDistance = tostring(oResponse["instruments"][1]["maximumTrailingStopDistance"]);
else Account.Instruments.MaximumTrailingStopDistance = tostring(nil); end
if pcall(function () Account.Instruments.MinimumTradeSize = tostring(oResponse["instruments"][1]["minimumTradeSize"]); end) then
Account.Instruments.MinimumTradeSize = tostring(oResponse["instruments"][1]["minimumTradeSize"]);
else Account.Instruments.MinimumTradeSize = tostring(nil); end
if pcall(function () Account.Instruments.MinimumTrailingStopDistance = tostring(oResponse["instruments"][1]["minimumTrailingStopDistance"]); end) then
Account.Instruments.MinimumTrailingStopDistance = tostring(oResponse["instruments"][1]["minimumTrailingStopDistance"]);
else Account.Instruments.MinimumTrailingStopDistance = tostring(nil); end
if pcall(function () Account.Instruments.Name = tostring(oResponse["instruments"][1]["name"]); end) then
Account.Instruments.Name = tostring(oResponse["instruments"][1]["name"]);
else Account.Instruments.Name = tostring(nil); end
if pcall(function () Account.Instruments.PipLocation = tostring(oResponse["instruments"][1]["pipLocation"]); end) then
Account.Instruments.PipLocation = tostring(oResponse["instruments"][1]["pipLocation"]);
else Account.Instruments.PipLocation = tostring(nil); end
if pcall(function () Account.Instruments.TradeUnitsPrecision = tostring(oResponse["instruments"][1]["tradeUnitsPrecision"]); end) then
Account.Instruments.TradeUnitsPrecision = tostring(oResponse["instruments"][1]["tradeUnitsPrecision"]);
else Account.Instruments.TradeUnitsPrecision = tostring(nil); end
if pcall(function () Account.Instruments.Type = tostring(oResponse["instruments"][1]["type"]); end) then
Account.Instruments.Type = tostring(oResponse["instruments"][1]["type"]);
else Account.Instruments.Type = tostring(nil); end
WriteToLog:debug("ParseResponse() | Account.Instruments.DisplayName: " .. Account.Instruments.DisplayName);
WriteToLog:debug("ParseResponse() | Account.Instruments.DisplayPrecision: " .. Account.Instruments.DisplayPrecision);
WriteToLog:debug("ParseResponse() | Account.Instruments.MarginRate: " .. Account.Instruments.MarginRate);
WriteToLog:debug("ParseResponse() | Account.Instruments.MaximumOrderUnits: " .. Account.Instruments.MaximumOrderUnits);
WriteToLog:debug("ParseResponse() | Account.Instruments.MaximumPositionSize: " .. Account.Instruments.MaximumPositionSize);
WriteToLog:debug("ParseResponse() | Account.Instruments.MaximumTrailingStopDistance: " .. Account.Instruments.MaximumTrailingStopDistance);
WriteToLog:debug("ParseResponse() | Account.Instruments.MinimumTradeSize: " .. Account.Instruments.MinimumTradeSize);
WriteToLog:debug("ParseResponse() | Account.Instruments.MinimumTrailingStopDistance: " .. Account.Instruments.MinimumTrailingStopDistance);
WriteToLog:debug("ParseResponse() | Account.Instruments.Name: " .. Account.Instruments.Name);
WriteToLog:debug("ParseResponse() | Account.Instruments.PipLocation: " .. Account.Instruments.PipLocation);
WriteToLog:debug("ParseResponse() | Account.Instruments.TradeUnitsPrecision: " .. Account.Instruments.TradeUnitsPrecision);
WriteToLog:debug("ParseResponse() | Account.Instruments.Type: " .. Account.Instruments.Type);
return Account;
-- http://developer.oanda.com/rest-live-v20/positions-ep/
elseif oType == "GET_OpenPositions" then
local Positions = {};
if pcall(function () Positions.Instrument = tostring(oResponse["positions"][1]["instrument"]); end) then
Positions.Instrument = tostring(oResponse["positions"][1]["instrument"]);
else Positions.Instrument = tostring(nil); end
if pcall(function () Positions.PL = tostring(oResponse["positions"][1]["pl"]); end) then
Positions.PL = tostring(oResponse["positions"][1]["pl"]);
else Positions.PL = tostring(nil); end
if pcall(function () Positions.ResettablePL = tostring(oResponse["positions"][1]["resettablePL"]); end) then
Positions.ResettablePL = tostring(oResponse["positions"][1]["resettablePL"]);
else Positions.ResettablePL = tostring(nil); end
if pcall(function () Positions.UnrealizedPL = tostring(oResponse["positions"][1]["unrealizedPL"]); end) then
Positions.UnrealizedPL = tostring(oResponse["positions"][1]["unrealizedPL"]);
else Positions.UnrealizedPL = tostring(nil); end
if pcall(function () Positions.LastTransactionID = tostring(oResponse["lastTransactionID"]); end) then
Positions.LastTransactionID = tostring(oResponse["lastTransactionID"]);
else Positions.LastTransactionID = tostring(nil); end
WriteToLog:debug("ParseResponse() | Positions.Instrument: " .. Positions.Instrument);
WriteToLog:debug("ParseResponse() | Positions.PL: " .. Positions.PL);
WriteToLog:debug("ParseResponse() | Positions.ResettablePL: " .. Positions.ResettablePL);
WriteToLog:debug("ParseResponse() | Positions.UnrealizedPL: " .. Positions.UnrealizedPL);
WriteToLog:debug("ParseResponse() | Positions.LastTransactionID: " .. Positions.LastTransactionID);
Positions.Long = {};
if pcall(function () Positions.Long.TradeIDs = tostring(oResponse["positions"][1]["long"]["tradeIDs"][1]); end) then
Positions.Long.TradeIDs = tostring(oResponse["positions"][1]["long"]["tradeIDs"][1]);
else Positions.Long.TradeIDs = tostring(nil); end
if pcall(function () Positions.Long.AveragePrice = tostring(oResponse["positions"][1]["long"]["averagePrice"]); end) then
Positions.Long.AveragePrice = tostring(oResponse["positions"][1]["long"]["averagePrice"]);
else Positions.Long.AveragePrice = tostring(nil); end
if pcall(function () Positions.Long.UnrealizedPL = tostring(oResponse["positions"][1]["long"]["unrealizedPL"]); end) then
Positions.Long.UnrealizedPL = tostring(oResponse["positions"][1]["long"]["unrealizedPL"]);
else Positions.Long.UnrealizedPL = tostring(nil); end
if pcall(function () Positions.Long.PL = tostring(oResponse["positions"][1]["long"]["pl"]); end) then
Positions.Long.PL = tostring(oResponse["positions"][1]["long"]["pl"]);
else Positions.Long.PL = tostring(nil); end
if pcall(function () Positions.Long.ResettablePL = tostring(oResponse["positions"][1]["long"]["resettablePL"]); end) then
Positions.Long.ResettablePL = tostring(oResponse["positions"][1]["long"]["resettablePL"]);
else Positions.Long.ResettablePL = tostring(nil); end
if pcall(function () Positions.Long.Units = tostring(oResponse["positions"][1]["long"]["units"]); end) then
Positions.Long.Units = tostring(oResponse["positions"][1]["long"]["units"]);
else Positions.Long.Units = tostring(nil); end
WriteToLog:debug("ParseResponse() | Positions.Long.TradeIDs: " .. Positions.Long.TradeIDs);
WriteToLog:debug("ParseResponse() | Positions.Long.AveragePrice: " .. Positions.Long.AveragePrice);
WriteToLog:debug("ParseResponse() | Positions.Long.UnrealizedPL: " .. Positions.Long.UnrealizedPL);
WriteToLog:debug("ParseResponse() | Positions.Long.PL: " .. Positions.Long.PL);
WriteToLog:debug("ParseResponse() | Positions.Long.ResettablePL: " .. Positions.Long.ResettablePL);
WriteToLog:debug("ParseResponse() | Positions.Long.Units: " .. Positions.Long.Units);
Positions.Short = {};
if pcall(function () Positions.Short.TradeIDs = tostring(oResponse["positions"][1]["short"]["tradeIDs"][1]); end) then
Positions.Short.TradeIDs = tostring(oResponse["positions"][1]["short"]["tradeIDs"][1]);
else Positions.Short.TradeIDs = tostring(nil); end
if pcall(function () Positions.Short.AveragePrice = tostring(oResponse["positions"][1]["short"]["averagePrice"]); end) then
Positions.Short.AveragePrice = tostring(oResponse["positions"][1]["short"]["averagePrice"]);
else Positions.Short.AveragePrice = tostring(nil); end
if pcall(function () Positions.Short.UnrealizedPL = tostring(oResponse["positions"][1]["short"]["unrealizedPL"]); end) then
Positions.Short.UnrealizedPL = tostring(oResponse["positions"][1]["short"]["unrealizedPL"]);
else Positions.Short.UnrealizedPL = tostring(nil); end
if pcall(function () Positions.Short.PL = tostring(oResponse["positions"][1]["short"]["pl"]); end) then
Positions.Short.PL = tostring(oResponse["positions"][1]["short"]["pl"]);
else Positions.Short.PL = tostring(nil); end
if pcall(function () Positions.Short.ResettablePL = tostring(oResponse["positions"][1]["short"]["resettablePL"]); end) then
Positions.Short.ResettablePL = tostring(oResponse["positions"][1]["short"]["resettablePL"]);
else Positions.Short.ResettablePL = tostring(nil); end
if pcall(function () Positions.Short.Units = tostring(oResponse["positions"][1]["short"]["units"]); end) then
Positions.Short.Units = tostring(oResponse["positions"][1]["short"]["units"]);
else Positions.Short.Units = tostring(nil); end
WriteToLog:debug("ParseResponse() | Positions.Short.TradeIDs: " .. Positions.Short.TradeIDs);
WriteToLog:debug("ParseResponse() | Positions.Short.AveragePrice: " .. Positions.Short.AveragePrice);
WriteToLog:debug("ParseResponse() | Positions.Short.UnrealizedPL: " .. Positions.Short.UnrealizedPL);
WriteToLog:debug("ParseResponse() | Positions.Short.PL: " .. Positions.Short.PL);
WriteToLog:debug("ParseResponse() | Positions.Short.ResettablePL: " .. Positions.Short.ResettablePL);
WriteToLog:debug("ParseResponse() | Positions.Short.Units: " .. Positions.Short.Units);
return Positions;
-- http://developer.oanda.com/rest-live-v20/orders-ep/
elseif oType == "POST_Orders" then
local Orders = {};
-- ADD SUPPORT FOR THIS RESPONSE TYPE
-- ADD SUPPORT FOR THIS RESPONSE TYPE
-- ADD SUPPORT FOR THIS RESPONSE TYPE
return Orders;
-- http://developer.oanda.com/rest-live-v20/pricing-ep/
elseif oType == "GET_Pricing" then
local Prices = {};
if pcall(function () Prices.Type = tostring(oResponse["prices"][1]["type"]); end) then
Prices.Type = tostring(oResponse["prices"][1]["type"]);
else Prices.Type = tostring(nil); end
if pcall(function () Prices.Time = tostring(oResponse["prices"][1]["time"]); end) then
Prices.Time = tostring(oResponse["prices"][1]["time"]);
else Prices.Time = tostring(nil); end
if pcall(function () Prices.CloseoutBid = tostring(oResponse["prices"][1]["closeoutBid"]); end) then
Prices.CloseoutBid = tostring(oResponse["prices"][1]["closeoutBid"]);
else Prices.CloseoutBid = tostring(nil); end
if pcall(function () Prices.CloseoutAsk = tostring(oResponse["prices"][1]["closeoutAsk"]); end) then
Prices.CloseoutAsk = tostring(oResponse["prices"][1]["closeoutAsk"]);
else Prices.CloseoutAsk = tostring(nil); end
if pcall(function () Prices.Status = tostring(oResponse["prices"][1]["status"]); end) then
Prices.Status = tostring(oResponse["prices"][1]["status"]);
else Prices.Status = tostring(nil); end
if pcall(function () Prices.Tradeable = tostring(oResponse["prices"][1]["tradeable"]); end) then
Prices.Tradeable = tostring(oResponse["prices"][1]["tradeable"]);
else Prices.Tradeable = tostring(nil); end
if pcall(function () Prices.Instrument = tostring(oResponse["prices"][1]["instrument"]); end) then
Prices.Instrument = tostring(oResponse["prices"][1]["instrument"]);
else Prices.Instrument = tostring(nil); end
WriteToLog:debug("ParseResponse() | Prices.Type: " .. Prices.Type);
WriteToLog:debug("ParseResponse() | Prices.Time: " .. Prices.Time);
WriteToLog:debug("ParseResponse() | Prices.CloseoutBid: " .. Prices.CloseoutBid);
WriteToLog:debug("ParseResponse() | Prices.CloseoutAsk: " .. Prices.CloseoutAsk);
WriteToLog:debug("ParseResponse() | Prices.Status: " .. Prices.Status);
WriteToLog:debug("ParseResponse() | Prices.Tradeable: " .. Prices.Tradeable);
WriteToLog:debug("ParseResponse() | Prices.Instrument: " .. Prices.Instrument);
Prices.UnitsAvailable = {};
Prices.UnitsAvailable.Default = {};
if pcall(function () Prices.UnitsAvailable.Default.Long = tostring(oResponse["prices"][1]["unitsAvailable"]["default"]["long"]); end) then
Prices.UnitsAvailable.Default.Long = tostring(oResponse["prices"][1]["unitsAvailable"]["default"]["long"]);
else Prices.UnitsAvailable.Default.Long = tostring(nil); end
if pcall(function () Prices.UnitsAvailable.Default.Short = tostring(oResponse["prices"][1]["unitsAvailable"]["default"]["short"]); end) then
Prices.UnitsAvailable.Default.Short = tostring(oResponse["prices"][1]["unitsAvailable"]["default"]["short"]);
else Prices.UnitsAvailable.Default.Short = tostring(nil); end
WriteToLog:debug("ParseResponse() | Prices.UnitsAvailable.Default.Long: " .. Prices.UnitsAvailable.Default.Long);
WriteToLog:debug("ParseResponse() | Prices.UnitsAvailable.Default.Short: " .. Prices.UnitsAvailable.Default.Short);
Prices.UnitsAvailable.OpenOnly = {};
if pcall(function () Prices.UnitsAvailable.OpenOnly.Long = tostring(oResponse["prices"][1]["unitsAvailable"]["openOnly"]["long"]); end) then
Prices.UnitsAvailable.OpenOnly.Long = tostring(oResponse["prices"][1]["unitsAvailable"]["openOnly"]["long"]);
else Prices.UnitsAvailable.OpenOnly.Long = tostring(nil); end
if pcall(function () Prices.UnitsAvailable.OpenOnly.Short = tostring(oResponse["prices"][1]["unitsAvailable"]["openOnly"]["short"]); end) then
Prices.UnitsAvailable.OpenOnly.Short = tostring(oResponse["prices"][1]["unitsAvailable"]["openOnly"]["short"]);
else Prices.UnitsAvailable.OpenOnly.Short = tostring(nil); end
WriteToLog:debug("ParseResponse() | Prices.UnitsAvailable.OpenOnly.Long: " .. Prices.UnitsAvailable.OpenOnly.Long);
WriteToLog:debug("ParseResponse() | Prices.UnitsAvailable.OpenOnly.Short: " .. Prices.UnitsAvailable.OpenOnly.Short);
Prices.UnitsAvailable.ReduceFirst = {};
if pcall(function () Prices.UnitsAvailable.ReduceFirst.Long = tostring(oResponse["prices"][1]["unitsAvailable"]["reduceFirst"]["long"]); end) then
Prices.UnitsAvailable.ReduceFirst.Long = tostring(oResponse["prices"][1]["unitsAvailable"]["reduceFirst"]["long"]);
else Prices.UnitsAvailable.ReduceFirst.Long = tostring(nil); end
if pcall(function () Prices.UnitsAvailable.ReduceFirst.Short = tostring(oResponse["prices"][1]["unitsAvailable"]["reduceFirst"]["short"]); end) then
Prices.UnitsAvailable.ReduceFirst.Short = tostring(oResponse["prices"][1]["unitsAvailable"]["reduceFirst"]["short"]);
else Prices.UnitsAvailable.ReduceFirst.Short = tostring(nil); end
WriteToLog:debug("ParseResponse() | Prices.UnitsAvailable.ReduceFirst.Long: " .. Prices.UnitsAvailable.ReduceFirst.Long);
WriteToLog:debug("ParseResponse() | Prices.UnitsAvailable.ReduceFirst.Short: " .. Prices.UnitsAvailable.ReduceFirst.Short);
Prices.UnitsAvailable.ReduceOnly = {};
if pcall(function () Prices.UnitsAvailable.ReduceOnly.Long = tostring(oResponse["prices"][1]["unitsAvailable"]["reduceOnly"]["long"]); end) then
Prices.UnitsAvailable.ReduceOnly.Long = tostring(oResponse["prices"][1]["unitsAvailable"]["reduceOnly"]["long"]);
else Prices.UnitsAvailable.ReduceOnly.Long = tostring(nil); end
if pcall(function () Prices.UnitsAvailable.ReduceOnly.Short = tostring(oResponse["prices"][1]["unitsAvailable"]["reduceOnly"]["short"]); end) then
Prices.UnitsAvailable.ReduceOnly.Short = tostring(oResponse["prices"][1]["unitsAvailable"]["reduceOnly"]["short"]);
else Prices.UnitsAvailable.ReduceOnly.Short = tostring(nil); end
WriteToLog:debug("ParseResponse() | Prices.UnitsAvailable.ReduceOnly.Long: " .. Prices.UnitsAvailable.ReduceOnly.Long);
WriteToLog:debug("ParseResponse() | Prices.UnitsAvailable.ReduceOnly.Short: " .. Prices.UnitsAvailable.ReduceOnly.Short);
Prices.QuoteHomeConversionFactors = {}
if pcall(function () Prices.QuoteHomeConversionFactors.PositiveUnits = tostring(oResponse["prices"][1]["quoteHomeConversionFactors"]["positiveUnits"]); end) then
Prices.QuoteHomeConversionFactors.PositiveUnits = tostring(oResponse["prices"][1]["quoteHomeConversionFactors"]["positiveUnits"]);
else Prices.QuoteHomeConversionFactors.PositiveUnits = tostring(nil); end
if pcall(function () Prices.QuoteHomeConversionFactors.NegativeUnits = tostring(oResponse["prices"][1]["quoteHomeConversionFactors"]["negativeUnits"]); end) then
Prices.QuoteHomeConversionFactors.NegativeUnits = tostring(oResponse["prices"][1]["quoteHomeConversionFactors"]["negativeUnits"]);
else Prices.QuoteHomeConversionFactors.NegativeUnits = tostring(nil); end
WriteToLog:debug("ParseResponse() | Prices.QuoteHomeConversionFactors.PositiveUnits: " .. Prices.QuoteHomeConversionFactors.PositiveUnits);
WriteToLog:debug("ParseResponse() | Prices.QuoteHomeConversionFactors.NegativeUnits: " .. Prices.QuoteHomeConversionFactors.NegativeUnits);
Prices.Bids = {};
Prices.Bids.Best = {};
if pcall(function () Prices.Bids.Best.Price = tostring(oResponse["prices"][1]["bids"][1]["price"]); end) then
Prices.Bids.Best.Price = tostring(oResponse["prices"][1]["bids"][1]["price"]);
else Prices.Bids.Best.Price = tostring(nil); end
if pcall(function () Prices.Bids.Best.Liquidity = tostring(oResponse["prices"][1]["bids"][1]["liquidity"]); end) then
Prices.Bids.Best.Liquidity = tostring(oResponse["prices"][1]["bids"][1]["liquidity"]);
else Prices.Bids.Best.Liquidity = tostring(nil); end
WriteToLog:debug("ParseResponse() | Prices.Bids.Best.Price: " .. Prices.Bids.Best.Price);
WriteToLog:debug("ParseResponse() | Prices.Bids.Best.Liquidity: " .. Prices.Bids.Best.Liquidity);
Prices.Asks = {};
Prices.Asks.Best = {};
if pcall(function () Prices.Asks.Best.Price = tostring(oResponse["prices"][1]["asks"][1]["price"]); end) then
Prices.Asks.Best.Price = tostring(oResponse["prices"][1]["asks"][1]["price"]);
else Prices.Asks.Best.Price = tostring(nil); end
if pcall(function () Prices.Asks.Best.Liquidity = tostring(oResponse["prices"][1]["asks"][1]["liquidity"]); end) then
Prices.Asks.Best.Liquidity = tostring(oResponse["prices"][1]["asks"][1]["liquidity"]);
else Prices.Asks.Best.Liquidity = tostring(nil); end
WriteToLog:debug("ParseResponse() | Prices.Asks.Best.Price: " .. Prices.Asks.Best.Price);
WriteToLog:debug("ParseResponse() | Prices.Asks.Best.Liquidity: " .. Prices.Asks.Best.Liquidity);
return Prices;
else
WriteToLog:error("ParseResponse() Failed | In Else Clause");
end
WriteToLog:debug("ParseResponse() Finished | Table Returned");
WriteToLog:info("ParseResponse() <<<<< FINISHED <<<<< FINISHED <<<<<");
end
--------------------------------------------------------------------------------------------
-- Formatting Functions
--------------------------------------------------------------------------------------------
function SendNotification(oType, oSymbol, oOpen, oMessage, oTime)
WriteToLog:info("SendNotification() >>>>> STARTING >>>>> STARTING >>>>>");
local zTimeInTable = {};
local zDate = nil;
local zTime = nil;
zTimeInTable = core.dateToTable(core.now());
zDate = string.format("%02i/%02i/%02i", zTimeInTable.month, zTimeInTable.day, zTimeInTable.year);
zTime = string.format("%02i:%02i", zTimeInTable.hour, zTimeInTable.min);
if oType == "Terminal" then
terminal:alertMessage(oSymbol, oOpen, oMessage, oTime)
elseif oType == "Trace" then
Host:trace(oMessage);
elseif oType == "StartEmail" then
if FXCM.SendEmailOrderExecution then
terminal:alertEmail(FXCM.EmailAddress, "Strategy Successfully Started on " .. zDate .. " at " .. zTime, oMessage);
end
elseif oType == "OrderExecution" then
if FXCM.SendEmailOrderExecution then
local zMessageEnd = zDate .. " at " .. zTime;
terminal:alertEmail(FXCM.EmailAddress, oMessage .. zMessageEnd, oMessage .. zMessageEnd);
end
elseif oType == "All" then
terminal:alertMessage(oSymbol, oOpen, oMessage, oTime)
Host:trace(oMessage);
if FXCM.SendEmailStrategyFailure then
terminal:alertEmail(FXCM.EmailAddress, "Review ".. oSymbol .. "; Issue on " .. zDate .. " at " .. zTime, oMessage);
end
else
WriteToLog:error("SendNotification() Failed | In Else Clause");
end
WriteToLog:info("SendNotification() >>>>> STARTING >>>>> STARTING >>>>>");
end
function FlipDirection(oDirection)
WriteToLog:info("FlipDirection() >>>>> STARTING >>>>> STARTING >>>>>");
local zDirection = nil;
if oDirection == "B" then
zDirection = "S";
elseif oDirection == "S" then
zDirection = "B";
else
WriteToLog:error("FlipDirection() Failed | In Else Clause");
end
WriteToLog:debug("FlipDirection() Finished with " .. zDirection);
WriteToLog:info("FlipDirection() <<<<< FINISHED <<<<< FINISHED <<<<<");
return zDirection;
end
function FormatDirection(oDirection, oSize)
WriteToLog:info("FormatDirection() >>>>> STARTING >>>>> STARTING >>>>>");
local zSize = nil;
if oDirection == "B" then
zSize = oSize;
elseif oDirection == "S" then
zSize = oSize * -1;
else
WriteToLog:error("FormatDirection() Failed | In Else Clause");
end
WriteToLog:debug("FormatDirection() Finished | " .. zSize);