Overall Statistics
Total Trades
99
Average Win
7.29%
Average Loss
-1.35%
Compounding Annual Return
365.973%
Drawdown
22.100%
Expectancy
1.792
Net Profit
357.536%
Sharpe Ratio
3.513
Loss Rate
56%
Win Rate
44%
Profit-Loss Ratio
5.39
Alpha
1.71
Beta
-0.082
Annual Standard Deviation
0.461
Annual Variance
0.213
Information Ratio
0.78
Tracking Error
0.679
Treynor Ratio
-19.846
Total Fees
$1234.70
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Python import PythonQuandl

import sys
import datetime
if sys.version_info[0] < 3:
    from StringIO import StringIO
else:
    from io import StringIO

import pandas as pd

TRANSACTIONS_STR = '''datetime,pred,close,future_close_change,profit_multiplier_long,profit_multiplier_short,profit_multiplier_long_short
2017-01-04 00:00:00,0,50.29,0.0511035991251,1.05110359913,0.948896400875,0.948896400875
2017-01-05 00:00:00,1,52.86,0.00245932652289,1.00245932652,0.997540673477,1.00245932652
2017-01-06 00:00:00,0,52.99,0.0128326099264,1.01283260993,0.987167390074,0.987167390074
2017-01-07 00:00:00,0,53.67,0.00279485746227,1.00279485746,0.997205142538,0.997205142538
2017-01-10 00:00:00,1,53.82,0.00724637681159,1.00724637681,0.992753623188,1.00724637681
2017-01-11 00:00:00,0,54.21,0.0206603947611,1.02066039476,0.979339605239,0.979339605239
2017-01-12 00:00:00,0,55.33,0.000903668895717,1.0009036689,0.999096331104,0.999096331104
2017-01-13 00:00:00,0,55.38,-0.00270855904659,0.997291440953,1.00270855905,1.00270855905
2017-01-14 00:00:00,0,55.23,0.00325909831613,1.00325909832,0.996740901684,0.996740901684
2017-01-18 00:00:00,1,55.41,0.00252661974373,1.00252661974,0.997473380256,1.00252661974
2017-01-19 00:00:00,1,55.55,-0.00810081008101,0.991899189919,1.00810081008,0.991899189919
2017-01-20 00:00:00,1,55.1,0.0390199637024,1.0390199637,0.960980036298,1.0390199637
2017-01-21 00:00:00,0,57.25,0.00908296943231,1.00908296943,0.990917030568,0.990917030568
2017-01-24 00:00:00,1,57.77,0.0463908603081,1.04639086031,0.953609139692,1.04639086031
2017-01-25 00:00:00,0,60.45,0.024317617866,1.02431761787,0.975682382134,0.975682382134
2017-01-26 00:00:00,1,61.92,-0.00532945736434,0.994670542636,1.00532945736,0.994670542636
2017-01-27 00:00:00,1,61.59,0.0120149374899,1.01201493749,0.98798506251,1.01201493749
2017-01-28 00:00:00,0,62.33,-0.0311246590727,0.968875340927,1.03112465907,1.03112465907
2017-01-31 00:00:00,0,60.39,0.00596125186289,1.00596125186,0.994038748137,0.994038748137
2017-02-01 00:00:00,1,60.75,0.0194238683128,1.01942386831,0.980576131687,1.01942386831
2017-02-02 00:00:00,1,61.93,-0.0108186662361,0.989181333764,1.01081866624,0.989181333764
2017-02-03 00:00:00,1,61.26,0.0233431276526,1.02334312765,0.976656872347,1.02334312765
2017-02-04 00:00:00,0,62.69,-0.000797575370873,0.999202424629,1.00079757537,1.00079757537
2017-02-07 00:00:00,1,62.64,-0.00766283524904,0.992337164751,1.00766283525,0.992337164751
2017-02-08 00:00:00,1,62.16,0.00305662805663,1.00305662806,0.996943371943,1.00305662806
2017-02-09 00:00:00,1,62.35,0.0216503608661,1.02165036087,0.978349639134,1.02165036087
2017-02-10 00:00:00,1,63.6999,0.0147582649266,1.01475826493,0.985241735073,1.01475826493
2017-02-11 00:00:00,0,64.64,0.0318688118812,1.03186881188,0.968131188119,0.968131188119
2017-02-14 00:00:00,1,66.7,0.0449775112444,1.04497751124,0.955022488756,1.04497751124
2017-02-15 00:00:00,1,69.7,-0.0364418938307,0.963558106169,1.03644189383,0.963558106169
2017-02-16 00:00:00,1,67.16,-0.00550923168553,0.994490768314,1.00550923169,0.994490768314
2017-02-17 00:00:00,1,66.79,-0.00374307531068,0.996256924689,1.00374307531,0.996256924689
2017-02-18 00:00:00,0,66.54,-0.0102194168921,0.989780583108,1.01021941689,1.01021941689
2017-02-22 00:00:00,0,65.86,-0.00212572122684,0.997874278773,1.00212572123,1.00212572123
2017-02-23 00:00:00,0,65.72,-0.0278454047474,0.972154595253,1.02784540475,1.02784540475
2017-02-24 00:00:00,0,63.89,0.00986069807482,1.00986069807,0.990139301925,0.990139301925
2017-02-25 00:00:00,1,64.52,0.0077495350279,1.00774953503,0.992250464972,1.00774953503
2017-02-28 00:00:00,0,65.02,-0.0264533989542,0.973546601046,1.02645339895,1.02645339895
2017-03-01 00:00:00,0,63.3,0.0214849921011,1.0214849921,0.978515007899,0.978515007899
2017-03-02 00:00:00,0,64.66,-0.000773275595422,0.999226724405,1.0007732756,1.0007732756
2017-03-03 00:00:00,0,64.61,0.0280142392818,1.02801423928,0.971985760718,0.971985760718
2017-03-04 00:00:00,0,66.42,0.0201746461909,1.02017464619,0.979825353809,0.979825353809
2017-03-07 00:00:00,0,67.76,0.0,1.0,1.0,1.0
2017-03-08 00:00:00,0,67.76,-0.00811688311688,0.991883116883,1.00811688312,1.00811688312
2017-03-09 00:00:00,0,67.21,0.00505877101622,1.00505877102,0.994941228984,0.994941228984
2017-03-10 00:00:00,0,67.55,0.0128793486306,1.01287934863,0.987120651369,0.987120651369
2017-03-11 00:00:00,0,68.42,0.021631102017,1.02163110202,0.978368897983,0.978368897983
2017-03-14 00:00:00,0,69.9,-0.0187410586552,0.981258941345,1.01874105866,1.01874105866
2017-03-15 00:00:00,0,68.59,0.0387811634349,1.03878116343,0.961218836565,0.961218836565
2017-03-16 00:00:00,0,71.25,0.019649122807,1.01964912281,0.980350877193,0.980350877193
2017-03-17 00:00:00,0,72.65,0.01280110117,1.01280110117,0.98719889883,0.98719889883
2017-03-18 00:00:00,0,73.58,-0.00244631693395,0.997553683066,1.00244631693,1.00244631693
2017-03-21 00:00:00,0,73.4,-0.0374659400545,0.962534059946,1.03746594005,1.03746594005
2017-03-22 00:00:00,0,70.65,-0.00537862703468,0.994621372965,1.00537862703,1.00537862703
2017-03-23 00:00:00,0,70.27,-0.0357193681514,0.964280631849,1.03571936815,1.03571936815
2017-03-24 00:00:00,0,67.76,0.0206611570248,1.02066115702,0.979338842975,0.979338842975
2017-03-25 00:00:00,1,69.16,0.0205320994795,1.02053209948,0.979467900521,1.02053209948
2017-03-28 00:00:00,0,70.58,0.053556248229,1.05355624823,0.946443751771,0.946443751771
2017-03-29 00:00:00,0,74.36,0.00537923614847,1.00537923615,0.994620763852,0.994620763852
2017-03-30 00:00:00,0,74.76,-0.00454788657036,0.99545211343,1.00454788657,1.00454788657
2017-03-31 00:00:00,0,74.42,-0.0186777747917,0.981322225208,1.01867777479,1.01867777479
2017-04-01 00:00:00,1,73.03,-0.00561413117897,0.994385868821,1.00561413118,0.994385868821
2017-04-04 00:00:00,1,72.62,0.0126686863123,1.01266868631,0.987331313688,1.01266868631
2017-04-05 00:00:00,0,73.54,-0.0333152026108,0.966684797389,1.03331520261,1.03331520261
2017-04-06 00:00:00,0,71.09,0.0170206780138,1.01702067801,0.982979321986,0.982979321986
2017-04-07 00:00:00,1,72.3,-0.0363762102351,0.963623789765,1.03637621024,0.963623789765
2017-04-08 00:00:00,1,69.67,-0.0345916463327,0.965408353667,1.03459164633,0.965408353667
2017-04-11 00:00:00,1,67.26,-0.0477252453167,0.952274754683,1.04772524532,0.952274754683
2017-04-12 00:00:00,1,64.05,-0.000468384074941,0.999531615925,1.00046838407,0.999531615925
2017-04-13 00:00:00,1,64.02,-0.0196813495783,0.980318650422,1.01968134958,0.980318650422
2017-04-14 00:00:00,1,62.76,0.0487571701721,1.04875717017,0.951242829828,1.04875717017
2017-04-18 00:00:00,0,65.82,0.00714068672136,1.00714068672,0.992859313279,0.992859313279
2017-04-19 00:00:00,0,66.29,-0.0239855181777,0.976014481822,1.02398551818,1.02398551818
2017-04-20 00:00:00,0,64.7,0.0261205564142,1.02612055641,0.973879443586,0.973879443586
2017-04-21 00:00:00,0,66.39,-0.00195812622383,0.998041873776,1.00195812622,1.00195812622
2017-04-22 00:00:00,1,66.26,0.105946272261,1.10594627226,0.894053727739,1.10594627226
2017-04-25 00:00:00,1,73.28,0.0238810043668,1.02388100437,0.976118995633,1.02388100437
2017-04-26 00:00:00,1,75.03,-0.00892976142876,0.991070238571,1.00892976143,0.991070238571
2017-04-27 00:00:00,1,74.36,0.00968262506724,1.00968262507,0.990317374933,1.00968262507
2017-04-28 00:00:00,1,75.08,0.000133191262653,1.00013319126,0.999866808737,1.00013319126
2017-04-29 00:00:00,1,75.09,0.0388866693301,1.03888666933,0.96111333067,1.03888666933
2017-05-02 00:00:00,1,78.01,-0.00384566081272,0.996154339187,1.00384566081,0.996154339187
2017-05-03 00:00:00,1,77.71,-0.0195599022005,0.9804400978,1.0195599022,0.9804400978
2017-05-04 00:00:00,1,76.19,0.025200157501,1.0252001575,0.974799842499,1.0252001575
2017-05-05 00:00:00,1,78.11,-0.00614517987454,0.993854820125,1.00614517987,0.993854820125
2017-05-06 00:00:00,1,77.63,0.0239598093521,1.02395980935,0.976040190648,1.02395980935
2017-05-09 00:00:00,1,79.49,0.00465467354384,1.00465467354,0.995345326456,1.00465467354
2017-05-10 00:00:00,0,79.86,-0.00400701227147,0.995992987729,1.00400701227,1.00400701227
2017-05-11 00:00:00,1,79.54,0.00352024138798,1.00352024139,0.996479758612,1.00352024139
2017-05-12 00:00:00,1,79.82,0.000375845652719,1.00037584565,0.999624154347,1.00037584565
2017-05-13 00:00:00,1,79.85,0.0207889793363,1.02078897934,0.979211020664,1.02078897934
2017-05-16 00:00:00,1,81.51,0.0109189056557,1.01091890566,0.989081094344,1.01091890566
2017-05-17 00:00:00,0,82.4,-0.178276699029,0.821723300971,1.17827669903,1.17827669903
2017-05-18 00:00:00,0,67.71,0.0225963668587,1.02259636686,0.977403633141,0.977403633141
2017-05-19 00:00:00,1,69.24,0.0639803581745,1.06398035817,0.936019641826,1.06398035817
2017-05-20 00:00:00,1,73.67,0.0438441699471,1.04384416995,0.956155830053,1.04384416995
2017-05-23 00:00:00,1,76.9,0.00130039011704,1.00130039012,0.998699609883,1.00130039012
2017-05-24 00:00:00,1,77.0,0.0254545454545,1.02545454545,0.974545454545,1.02545454545
2017-05-25 00:00:00,1,78.96,-0.0101317122594,0.989868287741,1.01013171226,0.989868287741
2017-05-26 00:00:00,1,78.16,0.01752814739,1.01752814739,0.98247185261,1.01752814739
2017-05-27 00:00:00,1,79.53,0.0,1.0,1.0,1.0
2017-05-31 00:00:00,1,79.53,-0.00238903558406,0.997610964416,1.00238903558,0.997610964416
2017-06-01 00:00:00,1,79.34,0.0245777665742,1.02457776657,0.975422233426,1.02457776657
2017-06-02 00:00:00,1,81.29,-0.00295239266822,0.997047607332,1.00295239267,0.997047607332
2017-06-03 00:00:00,1,81.05,-0.00431832202344,0.995681677977,1.00431832202,0.995681677977
2017-06-06 00:00:00,1,80.7,-0.0218091697646,0.978190830235,1.02180916976,0.978190830235
2017-06-07 00:00:00,1,78.94,0.0101342791994,1.0101342792,0.989865720801,1.0101342792
2017-06-08 00:00:00,1,79.74,0.0223225482819,1.02232254828,0.977677451718,1.02232254828
2017-06-09 00:00:00,1,81.52,-0.0137389597645,0.986261040236,1.01373895976,0.986261040236
2017-06-10 00:00:00,1,80.4,-0.0119402985075,0.988059701493,1.01194029851,0.988059701493
2017-06-13 00:00:00,1,79.44,0.0352467270896,1.03524672709,0.96475327291,1.03524672709
2017-06-14 00:00:00,1,82.24,0.00449902723735,1.00449902724,0.995500972763,1.00449902724
2017-06-15 00:00:00,1,82.61,-0.0118629705847,0.988137029415,1.01186297058,0.988137029415
2017-06-16 00:00:00,1,81.63,0.00980031851035,1.00980031851,0.99019968149,1.00980031851
2017-06-17 00:00:00,1,82.43,0.0302074487444,1.03020744874,0.969792551256,1.03020744874
2017-06-20 00:00:00,1,84.92,-0.0261422515309,0.973857748469,1.02614225153,0.973857748469
2017-06-21 00:00:00,1,82.7,0.00943168077388,1.00943168077,0.990568319226,1.00943168077
2017-06-22 00:00:00,1,83.48,0.00922376617154,1.00922376617,0.990776233828,1.00922376617
2017-06-23 00:00:00,1,84.25,0.0105637982196,1.01056379822,0.98943620178,1.01056379822
2017-06-24 00:00:00,1,85.14,0.0218463706836,1.02184637068,0.978153629316,1.02184637068
2017-06-27 00:00:00,1,87.0,-0.0329885057471,0.967011494253,1.03298850575,0.967011494253
2017-06-28 00:00:00,1,84.13,0.0287650065375,1.02876500654,0.971234993462,1.02876500654
2017-06-29 00:00:00,1,86.55,-0.0511842865396,0.94881571346,1.05118428654,0.94881571346
2017-06-30 00:00:00,1,82.12,0.0165611300536,1.01656113005,0.983438869946,1.01656113005
2017-07-01 00:00:00,1,83.48,-0.0206037374221,0.979396262578,1.02060373742,0.979396262578
2017-07-04 00:00:00,1,81.76,0.00843933463796,1.00843933464,0.991560665362,1.00843933464
2017-07-06 00:00:00,1,82.45,-0.0502122498484,0.949787750152,1.05021224985,0.949787750152
2017-07-07 00:00:00,1,78.31,0.0335844719704,1.03358447197,0.96641552803,1.03358447197
2017-07-08 00:00:00,1,80.94,0.0247096614776,1.02470966148,0.975290338522,1.02470966148
2017-07-11 00:00:00,1,82.94,0.00458162527128,1.00458162527,0.995418374729,1.00458162527
2017-07-12 00:00:00,1,83.32,0.0279644743159,1.02796447432,0.972035525684,1.02796447432
2017-07-13 00:00:00,1,85.65,0.0107413893754,1.01074138938,0.989258610625,1.01074138938
2017-07-14 00:00:00,1,86.57,0.0189442070001,1.018944207,0.981055793,1.018944207
2017-07-15 00:00:00,1,88.21,0.0256206779277,1.02562067793,0.974379322072,1.02562067793
2017-07-18 00:00:00,1,90.47,0.0124903282856,1.01249032829,0.987509671714,1.01249032829
2017-07-19 00:00:00,1,91.6,0.0120087336245,1.01200873362,0.987991266376,1.01200873362
2017-07-20 00:00:00,1,92.7,0.00852211434736,1.00852211435,0.991477885653,1.00852211435
2017-07-21 00:00:00,1,93.49,0.00930580810782,1.00930580811,0.990694191892,1.00930580811
2017-07-22 00:00:00,1,94.36,0.0170623145401,1.01706231454,0.98293768546,1.01706231454
2017-07-25 00:00:00,1,95.97,-0.00375117224133,0.996248827759,1.00375117224,0.996248827759
2017-07-26 00:00:00,1,95.61,-0.00198723982847,0.998012760172,1.00198723983,0.998012760172
2017-07-27 00:00:00,1,95.42,-0.00785998742402,0.992140012576,1.00785998742,0.992140012576
2017-07-28 00:00:00,1,94.67,-0.0105630083448,0.989436991655,1.01056300834,0.989436991655
2017-07-29 00:00:00,1,93.67,0.00779331696381,1.00779331696,0.992206683036,1.00779331696
2017-08-01 00:00:00,1,94.4,0.0131355932203,1.01313559322,0.98686440678,1.01313559322
2017-08-02 00:00:00,1,95.64,-0.00564617314931,0.994353826851,1.00564617315,0.994353826851
2017-08-03 00:00:00,1,95.1,-0.0127234490011,0.987276550999,1.012723449,0.987276550999
2017-08-04 00:00:00,1,93.89,0.00447331984237,1.00447331984,0.995526680158,1.00447331984
2017-08-05 00:00:00,0,94.31,0.0113455625066,1.01134556251,0.988654437493,0.988654437493
2017-08-08 00:00:00,0,95.38,-0.0290417278255,0.970958272174,1.02904172783,1.02904172783
2017-08-09 00:00:00,0,92.61,-0.0242954324587,0.975704567541,1.02429543246,1.02429543246
2017-08-10 00:00:00,0,90.36,-0.135236830456,0.864763169544,1.13523683046,1.13523683046
2017-08-11 00:00:00,0,78.14,-0.0432556949066,0.956744305093,1.04325569491,1.04325569491
2017-08-12 00:00:00,1,74.76,0.124130551097,1.1241305511,0.875869448903,1.1241305511
2017-08-15 00:00:00,1,84.04,0.00594954783436,1.00594954783,0.994050452166,1.00594954783
2017-08-16 00:00:00,0,84.54,0.00484977525432,1.00484977525,0.995150224746,0.995150224746
2017-08-17 00:00:00,0,84.95,-0.157033549147,0.842966450853,1.15703354915,1.15703354915
2017-08-18 00:00:00,1,71.61,0.0310012568077,1.03100125681,0.968998743192,1.03100125681
2017-08-19 00:00:00,1,73.83,0.0446972775295,1.04469727753,0.955302722471,1.04469727753
2017-08-22 00:00:00,0,77.13,0.0671593413717,1.06715934137,0.932840658628,0.932840658628
2017-08-23 00:00:00,1,82.31,-0.00716802332645,0.992831976674,1.00716802333,0.992831976674
2017-08-24 00:00:00,0,81.72,-0.0312041116006,0.968795888399,1.0312041116,1.0312041116
2017-08-25 00:00:00,0,79.17,0.0269041303524,1.02690413035,0.973095869648,0.973095869648
2017-08-26 00:00:00,1,81.3,0.010332103321,1.01033210332,0.989667896679,1.01033210332
2017-08-29 00:00:00,1,82.14,-0.0104699293888,0.989530070611,1.01046992939,0.989530070611
2017-08-30 00:00:00,1,81.28,0.00442913385827,1.00442913386,0.995570866142,1.00442913386
2017-08-31 00:00:00,1,81.64,0.0241303282705,1.02413032827,0.97586967173,1.02413032827
2017-09-01 00:00:00,1,83.61,0.00227245544791,1.00227245545,0.997727544552,1.00227245545
2017-09-02 00:00:00,1,83.8,-0.0476133651551,0.952386634845,1.04761336516,0.952386634845
2017-09-06 00:00:00,1,79.81,0.0192958275905,1.01929582759,0.980704172409,1.01929582759
2017-09-07 00:00:00,1,81.35,0.00516287645974,1.00516287646,0.99483712354,1.00516287646
2017-09-08 00:00:00,1,81.77,-0.024948024948,0.975051975052,1.02494802495,0.975051975052
2017-09-09 00:00:00,1,79.73,0.0583218361972,1.0583218362,0.941678163803,1.0583218362
2017-09-12 00:00:00,1,84.38,0.0244133680967,1.0244133681,0.975586631903,1.0244133681
2017-09-13 00:00:00,1,86.44,0.0342434058306,1.03424340583,0.965756594169,1.03424340583
2017-09-14 00:00:00,1,89.4,-0.0164429530201,0.98355704698,1.01644295302,0.98355704698
2017-09-15 00:00:00,1,87.93,0.0185374729899,1.01853747299,0.98146252701,1.01853747299
2017-09-16 00:00:00,1,89.56,0.0428762840554,1.04287628406,0.957123715945,1.04287628406
2017-09-19 00:00:00,1,93.4,0.0017130620985,1.0017130621,0.998286937901,1.0017130621
2017-09-20 00:00:00,1,93.56,0.0,1.0,1.0,1.0
2017-09-21 00:00:00,1,93.56,-0.00171013253527,0.998289867465,1.00171013254,0.998289867465
2017-09-22 00:00:00,1,93.4,-0.00578158458244,0.994218415418,1.00578158458,0.994218415418
2017-09-23 00:00:00,1,92.86,-0.00269222485462,0.997307775145,1.00269222485,0.997307775145
2017-09-26 00:00:00,1,92.61,0.0132815030774,1.01328150308,0.986718496923,1.01328150308
2017-09-27 00:00:00,1,93.84,0.00745950554135,1.00745950554,0.992540494459,1.00745950554
2017-09-28 00:00:00,1,94.54,0.013221916649,1.01322191665,0.986778083351,1.01322191665
2017-09-29 00:00:00,1,95.79,0.0182691303894,1.01826913039,0.981730869611,1.01826913039
2017-09-30 00:00:00,1,97.54,0.0200943202789,1.02009432028,0.979905679721,1.02009432028
2017-10-03 00:00:00,1,99.5,0.00321608040201,1.0032160804,0.996783919598,1.0032160804
2017-10-04 00:00:00,1,99.82,-0.00180324584252,0.998196754157,1.00180324584,0.998196754157
2017-10-05 00:00:00,1,99.64,0.0327177840225,1.03271778402,0.967282215978,1.03271778402
2017-10-06 00:00:00,1,102.9,-0.000971817298348,0.999028182702,1.0009718173,0.999028182702
2017-10-07 00:00:00,1,102.8,-0.0201361867704,0.97986381323,1.02013618677,0.97986381323
2017-10-10 00:00:00,1,100.73,0.0235282438201,1.02352824382,0.97647175618,1.02352824382
2017-10-11 00:00:00,1,103.1,0.0199806013579,1.01998060136,0.980019398642,1.01998060136
2017-10-12 00:00:00,1,105.16,0.00684670977558,1.00684670978,0.993153290224,1.00684670978
2017-10-13 00:00:00,1,105.88,0.0186059690215,1.01860596902,0.981394030978,1.01860596902
2017-10-14 00:00:00,1,107.85,0.0171534538711,1.01715345387,0.982846546129,1.01715345387
2017-10-17 00:00:00,1,109.7,-0.0024612579763,0.997538742024,1.00246125798,0.997538742024
2017-10-18 00:00:00,1,109.43,0.00466051357032,1.00466051357,0.99533948643,1.00466051357
2017-10-19 00:00:00,1,109.94,0.00782244860833,1.00782244861,0.992177551392,1.00782244861
2017-10-20 00:00:00,1,110.8,0.0175992779783,1.01759927798,0.982400722022,1.01759927798
2017-10-21 00:00:00,1,112.75,-0.0321951219512,0.967804878049,1.03219512195,0.967804878049
2017-10-24 00:00:00,0,109.12,-0.0115469208211,0.988453079179,1.01154692082,1.01154692082
2017-10-25 00:00:00,0,107.86,-0.0311514926757,0.968848507324,1.03115149268,1.03115149268
2017-10-26 00:00:00,0,104.5,0.00248803827751,1.00248803828,0.997511961722,0.997511961722
2017-10-27 00:00:00,1,104.76,0.0568919434899,1.05689194349,0.94310805651,1.05689194349
2017-10-28 00:00:00,1,110.72,-0.00776734104046,0.99223265896,1.00776734104,0.99223265896
2017-10-31 00:00:00,0,109.86,0.0176588385218,1.01765883852,0.982341161478,0.982341161478
2017-11-01 00:00:00,0,111.8,-0.00527728085868,0.994722719141,1.00527728086,1.00527728086
2017-11-02 00:00:00,1,111.21,0.00998111680604,1.00998111681,0.990018883194,1.00998111681
2017-11-03 00:00:00,1,112.32,0.00195868945869,1.00195868946,0.998041310541,1.00195868946
2017-11-04 00:00:00,1,112.54,0.010129731651,1.01012973165,0.989870268349,1.01012973165
2017-11-07 00:00:00,1,113.68,-0.00545390570021,0.9945460943,1.0054539057,0.9945460943
2017-11-08 00:00:00,1,113.06,-0.00141517778171,0.998584822218,1.00141517778,0.998584822218
2017-11-09 00:00:00,1,112.9,-0.0129317980514,0.987068201949,1.01293179805,0.987068201949
2017-11-10 00:00:00,1,111.44,-0.0235104091888,0.976489590811,1.02351040919,0.976489590811
2017-11-11 00:00:00,1,108.82,-0.00266495129572,0.997335048704,1.0026649513,0.997335048704
2017-11-14 00:00:00,0,108.53,-0.00792407629227,0.992075923708,1.00792407629,1.00792407629
2017-11-15 00:00:00,0,107.67,-0.0362217887991,0.963778211201,1.0362217888,1.0362217888
2017-11-16 00:00:00,0,103.77,0.040763226366,1.04076322637,0.959236773634,0.959236773634
2017-11-17 00:00:00,1,108.0,0.0150925925926,1.01509259259,0.984907407407,1.01509259259
2017-11-18 00:00:00,1,109.63,0.0311958405546,1.03119584055,0.968804159445,1.03119584055
2017-11-21 00:00:00,1,113.05,0.0379478107032,1.0379478107,0.962052189297,1.0379478107
2017-11-22 00:00:00,1,117.34,0.0095449122209,1.00954491222,0.990455087779,1.00954491222
2017-11-23 00:00:00,1,118.46,0.0030390005065,1.00303900051,0.996960999493,1.00303900051
2017-11-25 00:00:00,1,118.82,0.00244066655445,1.00244066655,0.997559333446,1.00244066655
2017-11-28 00:00:00,1,119.11,0.0138527411636,1.01385274116,0.986147258836,1.01385274116
2017-11-29 00:00:00,1,120.76,-0.0219443524346,0.978055647565,1.02194435243,0.978055647565
2017-11-30 00:00:00,0,118.11,-0.00194733722801,0.998052662772,1.00194733723,1.00194733723
2017-12-01 00:00:00,1,117.88,-0.0270614183916,0.972938581608,1.02706141839,0.972938581608
2017-12-02 00:00:00,1,114.69,0.00793443194699,1.00793443195,0.992065568053,1.00793443195
2017-12-05 00:00:00,1,115.6,0.00501730103806,1.00501730104,0.994982698962,1.00501730104
2017-12-06 00:00:00,1,116.18,0.000602513341367,1.00060251334,0.999397486659,1.00060251334
2017-12-07 00:00:00,0,116.25,0.0323440860215,1.03234408602,0.967655913978,0.967655913978
2017-12-08 00:00:00,1,120.01,0.0321639863345,1.03216398633,0.967836013666,1.03216398633
2017-12-09 00:00:00,1,123.87,0.0338257850973,1.0338257851,0.966174214903,1.0338257851
2017-12-12 00:00:00,1,128.06,-0.00179603310948,0.998203966891,1.00179603311,0.998203966891
2017-12-13 00:00:00,1,127.83,0.00367675819448,1.00367675819,0.996323241806,1.00367675819
2017-12-14 00:00:00,1,128.3,0.00592361652377,1.00592361652,0.994076383476,1.00592361652
2017-12-15 00:00:00,1,129.06,0.033240353324,1.03324035332,0.966759646676,1.03324035332
2017-12-16 00:00:00,1,133.35,0.0141732283465,1.01417322835,0.985826771654,1.01417322835
2017-12-19 00:00:00,1,135.24,-0.0036231884058,0.996376811594,1.00362318841,0.996376811594
2017-12-20 00:00:00,0,134.75,0.00296846011132,1.00296846011,0.997031539889,0.997031539889
2017-12-21 00:00:00,1,135.15,0.00673325934147,1.00673325934,0.993266740659,1.00673325934
2017-12-22 00:00:00,1,136.06,-0.0052917830369,0.994708216963,1.00529178304,0.994708216963
2017-12-23 00:00:00,1,135.34,-0.000295551943254,0.999704448057,1.00029555194,0.999704448057
2017-12-27 00:00:00,0,135.3,-0.00569105691057,0.994308943089,1.00569105691,1.00569105691
2017-12-28 00:00:00,0,134.53,0.0165762283506,1.01657622835,0.983423771649,0.983423771649
2017-12-29 00:00:00,0,136.76,-0.0178414741152,0.982158525885,1.01784147412,1.01784147412
'''


class GaboTestML(QCAlgorithm):

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.df = pd.read_csv(StringIO(TRANSACTIONS_STR), index_col='datetime')
        self.df.set_index(pd.to_datetime(self.df.index), inplace=True)

        self._trade_mins_before_market_close = 10
        self._posible_trades = set([
            'long',
            'short',
        ])
        self._leverage = 1

        self.SetStartDate(2017, 1, 3)   #Set Start Date
        self.SetEndDate(2017, 12, 31)     #Set End Date
        self.SetCash(100000)          #Set Strategy Cash

        self.underlying = 'XIV'
        self.benchmark = self.underlying
        self.asset = self.AddEquity(self.underlying, Resolution.Minute)
        self.asset.SetDataNormalizationMode(DataNormalizationMode.SplitAdjusted)

        self.Schedule.On(
            self.DateRules.EveryDay(self.underlying),
            self.TimeRules.BeforeMarketClose(
                self.underlying,
                self._trade_mins_before_market_close),
            Action(self._trade)
        )

        self.SetBenchmark(self.benchmark)


    def _trade(self):
        date_to_search = self.Time.date() + datetime.timedelta(days=1)

        try:
            # self.df.loc[datetime.date(2020, 1, 1)]
            current_model_output = self.df.loc[date_to_search]
        except KeyError:
            self.Log('No transaction found on %s' % str(self.Time.date()))
            current_model_output = None

        if current_model_output is None:
            # TODO: Sell the portfolio
            if self.Portfolio[self.underlying].Quantity:
                self.Log('As there are no model output, keeping portfolio empty')
            self.SetHoldings(self.underlying, 0)
            
        elif current_model_output.pred:
            if 'long' in self._posible_trades:
                self.Log('Real %s close prince: %f. Model close price: %f.' % (
                    self.underlying,
                    self.Securities[self.underlying].Close,
                    current_model_output.close)
                )
                self.Log('Buying %s' % self.underlying)
                self.SetHoldings(self.underlying, self._leverage)
            else:
                if self.Portfolio[self.underlying].Quantity:
                    self.SetHoldings(self.underlying, 0)
                    self.Log('Buying to cover %s', self.underlying)

        elif current_model_output.pred==0:
            if 'short' in self._posible_trades:
                self.Log('Real %s close prince: %f. Model close price: %f.' % (
                    self.underlying,
                    self.Securities[self.underlying].Close,
                    current_model_output.close)
                )
                self.Log('Shorting %s' % self.underlying)
                self.SetHoldings(self.underlying, -self._leverage)
            else:
                if self.Portfolio[self.underlying].Quantity:
                    self.SetHoldings(self.underlying, 0)
                    self.Log('Selling %s', self.underlying)

        return