Je suis nouveau avec quantstrat et je voudrais l'utiliser pour simuler ma stratégie qui est essentiellement une bande de Bollinger. Mon code ne fonctionne pas pour clôturer la position ouverte lorsque la bande de Bollinger est ouverte. Premium traverse le Moy. .
Les algo logiques qui définissent la stratégie (signaux/règles) sont :
- Ouvrir une position courte : si Premium est supérieure ou égale à la Up La bande ouvre alors une position courte au prix du marché ;
- Fermer une position courte ouverte : si Premium est inférieur ou égal à la Moy. puis fermez la position courte (croise la moyenne) ;
- Position longue ouverte :si Premium est inférieur ou égal à la Dwn La bande ouvre alors une position longue au prix du marché ;
- Fermer une position longue ouverte si la "prime" est supérieure ou égale à la valeur de l'indice de référence Moy. puis fermez la position longue (croise la moyenne) ;
Mon xts
Observé ont toutes les séries qui seront utilisées : Observed.Close , Premium , Up , Dwn , Moy. .
En dessous de la Observé les données et mon code :
> Observed
Observed.Close Premium Up Dwn Avg
2015-06-26 3.1293 1.965206103 2.0527059 -1.5056387 0.2735335950
2015-06-29 3.1176 1.474362061 2.1247886 -1.4171072 0.3538407214
2015-06-30 3.1030 1.011736997 2.1689062 -1.3967265 0.3860898669
2015-07-01 3.1492 1.711709129 2.2667986 -1.3371877 0.4648054514
2015-07-02 3.0967 0.481718930 2.2727613 -1.3274414 0.4726599480
2015-07-03 3.1341 1.249455394 2.2682612 -1.3261065 0.4710773679
2015-07-06 3.1376 0.642722112 2.2174802 -1.3210587 0.4482107662
2015-07-07 3.1863 0.553009282 2.1123671 -1.2940353 0.4091659066
2015-07-08 3.2353 1.115494115 2.1592003 -1.2834765 0.4378618993
2015-07-09 3.2130 0.952882431 2.1922315 -1.2485792 0.4718261388
2015-07-10 3.1599 0.665690890 2.2048896 -1.2356460 0.4846218201
2015-07-13 3.1344 0.060862823 2.2047372 -1.2400712 0.4823329800
2015-07-14 3.1393 0.321069026 2.2023548 -1.1986208 0.5018670111
2015-07-15 3.1401 -0.236291845 2.1784579 -1.1417133 0.5183722920
2015-07-16 3.1568 0.139251221 2.0806787 -0.9452963 0.5676911948
2015-07-17 3.1881 0.620612235 1.9791898 -0.7194874 0.6298511901
2015-07-20 3.1958 0.189529583 1.9001714 -0.5685196 0.6658259294
2015-07-21 3.1716 -0.281733702 1.9072905 -0.5828426 0.6622239760
2015-07-22 3.2231 0.377200258 1.9024721 -0.5963551 0.6530584968
2015-07-23 3.2854 0.869682711 1.8167342 -0.3926226 0.7120558074
2015-07-24 3.3554 0.763388365 1.8161428 -0.3930004 0.7115711720
2015-07-27 3.3637 -0.149080328 1.8204624 -0.4690792 0.6756915849
2015-07-28 3.3567 0.637889087 1.8201526 -0.4694712 0.6753407023
2015-07-29 3.3314 -0.045153506 1.8164318 -0.4614529 0.6774894519
2015-07-30 3.3713 1.862338163 1.9326375 -0.4607578 0.7359398900
2015-07-31 3.4211 1.480222141 1.9896962 -0.4637566 0.7629698082
2015-08-03 3.4508 0.495918663 1.9781690 -0.4800864 0.7490412905
2015-08-04 3.4711 2.140586835 2.1054125 -0.5412551 0.7820787099
2015-08-05 3.4851 2.187488885 2.2239655 -0.5971571 0.8134042173
2015-08-06 3.5363 1.774638281 2.2836179 -0.6147908 0.8344135447
2015-08-07 3.5071 0.149059477 2.1786688 -0.6309181 0.7738753238
2015-08-10 3.4404 0.412479597 2.1236127 -0.6466542 0.7384792417
2015-08-11 3.4745 0.527214988 2.1055755 -0.6609185 0.7223285080
2015-08-12 3.4807 0.396682496 2.0145450 -0.6575564 0.6784942870
2015-08-13 3.5187 1.064117266 2.0390422 -0.6432271 0.6979075648
2015-08-14 3.4820 -0.247016300 2.0153374 -0.7192870 0.6480251750
2015-08-17 3.4810 -1.184774093 2.1094473 -0.9352301 0.5871086349
2015-08-18 3.4681 -1.212364308 2.1864729 -1.1299472 0.5282628485
2015-08-19 3.4926 -2.540271699 2.3911950 -1.5783870 0.4064039880
2015-08-20 3.4579 -1.968362932 2.4623598 -1.8443015 0.3090291426
2015-08-21 3.4998 -1.885025633 2.5160255 -2.0680150 0.2240052585
2015-08-24 3.5524 -1.918249920 2.5797446 -2.2636749 0.1580348337
2015-08-25 3.6172 -0.073792880 2.5672080 -2.2774624 0.1448727701
2015-08-26 3.5968 -0.654065425 2.5671136 -2.3052196 0.1309469841
2015-08-27 3.5526 -0.610844638 2.5571085 -2.3452209 0.1059437888
2015-08-28 3.5819 2.068485500 2.7023988 -2.3939864 0.1542062310
2015-08-31 3.6198 3.276461295 3.0488630 -2.5346551 0.2571039547
2015-09-01 3.6977 3.012968160 3.3252241 -2.5913694 0.3669273501
2015-09-02 3.7609 1.319458889 3.3770219 -2.5803500 0.3983359711
2015-09-03 3.7397 1.222954220 3.3992829 -2.5790596 0.4101116881
2015-09-04 3.8430 1.388474585 3.4389640 -2.5770682 0.4309478954
2015-09-07 3.8430 2.375363664 3.5963196 -2.5661276 0.5150960285
2015-09-08 3.8203 1.260535328 3.6288627 -2.5571609 0.5358509032
2015-09-09 3.7792 -0.913740775 3.6384359 -2.6246399 0.5068979942
2015-09-10 3.8499 -1.208280022 3.5534391 -2.7443510 0.4045440547
2015-09-11 3.8735 0.304095228 3.4879948 -2.7573152 0.3653398243
2015-09-14 3.8156 -0.824477698 3.4734482 -2.8307950 0.3213266123
2015-09-15 3.8625 1.928794976 3.4504366 -2.8219029 0.3142668836
2015-09-16 3.8286 2.441458663 3.4811182 -2.8356531 0.3227325429
2015-09-17 3.8986 3.571472359 3.7181269 -2.9528729 0.3826270121
2015-09-18 3.9446 3.297716378 3.9868045 -3.0116400 0.4875822422
2015-09-21 3.9851 2.407020170 4.1224922 -3.0143584 0.5540669280
2015-09-22 4.0504 2.445625463 4.2525916 -3.0165638 0.6180139438
2015-09-23 4.1790 1.414947088 4.2969844 -2.9930722 0.6519560969
2015-09-24 3.9370 0.142556643 4.2674254 -3.0249506 0.6212374095
2015-09-25 3.9765 0.543916266 4.2792205 -2.9840168 0.6476018283
2015-09-28 4.1106 1.102622777 4.2917663 -2.8440695 0.7238483906
2015-09-29 4.0608 3.146668217 4.4657099 -2.7274110 0.8691494748
2015-09-30 3.9471 1.002501203 4.3453160 -2.3708322 0.9872419049
2015-10-01 4.0098 0.207299529 4.2431416 -2.1236136 1.0597639869
2015-10-02 3.9325 0.257836640 4.1320883 -1.8697028 1.1311927293
2015-10-05 3.9115 -0.259679877 4.0108231 -1.6378663 1.1864783975
2015-10-06 3.8548 -0.229135670 4.0157575 -1.6531568 1.1813003044
2015-10-07 3.8857 -1.008262382 4.0383275 -1.6993401 1.1694937392
2015-10-08 3.7840 -0.827853080 4.0506966 -1.7261764 1.1622601245
2015-10-09 3.7662 -1.549022902 4.0721157 -1.9887627 1.0416765111
2015-10-12 3.7662 -1.182302729 3.9072692 -2.1211672 0.8930510436
2015-10-13 3.8931 -0.151540748 3.7150419 -2.1399071 0.7875674134
2015-10-14 3.8126 -1.394567527 3.7226588 -2.3284591 0.6970998662
2015-10-15 3.7994 -0.765332852 3.6955723 -2.4339250 0.6308236304
2015-10-16 3.9253 1.755707483 3.7232303 -2.4371008 0.6430647271
2015-10-19 3.8864 -0.005412208 3.5812295 -2.4538185 0.5637055313
2015-10-20 3.9053 1.240222611 3.5799145 -2.4538576 0.5630284407
2015-10-21 3.9399 0.481642094 3.5747994 -2.3557170 0.6095412030
2015-10-22 3.9070 0.595954568 3.5544745 -2.2151098 0.6696823560
2015-10-23 3.8771 -0.696022671 3.5614526 -2.2887624 0.6363450928
2015-10-26 3.9067 -0.097939430 3.5474039 -2.2262779 0.6605630350
2015-10-27 3.8884 -0.179690333 3.4519132 -2.2713528 0.5902801914
2015-10-28 3.9048 1.082021586 3.3272540 -2.2373228 0.5449656222
2015-10-29 3.8489 -0.227289592 2.9665999 -2.1299195 0.4183402238
2015-10-30 3.8568 0.272833919 2.6220541 -1.9870325 0.3175108085
2015-11-02 3.8568 0.300689669 2.4125588 -1.9179592 0.2472997918
2015-11-03 3.7694 -1.042205419 2.1792576 -1.9171801 0.1310387624
2015-11-04 3.8009 -0.700582164 2.0711547 -1.9501124 0.0605211207
2015-11-05 3.7802 -1.534101153 2.0973647 -2.0880996 0.0046325274
2015-11-06 3.7692 -1.330570385 2.0797085 -2.1954092 -0.0578503609
2015-11-09 3.7993 -0.612073064 1.9855301 -2.2155439 -0.1150068889
2015-11-10 3.7476 -1.570361354 1.4982723 -2.0427547 -0.2722412080
2015-11-11 3.7609 -0.918237575 1.3816348 -2.0541665 -0.3362658339
2015-11-12 3.7710 -0.360032867 1.3504101 -2.0607640 -0.3551769137
2015-11-13 3.8494 0.843071163 1.4118037 -2.0831419 -0.3356690963
2015-11-16 3.8190 -0.015521438 1.4236770 -2.0787380 -0.3275304817
2015-11-17 3.8138 -0.178323100 1.4258628 -2.0775363 -0.3258367293
2015-11-18 3.7648 -1.143696894 1.4293059 -2.0900083 -0.3303512130
2015-11-19 3.7177 -1.970379298 1.4828464 -2.2197173 -0.3684354203
2015-11-20 3.7080 -1.981097208 1.5126446 -2.2783204 -0.3828378971
2015-11-23 3.7323 -1.167446302 1.5122831 -2.2769685 -0.3823426829
2015-11-24 3.6990 -0.878883592 1.4944213 -2.3075962 -0.4065874444
2015-11-25 3.7450 -0.525190572 1.4872415 -2.2424579 -0.3776082126
2015-11-26 3.7430 -1.572016300 1.5061858 -2.3151811 -0.4044976608
2015-11-27 3.8467 0.413647231 1.3089202 -2.2073862 -0.4492330026
2015-11-30 3.8685 -0.446457318 1.2862198 -2.2140888 -0.4639345063
2015-12-01 3.8500 -0.700495892 1.1001380 -2.1573883 -0.5286251230
2015-12-02 3.8366 -0.420377493 1.0255951 -2.1429800 -0.5586924426
2015-12-03 3.7592 -2.162425617 0.9759674 -2.2772443 -0.6506384488
2015-12-04 3.7524 -1.897908529 0.9985287 -2.3799313 -0.6907013108
2015-12-07 3.7680 -0.268156500 0.9857446 -2.3784951 -0.6963752131
2015-12-08 3.7950 0.555521511 1.0620267 -2.4057630 -0.6718681516
2015-12-09 3.7502 -0.023184365 0.9144126 -2.3318293 -0.7087083500
2015-12-10 3.8134 0.280148176 0.9623660 -2.3459535 -0.6917937577
2015-12-11 3.8739 1.567306950 1.1690821 -2.4663714 -0.6486446567
2015-12-14 3.8733 -0.730348778 1.0990804 -2.4651056 -0.6830126049
2015-12-15 3.8710 0.857622984 1.2428005 -2.4821705 -0.6196849915
2015-12-16 3.8816 -1.199396846 1.2380302 -2.5106545 -0.6363121475
2015-12-17 3.8776 0.698808726 1.3420427 -2.4658063 -0.5618818182
2015-12-18 3.9833 1.647828074 1.5809573 -2.5061611 -0.4626018696
2015-12-21 4.0112 1.467803100 1.7670880 -2.5536334 -0.3932726642
2015-12-22 3.9886 1.320477177 1.9037072 -2.4975299 -0.2969113798
2015-12-23 3.9412 1.515472405 2.0679174 -2.4994928 -0.2157877138
2015-12-24 3.9420 1.716289282 2.2424613 -2.5356152 -0.1465769755
2015-12-25 3.9420 1.688648523 2.3379650 -2.5747472 -0.1183910635
2015-12-28 3.8594 -0.997041641 2.3256400 -2.6278568 -0.1511084036
2015-12-29 3.8649 0.188089543 2.3409104 -2.6186997 -0.1388946488
2015-12-30 3.9582 2.046467674 2.5407912 -2.6059028 -0.0325558299
2015-12-31 3.9640 2.582933285 2.7559608 -2.5175183 0.1192212562
2016-01-01 3.9640 2.121572573 2.8674074 -2.3554536 0.2559769155
2016-01-04 4.0391 2.152289688 3.0096318 -2.2763622 0.3666347819
2016-01-05 4.0084 0.971572282 3.0371844 -2.1805511 0.4283166444
2016-01-06 4.0295 1.853863615 3.1410698 -2.1258329 0.5076184506
2016-01-07 4.0441 1.363761742 3.1353008 -1.9243454 0.6054777187
2016-01-08 4.0248 0.890529765 3.1522020 -1.9094544 0.6213738032
2016-01-11 4.0534 -0.078758449 3.1465578 -1.8792969 0.6336304322
2016-01-12 4.0280 -1.184617382 3.1717497 -1.9367636 0.6174930492
2016-01-13 4.0152 -1.383070217 3.2166435 -2.0458369 0.5854032917
2016-01-14 3.9997 -0.606174520 3.1003405 -1.8257838 0.6372783283
2016-01-15 4.0483 -0.481316516 2.9961055 -1.6271094 0.6844980621
2016-01-18 4.0333 -0.880923406 3.0208986 -1.6927537 0.6640724986
2016-01-19 4.0643 -0.447985905 3.0220549 -1.7608104 0.6306222513
2016-01-20 4.0975 -0.728037769 3.0386534 -1.8243991 0.6071271379
2016-01-21 4.1548 0.286489957 3.0387483 -1.8240712 0.6073385306
2016-01-22 4.0934 -0.520269076 2.9749615 -1.8994562 0.5377526630
2016-01-25 4.0892 -0.620436957 2.9710559 -1.8882231 0.5414163904
2016-01-26 4.0514 -1.272880026 2.9848601 -2.0440609 0.4703996234
2016-01-27 4.1104 0.529935516 2.9621082 -1.9060201 0.5280440354
2016-01-28 4.0695 -0.365788116 2.9472741 -1.9621591 0.4925574740
2016-01-29 3.9991 -0.888213736 2.8727627 -2.0567172 0.4080227471
2016-02-01 3.9646 -0.318356773 2.7935068 -2.0965386 0.3484840846
2016-02-02 3.9910 -0.051768597 2.7237469 -2.1182617 0.3027425588
2016-02-03 3.8960 -1.151537473 2.6464162 -2.2187317 0.2138422296
2016-02-04 3.8899 -1.307516034 2.5385930 -2.3124955 0.1130487190
2016-02-05 3.9066 -1.256526416 2.4148118 -2.3850593 0.0148762211
2016-02-08 3.9066 -1.476647149 2.4328659 -2.4350872 -0.0011106292
2016-02-09 3.9066 -1.692138415 2.4456920 -2.5732618 -0.0637848945
2016-02-10 3.9295 -1.081713189 2.2363498 -2.5724650 -0.1680575899
2016-02-11 3.9925 0.316242386 1.9349288 -2.4221567 -0.2436139532
2016-02-12 4.0017 0.804481727 1.7417660 -2.3168000 -0.2875169814
2016-02-15 3.9973 0.596191436 1.5027630 -2.1815368 -0.3393869232
2016-02-16 4.0680 1.653877540 1.6075031 -2.2407900 -0.3166434146
2016-02-17 3.9895 -0.543827126 1.3450478 -2.1381807 -0.3965664393
2016-02-18 4.0294 0.704174652 1.2460574 -2.0831628 -0.4185526757
2016-02-19 4.0219 0.375323631 1.1829967 -2.0544492 -0.4357262135
2016-02-22 3.9459 -1.349452836 1.1682534 -2.1244187 -0.4780826930
2016-02-23 3.9586 -1.076238560 1.1659155 -2.1148556 -0.4744700656
2016-02-24 3.9577 -0.197325598 1.1716429 -2.0415333 -0.4349452450
2016-02-25 3.9565 -0.357598879 1.1788381 -2.0321569 -0.4266593903
2016-02-26 4.0007 -0.053616639 1.1986726 -2.0234781 -0.4124027277
2016-02-29 4.0163 0.173727893 1.2375459 -1.9920413 -0.3772476844
2016-03-01 3.9335 -2.093237169 1.3001281 -2.1643069 -0.4320893932
2016-03-02 3.8945 -2.571784512 1.4049666 -2.3920618 -0.4935476180
2016-03-03 3.7984 -3.290751806 1.5181302 -2.7437082 -0.6127890101
2016-03-04 3.7516 -3.126932239 1.6198580 -3.0192136 -0.6996777821
2016-03-07 3.7850 -2.557596088 1.6519965 -3.1804960 -0.7642497532
2016-03-08 3.7545 -0.586914375 1.6679179 -3.1506863 -0.7413842315
2016-03-09 3.6885 -3.361097208 1.6703319 -3.4125025 -0.8710853223
2016-03-10 3.6271 -2.079012644 1.6430604 -3.4994460 -0.9281928066
2016-03-11 3.5855 -0.526200873 1.6592984 -3.4915498 -0.9161257111
2016-03-14 3.6617 -1.163611935 1.6225427 -3.5111445 -0.9443008832
2016-03-15 3.7674 0.203375949 1.6449376 -3.5165298 -0.9357960650
2016-03-16 3.7422 -0.123362083 1.6946173 -3.4976644 -0.9015235520
2016-03-17 3.6276 -1.615663900 1.6934150 -3.5170053 -0.9117951476
2016-03-18 3.6248 -2.154775060 1.7002553 -3.5837288 -0.9417367690
2016-03-21 3.6182 -2.382728840 1.7156783 -3.6595573 -0.9719394920
2016-03-22 3.5803 -2.011436432 1.7193276 -3.6844931 -0.9825827593
2016-03-23 3.6854 -1.039122091 1.7205765 -3.6829026 -0.9811630560
2016-03-24 3.6771 -0.850682876 1.6376283 -3.6777495 -1.0200605647
2016-03-25 3.6771 -0.499577480 1.5120661 -3.6391245 -1.0635292049
2016-03-28 3.6280 -0.903713591 1.3858570 -3.6129091 -1.1135260391
2016-03-29 3.6383 -0.869539387 1.0760140 -3.4712939 -1.1976399367
2016-03-30 3.6065 -2.522360424 1.0460833 -3.5732654 -1.2635910466
2016-03-31 3.5928 -2.489158382 0.8572505 -3.5973214 -1.3700354811
Code :
library(quantstrat)
library(quantmod)
library(TTR)
library(PerformanceAnalytics)
library(tidyverse)
library(xts)
library(zoo)
###################################### Initial Setup ######################################
initDate <- '2015-06-28'
from <- '2015-06-29'
to <- '2016-03-31'
currency('USD')
Sys.setenv(TZ = 'UTC')
symbols <- "Observed"
stock(symbols, currency = 'USD', multiplier = 1)
Observed <- merge(DfDygraphPredictionObservedComps$Observed, DfDygraphPremium) %>% na.exclude(.)
Observed <- xts(x = Observed, order.by = as.POSIXct(index(Observed)))
Observed <- Observed[2451:nrow(Observed),]
colnames(Observed) <- c("Observed.Close","Premium","Up","Dwn","Avg")
###################################### Initial Setup Backtest ###############################
strat.name <- "FactorModelBT"
portfolio.st <- "FactorModelBT"
account.st <- "FactorModelBT"
rm.strat(strat.name)
rm.strat(portfolio.st)
initPortf(portfolio.st,
symbols = symbols,
initDate = initDate,
currency = 'USD')
initAcct(account.st,
portfolios = portfolio.st,
initDate = initDate,
currency = 'USD')
initOrders(portfolio.st,
initDate = initDate)
strategy(strat.name, store = TRUE)
###################################### Signals ##############################################
# Open Short
add.signal(strat.name,
name = "sigCrossover",
arguments = list(columns = c("Premium", "Up"), relationship = "gt"),
label = "Premium.gt.Upper.Band")
# Close Short
add.signal(strat.name,
name = "sigCrossover",
arguments = list(columns = c("Premium", "Avg"), relationship = "lte"),
label = "Premium.lte.Avg")
# Open Long
add.signal(strat.name,
name = "sigCrossover",
arguments = list(columns = c("Premium", "Dwn"), relationship = "lt"),
label = "Premium.lt.Lower.Band")
# Close Long
add.signal(strat.name,
name = "sigCrossover",
arguments = list(columns = c("Premium", "Avg"), relationship = "gte"),
label = "Premium.gte.Avg")
tmp <- applySignals(strategy = strat.name, mktdata = Observed)
################################ Trading Rules - Actions ###################################
# Open short Position
add.rule(strat.name,
name = "ruleSignal",
arguments = list(sigcol = "Premium.gt.Upper.Band",
sigval = TRUE,
orderqty = -1000,
ordertype = "market",
orderside = NULL,
threshold = NULL),
type = "enter",
label = "OpenShort",
path.dep = TRUE)
# Close short Position
add.rule(strat.name,
name = "ruleSignal",
arguments = list(sigcol = "Premium.lte.Avg",
sigval = TRUE,
orderqty = "all",
ordertype = "market",
orderside = "short",
replace = TRUE),
type = "exit",
label = "CloseShort",
path.dep = TRUE)
# Open Long Position
add.rule(strat.name,
name = "ruleSignal",
arguments = list(sigcol = "Premium.lt.Lower.Band",
sigval = TRUE,
orderqty = 1000,
ordertype = "market",
orderside = NULL,
threshold = NULL),
type = "enter",
label = "OpenLong",
path.dep = TRUE)
# Close Long Position
add.rule(strat.name,
name = "ruleSignal",
arguments = list(sigcol = "Premium.gte.Avg",
sigval = TRUE,
orderqty = "all",
ordertype = "market",
orderside = "long",
replace = TRUE),
type = "exit",
label = "CloseLong",
path.dep = TRUE)
################################ Apply Strategy ############################
out <- applyStrategy(strategy = strat.name, portfolios = portfolio.st)
#set up analytics
updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)
chart.Posn(portfolio.st, 'Observed')
Toute aide sera la bienvenue ! :)