-
Notifications
You must be signed in to change notification settings - Fork 1
/
Investment-Dashboard.lua
776 lines (648 loc) · 34.5 KB
/
Investment-Dashboard.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
---------------------------------------- Overview ------------------------------------------
-- Name: Investment Dashboard
-- Notes: Copyright (c) 2017 Jeremy Gulickson
-- Version: 1.6.08312017
-- Format: major.minor.mmddyyyy
--
-- Description: Proof of concept to calculate and aggregate select values from
-- user specified FXCM, Oanda and/or Robinhood account(s). Currently
-- includes equity, day p/l and leverage though modifying or adding
-- additional data points is trivial. Oanda and Robinhood data is sourced
-- via RESTful API queries.
--
-- Platform Requirements: FXTS (FXCM Trading Station) -> http://download.fxcorporate.com/FXCM/FXTS2Install.EXE
-- http_lua.dll -> Included by default in FXTS
-- JSON.lua -> http://regex.info/blog/lua/json
--
-- Login Requirements: FXCM -> Username + password
-- // Note this is entered in FXTS not in indicator options
-- Oanda -> Token
-- // Generated and retrieved from Oanda's online account portal
-- Robinhood -> Username + password + 2FA code (optional)
-- // Token appears to only be valid for a session only and thus
-- // the indicator will grab token at initialization
--
-- 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/
-- Robinhood Documentation: https://github.com/sanko/Robinhood
--
-- Know Limitations: -> Does not support 2FA for Oanda accounts
-- -> 2FA support for Robhinhood accounts is clunky
-- -> Oanda account must be type v20
-- -> Robinhood API is undocumented and therefore unannounced changes
-- may break funtionality
-- -> Minimal error handling for incorrect username or password values
-- -> Minimal error handling for api endpoint conductivity
--
-------------------------------------- Version History -------------------------------------
-- v1.0.03272017: Feature Release
-- -> Initial release; proof of concept
--
-- v1.1.04052017: Cosmetic Release
-- -> Added color formatting for values
-- -> Update x & y coordinates calculation
--
-- v1.2.04102017: Cosmetic Release
-- -> Added side variable to control presentation
--
-- v1.3.04202017: Cosmetic Release
-- -> Made Github ready
-- -> Removed email functionality
--
-- v1.4.08032017: Feature Release
-- -> Updated Robinhood data to include extended hours values
-- -> Added Robinhood support for 2 factor authentication
--
-- v1.5.08072017 Bug Fix Release
-- -> Improved Robinhood 2 factor authentication experience
-- -> Addressed issues with Robinhood extended hours values not populating from server
--
-- v1.6.08312017 Bug Fix Release
-- -> Addressed issues with Robinhood leverage values
--
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
-- Global Variable Setup
--------------------------------------------------------------------------------------------
-- Routine Notes: Global variables are initialized as arrays *solely* for
-- readability purposes.
--
--------------------------------------------------------------------------------------------
-- Require LUA extension to support HTTPS protocol
require("http_lua");
-- Global Oanda variables
local Oanda = {};
Oanda.Demo_URL = "api-fxpractice.oanda.com";
Oanda.Real_URL = "api-fxtrade.oanda.com";
Oanda.URL = nil;
Oanda.Account_ID = nil;
Oanda.Account_Type = nil;
Oanda.API_Token = nil;
-- Global Robinhood variables
local Robinhood = {};
Robinhood.TFA_Mode = nil;
Robinhood.TFA_Type = nil;
Robinhood.TFA_Code = nil;
Robinhood.URL = "api.robinhood.com"
Robinhood.Portfolio_URL = nil;
Robinhood.Account_ID = nil;
Robinhood.Password = nil;
Robinhood.API_Token = nil;
-- Global FXCM variables
local FXCM = {};
FXCM.Account_ID = nil;
-- Global timer variables
local Timer = {};
Timer.Initialized = nil;
Timer.Send_Request = nil;
Timer.Update_Frequency = nil;
-- Global font variables
local Font = {};
Font.Equity = nil;
Font.Floating_PL = nil;
Font.Day_PL = nil;
Font.Leverage = nil;
-- Global format variables
local Format = {};
Format.Equity = nil;
Format.Leverage = nil;
Format.Neutral = nil;
Format.Positive = nil;
Format.Negative = nil;
Format.Side = nil;
Format.Digits = 0;
--------------------------------------------------------------------------------------------
-- Initialize Function
--------------------------------------------------------------------------------------------
function Init()
indicator:name("Investment Dashboard");
indicator:description("Investment Dashboard")
indicator:type(core.Indicator);
indicator.parameters:addGroup("General");
indicator.parameters:addInteger("Update_Frequency", "Update Frequency", "Enter time between updates in minutes.", 10, 1, 60);
indicator.parameters:addGroup("Format");
indicator.parameters:addColor("Format_Equity", "Equity Value", "Select the color of equity vales.", core.rgb(255, 255, 255));
indicator.parameters:addColor("Format_Leverage", "Leverage Value", "Select the color of leverage vales.", core.rgb(255, 255, 255));
indicator.parameters:addColor("Format_Neutral", "Neutral Values", "Select the color of neutral values.", core.rgb(192, 192, 192));
indicator.parameters:addColor("Format_Positive", "Positive Values", "Select the color of positive values.", core.rgb(128, 255, 0));
indicator.parameters:addColor("Format_Negative", "Negative Values", "Select the color of negative values.", core.rgb(255, 53, 53));
indicator.parameters:addString("Format_Side", "Side", "Select which side to display values.", "Right")
indicator.parameters:addStringAlternative("Format_Side", "Right", "", "Right");
indicator.parameters:addStringAlternative("Format_Side", "Left", "", "Left");
indicator.parameters:addGroup("Robinhood");
indicator.parameters:addString("Robinhood_Account_ID", "Account ID", "Enter the account id to monitor.", "");
indicator.parameters:addString("Robinhood_Password", "Password", "Enter the password.", "");
indicator.parameters:addBoolean("Robinhood_TFA_Mode", "2FA Mode", "Enable two factor authentication mode.", true);
indicator.parameters:addString("Robinhood_TFA_Code", "2FA Code", "Enter the two factor authentication code.", "");
indicator.parameters:addGroup("Oanda");
indicator.parameters:addString("Oanda_Account_ID", "Account ID", "Enter the account id to monitor.", "");
indicator.parameters:addString("Oanda_Account_Type", "Account Type", "Select the account type.", "Real")
indicator.parameters:addStringAlternative("Oanda_Account_Type", "Demo", "", "Demo");
indicator.parameters:addStringAlternative("Oanda_Account_Type", "Real", "", "Real");
indicator.parameters:addString("Oanda_API_Token", "API Token", "Enter the API token.","");
indicator.parameters:addGroup("FXCM");
indicator.parameters:addString("FXCM_Account_ID", "Account ID", "Select the account id to monitor.", "");
indicator.parameters:setFlag("FXCM_Account_ID", core.FLAG_ACCOUNT);
end
--------------------------------------------------------------------------------------------
-- Prepare Function
--------------------------------------------------------------------------------------------
function Prepare()
instance:name("Investment Dashboard");
Timer.Initialized = core.host:execute("setTimer", 100, 10);
Timer.Update_Frequency = core.host:execute("setTimer", 200, instance.parameters.Update_Frequency * 60);
Format.Equity = instance.parameters.Format_Equity;
Format.Leverage = instance.parameters.Format_Leverage;
Format.Neutral = instance.parameters.Format_Neutral;
Format.Positive = instance.parameters.Format_Positive;
Format.Negative = instance.parameters.Format_Negative;
Format.Side = instance.parameters.Format_Side;
Robinhood.Account_ID = instance.parameters.Robinhood_Account_ID;
Robinhood.Password = instance.parameters.Robinhood_Password;
Robinhood.TFA_Mode = instance.parameters.Robinhood_TFA_Mode;
if Robinhood.TFA_Mode then Get_Robinhood_API_Token_TFA_Step_1(); end
Robinhood.TFA_Code = instance.parameters.Robinhood_TFA_Code;
Oanda.Account_ID = instance.parameters.Oanda_Account_ID;
if instance.parameters.Oanda_Account_Type == "Real" then
Oanda.URL = Oanda.Real_URL;
elseif instance.parameters.Oanda_Account_Type == "Demo" then
Oanda.URL = Oanda.Demo_URL;
end
Oanda.API_Token = instance.parameters.Oanda_API_Token;
FXCM.Account_ID = instance.parameters.FXCM_Account_ID;
Font.Equity = core.host:execute("createFont", "Verdana", 70, false, true);
Font.Floating_PL = core.host:execute("createFont", "Verdana", 30, false, false);
Font.Day_PL = core.host:execute("createFont", "Verdana", 30, false, false);
Font.Leverage = core.host:execute("createFont", "Verdana", 30, false, false);
end
--------------------------------------------------------------------------------------------
-- Get Robinhood API Token
--------------------------------------------------------------------------------------------
function Get_Robinhood_API_Token()
local zURL = Create_Robinhood_URL_Syntax("Authentication", "POST_Login", Robinhood.URL, nil, nil, nil, nil);
local zAuthentication = {};
zAuthentication.username = Robinhood.Account_ID;
zAuthentication.password = Robinhood.Password;
local zResponse = Send_Request("Robinhood", "POST", zURL, nil, nil , nil, Encode_Request(zAuthentication));
Robinhood.API_Token = Parse_Robinhood_Response("Authentication", "POST_Login", Decode_Response(zResponse)).Token;
end
--------------------------------------------------------------------------------------------
-- Get Robinhood API Token Two Factor Authentication Step 1
--------------------------------------------------------------------------------------------
function Get_Robinhood_API_Token_TFA_Step_1()
local zURL = Create_Robinhood_URL_Syntax("Authentication", "POST_Login", Robinhood.URL, nil, nil, nil, nil);
local zAuthentication = {};
zAuthentication.username = Robinhood.Account_ID;
zAuthentication.password = Robinhood.Password;
local zResponse = Send_Request("Robinhood", "POST", zURL, nil, nil , nil, Encode_Request(zAuthentication));
Robinhood.TFA_Type = Parse_Robinhood_Response("Authentication", "POST_Login", Decode_Response(zResponse)).TFA_Type;
end
--------------------------------------------------------------------------------------------
-- Get Robinhood API Token Two Factor Authentication Step 2
--------------------------------------------------------------------------------------------
function Get_Robinhood_API_Token_TFA_Step_2()
if Robinhood.TFA_Type == "sms" then
local zURL = Create_Robinhood_URL_Syntax("Authentication", "POST_Login", Robinhood.URL, nil, nil, nil, nil);
local zAuthentication = {};
zAuthentication.username = Robinhood.Account_ID;
zAuthentication.password = Robinhood.Password;
zAuthentication.mfa_code = Robinhood.TFA_Code;
local zResponse = Send_Request("Robinhood", "POST", zURL, nil, nil , nil, Encode_Request(zAuthentication));
Robinhood.API_Token = Parse_Robinhood_Response("Authentication", "POST_Login", Decode_Response(zResponse)).Token;
else
Send_Notification("Alert", "", 0, "Get_Robinhood_API_Token_TFA_Step_2() Failed | In Else Clause", core.now(), "");
end
end
--------------------------------------------------------------------------------------------
-- Get Robinhood Portfolio URL
--------------------------------------------------------------------------------------------
function Get_Robinhood_Portfolio_URL()
local zURL = Create_Robinhood_URL_Syntax("Accounts", "GET_Accounts", Robinhood.URL, nil, nil, nil, nil);
local zResponse = Send_Request("Robinhood", "GET", zURL, nil, nil , Robinhood.API_Token, nil);
Robinhood.Portfolio_URL = Parse_Robinhood_Response("Accounts", "GET_Accounts", Decode_Response(zResponse)).Portfolio_URL;
end
--------------------------------------------------------------------------------------------
-- Update Function
--------------------------------------------------------------------------------------------
function Update()
-- Not employed
end
--------------------------------------------------------------------------------------------
-- Update Values Function
--------------------------------------------------------------------------------------------
function Update_Display(aType, aEquity, aDay_PL, aFloating_PL, aLeverage)
if Format.Side == "Left" then
Update_Display_Format_Left(aType, aEquity, aDay_PL, aFloating_PL, aLeverage)
elseif Format.Side == "Right" then
Update_Display_Format_Right(aType, aEquity, aDay_PL, aFloating_PL, aLeverage)
end
end
--------------------------------------------------------------------------------------------
-- Update Values Function - Left Alignment
--------------------------------------------------------------------------------------------
-- Routine Notes: This code is inefficient but works! Variable "Format.Digits"
-- *may* be incorrect on first run but subsequent attempts will
-- be accurate; impact is temporarily misaligned equity values.
--
--------------------------------------------------------------------------------------------
function Update_Display_Format_Left(aType, aEquity, aDay_PL, aFloating_PL, aLeverage)
local Display = {};
Display.yRobinhood_Equity_Offset = 46;
Display.yRobinhood_Day_PL_Offset = Display.yRobinhood_Equity_Offset - 19;
Display.yRobinhood_Floating_PL_Offset = Display.yRobinhood_Equity_Offset - 19;
Display.yRobinhood_Leverage_Offset = Display.yRobinhood_Equity_Offset + 21;
Display.yOanda_Equity_Offset = Display.yRobinhood_Equity_Offset * 3;
Display.yOanda_Day_PL_Offset = Display.yOanda_Equity_Offset - 19;
Display.yOanda_Floating_PL_Offset = Display.yOanda_Equity_Offset - 19;
Display.yOanda_Leverage_Offset = Display.yOanda_Equity_Offset + 21;
Display.xEquity_Offset = 5;
Display.xDay_PL_Offset = nil;
Display.xLeverage_Offset = nil;
Format.Day_PL = Format_Color(aDay_PL);
Format.Floating_PL = Format_Color(aFloating_PL);
if aEquity ~= nil then
aEquity = Format_Financial(aEquity, 0);
else aEquity = "0"; end
if aFloating_PL ~= nil then
aFloating_PL = Format_Financial(aFloating_PL, 0);
else aFloating_PL = "0"; end
if aDay_PL ~= nil then
aDay_PL = Format_Financial(aDay_PL, 0);
else aDay_PL = "0"; end
if aLeverage ~= nil then
aLeverage = Format_Leverage(aLeverage, 2);
else aLeverage = "0"; end
if string.len(aEquity) == 6 then
Display.Equity_Digits = string.len(aEquity) + 1;
Display.xEquity_Offset = 65;
else
Display.Equity_Digits = string.len(aEquity);
end
Display.xDay_PL_Offset = 58 * Display.Equity_Digits;
Display.xFloating_PL_Offset = Display.xDay_PL_Offset;
Display.xLeverage_Offset = Display.xDay_PL_Offset;
if aType == "Robinhood" then
core.host:execute("drawLabel1", 1, Display.xEquity_Offset, core.CR_LEFT, Display.yRobinhood_Equity_Offset, core.CR_TOP, core.H_Right, core.V_Center, Font.Equity, Format.Equity, aEquity);
core.host:execute("drawLabel1", 2, Display.xDay_PL_Offset, core.CR_LEFT, Display.yRobinhood_Day_PL_Offset, core.CR_TOP, core.H_Right, core.V_Center, Font.Day_PL, Format.Day_PL, aDay_PL);
core.host:execute("drawLabel1", 3, Display.xLeverage_Offset, core.CR_LEFT, Display.yRobinhood_Leverage_Offset, core.CR_TOP, core.H_Right, core.V_Center, Font.Leverage, Format.Leverage, aLeverage);
elseif aType == "Oanda" then
core.host:execute("drawLabel1", 4, Display.xEquity_Offset, core.CR_LEFT, Display.yOanda_Equity_Offset, core.CR_TOP, core.H_Right, core.V_Center, Font.Equity, Format.Equity, aEquity);
core.host:execute("drawLabel1", 5, Display.xLeverage_Offset, core.CR_LEFT, Display.yOanda_Leverage_Offset, core.CR_TOP, core.H_Right, core.V_Center, Font.Leverage, Format.Leverage, aLeverage);
elseif aType == "FXCM" then
core.host:execute("drawLabel1", 6, Display.xFloating_PL_Offset, core.CR_LEFT, Display.yOanda_Day_PL_Offset, core.CR_TOP, core.H_Right, core.V_Center, Font.Floating_PL, Format.Day_PL, aDay_PL);
end
end
--------------------------------------------------------------------------------------------
-- Update Values Function - Right Alignment
--------------------------------------------------------------------------------------------
-- Routine Notes: This code is inefficient but works! Variable "Format.Digits"
-- *may* be incorrect on first run but subsequent attempts will
-- be accurate; impact is temporarily misaligned equity values.
--
--------------------------------------------------------------------------------------------
function Update_Display_Format_Right(aType, aEquity, aDay_PL, aFloating_PL, aLeverage)
Format.Day_PL = Format_Color(aDay_PL);
Format.Floating_PL = Format_Color(aFloating_PL);
if aFloating_PL ~= nil then
aFloating_PL = Format_Financial(aFloating_PL, 0);
else aFloating_PL = "0"; end
if aDay_PL ~= nil then
aDay_PL = Format_Financial(aDay_PL, 0);
else aDay_PL = "0"; end
if aLeverage ~= nil then
aLeverage = Format_Leverage(aLeverage, 2);
else aLeverage = "0"; end
if aEquity ~= nil then
aEquity = Format_Financial(aEquity, 0);
else aEquity = "0"; end
local Display = {};
Display.Day_PL_Digits = string.len(aFloating_PL);
Display.Floating_PL_Digits = string.len(aDay_PL);
Display.Leverage_Digits = string.len(aLeverage);
if Display.Leverage_Digits > Display.Day_PL_Digits then
if Display.Leverage_Digits > Display.Floating_PL_Digits then
Display.Digits = Display.Leverage_Digits;
else
Display.Digits = Display.Floating_PL_Digits;
end
elseif Display.Day_PL_Digits > Display.Floating_PL_Digits then
Display.Digits = Display.Day_PL_Digits;
else
Display.Digits = Display.Floating_PL_Digits;
end
if Display.Digits > Format.Digits then Format.Digits = Display.Digits end;
Display.xDay_PL_Offset = -5;
Display.xFloating_PL_Offset = Display.xDay_PL_Offset;
Display.xLeverage_Offset = Display.xDay_PL_Offset;
Display.xEquity_Offset = -24 * Format.Digits;
Display.yRobinhood_Equity_Offset = 46;
Display.yRobinhood_Day_PL_Offset = Display.yRobinhood_Equity_Offset - 19;
Display.yRobinhood_Floating_PL_Offset = Display.yRobinhood_Equity_Offset - 19;
Display.yRobinhood_Leverage_Offset = Display.yRobinhood_Equity_Offset + 21;
Display.yOanda_Equity_Offset = Display.yRobinhood_Equity_Offset * 3;
Display.yOanda_Day_PL_Offset = Display.yOanda_Equity_Offset - 19;
Display.yOanda_Floating_PL_Offset = Display.yOanda_Equity_Offset - 19;
Display.yOanda_Leverage_Offset = Display.yOanda_Equity_Offset + 21;
if aType == "Robinhood" then
core.host:execute("drawLabel1", 1, Display.xEquity_Offset, core.CR_RIGHT, Display.yRobinhood_Equity_Offset, core.CR_TOP, core.H_Left, core.V_Center, Font.Equity, Format.Equity, aEquity);
core.host:execute("drawLabel1", 2, Display.xDay_PL_Offset, core.CR_RIGHT, Display.yRobinhood_Day_PL_Offset, core.CR_TOP, core.H_Left, core.V_Center, Font.Day_PL, Format.Day_PL, aDay_PL);
core.host:execute("drawLabel1", 3, Display.xLeverage_Offset, core.CR_RIGHT, Display.yRobinhood_Leverage_Offset, core.CR_TOP, core.H_Left, core.V_Center, Font.Leverage, Format.Leverage, aLeverage);
elseif aType == "Oanda" then
core.host:execute("drawLabel1", 4, Display.xEquity_Offset, core.CR_RIGHT, Display.yOanda_Equity_Offset, core.CR_TOP, core.H_Left, core.V_Center, Font.Equity, Format.Equity, aEquity);
core.host:execute("drawLabel1", 5, Display.xLeverage_Offset, core.CR_RIGHT, Display.yOanda_Leverage_Offset, core.CR_TOP, core.H_Left, core.V_Center, Font.Leverage, Format.Leverage, aLeverage);
elseif aType == "FXCM" then
core.host:execute("drawLabel1", 6, Display.xFloating_PL_Offset, core.CR_RIGHT, Display.yOanda_Day_PL_Offset, core.CR_TOP, core.H_Left, core.V_Center, Font.Floating_PL, Format.Day_PL, aDay_PL);
end
end
--------------------------------------------------------------------------------------------
-- Get Robinhood Account Data
--------------------------------------------------------------------------------------------
function Get_Robinhood_Account_Data()
local zURL = Create_Robinhood_URL_Syntax("Portfolio", "GET_Portfolio", Robinhood.URL, nil, nil, nil, nil);
local zResponse = Send_Request("Robinhood", "GET", zURL, nil, nil , Robinhood.API_Token, nil);
local zEquity = Parse_Robinhood_Response("Portfolio", "GET_Portfolio", Decode_Response(zResponse)).Equity;
local zStart_Equity = Parse_Robinhood_Response("Portfolio", "GET_Portfolio", Decode_Response(zResponse)).Start_Equity;
local zSize_In_USD = Parse_Robinhood_Response("Portfolio", "GET_Portfolio", Decode_Response(zResponse)).Size_In_USD;
local zDay_PL
local zLeverage = nil;
if pcall(function () zDay_PL = zEquity - zStart_Equity; end) then
zDay_PL = zEquity - zStart_Equity;
if zEquity ~= nil and zSize_In_USD ~= nil then
zLeverage = zSize_In_USD / zEquity;
else
zLeverage = "0";
end
else
zDay_PL = nil;
zEquity = nil;
zStart_Equity = nil;
end
Update_Display("Robinhood", zEquity, zDay_PL, nil, zLeverage);
end
--------------------------------------------------------------------------------------------
-- Get Oanda Account Data
--------------------------------------------------------------------------------------------
function Get_Oanda_Account_Data()
local zURL = Create_Oanda_URL_Syntax("Account", "GET_Summary", Oanda.URL, Oanda.Account_ID, nil, nil, nil);
local zReponse = Send_Request("Oanda", "GET", zURL, nil, nil, Oanda.API_Token, nil);
local zEquity = Parse_Oanda_Response("Account", "GET_Summary", Decode_Response(zReponse)).NAV;
local zFloating_PL = Parse_Oanda_Response("Account", "GET_Summary", Decode_Response(zReponse)).Unrealized_PL;
local zSize_In_USD = Parse_Oanda_Response("Account", "GET_Summary", Decode_Response(zReponse)).Size_In_USD;
local zLeverage = nil;
if pcall(function () zLeverage = zSize_In_USD / zEquity; end) then
zLeverage = zSize_In_USD / zEquity;
end
Update_Display("Oanda", zEquity, nil, zFloating_PL, zLeverage);
end
--------------------------------------------------------------------------------------------
-- Get FXCM Account Data
--------------------------------------------------------------------------------------------
function Get_FXCM_Account_Data()
if core.host:execute("isTableFilled", "accounts") then
local zRow = core.host:findTable("accounts"):find("AccountID", FXCM.Account_ID);
Update_Display("FXCM", zRow.Equity, zRow.DayPL, zRow.GrossPL, nil);
end
end
--------------------------------------------------------------------------------------------
-- Create Robinhood URL Function
--------------------------------------------------------------------------------------------
function Create_Robinhood_URL_Syntax(aEndpoint, aEndpoint_Type, aSubdomain, aAccount_ID, aOrder_ID, aTrade_ID, aInstrument)
local zBase_URL = "https://" .. aSubdomain .. "/";
local zComplete_URL = nil;
if aEndpoint == "Authentication" then
if aEndpoint_Type == "POST_Login" then zComplete_URL = zBase_URL .. "api-token-auth/";
else Send_Notification("Alert", "", 0, "Create_Robinhood_URL_Syntax() Failed | In Sub Else Clause", core.now(), "");
end
elseif aEndpoint == "Accounts" then
if aEndpoint_Type == "GET_Accounts" then zComplete_URL = zBase_URL .. "accounts/";
else Send_Notification("Alert", "", 0, "Create_Robinhood_URL_Syntax() Failed | In Sub Else Clause", core.now(), "");
end
elseif aEndpoint == "Portfolio" then
if aEndpoint_Type == "GET_Portfolio" then zComplete_URL = Robinhood.Portfolio_URL;
else Send_Notification("Alert", "", 0, "Create_Robinhood_URL_Syntax() Failed | In Sub Else Clause", core.now(), "");
end
else
Send_Notification("Alert", "", 0, "Create_Robinhood_URL_Syntax() Failed | In Else Clause", core.now(), "");
end
return zComplete_URL;
end
--------------------------------------------------------------------------------------------
-- Create Oanda URL Function
--------------------------------------------------------------------------------------------
function Create_Oanda_URL_Syntax(aEndpoint, aEndpoint_Type, aSubdomain, aAccount_ID, aOrder_ID, aTrade_ID, aInstrument)
local zBase_URL = "https://" .. aSubdomain .. "/v3/accounts/" .. aAccount_ID;
local zComplete_URL = nil;
if aEndpoint == "Account" then
if aEndpoint_Type == "GET_Accounts" then zComplete_URL = "https://" .. aSubdomain .. "/v3/accounts";
elseif aEndpoint_Type == "GET_Account" then zComplete_URL = zBase_URL;
elseif aEndpoint_Type == "GET_Summary" then zComplete_URL = zBase_URL .. "/summary";
else Send_Notification("Alert", "", 0, "Create_Oanda_URL_Syntax() Failed | In Sub Else Clause", core.now(), "");
end
else
Send_Notification("Alert", "", 0, "Create_Oanda_URL_Syntax() Failed | In Else Clause", core.now(), "");
end
return zComplete_URL;
end
--------------------------------------------------------------------------------------------
-- Send Request Function
--------------------------------------------------------------------------------------------
function Send_Request(aRequest_Type, aHTTP_Type, aComplete_URL, aAccount_ID, aPassword, aAPI_Token, zPost_Payload)
local zObject = http_lua.createRequest();
zObject:setRequestHeader("Content-Type", "application/json");
if aHTTP_Type == "GET" then
local zHeaders_Authorization = nil;
if aRequest_Type == "Oanda" then
zHeaders_Authorization = "Bearer " .. aAPI_Token;
elseif aRequest_Type == "Robinhood" then
zHeaders_Authorization = "Token " .. aAPI_Token;
else
Send_Notification("Alert", "", 0, "Send_Request() Failed | In Else Sub Clause", core.now(), "");
end
zObject:setRequestHeader("Authorization", zHeaders_Authorization);
zObject:start(aComplete_URL, "GET");
elseif aHTTP_Type == "POST" then
zObject:start(aComplete_URL, "POST", zPost_Payload);
else
Send_Notification("Alert", "", 0, "Send_Request() Failed | In Else Clause", core.now(), "");
end
Timer.Send_Request = core.host:execute("setTimer", 500, 10);
while zObject:loading() do
end
core.host:execute("killTimer", Timer.Send_Request);
if not(zObject:success()) then
Send_Notification("Alert", "", 0, "Send_Request() Failed | HTTP Status " .. zObject:httpStatus(), core.now(), "");
end
if zObject:httpStatus() ~= 200 and zObject:httpStatus() ~= 201 then
Send_Notification("Trace", "", 0, "Send_Request() Returned | HTTP Status " .. zObject:httpStatus(), core.now(), "");
end
return zObject:response();
end
--------------------------------------------------------------------------------------------
-- Encode Response Function
--------------------------------------------------------------------------------------------
function Encode_Request(aRequest)
JSON = assert(loadfile "JSON.lua")();
aRequest = JSON:encode(aRequest);
return aRequest;
end
--------------------------------------------------------------------------------------------
-- Decode Response Function
--------------------------------------------------------------------------------------------
function Decode_Response(aResponse)
JSON = assert(loadfile "JSON.lua")();
aResponse = JSON:decode(aResponse);
return aResponse;
end
--------------------------------------------------------------------------------------------
-- Parse Robinhood Response Function
--------------------------------------------------------------------------------------------
function Parse_Robinhood_Response(oEndpoint, oEndpoint_Type, aResponse)
if oEndpoint == "Authentication" then
if oEndpoint_Type == "POST_Login" then
local POST_Login = {};
if pcall(function () POST_Login.Token = tostring(aResponse["token"]); end) then
POST_Login.Token = tostring(aResponse["token"]);
else POST_Login.Token = nil; end
if pcall(function () POST_Login.TFA_Type = tostring(aResponse["mfa_type"]); end) then
POST_Login.TFA_Type = tostring(aResponse["mfa_type"]);
else POST_Login.TFA_Type = nil; end
return POST_Login;
else
Send_Notification("Alert", "", 0, "Parse_Robinhood_Response() Failed | In Sub Else Clause", core.now(), "");
end
elseif oEndpoint == "Accounts" then
if oEndpoint_Type == "GET_Accounts" then
local GET_Accounts = {};
if pcall(function () GET_Accounts.Portfolio_URL = tostring(aResponse["results"][1]["portfolio"]); end) then
GET_Accounts.Portfolio_URL = tostring(aResponse["results"][1]["portfolio"]);
else GET_Accounts.Portfolio_URL = nil; end
return GET_Accounts;
else
Send_Notification("Alert", "", 0, "Parse_Robinhood_Response() Failed | In Sub Else Clause", core.now(), "");
end
elseif oEndpoint == "Portfolio" then
if oEndpoint_Type == "GET_Portfolio" then
local GET_Portfolio = {};
if pcall(function () GET_Portfolio.Equity = tostring(aResponse["equity"]); end) then
GET_Portfolio.Equity = tostring(aResponse["equity"]);
else GET_Accounts.Equity = nil; end
if pcall(function () GET_Portfolio.Extened_Hours_Equity = tostring(aResponse["extended_hours_equity"]); end) then
GET_Portfolio.Extened_Hours_Equity = tostring(aResponse["extended_hours_equity"]);
else GET_Accounts.Extened_Hours_Equity = nil; end
if not string.match(GET_Portfolio.Extened_Hours_Equity, "%a") then
GET_Portfolio.Equity = GET_Portfolio.Extened_Hours_Equity;
end
if pcall(function () GET_Portfolio.Start_Equity = tostring(aResponse["equity_previous_close"]); end) then
GET_Portfolio.Start_Equity = tostring(aResponse["equity_previous_close"]);
else GET_Portfolio.Start_Equity = nil; end
if pcall(function () GET_Portfolio.Size_In_USD = tostring(aResponse["market_value"]); end) then
GET_Portfolio.Size_In_USD = tostring(aResponse["market_value"]);
else GET_Accounts.Size_In_USD = nil; end
if pcall(function () GET_Portfolio.Extened_Hours_Size_In_USD = tostring(aResponse["extended_hours_market_value"]); end) then
GET_Portfolio.Extened_Hours_Size_In_USD = tostring(aResponse["extended_hours_market_value"]);
else GET_Accounts.Extened_Hours_Size_In_USD = nil; end
if not string.match(GET_Portfolio.Extened_Hours_Size_In_USD, "%a") then
GET_Portfolio.Size_In_USD = GET_Portfolio.Extened_Hours_Size_In_USD;
end
return GET_Portfolio;
else
Send_Notification("Alert", "", 0, "Parse_Robinhood_Response() Failed | In Sub Else Clause", core.now(), "");
end
else
Send_Notification("Alert", "", 0, "Parse_Robinhood_Response() Failed | In Else Clause", core.now(), "");
end
end
--------------------------------------------------------------------------------------------
-- Parse Oanda Response Function
--------------------------------------------------------------------------------------------
function Parse_Oanda_Response(aEndpoint, aEndpoint_Type, aResponse)
if aEndpoint == "Account" then
if aEndpoint_Type == "GET_Summary" then
local GET_Summary = {};
if pcall(function () GET_Summary.NAV = tostring(aResponse["account"]["NAV"]); end) then
GET_Summary.NAV = tostring(aResponse["account"]["NAV"]);
else GET_Summary.NAV = nil; end
if pcall(function () GET_Summary.Size_In_USD = tostring(aResponse["account"]["positionValue"]); end) then
GET_Summary.Size_In_USD = tostring(aResponse["account"]["positionValue"]);
else GET_Summary.Size_In_USD = nil; end
if pcall(function () GET_Summary.Unrealized_PL = tostring(aResponse["account"]["unrealizedPL"]); end) then
GET_Summary.Unrealized_PL = tostring(aResponse["account"]["unrealizedPL"]);
else GET_Summary.Unrealized_PL = nil; end
return GET_Summary;
else
Send_Notification("Alert", "", 0, "Parse_Oanda_Response() Failed | In Else Clause", core.now(), "");
end
else
Send_Notification("Alert", "", 0, "Parse_Oanda_Response() Failed | In Else Clause", core.now(), "");
end
end
--------------------------------------------------------------------------------------------
-- Send Notification Function
--------------------------------------------------------------------------------------------
function Send_Notification(aType, aSymbol, aOpen, aMessage, aTime)
if aType == "Alert" then
terminal:alertMessage(aSymbol, aOpen, aMessage, aTime);
elseif aType == "Trace" then
core.host:trace(aMessage);
else
core.host:trace("Send_Notification() Failed | In Else Clause");
end
end
--------------------------------------------------------------------------------------------
-- Formatting Functions
--------------------------------------------------------------------------------------------
function Format_Precision(aInput, aDecimals)
return string.format("%." .. aDecimals .. "f", aInput);
end
function Format_Percentage(aInput, aDecimals)
return string.format("%." .. aDecimals .. "f", aInput) .. "%";
end
function Format_Leverage(aInput, aDecimals)
return string.format("%." .. aDecimals .. "f", aInput) .. ":1";
end
function Format_Financial(aInput, aDecimals)
-- Sourced from http://www.gammon.com.au/forum/?id=7805
aInput = string.format("%." .. aDecimals .. "f", aInput);
local aResult = "";
local aSign, aBefore, aAfter = string.match(tostring(aInput), "^([%+%-]?)(%d*)(%.?.*)$")
while string.len(aBefore) > 3 do
aResult = "," .. string.sub(aBefore, -3, -1) .. aResult;
aBefore = string.sub(aBefore, 1, -4);
end
return aSign .. "$" .. aBefore .. aResult .. aAfter;
end
function Format_Color(aInput)
aInput = tonumber(aInput);
if aInput == nil then
return Format.Neutral;
elseif aInput > 0 then
return Format.Positive;
elseif aInput < 0 then
return Format.Negative;
else
return Format.Neutral;
end
end
--------------------------------------------------------------------------------------------
-- Async Operations Function
--------------------------------------------------------------------------------------------
function AsyncOperationFinished(aReference, aSuccess, aMessage, aMessage1, aMessage2)
if aReference == 100 then
if not Robinhood.TFA_Mode then
Get_Robinhood_API_Token();
else
Get_Robinhood_API_Token_TFA_Step_2();
end
Get_Robinhood_Portfolio_URL();
Get_Robinhood_Account_Data();
Get_Oanda_Account_Data();
Get_FXCM_Account_Data();
core.host:execute("killTimer", Timer.Initialized);
elseif aReference == 200 then
Get_Robinhood_Account_Data();
Get_Oanda_Account_Data();
Get_FXCM_Account_Data();
elseif aReference == 500 then
core.host:execute("killTimer", Timer.Send_Request);
end
end