Saturday 23 December 2017

Quantstrat handel forex


Korzystanie z quantstrat. do oceny strategii handlu śróddziennego. ta wersja: 1 Korzystanie z quantstrat do oceny strategii handlu intraday w tej wersji: 2 Spis treści Tworzenie strategii Narzędzia R Luksor strategia: prosta implementacja Optymalizacja parametrów Wyjście i zarządzanie ryzykiem Dalsza analiza Dodatek 3 Przegląd Richard Feynman powiedział, że nauka była procesem po raz pierwszy zgadywania , następnie obliczając konsekwencje tego odgadnięcia (budując model), a na końcu potwierdzając lub odrzucając domysły za pomocą eksperymentu. Dlaczego przeprowadzamy analizę historyczną Co mamy nadzieję osiągnąć Jakie narzędzia wykorzystujemy, tworzą naukowe, oparte na hipotezach podejście do strategii ilościowej 4 Stworzenie strategii: Idea Lejek sformułuj swój pomysł testowy hipotezy na platformie badawczej (R, dane historyczne (np. Reuters)) udoskonalić, jeśli obiecujące (lub nowe pomysły sugerują się podczas testów) odrzucić szybko, jeśli nie obiecujące Zdecydowana większość (90) pomysłów nie zadziała, wykona parametr solidności i podejdzie do testów optymalizacji, gdy masz wiarygodny model badawczy (twój kod kwantowy), implementacja na wdrożenie produkcji Pomysły w strategiach 5 Tworzenie strategii. Testowanie produktu Testowanie produktu może być przeprowadzane zarówno przed, jak i po wdrożeniu produkcji, identyfikując hipotezy dotyczące mechanizmu działania strategii. W jaki sposób twoja strategia sprawia, że ​​pieniądze generują listy instrumentów, które mogą podlegać tej samej metodzie testu działania Instrumenty docelowe zaczynają się od standardowych parametrów lub zakresów parametrów uruchom Parameter Solidność i Walk Forward Optimization na obiecujących aktywach wdrażają nowe instrumenty do produkcji 6 Strategia poprawy Proces nie ma strategii dopóki nie wyłączysz zbierania pomysłów na ulepszenie strategii formalizacji Hipotezy z pomysłów określają testy, które pokażą ulepszenie w oparciu o hipotezę określają kryteria sukcesu dla mierzenia wyników hipoteza poprawy kodu w referencyjnej realizacji badań strategii wykonują standardowe optymalizacje na instrumenty obecnie sprzedawane w ramach strategii Ocena wydajności: jeśli wydajność próby nie poprawi się, zmiany w kodzie zostaną wprowadzone do produkcji Zapoznaj się z hipotezą Określ testy Zastosuj testy Poprawa wydajności Tak Zastosuj do strategii Nr 7 Narzędzia R Jak to zrobić 8 Symulacja handlu Łańcuch narzędziowy M anage Data Evaluate Data Determine Trades Size Transakcje Oblicz wydajność Performance Analyze Performance Określ specyfikacje kontraktów Oblicz zwroty i ryzyko Transakcje przechwytywania Porównaj z benchmarkami Oblicz pozycje Dostarcz atrybucję Oblicz PampL Analyze risk Rodzaje działań Połącz z bazą danych Oblicz wskaźniki Prognozuj pretrade ceny Optymalizuj portfolio Pobierz dane historyczne Przekształć ceny Prognoza zwrotu Ryzyko budżetowe Niepewność prognozy Ryzyko prognozy Czyste i wyrównane dane Wskaźniki i wskaźniki wykresu Obliczanie wolumenu końcowego Obliczanie reguł Generowanie sygnałów Obliczanie pozycji docelowej Oblicz wielkość handlu Oceń koszty transakcyjne Zbiorcze portfolio Przykład R Pakiety indeksowania ilościowego RTAQ xts. TTR signalextraction urca. quantstrat quantmod LSPM PortfolioAnalytics parma blotter Instrument finansowy Wydajność Analityka 9 quantstrat Wskaźniki Sygnały Reguły - Ilościowa wartość pochodząca z danych rynkowych - Stosowana w wektoryzacji lub w modzie strumieniowej - Zakładana, że ​​można ją obliczyć w sposób niezależny od rodzaju - Brak znajomości aktualnej pozycji lub transakcji - Przykłady: średnie kroczące, pasma zmienności, RSI, MACD, kanały, dowolne wskaźniki analizy technicznej - opisz interakcje między danymi rynkowymi i wskaźnikami - opisz potencjalną potrzebę działania, ale może nie być możliwe do zastosowania - zastosowane w wektoryzacji lub strumieniu informowanie o decyzjach dotyczących zasad Przykłady: zwroty, progi, wielokrotności - wyceniane w sposób zależny od ścieżki - udostępniają wszystkie dane rynkowe przed bieżącą obserwacją - znają aktualną pozycję w czasie oceny - generują zlecenia wejścia, wyjścia i zarządzania ryzykiem - maj wprowadź nowe zamówienia lub zmodyfikuj istniejące zamówienia - Typy: Wejście, Wyjście, Ryzyko, Rebalance, Chain Des wyznaczone i wykorzystywane do rzeczywistych strategii ilościowych na wszystkich częstotliwościach Wiele strategii może być konstruowanych ze wszystkich komponentów Open Source. Własne strategie dodają własne wskaźniki. Funkcje sygnałowe. Zamów logikę rozmiarów. 10 Instalacja pakietu quantstrat, quantmod, Financial Instrument, xts cran quantstrat (jeszcze) dostępnych na cranu tarzip: svn svn svn: r-forge. r-project. orgsvnrootblotter githubmilktraderblotterwikiinstallationide-for-windows Lista mailingowa: 11 Strategia Luxor Prosta implementacja Zobacz: Systemy transakcyjne: Nowe podejście do rozwoju systemu i optymalizacji portfela Emilio Tomasini, Urban Jaekle - Harriman House London ISBN (s). 12 O kodzie Nie wycinaj i nie wklejaj kodu z tych slajdów najbardziej aktualny kod dla tych funkcji znajduje się w repozytorium pakietów na R-Forge, a Luxorowe demo zostanie utrzymane. Znajdź skrypty w katalogu demo quantstrat jest nadal w fazie rozwoju, a to będzie nadal zmieniaj się w małych (a czasem dużych) sposobach kompatybilność wstecz jest ważna, ale nie będziemy poświęcać cech dla kompatybilności 13 quot StrategiaLuxorquot Dane symbolu: GBPUSD 30 min OHLC Przekraczanie 2 SMA: SMA n10 (quotfastquot) SMA n30 (quotslowquot) Logika wejścia : Long: limit stopu na wysokim pasku przekroczenia SMA Short: limit stopu na dole poprzeczki SMA 14 Instrument finansowy Financial Instrument przechowuje metadane instrumentu. Zdefiniuj instrumenty, tj. waluty: wymagają waluty (instrumentu finansowego) (c (gbp, USD)) albo wymieniają (primaryidgbpusd, ticksize0.0001) lub exchangerate (currencyusd, countercurrencygbp, ticksize0.0001) w środowisku produkcyjnym, najbardziej chciałabyś użyć saveinstruments () i loadinstruments (), aby ponownie użyć całego środowiska instrumentu 15 Wyszukiwanie danych symbolu data. dir lt - R. symbols na przykład. z powodu - ltto. tv-lubin. cs (Symbole GBPUSD, dirdata. dir, from. from, to. to) lub (bardziej ogólne) setsymbollookup. fi (basedirdata. dir, SymbolsGBPUSD) getsymbols (gbpusd, from. from, to. to) lub (quantstratdemo) getsymbols. fi (SymbolsGBPUSD, dirsystem. file (extdata, packagequantstrat), from. from, to. to) 16 xts GBPUSD obiekt danych gt getsymbols. fi (symbolsgbpusd, dirsystem. file (extdata, packagequantstrat), od, do) loading GBPUSD. nie potrzebujemy getsymbols (), wszystko to można zrobić za pomocą save () i load () skoryguj zmiany okresowości skala czasowa dopasuj (opcjonalnie) odczyt GBPUSD. rda. Gotowe. Czytanie GBPUSD. rda. Gotowe. lt. gt Czytanie GBPUSD. rda. Gotowe. Czytanie GBPUSD. rda. Gotowe. dane rbinding. Gotowe. 1 quotGBPUSDwuk GBPUSD do. minut30 (gbpusd) gt GBPUSD align. time (gbpusd, 1800) gt head (gbpusd) Otwórz Wysoko Niski Zamknij Wolumin: 30. 00. 30. 00. 30. 00: Twoje dane mogą się różnić, ale formatowanie ich zgodnie z is. ohlc () lub is. bbo () z quantmodu ułatwi Ci życie 17 Tworzenie nowej strategii obiektu strategii (nameluxor, storetrue).strategy obiekt strategii ochrony środowiska (storetrue) porządek obiektu strategii komponenty wskaźniki sygnały reguły paramsets tworzy obiekt mktdata 18 Obiekt mktdata arkusz kalkulacyjny xts, strategia scratchpad początkowo: dane symbolu z portfolio gt head (gbpusd) Open High Low Close Tom: 01. 02. 03. 04. 05. 06: wskaźniki będą dodawać kolumny sygnały będą dodawać kolumny użytkownik może dodawać kolumny 19 quantstrat Wszystkie quantstrat jest modułowy Wskaźniki - Ilościowa wartość pochodząca z danych rynkowych - Stosowana w wektoryzacji lub strumieniu mody - Zakłada się, że jest w stanie do obliczenia w sposób niezależny od branży - Brak znajomości aktualnej pozycji lub transakcji - Przykłady: średnie kroczące, pasma zmienności, RSI, MACD, kanały, dowolne wskaźniki analizy technicznej Sygnały - Opis interakcji między danymi rynkowymi i wskaźnikami - Opisać możliwe pragnienie działania , ale może nie być możliwe do zastosowania - Zastosowane w wektoryzacji lub strumieniującym Wykorzystywane do informowania o decyzjach dotyczących zasad Przykłady: Zwrotnice, Progi, Zasady wielokrotności - Oceniane w sposób zależny od ścieżki - Mają dostępne wszystkie dane rynkowe przed bieżącą obserwacją - Są świadome bieżących pozycja w czasie oceny - Generowanie zleceń wejścia, wyjścia i zarządzania ryzykiem - Może wprowadzać nowe zamówienia lub modyfikować istniejące zamówienia - Typy: Wpis, Wyjście, RIS k, Rebalance, Chain Właściwość name dla większości funkcji quantstrat nazywa funkcję, która ma zostać wywołana, Wykorzystuje przewagę opóźnionego wykonania. Twoja specyfikacja strategii może dotyczyć wielu różnych instrumentów lub ram czasowych. 20 Wskaźniki Wskaźnik: obliczenia matematyczne oparte na cenie i woluminie bezpieczeństwa (Investopedia), np. MA, MACD, RSI, wskaźniki niestandardowe Pakiet TTR zawiera wiele klasycznych wskaźników, np. MA, MACD, RSI itp. Wiele autorskich strategii wykorzystuje własne wskaźniki 21 quantstrat :: add. indicator () add. indicator (strategia luxor, nazwa SMA, lista argumentów (x quote (cl (mktdata), 1), n ​​10), labelquotnfastquot ) SMA (), EMA () RSI () stoch () data add. indicator (strategia luxor, nazwa SMA, lista argumentów (x quote (cl (mktdata), 1), n ​​30), labelquotnslowquot) nazwa wskaźnika, np. . Wskaźniki TTR używają obiektu mktdata Zastosuj funkcję Cl (): pierwsza kolumna o nazwie Zamknij cytat (): oceń później etykietę mktdata nazwa kolumny 22 Sygnały Investopedia: Znak, zwykle oparty na wskaźnikach technicznych, że jest to dobry czas na kupno lub sprzedaż szczególności bezpieczeństwo Quantstrat: Signals oznaczają czasy, w których strategia może chcieć podjąć działanie. na przykład sigcrossover (), sigthreshold (), sigformula () 23 quantstrat :: add. signal () add. signal (strategialuxor, nazwyigcrossover, lista argumentów (columnsc (quotnfastquot, quotnslowquot), relationshipquotgtquot), labellong) add. signal (strategyluxor, namesigcrossover , lista argumentów (columnsc (quotnfastquot, quotnslowquot), relationshipquotltquot), labelshort) przykładowe funkcje: sigcrossover () sigthreshold () sigcomparison () sigformula () sigpeak () argumenty: column relationship, quotgtquot, quotltquot, quoteqquot, etc nazwa kolumny etykiety do miejsce w mktdata 24 Reguły reguł są zależne od ścieżki, mają dostęp do bieżącego rynku i stanu portfela zwykle wyzwalanego przez reguły reguł typów ryzyka Zrównoważyć kolejność wyjść Reguły łańcucha wejściowego są punktem, w którym strategia może wprowadzić lub anulować zlecenie 25 quantstrat :: add. rule () add. rule (strategia, funkcja do zastosowania, lista argumentów dla funkcji, aby zastosować typ, etykieta) funkcja: zwykle ruleignal () typ reguły: enter, exit, risk. label: pojawia się w książce zamówień, jeśli nie używasz sygnatury standardowej, użyj funkcji z podobnym podpisem argumentu, aby skorzystać z redukcji wymiarów 26 quantstrat :: rulesignal () ruleignal (sigcollong, sigvaltrue, ordersidelong, ordertypestoplimit, preferhigh, threshold0.0005, orderqty100000 , replacefalse) ordertypes: limit stoplimit zdzierżuje transakcje rynkowe na następnych barobach. domyślnie preferclose góry lodowe wolą: kolumna cenowa do wykorzystania od progu obiektu danych: dodana do ceny 27 add. rule () - luxor entry rules add. rule (strategyluxor, namerulesignal, argumentslist (sigcollong, sigvaltrue, ordersidelong, ordertypestoplimit, preferhigh, threshold. threshold , orderqty. orderqty, replacefalse), typeenter, labelenterlong) add. rule (strategyluxor, namerulesignal, argumentslist (sigcolshort, sigvaltrue, ordersideshort, ordertypestoplimit, preferlow, threshold-.threshold, orderqty-.orderqty, replacefalse), typeenter, labelentershort) stoplimit zlecenie przy użyciu Highthreshold (Low-threshold) jako ceny granicznej nie chcemy zastępować żadnych oczekujących zleceń wyjścia 28 add. rule () - luxor exit rules add. rule (strategy. st, namerulesignal, argumentslist (sigcollong, sigvaltrue, ordersideshort, ordertypemarket , orderqtyall, TxnFees. txn. fees, replaceetrue), typeexit, labelexit2long) add. rule (strategy. st, namerulesignal, argumentslist (sigcolshort, sigvaltrue, ordersidelong, ordertypemarket, orde rqtyall, marketall TxnFees tylko na regułach wyjścia: koniec znacznika handlu dla tradestats () zamień wszelkie oczekujące (otwarte) zlecenia wejścia TxnFees. txn. fees, replaceetrue), typeexit, labelexit2short) na przemian, użyj metody methodtrade dla tradestats () 29 quantstrat :: applystrategy () gt applystrategy (strategy. st, portfolio. st) 1 quot: 00: 00 GBPUSD quot It Transakcje 1 quot: 30: 00 GBPUSD quot 1 quot: 30: 00 GBPUSD quot 1 quot: 00: 00 GBPUSD quot 1 quot : 00: 00 GBPUSD quot 1 quot: 30: 00 GBPUSD quot 1 quot: 00: 00 GBPUSD quot 1 quot: 30: 00 GBPUSD quot 1 quot: 00: GBP GBP quot 1 quot: 00: 00 GBPUSD 1,553quot 1 quot: 00:00 GBPUSD 1,553 kwot 1 quot: 00 GBP PLNUSD quot 1 quot: 30: 00 GBPUSD quot 1 quot: 30: 00 GBPUSD quot 1 quot: 30: 00 GBPUS 1,555 kw. 1 quot: 00: 00 GBPUSD quot 1 quot: 30:00 GBPUSD quot Brak transakcji w tym miejscu prawdopodobnie oznacza, że ​​masz problem 30 Obiekt mktdata: gt Wyświetl (mktdata) 31 quantstrat :: getorderbook () gt Wyświetl (getOrderBook (portfolio. st) forex forexgbpusd) 32 blotter :: updateportf () updat eportf (portfolioportfolio. st, SymbolsGBPUSD, Datespaste (::, as. Date (Sys. time ()), sep)) gt paste (::, as. date (sys. time ()), sep) 1 quot. quot updateportf () oznacza księgę portfela z różnymi miarami rachunkowości, jeśli pracujesz z danymi o wysokiej częstotliwości, powszechne jest oznaczanie książki na niższych częstotliwościach wybór częstotliwości do oznaczenia książki nie zmienia żadnych danych handlowych, które mogą nadal będzie wyższa częstotliwość 35 bibularek :: tradestats () gt Widok (t (tradeStats (forex))) tradestats () oblicza statystyki powszechnie spotykane w raportach strategii z książek, a transakcje na platformach wykonawczych są obliczane płasko na płaskie dla tego raportu, przedstawiając pełne w obie strony wszystkie statystyki są objaśnione w całości w dokumentacji 36. bibuła :: pertradestats () pertradestats () zbiera statystyki dotyczące transakcji typu flat-to-flat, które są wywoływane przez charts. me dla danych surowych, które te wykresy czerpią z tego, że możesz użyć tych pojedynczych transakcji jako wejście do analizy Monte Carlo twojej strategii 37 quantstrat :: save. strategy () Zapisz strategię, abyśmy mogli załadować ją później i dodać więcej komponentów lub zastosować do różnych scenariuszy save. strategy (strategy. st) ls - l luxor. rdata - rw-r - r-- 1 jan users Apr 7 22:53 luxor. rdata load. strategy (strategy. st) 38 quantstrat Optymalizacja parametrów 39 Parametr Optymalizacja wszystkich strategii ma parametry: Jakie są właściwe Większa liczba wolnych parametrów znacznie zwiększysz ilość potrzebnych danych, obniżając stopnie swobody i zwiększając szansę na przeuczenie nawet przy wyborze początkowych parametrów optymalizacji, wybrane wartości, które według ciebie mogą być optymalną optymalizacją parametrów, po prostu dodają proces i narzędzia do twojej decyzji inwestycyjnej 40 Parametr ustawia Parametry strategii optymalizacji Zestawy parametrów są częścią strategii paramset. label Dystrybucje SMA Zakres FastSMA od 1 do 30 Zakres SlowSMA od 20 do 80 ograniczeń FastSMA lt SlowSMA apply. paramset () wywołuje applystrategy () losowy wybór lub wszystkie kombinacje 41 Optymalizacja Luksorowych wartości SMA. fastsma (1:30).SlowSMA (20:80) add. constraint (s, paramset. label SMA, distribution. label.1 nfast, load. strategy (strategia. st) distribution. label.2 nslow, operator lt, add. distribution (strategy. st, label SMA) paramset. label SMA, wskaźnik component. type, save. strategy (strategy. st) component. label nfast, lista zmiennych (n. FastSMA), label nfast) add. distribution (strategy. st, paramset. label SMA, wskaźnik component. type, component. label nslow, chcę usunąć listę zmiennych paramset (n. SlowSMA), label nslow) delete. paramset (strategy. st, SMA) 42 foreach package hybrid standardu dla funkcji pętli i lapply ułatwia równoległość zwraca funkcję valuebine default a lista niektórych równoległych backendów: registerdoseq (): default backend (sequential) domcdoparallel: wiele procesorów lub rdzeni doredis: backend między platformami serwer Redis Redis workers 43 apply. paramset () wymagają (domc) registerdomc (cores8) load. strategy (strategy. st) wyników lt - apply. paramset (strategy. st, paramset. labelsma, portfolio. stportfolio. st, nsamples 80, audit NULL, verbosetrue) print (resultstradestats) ) tworzy parambos z dystrybucji i stosuje się ograniczenia strategia () dla portfela dla każdego parametru. combo nsamples: losuje audyt wyboru losowego: nazwa pliku do przechowywania wszystkich portfeli i portfeli zamówień 44 Tradycje SMA paramset () Widok (t (resultstradeStats)) 45 quantstrat :: tradegraphs () pokazuje wykresy handlu ze statystyk tradegraphs (statystyki statystyk, free. params c (quotParam. indicator.1.nFastquot, quotParam. indicator.2.nSlowquot), statystyki c (quotNet. Trading. PLquot, quotmaxdrawdownquot, quotAvg. Trade. PLquot, quotNum. Tradesquot, quotProfit. Factorquot), tytuł Luxor SMA Parameter Scan) tworzy trójwymiarowy rendering powierzchni parametru idealnie, strategia będzie parametrem-solidna względnie gładka brak dużych pików na podstawie małych zmian tradegraphs () może generować wiele wykresów za pomocą pojedynczego połączenia wymaga pakietu rgl 47 quantstrat Zarządzanie wyjściem i ryzykiem 48 Zamówienie Zestaw zestawów składający się z jednego lub większej liczby zamówień OCO OCO - jeden anuluje drugi, jeśli jeden zostanie wypełniony, wszystkie inne zostaną anulowane identyfikowane przez znacznik nazwy zestawu zamówień, np. zlecenie Stop-Loss zlecenie zlecenia typu "Take-Profit" (ordersetocolong.) zwykle w połączeniu z łańcuchem zamówień 49 Zamówienie Łańcuchy reguła typechain wypełnienie jednego zamówienia tworzy nowe zlecenia Kolejność nadrzędna identyfikowana przez quotlabelquot zamówienia podrzędne używają parametru quotparentquot w odniesieniu do nadrzędnego zwykle połączone z zestawami zleceń 50 Zestawy zamówień amp Zamówienie zleceń zleceń z limitem zatrzymania Zamówienie zleceń dla zleceniodawców Order order stoplimit order quotstop lossquot limit limitów zlecenie quotstop końcowe zlecenie limitów quottake profitquot 51 Reguła dla Stop-loss add. rule (strategy. st, name rulesignal, argumentslist (sigcollong , sigvaltrue, replacefalse, ordersidelong, ordertypestoplimit, tmulttrue, thresholdquote (.stoploss), TxnFees. txnfees, orderqtyall, ordersetocolong), typechain, parententerlong, labelstoplosslong, enabledfalse) add. rule (strategy. st, nazwa ruleignal, argumentslist (sigcolshort, sigvaltrue , replacefalse, ordersideshort, ordertypestoplimit, tmulttrue, thresholdquote (.stoploss), TxnFees. txnfees, orderqtyal l, ordersetocoshort), typechain, parententershort, labelstoplossshort, enabledfalse) próg rodzicielski rodzica próg tmult próg quote () włączony zamień musi być FALSE (zestawy zleceń) 52 Order Sizing Order Sizing, podobnie jak wszystko inne w quantstrat, jest modułowe. Możesz pisać własne funkcje sortowania zamówień. Zawarte są: ruleignal () zawiera argument osfun osnoop () no operation, domyślny osmaxpos () implementuje proste poziomy oparte na maksimum działa z addposlimit () i getposlimit () 53 addposlimit () amp osmaxpos () addposlimit (portfolioportfolio. st, symbolgbpusd , timestampinitdate, maxpos. orderqty oddziela maksymalne pozycje od reguł) add. rule (strategia. st, nazwa ruleignal, argumentslist (sigcollong, sigvaltrue, replacefalse, ordersidelong, ordertypestoplimit, preferhigh, podejmuje decyzje w oparciu o bieżącą pozycję, nie otwiera progu zamówień. próg, TxnFees0, orderqty. orderqty, potrzebne z zestawami zamówień osfunosmaxpos, ordersetocolong), typeenter, timespan. timespan, labelenterlong) Możliwe są inne funkcje sortowania zamówień, osmaxpos () jest przykładem 54 Optymalizacja handlu podchodzi do paramsetów, a podejście oparte na quotbrute forcequot obejmuje dużą część całkowitej przestrzeni parametrów MAEMFE - liczba matematyczna, kwantyle - posłuż się statystykami, wykorzystaj dane z testu historycznego lub procesu produkcyjnego do oceny tam, gdzie można dodawać zlecenia stop loss lub take profit, idź naprzód użyj funkcji celu, aby zmienić parametry w czasie 55 Stop loss za pomocą paramsets. stoploss seq (0.05, 2.4, length. out48) 100 require (foreach) require (domc) add. distribution (strategia. st, registerdomc (cores8) paramset. label StopLoss, łańcuch component. type, component. label StopLossLONG, lista zmiennych (próg. StopLoss), wyniki lt - apply. paramset (strategy. st, paramset. labelstoploss, portfolio. stportfolio. st, account. staccount. st, nsamples80, verbosetrue) label StopLossLONG plot (100resultstradestatsstoplosslong, statsnet. trading. pl, typeb, xlabstoploss, ylabnet. trading. pl, mainluxor)) add. distribution (strategy. st, paramset. etykieta StopLoss, łańcuch component. type, component. label StopLossSHORT, lista zmiennych (próg. StopLoss), etykieta StopLossSHORT) add. constraint (strategia. st, paramset. label StopLoss, distribution. label.1 StopLossLONG, distribution. label.2 StopLossSHORT , operator, etykieta StopLo ss) 56 Tradycyjne tarcze stop-loss () (t (resultstradeStats)) 57 MAE: Max Adverse excursion chart. me (Portfolioforex, SymbolGBPUSD, typemae, scalepercent) notowane na osi diagonalnej zamkniętej na najniższych obrotach Transakcje na osi pionowej nie podlegały wypłacie Chart. ME typ MAE MFE skala gotówki procent tyka 58 MAE z 0,4 stop-loss 59 MFE: Max Favorable Excursion chart. me (portfoliooluor, SymbolGBPUSD, typemfe, scalepercent) handluje na osi diagonalnej zamkniętej na swoich transakcjach highs na osi pionowej doświadczonej brak pozytywnej PULL pozioma linia pokazuje wpływ zatrzymania w tym zbiorze danych 60 Take-Profit za pomocą paramsetów (100statstakeprofitlong, statsnet. trading. pl, typeb, xlabtakeprofit, ylabnet. trading. pl, mainluxor) fabuła (100statstakeprofitlong, statsmax. drawdownl, typeb, xlabmax. drawdown, ylabmax. drawdown, mainluxor) 61 PerformanceAnalytics :: chart. Histogram gt ps lttradestry (forex, SymbolGBPUSD) gt chart. histogram (pspct. trading. pl, methodsc (add. normal), mainluxor re skręty) Zauważaj szczyty: Stop Strata Zysk Zysk inny obs: Ujemny skos Nadmiar pozytywnej kurtozy 62 bibularz: tradequantiles () gt tradequantiles (forex, SymbolGBPUSD, scalepercent, probsc (.5, .9, .99)) forex. gbpusd pospctpl 50 pospctpl 90 pospctpl 99 negpctpl 50 negpctpl 90 negpctpl 99 pospctmfe 50 pospctmfe 90 pospctmfe 99 pospctmae 50 pospctmae 90 pospctmae 99 negpctmfe 50 negpctmfe 90 negpctmfe 99 negpctmae 50 negpctmae 90 negpctmae 99 tradequantiles () oblicza i zwraca statystyki dla kwantyli pozytywnych i negatywnych PampL MAE MFE MAE max line przechwytuje szczyt rozkładu (z perfekcyjną perspektywą) MAE max (cumPL) Inne opcje obejmują kwantyle jako ticks lub gotówkę PampL 63 quantstrat Walk Forward Analysis 64 Walk Forward Analysis Analiza spacerów do przodu okresowo reparametryzuje strategię w produkcji, może to być wykonywane codziennie, co tydzień, co miesiąc lub co kwartał, analiza krocząca jest najbardziej rozpowszechniona, zakłada ewolucję zakotwiczonej analizy przestrzeni parametrycznej zakłada, że Informacje z wcześniejszych okresów pomagają w podejmowaniu decyzji 65. walk. forward () ess lt-function (account. st, portfolio. st) my. obj. func lt - function (x) walk. forward (strategy. st, paramset. labelwfa, portfolio. stportfolio. st, account. staccount. st, periodmonths, k. training3, k. testing1, obj. funcmy. obj. func, obj. argslist (xquote (resultapply. paramset)), user. funcess, user. argslist (account. staccount. st, portfolio. stportfolio. st), audit. prefixwfa. ples, anchoredfalse) chart. posn (portfolio. st) View (t (tradeStats (portfolio. st))) funkcja celu okresu paramset (działa na master) opcjonalna funkcja użytkownika (działa na slave) audit. prefix zakotwiczony 66 walk. forward () wykresy chart. forward. testing (wfa. trading. pl. results. rdata) RData) chart. forward. testing (wfa. max. drawdown. wyniki. 67 chodzić. do przodu () przy użyciu wyboru ES z funkcji celu jest bardzo ważne, aby przejść do przodu analizy w tym przypadku, funkcja celu wybiera raczej słabe parametry out-of-sample pogoni za najwyższy przykład End. Eq nie działa. 68 Dodatek Inne rzeczy, których prawdopodobnie nie mamy czasu na 69 Prędkość: przyspieszenie prędkości kwantyfikatorów jest prawie liniową funkcją liczby ocen reguł, co oznacza, że ​​należy uważać, aby nie dodawać niepotrzebnych reguł, należy również rozważyć wstępne przetworzenie sygnałów za pomocą operacji logicznych i typu możliwe jest uzyskanie prędkości 1 rdzenia minuty na symbol dziennie na danych wysokiej częstotliwości. 70 Analizowanie wartości zwracanych Quantstrat (i bibularz) działa w wartościach cenowych i przestrzeni handlowej Czasami trzeba wykonać analizę w funkcji quasi-zwrotnej funkcji ilościowej PortfReturns () generuje zwroty z kapitału własnego Pamiętaj, że jest to wrażliwe na kapitał początkowy, patrz przykłady (demo (rsi) i demo (faber) na przykładach, sprawia, że ​​wszystkie analizy oparte na dochodach oparte na pakietach, takie jak PerformanceAnalytics lub PortfolioAnalytics, są dostępne. 71 Ponowne równoważenie sposobu alokacji kapitału pomiędzy różne instrumenty w strategia okresowo stosuje zasadę równoważenia ilościowego, która obejmuje rebalans typu specjalnej reguły i specjalną wersję applystrategy (). applystrategy. rebalancing () przykładowe reguły rebalance rulepctequity i ruleweight są zapewnione zobacz demo (faberrebal) na przykład alokacja na strategie prawdopodobnie wymaga PortfolioAnalytics, nie tylko rebalancing rules 72 Dalsze prace nad którymi pracowali lub chcieliby skompilować applyrules przesuwać pętlę stanu state-machine do szybszej kompilacji strategii inicjowania kodu i zawijania cięciw po kodzie standardowym zmniejszyłoby duplikację kodu np sparametryzowana analiza losowych transakcji w stylu Monte Carlo lub Burnsa uogólniony cel do dalszej analizy Encyklopedia strategii handlowych angażuje się w tworzenie demówek, które możemy uwzględnić w pakiecie 73 Wnioski zmierzają do naukowego podejścia do analizy historycznej: zgadnij, zbuduj model, przetestuj swoją hipotezę poprzez eksperyment overfitting Niebezpieczeństwo polega na tym, aby jak najwięcej swoich założeń, jak to tylko możliwe, było wyraźną, a nie niejawną testem historycznym, który sprawia, że ​​pieniądz daje ci możliwość zarobienia pieniędzy na produkcji za pomocą twojego modelu, przegrany test historyczny jest prawie gwarancją większych strat produkcyjnych. 74 Podsumowanie Tworzenie strategii R narzędzia Strategia Luksor: prosta implementacja Optymalizacja parametrów Wyjście i zarządzanie ryzykiem Dalsza analiza 75 Dodatek Dziękujemy za uwagę 76 Bios Jan Humme Niezależny inwestor finansowy komputer i programista pakietu Quantstrat współtwórca Brian G. Peterson Partner i dyrektor Quantitative Trading w DV Trading w Chicago autor lub co-au thor ponad 10 pakietów R dla finansów regularnie pisze i mówi o budowie portfela i ryzyku 77 Zasoby R-Forge r-forge. r-project. orgprojectsblotter R-SIG-Finance lista mailingowa stat. ethz. chmailmanlistinfor-sig-finance Blogi github, StackOverflow, itp. Us 78 Odsyłacz do slajdów Najnowsze slajdy dostępne na Dysku Google tutaj: docs. googlepresentationd1fgzdclffcqjkhhzaonspux1ttm1eb5hlvcedsz7zwpubThis post przedstawia mechanizm automatycznej korekcji periodogramu Johna Ehlers8217s przeznaczony do dynamicznego wyszukiwania okresu ważności. Oznacza to, że najczęstszym parametrem zoptymalizowanym w testach historycznych jest okres ważności. Zanim zacznę ten post, muszę przyznać kredyt tam, gdzie jest należny, jednemu panu Fabrizio Maccallini. szef strukturyzowanych instrumentów pochodnych na Nordea Markets w Londynie. Możesz znaleźć resztę repozytorium, które zrobił dla Dr. John Ehlers8217s Cycle Analytics for Traders na swoim githubie. Jestem wdzięczny i zaszczycony, że takie inteligentne i doświadczone osoby pomagają wprowadzić niektóre metody Dr. Ehlers8217 do R. Punkt Periodogramu autokorelacji Ehlersa polega na dynamicznym wyznaczaniu okresu między minimalną a maksymalną długością okresu. Chociaż pozostawiam dokładne wyjaśnienie mechaniki w książce Dr. Ehlers8217s, dla wszystkich praktycznych intencji i celów, moim zdaniem, punchline tej metody jest próba usunięcia ogromnego źródła przeuczenia z tworzenia systemu handlu82, mianowicie określając okres ważności. SMA 50 dni 100 dni 200 dni Cóż, ten algorytm wykorzystuje możliwość nadrobienia z rąk. Po prostu określ górną i dolną granicę dla swojego podsumowania, a resztę wykonasz. Jak to działa, jest to temat dyskusji dla osób dobrze znających metodykę elektrotechniki (nie jest to I8217m), więc nie krępuj się, zostawiając komentarze, które omawiają, jak dobrze algorytm wykonuje swoją pracę, i nie krępuj się pisać o tym jako dobrze. W każdym razie tutaj jest oryginalny kod algorytmu, dzięki uprzejmości pana Maccalliniego: Jedną rzeczą, którą zauważam jest to, że ten kod używa pętli, która mówi (i in 1: length (filt)), która jest O (punkty danych) Pętla, którą widzę jako dżumę w R. Chociaż I8217ve używał Rcpp wcześniej, it8217 był tylko dla najbardziej podstawowych pętli, więc jest to z pewnością miejsce, w którym algorytm może być poprawiony z Rcpp z powodu słabej pętli R8217s. Ci, którzy interesują się dokładną logiką algorytmu, znajdą go ponownie w książce "Cycle Analytics dla traderów" Johna Ehlers8217s (zobacz link wcześniej w poście). Oczywiście, pierwszą rzeczą do zrobienia jest sprawdzenie, jak dobrze algorytm robi to, co chce zrobić, czyli dyktować okres ważności algorytmu. Let8217s uruchomi to na niektórych danych. Teraz, jak wygląda okres ważności ustawiony przez algorytm? Let8217s powiększają się od 2001 do 2003 roku, kiedy rynki przeszły pewne wstrząsy. W tym powiększonym obrazie widzimy, że oszacowania algorytmu8217 wydają się dość skoczne. Poniżej przedstawiono kod służący do obliczenia wartości szacunkowej algorytmu liczby n w indeksie w celu obliczenia wskaźnika z dynamicznym okresem ważności określonym przez EHlers8217s periodogram autokorelacji. A oto funkcja zastosowana z SMA, aby dostroić od 120 do 252 dni. Jak widać, ten algorytm jest mniej spójny niż chciałbym, przynajmniej jeśli chodzi o użycie prostej średniej ruchomej. Na razie I8217m opuści ten kod tutaj i pozwoli ludziom eksperymentować z nim. Mam nadzieję, że ktoś odkryje, że ten wskaźnik jest dla nich przydatny. Dziękuje za przeczytanie. UWAGI: Zawsze interesuje mnie networking-up-up na północnym wschodzie (PhiladelphiaNYC). Ponadto, jeśli uważasz, że Twoja firma skorzysta z moich umiejętności, nie wahaj się skontaktować ze mną. Mój profil linków można znaleźć tutaj. Wreszcie, jestem wolontariuszem, aby zwołać sekcję R dla książek o kwantokracji. Jeśli masz książkę o R, która może ubiegać się o finansowanie, poinformuj mnie o tym, abym mógł ją przejrzeć i ewentualnie ją polecić. Thakn you. Ten post będzie dogłębną recenzją książki Quantitative Momentum autorstwa Alpha Architect8217s. Ogólnie rzecz biorąc, moim zdaniem książka jest wspaniała dla tych, którzy są praktykami w zarządzaniu funduszami w indywidualnej przestrzeni równości i wciąż zawiera pomysły warte przemyślenia poza tą przestrzenią. Jednak system wyszczególniony w książce korzysta z zagnieżdżonego rankingu (pozycja wzdłuż osi X, weź górny decyl, ranguj wzdłuż osi Y w górnym decile w X, i weź górny decyl wzdłuż osi Y, zasadniczo ograniczając wybór do 1 z wszechświat). Co więcej, książka nie robi zbyt wiele, aby dotknąć kontroli zmienności, co mogło znacznie wzmocnić system. Zanim przejdę do tego postu, chciałbym powiedzieć moim czytelnikom, że sformalizowałem swoje nakrętki i śruby serii postów w formacie quantstrat jako formalny kurs datacamp. Datacamp to bardzo tani sposób poznania pakietu R, a aplikacje finansowe są wśród tych tematów. Mój kurs obejmuje podstawy programu quantstrat, a jeśli ci, którzy ukończą ten kurs, mogą bardzo dobrze tworzyć bardziej zaawansowane moduły kwantowe na datacampie. Mam nadzieję, że kursy finansowe są dobrze przyjmowane, ponieważ w R8282d są tematy finansowe, jak się przekonać, że 45-minutowa wykłada naprawdę nie wystarczy (tak jak dr David Matteson8217s zmienia magiczne punkty, PortfolioAnalytics, i tak dalej). W każdym razie tutaj jest link. So, let8217s start with a summary of the book: Part 1 is several chapters that are the giant expose - of why momentum works (or at least, has worked for at least 20 years since 1993)8230namely that human biases and irrational behaviors act in certain ways to make the anomaly work. Then there8217s also the career risk (AKA it8217s a risk factor, and so, if your benchmark is SPY and you run across a 3 year period of underperformance, you have severe career risk), and essentially, a whole litany of why a professional asset manager would get fired but if you just stick with the anomaly over many many years and ride out multi-year stretches of relative underperformance, you8217ll come out ahead in the very long run. Generally, I feel like there8217s work to be done if this is the best that can be done, but okay, I8217ll accept it. Essentially, part 1 is for the uninitiated. For those that have been around the momentum block a couple of times, they can skip right past this. Unfortunately, it8217s half the book, so that leaves a little bit of a sour taste in the mouth. Next, part two is where, in my opinion, the real meat and potatoes of the book8211the 8220how8221. Essentially, the algorithm can be boiled down into the following: Taking the universe of large and mid-cap stocks, do the following: 1) Sort the stocks into deciles by 2-12 momentum8211that is, at the end of every month, calculate momentum by last month8217s closing price minus the closing price 12 months ago. Essentially, research states that there8217s a reversion effect on the 1-month momentum. However, this effect doesn8217t carry over into the ETF universe in my experience. 2) Here8217s the interesting part which makes the book worth picking up on its own (in my opinion): after sorting into deciles, rank the top decile by the following metric: multiply the sign of the 2-12 momentum by the following equation: ( negative returns 8211 positive). Essentially, the idea here is to determine smoothness of momentum. That is, in the most extreme situation, imagine a stock that did absolutely nothing for 230 days, and then had one massive day that gave it its entire price appreciation (think Google when it had a 10 jump off of better-than-expected numbers reports), and in the other extreme, a stock that simply had each and every single day be a small positive price appreciation. Obviously, you8217d want the second type of stock. That8217s this idea. Again, sort into deciles, and take the top decile. Therefore, taking the top decile of the top decile leaves you with 1 of the universe. Essentially, this makes the idea very difficult to replicate8211since you8217d need to track down a massive universe of stocks. That stated, I think the expression is actually a pretty good idea as a stand-in for volatility. That is, regardless of how volatile an asset is8211whether it8217s as volatile as a commodity like DBC, or as non-volatile as a fixed-income product like SHY, this expression is an interesting way of stating 8220this path is choppy8221 vs. 8220this path is smooth8221. I might investigate this expression on my blog further in the future. 3) Lastly, if the portfolio is turning over quarterly instead of monthly, the best months to turn it over are the months preceding end-of-quarter month (that is, February, May, August, November) because a bunch of amateur asset managers like to 8220window dress8221 their portfolios. That is, they had a crummy quarter, so at the last month before they have to send out quarterly statements, they load up on some recent winners so that their clients don8217t think they8217re as amateur as they really let on, and there8217s a bump for this. Similarly, January has some selling anomalies due to tax-loss harvesting. As far as practical implementations go, I think this is a very nice touch. Conceding the fact that turning over every month may be a bit too expensive, I like that Wes and Jack say 8220sure, you want to turn it over once every three months, but on which months8221. It8217s a very good question to ask if it means you get an additional percentage point or 150 bps a year from that, as it just might cover the transaction costs and then some. All in all, it8217s a fairly simple to understand strategy. However, the part that sort of gates off the book to a perfect replication is the difficulty in obtaining the CRSP data. However, I do commend Alpha Architect for disclosing the entire algorithm from start to finish. Furthermore, if the basic 2-12 momentum is not enough, there8217s an appendix detailing other types of momentum ideas (earnings momentum, ranking by distance to 52-week highs, absolute historical momentum, and so on). None of these strategies are really that much better than the basic price momentum strategy, so they8217re there for those interested, but it seems there8217s nothing really ground-breaking there. That is, if you8217re trading once a month, there8217s only so many ways of saying 8220hey, I think this thing is going up8221 I also like that Wes and Jack touched on the fact that trend-following, while it doesn8217t improve overall CAGR or Sharpe, does a massive amount to improve on max drawdown. That is, if faced with the prospect of losing 70-80 of everything, and losing only 30, that8217s an easy choice to make. Trend-following is good, even a simplistic version. All in all, I think the book accomplishes what it sets out to do, which is to present a well-researched algorithm. Ultimately, the punchline is on Alpha Architect8217s site (I believe they have some sort of monthly stock filter). Furthermore, the book states that there are better risk-adjusted returns when combined with the algorithm outlined in the 8220quantitative value8221 book. In my experience, I8217ve never had value algorithms impress me in the backtests I8217ve done, but I can chalk that up to me being inexperienced with all the various valuation metrics. My criticism of the book, however, is this: The momentum algorithm in the book misses what I feel is one key component: volatility targeting control. Simply, the paper 8220momentum has its moments8221 (which I covered in my hypothesis-driven development series of posts) essentially states that the usual Fama-French momentum strategy does far better from a risk-reward strategy by deleveraging during times of excessive volatility, and avoiding momentum crashes. I8217m not sure why Wes and Jack didn8217t touch upon this paper, since the implementation is very simple (targetrealized volatility leverage factor). Ideally, I8217d love if Wes or Jack could send me the stream of returns for this strategy (preferably daily, but monthly also works). Essentially, I think this book is very comprehensive. However, I think it also has a somewhat 8220don8217t try this at home8221 feel to it due to the data requirement to replicate it. Certainly, if your broker charges you 8 a transaction, it8217s not a feasible strategy to drop several thousand bucks a year on transaction costs that8217ll just give your returns to your broker. However, I do wonder if the QMOM ETF (from Alpha Architect, of course) is, in fact, a better version of this strategy, outside of the management fee. In any case, my final opinion is this: while this book leaves a little bit of knowledge on the table, on a whole, it accomplishes what it sets out to do, is clear with its procedures, and provides several worthwhile ideas. For the price of a non-technical textbook (aka those 60 books on amazon), this book is a steal. Dziękuje za przeczytanie. NOTE: While I am currently employed in a successful analytics capacity, I am interested in hearing about full-time positions more closely related to the topics on this blog. If you have a full-time position which can benefit from my current skills, please let me know. My Linkedin can be found here. This post will be about attempting to use the Depmix package for online state prediction. While the depmix package performs admirably when it comes to describing the states of the past, when used for one-step-ahead prediction, under the assumption that tomorrow8217s state will be identical to today8217s, the hidden markov model process found within the package does not perform to expectations. So, to start off, this post was motivated by Michael Halls-Moore, who recently posted some R code about using the depmixS4 library to use hidden markov models. Generally, I am loath to create posts on topics I don8217t feel I have an absolutely front-to-back understanding of, but I8217m doing this in the hope of learning from others on how to appropriately do online state-space prediction, or 8220regime switching8221 detection, as it may be called in more financial parlance. While I8217ve seen the usual theory of hidden markov models (that is, it can rain or it can be sunny, but you can only infer the weather judging by the clothes you see people wearing outside your window when you wake up), and have worked with toy examples in MOOCs (Udacity8217s self-driving car course deals with them, if I recall correctly8211or maybe it was the AI course), at the end of the day, theory is only as good as how well an implementation can work on real data. For this experiment, I decided to take SPY data since inception, and do a full in-sample 8220backtest8221 on the data. That is, given that the HMM algorithm from depmix sees the whole history of returns, with this 8220god8217s eye8221 view of the data, does the algorithm correctly classify the regimes, if the backtest results are any indication Here8217s the code to do so, inspired by Dr. Halls-Moore8217s. Essentially, while I did select three states, I noted that anything with an intercept above zero is a bull state, and below zero is a bear state, so essentially, it reduces to two states. With the result: So, not particularly terrible. The algorithm works, kind of, sort of, right Well, let8217s try online prediction now. So what I did here was I took an expanding window, starting from 500 days since SPY8217s inception, and kept increasing it, by one day at a time. My prediction, was, trivially enough, the most recent day, using a 1 for a bull state, and a -1 for a bear state. I ran this process in parallel (on a linux cluster, because windows8217s doParallel library seems to not even know that certain packages are loaded, and it8217s more messy), and the first big issue is that this process took about three hours on seven cores for about 23 years of data. Not exactly encouraging, but computing time isn8217t expensive these days. So let8217s see if this process actually works. First, let8217s test if the algorithm does what it8217s actually supposed to do and use one day of look-ahead bias (that is, the algorithm tells us the state at the end of the day8211how correct is it even for that day). With the result: So, allegedly, the algorithm seems to do what it was designed to do, which is to classify a state for a given data set. Now, the most pertinent question: how well do these predictions do even one day ahead You8217d think that state space predictions would be parsimonious from day to day, given the long history, correct With the result: That is, without the lookahead bias, the state space prediction algorithm is atrocious. Why is that Well, here8217s the plot of the states: In short, the online hmm algorithm in the depmix package seems to change its mind very easily, with obvious (negative) implications for actual trading strategies. So, that wraps it up for this post. Essentially, the main message here is this: there8217s a vast difference between loading doing descriptive analysis (AKA 8220where have you been, why did things happen8221) vs. predictive analysis (that is, 8220if I correctly predict the future, I get a positive payoff8221). In my opinion, while descriptive statistics have their purpose in terms of explaining why a strategy may have performed how it did, ultimately, we8217re always looking for better prediction tools. In this case, depmix, at least in this 8220out-of-the-box8221 demonstration does not seem to be the tool for that. If anyone has had success with using depmix (or other regime-switching algorithm in R) for prediction, I would love to see work that details the procedure taken, as it8217s an area I8217m looking to expand my toolbox into, but don8217t have any particular good leads. Essentially, I8217d like to think of this post as me describing my own experiences with the package. Dziękuje za przeczytanie. NOTE: On Oct. 5th, I will be in New York City. On Oct. 6th, I will be presenting at The Trading Show on the Programming Wars panel. NOTE: My current analytics contract is up for review at the end of the year, so I am officially looking for other offers as well. If you have a full-time role which may benefit from the skills you see on my blog, please get in touch with me. My linkedin profile can be found here. This post will introduce component conditional value at risk mechanics found in PerformanceAnalytics from a paper written by Brian Peterson, Kris Boudt, and Peter Carl. This is a mechanism that is an easy-to-call mechanism for computing component expected shortfall in asset returns as they apply to a portfolio. While the exact mechanics are fairly complex, the upside is that the running time is nearly instantaneous, and this method is a solid tool for including in asset allocation analysis. For those interested in an in-depth analysis of the intuition of component conditional value at risk, I refer them to the paper written by Brian Peterson, Peter Carl, and Kris Boudt. Essentially, here8217s the idea: all assets in a given portfolio have a marginal contribution to its total conditional value at risk (also known as expected shortfall)8211that is, the expected loss when the loss surpasses a certain threshold. For instance, if you want to know your 5 expected shortfall, then it8217s the average of the worst 5 returns per 100 days, and so on. For returns using daily resolution, the idea of expected shortfall may sound as though there will never be enough data in a sufficiently fast time frame (on one year or less), the formula for expected shortfall in the PerformanceAnalytics defaults to an approximation calculation using a Cornish-Fisher expansion, which delivers very good results so long as the p-value isn8217t too extreme (that is, it works for relatively sane p values such as the 1-10 range). Component Conditional Value at Risk has two uses: first off, given no input weights, it uses an equal weight default, which allows it to provide a risk estimate for each individual asset without burdening the researcher to create his or her own correlationcovariance heuristics. Secondly, when provided with a set of weights, the output changes to reflect the contribution of various assets in proportion to those weights. This means that this methodology works very nicely with strategies that exclude assets based on momentum, but need a weighting scheme for the remaining assets. Furthermore, using this methodology also allows an ex-post analysis of risk contribution to see which instrument contributed what to risk. First, a demonstration of how the mechanism works using the edhec data set. There is no strategy here, just a demonstration of syntax. This will assume an equal-weight contribution from all of the funds in the edhec data set. So tmp is the contribution to expected shortfall from each of the various edhec managers over the entire time period. Here8217s the output: The salient part of this is the percent contribution (the last output). Notice that it can be negative, meaning that certain funds gain when others lose. At least, this was the case over the current data set. These assets diversify a portfolio and actually lower expected shortfall. In this case, I equally weighted the first ten managers in the edhec data set, and put zero weight in the last three. Furthermore, we can see what happens when the weights are not equal. This time, notice that as the weight increased in the convertible arb manager, so too did his contribution to maximum expected shortfall. For a future backtest, I would like to make some data requests. I would like to use the universe found in Faber8217s Global Asset Allocation book. That said, the simulations in that book go back to 1972, and I was wondering if anyone out there has daily returns for those assetsindices. While some ETFs go back into the early 2000s, there are some that start rather late such as DBC (commodities, early 2006), GLD (gold, early 2004), BWX (foreign bonds, late 2007), and FTY (NAREIT, early 2007). As an eight-year backtest would be a bit short, I was wondering if anyone had data with more history. One other thing, I will in New York for the trading show. and speaking on the 8220programming wars8221 panel on October 6th. Dziękuje za przeczytanie. NOTE: While I am currently contracting, I am also looking for a permanent position which can benefit from my skills for when my current contract ends. If you have or are aware of such an opening, I will be happy to speak with you. This post will cover a function to simplify creating Harry Long type rebalancing strategies from SeekingAlpha for interested readers. As Harry Long has stated, most, if not all of his strategies are more for demonstrative purposes rather than actual recommended investments. So, since Harry Long has been posting some more articles on Seeknig Alpha, I8217ve had a reader or two ask me to analyze his strategies (again). Instead of doing that, however, I8217ll simply put this tool here, which is a wrapper that automates the acquisition of data and simulates portfolio rebalancing with one line of code. Here8217s the tool. It fetches the data for you (usually from Yahoo, but a big thank you to Mr. Helumth Vollmeier in the case of ZIV and VXX), and has the option of either simply displaying an equity curve and some statistics (CAGR, annualized standard dev, Sharpe, max Drawdown, Calmar), or giving you the return stream as an output if you wish to do more analysis in R. Here8217s an example of simply getting the statistics, with an 80 XLPSPLV (they8217re more or less interchangeable) and 20 TMF (aka 60 TLT, so an 8060 portfolio), from one of Harry Long8217s articles . Nothing out of the ordinary of what we might expect from a balanced equitybonds portfolio. Generally does well, has its largest drawdown in the financial crisis, and some other bumps in the road, but overall, I8217d think a fairly vanilla 8220set it and forget it8221 sort of thing. And here would be the way to get the stream of individual daily returns, assuming you wanted to rebalance these two instruments weekly, instead of yearly (as is the default). And now let8217s get some statistics. Turns out, moving the rebalancing from annually to weekly didn8217t have much of an effect here (besides give a bunch of money to your broker, if you factored in transaction costs, which this doesn8217t). So, that8217s how this tool works. The results, of course, begin from the latest instrument8217s inception. The trick, in my opinion, is to try and find proxy substitutes with longer histories for newer ETFs that are simply leveraged ETFs, such as using a 60 weight in TLT with an 80 weight in XLP instead of a 20 weight in TMF with 80 allocation in SPLV. For instance, here are some proxies: SPXL XLP SPXLUPRO SPY 3 TMF TLT 3 That said, I8217ve worked with Harry Long before, and he develops more sophisticated strategies behind the scenes, so I8217d recommend that SeekingAlpha readers take his publicly released strategies as concept demonstrations, as opposed to fully-fledged investment ideas, and contact Mr. Long himself about more customized, private solutions for investment institutions if you are so interested. Dziękuje za przeczytanie. NOTE: I am currently in the northeast. While I am currently contracting, I am interested in networking with individuals or firms with regards to potential collaboration opportunities. This post will demonstrate how to take into account turnover when dealing with returns-based data using PerformanceAnalytics and the Return. Portfolio function in R. It will demonstrate this on a basic strategy on the nine sector SPDRs. So, first off, this is in response to a question posed by one Robert Wages on the R-SIG-Finance mailing list. While there are many individuals out there with a plethora of questions (many of which can be found to be demonstrated on this blog already), occasionally, there will be an industry veteran, a PhD statistics student from Stanford, or other very intelligent individual that will ask a question on a topic that I haven8217t yet touched on this blog, which will prompt a post to demonstrate another technical aspect found in R. This is one of those times. So, this demonstration will be about computing turnover in returns space using the PerformanceAnalytics package. Simply, outside of the PortfolioAnalytics package, PerformanceAnalytics with its Return. Portfolio function is the go-to R package for portfolio management simulations, as it can take a set of weights, a set of returns, and generate a set of portfolio returns for analysis with the rest of PerformanceAnalytics8217s functions. Again, the strategy is this: take the 9 three-letter sector SPDRs (since there are four-letter ETFs now), and at the end of every month, if the adjusted price is above its 200-day moving average, invest into it. Normalize across all invested sectors (that is, 19th if invested into all 9, 100 into 1 if only 1 invested into, 100 cash, denoted with a zero return vector, if no sectors are invested into). It8217s a simple, toy strategy, as the strategy isn8217t the point of the demonstration. Here8217s the basic setup code: So, get the SPDRs, put them together, compute their returns, generate the signal, and create the zero vector, since Return. Portfolio treats weights less than 1 as a withdrawal, and weights above 1 as the addition of more capital (big FYI here). Now, here8217s how to compute turnover: So, the trick is this: when you call Return. portfolio, use the verbose TRUE option. This creates several objects, among them returns, BOP. Weight, and EOP. Weight. These stand for Beginning Of Period Weight, and End Of Period Weight. The way that turnover is computed is simply the difference between how the day8217s return moves the allocated portfolio from its previous ending point to where that portfolio actually stands at the beginning of next period. That is, the end of period weight is the beginning of period drift after taking into account the day8217s driftreturn for that asset. The new beginning of period weight is the end of period weight plus any transacting that would have been done. Thus, in order to find the actual transactions (or turnover), one subtracts the previous end of period weight from the beginning of period weight. This is what such transactions look like for this strategy. Something we can do with such data is take a one-year rolling turnover, accomplished with the following code: It looks like this: This essentially means that one year8217s worth of two-way turnover (that is, if selling an entirely invested portfolio is 100 turnover, and buying an entirely new set of assets is another 100, then two-way turnover is 200) is around 800 at maximum. That may be pretty high for some people. Now, here8217s the application when you penalize transaction costs at 20 basis points per percentage point traded (that is, it costs 20 cents to transact 100). So, at 20 basis points on transaction costs, that takes about one percent in returns per year out of this (admittedly, terrible) strategy. This is far from negligible. So, that is how you actually compute turnover and transaction costs. In this case, the transaction cost model was very simple. However, given that Return. portfolio returns transactions at the individual asset level, one could get as complex as they would like with modeling the transaction costs. Dziękuje za przeczytanie. NOTE: I will be giving a lightning talk at RFinance, so for those attending, you8217ll be able to find me there. This post will outline an easy-to-make mistake in writing vectorized backtests8211namely in using a signal obtained at the end of a period to enter (or exit) a position in that same period. The difference in results one obtains is massive. Today, I saw two separate posts from Alpha Architect and Mike Harris both referencing a paper by Valeriy Zakamulin on the fact that some previous trend-following research by Glabadanidis was done with shoddy results, and that Glabadanidis8217s results were only reproducible through instituting lookahead bias. The following code shows how to reproduce this lookahead bias. First, the setup of a basic moving average strategy on the SampP 500 index from as far back as Yahoo data will provide. And here is how to institute the lookahead bias. These are the 8220results8221: Of course, this equity curve is of no use, so here8217s one in log scale. As can be seen, lookahead bias makes a massive difference. Here are the numerical results: Again, absolutely ridiculous. Note that when using Return. Portfolio (the function in PerformanceAnalytics), that package will automatically give you the next period8217s return, instead of the current one, for your weights. However, for those writing 8220simple8221 backtests that can be quickly done using vectorized operations, an off-by-one error can make all the difference between a backtest in the realm of reasonable, and pure nonsense. However, should one wish to test for said nonsense when faced with impossible-to-replicate results, the mechanics demonstrated above are the way to do it. Now, onto other news: I8217d like to thank Gerald M for staying on top of one of the Logical Invest strategies8211namely, their simple global market rotation strategy outlined in an article from an earlier blog post. Up until March 2018 (the date of the blog post), the strategy had performed well. However, after said date It has been a complete disaster, which, in hindsight, was evident when I passed it through the hypothesis-driven development framework process I wrote about earlier. So, while there has been a great deal written about not simply throwing away a strategy because of short-term underperformance, and that anomalies such as momentum and value exist because of career risk due to said short-term underperformance, it8217s never a good thing when a strategy creates historically large losses, particularly after being published in such a humble corner of the quantitative financial world. In any case, this was a post demonstrating some mechanics, and an update on a strategy I blogged about not too long ago. Dziękuje za przeczytanie. NOTE: I am always interested in hearing about new opportunities which may benefit from my expertise, and am always happy to network. You can find my LinkedIn profile here . This post will shed light on the values of R2s behind two rather simplistic strategies 8212 the simple 10 month SMA, and its relative, the 10 month momentum (which is simply a difference of SMAs, as Alpha Architect showed in their book DIY Financial Advisor . Not too long ago, a friend of mine named Josh asked me a question regarding R2s in finance. He8217s finishing up his PhD in statistics at Stanford, so when people like that ask me questions, I8217d like to answer them. His assertion is that in some instances, models that have less than perfect predictive power (EG R2s of .4, for instance), can still deliver very promising predictions, and that if someone were to have a financial model that was able to explain 40 of the variance of returns, they could happily retire with that model making them very wealthy. Indeed. 4 is a very optimistic outlook (to put it lightly), as this post will show. In order to illustrate this example, I took two 8220staple8221 strategies 8212 buy SPY when i ts closing monthly price is above its ten month simple moving average, and when its ten month momentum (basically the difference of a ten month moving average and its lag) is positive. While these models are simplistic, they are ubiquitously talked about, and many momentum strategies are an improvement upon these baseline, 8220out-of-the-box8221 strategies. Here8217s the code to do that: And here are the results: In short, the SMA10 and the 10-month momentum (aka ROC 10 aka MOM10) both handily outperform the buy and hold, not only in absolute returns, but especially in risk-adjusted returns (Sharpe and Calmar ratios). Again, simplistic analysis, and many models get much more sophisticated than this, but once again, simple, illustrative example using two strategies that outperform a benchmark (over the long term, anyway). Now, the question is, what was the R2 of these models To answer this, I took a rolling five-year window that essentially asked: how well did these quantities (the ratio between the closing price and the moving average 8211 1, or the ten month momentum) predict the next month8217s returns That is, what proportion of the variance is explained through the monthly returns regressed against the previous month8217s signals in numerical form (perhaps not the best framing, as the signal is binary as opposed to continuous which is what is being regressed, but let8217s set that aside, again, for the sake of illustration). Here8217s the code to generate the answer. And the answer, in pictorial form: In short, even in the best case scenarios, namely, crises which provide momentumtrend-followingcall it what you will its raison d8217etre, that is, its risk management appeal, the proportion of variance explained by the actual signal quantities was very small. In the best of times, around 20. But then again, think about what the R2 value actually is8211it8217s the percentage of variance explained by a predictor. If a small set of signals (let alone one) was able to explain the majority of the change in the returns of the SampP 500, or even a not-insignificant portion, such a person would stand to become very wealthy. More to the point, given that two strategies that handily outperform the market have R2s that are exceptionally low for extended periods of time, it goes to show that holding the R2 up as some form of statistical holy grail certainly is incorrect in the general sense, and anyone who does so either is painting with too broad a brush, is creating disingenuous arguments, or should simply attempt to understand another field which may not work the way their intuition tells them. Dziękuje za przeczytanie. This review will review the 8220Adaptive Asset Allocation: Dynamic Global Portfolios to Profit in Good Times 8211 and Bad8221 book by the people at ReSolve Asset Management. Overall, this book is a definite must-read for those who have never been exposed to the ideas within it. However, when it comes to a solution that can be fully replicated, this book is lacking. Okay, it8217s been a while since I reviewed my last book, DIY Financial Advisor. from the awesome people at Alpha Architect. This book in my opinion, is set up in a similar sort of format. This is the structure of the book, and my reviews along with it: Part 1: Why in the heck you actually need to have a diversified portfolio, and why a diversified portfolio is a good thing. In a world in which there is so much emphasis put on single-security performance, this is certainly something that absolutely must be stated for those not familiar with portfolio theory. It highlights the example of two people8211one from Abbott Labs, and one from Enron, who had so much of their savings concentrated in their company8217s stock. Mr. Abbott got hit hard and changed his outlook on how to save for retirement, and Mr. Enron was never heard from again. Long story short: a diversified portfolio is good, and a properly diversified portfolio can offset one asset8217s zigs with another asset8217s zags. This is the key to establishing a stream of returns that will help meet financial goals. Basically, this is your common sense story (humans love being told stories) so as to motivate you to read the rest of the book. It does its job, though for someone like me, it8217s more akin to a big 8220wait for it, wait for it8230and there8217s the reason why we should read on, as expected8221. Part 2: Something not often brought up in many corners of the quant world (because it8217s real life boring stuff) is the importance not only of average returns, but when those returns are achieved. Namely, imagine your everyday saver. At the beginning of their careers, they8217re taking home less salary and have less money in their retirement portfolio (or speculation portfolio, but the book uses retirement portfolio). As they get into middle age and closer to retirement, they have a lot more money in said retirement portfolio. Thus, strong returns are most vital when there is more cash available to the portfolio, and the difference between mediocre returns at the beginning and strong returns at the end of one8217s working life as opposed to vice versa is astronomical and cannot be understated. Furthermore, once in retirement, strong returns in the early years matter far more than returns in the later years once money has been withdrawn out of the portfolio (though I8217d hope that a portfolio8217s returns can be so strong that one can simply 8220live off the interest8221). Or, put more intuitively: when you have 10,000 in your portfolio, a 20 drawdown doesn8217t exactly hurt because you can make more money and put more into your retirement account. But when you8217re 62 and have 500,000 and suddenly lose 30 of everything, well, that8217s massive. How much an investor wants to avoid such a scenario cannot be understated. Warren Buffett once said that if you can8217t bear to lose 50 of everything, you shouldn8217t be in stocks. I really like this part of the book because it shows just how dangerous the ideas of 8220a 50 drawdown is unavoidable8221 and other 8220stay invested for the long haul8221 refrains are. Essentially, this part of the book makes a resounding statement that any financial adviser keeping his or her clients invested in equities when they8217re near retirement age is doing something not very advisable, to put it lightly. In my opinion, those who advise pension funds should especially keep this section of the book in mind, since for some people, the long-term may be coming to an end, and what matters is not only steady returns, but to make sure the strategy doesn8217t fall off a cliff and destroy decades of hard-earned savings. Part 3: This part is also one that is a very important read. First off, it lays out in clear terms that the long-term forward-looking valuations for equities are at rock bottom. That is, the expected forward 15-year returns are very low, using approximately 75 years of evidence. Currently, according to the book, equity valuations imply a negative 15-year forward return. However, one thing I will take issue with is that while forward-looking long-term returns for equities may be very low, if one believed this chart and only invested in the stock market when forecast 15-year returns were above the long term average, one would have missed out on both the 2003-2007 bull runs, and the one since 2009 that8217s just about over. So, while the book makes a strong case for caution, readers should also take the chart with a grain of salt in my opinion. However, another aspect of portfolio construction that this book covers is how to construct a robust (assets for any economic environment) and coherent (asset classes balanced in number) universe for implementation with any asset allocation algorithm. I think this bears repeating: universe selection is an extremely important topic in the discussion of asset allocation, yet there is very little discussion about it. Most researchtopics simply take some 8220conventional universe8221, such as 8220all stocks on the NYSE8221, or 8220all the stocks in the SampP 5008221, or 8220the entire set of the 50-60 most liquid futures8221 without consideration for robustness and coherence. This book is the first source I8217ve seen that actually puts this topic under a magnifying glass besides 8220finger in the air pick and choose8221. Part 4: and here8217s where I level my main criticism at this book. For those that have read 8220Adaptive Asset Allocation: A Primer8221. this section of the book is basically one giant copy and paste. It8217s all one large buildup to 8220momentum rank min-variance optimization8221. All well and good, until there8217s very little detail beyond the basics as to how the minimum variance portfolio was constructed. Namely, what exactly is the minimum variance algorithm in use Is it one of the poor variants susceptible to numerical instability inherent in inverting sample covariance matrices Or is it a heuristic like David Varadi8217s minimum variance and minimum correlation algorithm The one feeling I absolutely could not shake was that this book had a perfect opportunity to present a robust approach to minimum variance, and instead, it8217s long on concept, short on details. While the theory of 8220maximize return for unit risk8221 is all well and good, the actual algorithm to implement that theory into practice is not trivial, with the solutions taught to undergrads and master8217s students having some well-known weaknesses. On top of this, one thing that got hammered into my head in the past was that ranking also had a weakness at the inclusionexclusion point. E. G. if, out of ten assets, the fifth asset had a momentum of say, 10.9, and the sixth asset had a momentum of 10.8, how are we so sure the fifth is so much better And while I realize that this book was ultimately meant to be a primer, in my opinion, it would have been a no-objections five-star if there were an appendix that actually went into some detail on how to go from the simple concepts and included a small numerical example of some algorithms that may address the well-known weaknesses. This doesn8217t mean Greekmathematical jargon. Just an appendix that acknowledged that not every reader is someone only picking up his first or second book about systematic investing, and that some of us are familiar with the 8220whys8221 and are more interested in the 8220hows8221. Furthermore, I8217d really love to know where the authors of this book got their data to back-date some of these ETFs into the 90s. Part 5: some more formal research on topics already covered in the rest of the book8211namely a section about how many independent bets one can take as the number of assets grow, if I remember it correctly. Long story short You easily get the most bang for your buck among disparate asset classes, such as treasuries of various duration, commodities, developed vs. emerging equities, and so on, as opposed to trying to pick among stocks in the same asset class (though there8217s some potential for alpha there8230just8230a lot less than you imagine). So in case the idea of asset class selection, not stock selection wasn8217t beaten into the reader8217s head before this point, this part should do the trick. The other research paper is something I briefly skimmed over which went into more depth about volatility and retirement portfolios, though I felt that the book covered this topic earlier on to a sufficient degree by building up the intuition using very understandable scenarios. So that8217s the review of the book. Overall, it8217s a very solid piece of writing, and as far as establishing the why, it does an absolutely superb job. For those that aren8217t familiar with the concepts in this book, this is definitely a must-read, and ASAP. However, for those familiar with most of the concepts and looking for a detailed 8220how8221 procedure, this book does not deliver as much as I would have liked. And I realize that while it8217s a bad idea to publish secret sauce, I bought this book in the hope of being exposed to a new algorithm presented in the understandable and intuitive language that the rest of the book was written in, and was left wanting. Still, that by no means diminishes the impact of the rest of the book. For those who are more likely to be its target audience, it8217s a 55. For those that wanted some specifics, it still has its gem on universe construction. Overall, I rate it a 45. Thanks for reading. Happy new year. This post will be a quick one covering the relationship between the simple moving average and time series momentum. The implication is that one can potentially derive better time series momentum indicators than the classical one applied in so many papers. Okay, so the main idea for this post is quite simple: I8217m sure we8217re all familiar with classical momentum. That is, the price now compared to the price however long ago (3 months, 10 months, 12 months, etc.). E. G. P(now) 8211 P(10) And I8217m sure everyone is familiar with the simple moving average indicator, as well. E. G. SMA(10). Well, as it turns out, these two quantities are actually related. It turns out, if instead of expressing momentum as the difference of two numbers, it is expressed as the sum of returns, it can be written (for a 10 month momentum) as: MOM10 return of this month return of last month return of 2 months ago 8230 return of 9 months ago, for a total of 10 months in our little example. This can be written as MOM10 (P(0) 8211 P(1)) (P(1) 8211 P(2)) 8230 (P(9) 8211 P(10)). (Each difference within parentheses denotes one month8217s worth of returns.) Which can then be rewritten by associative arithmetic as: (P(0) P(1) 8230 P(9)) 8211 (P(1) P(2) 8230 P(10)). In other words, momentum 8212 aka the difference between two prices, can be rewritten as the difference between two cumulative sums of prices. And what is a simple moving average Simply a cumulative sum of prices divided by however many prices summed over. Here8217s some R code to demonstrate. With the resulting number of times these two signals are equal: In short, every time. Now, what exactly is the punchline of this little example Here8217s the punchline: The simple moving average is8230fairly simplistic as far as filters go. It works as a pedagogical example, but it has some well known weaknesses regarding lag, windowing effects, and so on. Here8217s a toy example how one can get a different momentum signal by changing the filter. With the following results: While the difference of EMA10 strategy didn8217t do better than the difference of SMA10 (aka standard 10-month momentum), that8217s not the point. The point is that the momentum signal is derived from a simple moving average filter, and that by using a different filter, one can still use a momentum type of strategy. Or, put differently, the maingeneral takeaway here is that momentum is the slope of a filter, and one can compute momentum in an infinite number of ways depending on the filter used, and can come up with a myriad of different momentum strategies. Dziękuje za przeczytanie. NOTE: I am currently contracting in Chicago, and am always open to networking. Contact me at my email at ilya. kipnisgmail or find me on my LinkedIn here. Post navigation CategoriesCategory Archives: Trading This post will introduce John Ehlers8217s Autocorrelation Periodogram mechanism8211a mechanism designed to dynamically find a lookback period. That is, the most common parameter optimized in backtests is the lookback period. Before beginning this post, I must give credit where it8217s due, to one Mr. Fabrizio Maccallini. the head of structured derivatives at Nordea Markets in London. You can find the rest of the repository he did for Dr. John Ehlers8217s Cycle Analytics for Traders on his github. I am grateful and honored that such intelligent and experienced individuals are helping to bring some of Dr. Ehlers8217s methods into R. The point of the Ehlers Autocorrelation Periodogram is to dynamically set a period between a minimum and a maximum period length. While I leave the exact explanation of the mechanic to Dr. Ehlers8217s book, for all practical intents and purposes, in my opinion, the punchline of this method is to attempt to remove a massive source of overfitting from trading system creation8211namely specifying a lookback period. SMA of 50 days 100 days 200 days Well, this algorithm takes that possibility of overfitting out of your hands. Simply, specify an upper and lower bound for your lookback, and it does the rest. How well it does it is a topic of discussion for those well-versed in the methodologies of electrical engineering (I8217m not), so feel free to leave comments that discuss how well the algorithm does its job, and feel free to blog about it as well. In any case, here8217s the original algorithm code, courtesy of Mr. Maccallini: One thing I do notice is that this code uses a loop that says for(i in 1:length(filt)), which is an O(data points) loop, which I view as the plague in R. While I8217ve used Rcpp before, it8217s been for only the most basic of loops, so this is definitely a place where the algorithm can stand to be improved with Rcpp due to R8217s inherent poor looping. Those interested in the exact logic of the algorithm will, once again, find it in John Ehlers8217s Cycle Analytics For Traders book (see link earlier in the post). Of course, the first thing to do is to test how well the algorithm does what it purports to do, which is to dictate the lookback period of an algorithm. Let8217s run it on some data. Now, what does the algorithm-set lookback period look like Let8217s zoom in on 2001 through 2003, when the markets went through some upheaval. In this zoomed-in image, we can see that the algorithm8217s estimates seem fairly jumpy. Here8217s some code to feed the algorithm8217s estimates of n into an indicator to compute an indicator with a dynamic lookback period as set by Ehlers8217s autocorrelation periodogram. And here is the function applied with an SMA, to tune between 120 and 252 days. As seen, this algorithm is less consistent than I would like, at least when it comes to using a simple moving average. For now, I8217m going to leave this code here, and let people experiment with it. I hope that someone will find that this indicator is helpful to them. Dziękuje za przeczytanie. NOTES: I am always interested in networkingmeet-ups in the northeast (PhiladelphiaNYC). Furthermore, if you believe your firm will benefit from my skills, please do not hesitate to reach out to me. My linkedin profile can be found here. Lastly, I am volunteering to curate the R section for books on quantocracy. If you have a book about R that can apply to finance, be sure to let me know about it, so that I can review it and possibly recommend it. Thakn you. This post will be about attempting to use the Depmix package for online state prediction. While the depmix package performs admirably when it comes to describing the states of the past, when used for one-step-ahead prediction, under the assumption that tomorrow8217s state will be identical to today8217s, the hidden markov model process found within the package does not perform to expectations. So, to start off, this post was motivated by Michael Halls-Moore, who recently posted some R code about using the depmixS4 library to use hidden markov models. Generally, I am loath to create posts on topics I don8217t feel I have an absolutely front-to-back understanding of, but I8217m doing this in the hope of learning from others on how to appropriately do online state-space prediction, or 8220regime switching8221 detection, as it may be called in more financial parlance. While I8217ve seen the usual theory of hidden markov models (that is, it can rain or it can be sunny, but you can only infer the weather judging by the clothes you see people wearing outside your window when you wake up), and have worked with toy examples in MOOCs (Udacity8217s self-driving car course deals with them, if I recall correctly8211or maybe it was the AI course), at the end of the day, theory is only as good as how well an implementation can work on real data. For this experiment, I decided to take SPY data since inception, and do a full in-sample 8220backtest8221 on the data. That is, given that the HMM algorithm from depmix sees the whole history of returns, with this 8220god8217s eye8221 view of the data, does the algorithm correctly classify the regimes, if the backtest results are any indication Here8217s the code to do so, inspired by Dr. Halls-Moore8217s. Essentially, while I did select three states, I noted that anything with an intercept above zero is a bull state, and below zero is a bear state, so essentially, it reduces to two states. With the result: So, not particularly terrible. The algorithm works, kind of, sort of, right Well, let8217s try online prediction now. So what I did here was I took an expanding window, starting from 500 days since SPY8217s inception, and kept increasing it, by one day at a time. My prediction, was, trivially enough, the most recent day, using a 1 for a bull state, and a -1 for a bear state. I ran this process in parallel (on a linux cluster, because windows8217s doParallel library seems to not even know that certain packages are loaded, and it8217s more messy), and the first big issue is that this process took about three hours on seven cores for about 23 years of data. Not exactly encouraging, but computing time isn8217t expensive these days. So let8217s see if this process actually works. First, let8217s test if the algorithm does what it8217s actually supposed to do and use one day of look-ahead bias (that is, the algorithm tells us the state at the end of the day8211how correct is it even for that day). With the result: So, allegedly, the algorithm seems to do what it was designed to do, which is to classify a state for a given data set. Now, the most pertinent question: how well do these predictions do even one day ahead You8217d think that state space predictions would be parsimonious from day to day, given the long history, correct With the result: That is, without the lookahead bias, the state space prediction algorithm is atrocious. Why is that Well, here8217s the plot of the states: In short, the online hmm algorithm in the depmix package seems to change its mind very easily, with obvious (negative) implications for actual trading strategies. So, that wraps it up for this post. Essentially, the main message here is this: there8217s a vast difference between loading doing descriptive analysis (AKA 8220where have you been, why did things happen8221) vs. predictive analysis (that is, 8220if I correctly predict the future, I get a positive payoff8221). In my opinion, while descriptive statistics have their purpose in terms of explaining why a strategy may have performed how it did, ultimately, we8217re always looking for better prediction tools. In this case, depmix, at least in this 8220out-of-the-box8221 demonstration does not seem to be the tool for that. If anyone has had success with using depmix (or other regime-switching algorithm in R) for prediction, I would love to see work that details the procedure taken, as it8217s an area I8217m looking to expand my toolbox into, but don8217t have any particular good leads. Essentially, I8217d like to think of this post as me describing my own experiences with the package. Dziękuje za przeczytanie. NOTE: On Oct. 5th, I will be in New York City. On Oct. 6th, I will be presenting at The Trading Show on the Programming Wars panel. NOTE: My current analytics contract is up for review at the end of the year, so I am officially looking for other offers as well. If you have a full-time role which may benefit from the skills you see on my blog, please get in touch with me. My linkedin profile can be found here. This post will demonstrate how to take into account turnover when dealing with returns-based data using PerformanceAnalytics and the Return. Portfolio function in R. It will demonstrate this on a basic strategy on the nine sector SPDRs. So, first off, this is in response to a question posed by one Robert Wages on the R-SIG-Finance mailing list. While there are many individuals out there with a plethora of questions (many of which can be found to be demonstrated on this blog already), occasionally, there will be an industry veteran, a PhD statistics student from Stanford, or other very intelligent individual that will ask a question on a topic that I haven8217t yet touched on this blog, which will prompt a post to demonstrate another technical aspect found in R. This is one of those times. So, this demonstration will be about computing turnover in returns space using the PerformanceAnalytics package. Simply, outside of the PortfolioAnalytics package, PerformanceAnalytics with its Return. Portfolio function is the go-to R package for portfolio management simulations, as it can take a set of weights, a set of returns, and generate a set of portfolio returns for analysis with the rest of PerformanceAnalytics8217s functions. Again, the strategy is this: take the 9 three-letter sector SPDRs (since there are four-letter ETFs now), and at the end of every month, if the adjusted price is above its 200-day moving average, invest into it. Normalize across all invested sectors (that is, 19th if invested into all 9, 100 into 1 if only 1 invested into, 100 cash, denoted with a zero return vector, if no sectors are invested into). It8217s a simple, toy strategy, as the strategy isn8217t the point of the demonstration. Here8217s the basic setup code: So, get the SPDRs, put them together, compute their returns, generate the signal, and create the zero vector, since Return. Portfolio treats weights less than 1 as a withdrawal, and weights above 1 as the addition of more capital (big FYI here). Now, here8217s how to compute turnover: So, the trick is this: when you call Return. portfolio, use the verbose TRUE option. This creates several objects, among them returns, BOP. Weight, and EOP. Weight. These stand for Beginning Of Period Weight, and End Of Period Weight. The way that turnover is computed is simply the difference between how the day8217s return moves the allocated portfolio from its previous ending point to where that portfolio actually stands at the beginning of next period. That is, the end of period weight is the beginning of period drift after taking into account the day8217s driftreturn for that asset. The new beginning of period weight is the end of period weight plus any transacting that would have been done. Thus, in order to find the actual transactions (or turnover), one subtracts the previous end of period weight from the beginning of period weight. This is what such transactions look like for this strategy. Something we can do with such data is take a one-year rolling turnover, accomplished with the following code: It looks like this: This essentially means that one year8217s worth of two-way turnover (that is, if selling an entirely invested portfolio is 100 turnover, and buying an entirely new set of assets is another 100, then two-way turnover is 200) is around 800 at maximum. That may be pretty high for some people. Now, here8217s the application when you penalize transaction costs at 20 basis points per percentage point traded (that is, it costs 20 cents to transact 100). So, at 20 basis points on transaction costs, that takes about one percent in returns per year out of this (admittedly, terrible) strategy. This is far from negligible. So, that is how you actually compute turnover and transaction costs. In this case, the transaction cost model was very simple. However, given that Return. portfolio returns transactions at the individual asset level, one could get as complex as they would like with modeling the transaction costs. Dziękuje za przeczytanie. NOTE: I will be giving a lightning talk at RFinance, so for those attending, you8217ll be able to find me there. This post will outline an easy-to-make mistake in writing vectorized backtests8211namely in using a signal obtained at the end of a period to enter (or exit) a position in that same period. The difference in results one obtains is massive. Today, I saw two separate posts from Alpha Architect and Mike Harris both referencing a paper by Valeriy Zakamulin on the fact that some previous trend-following research by Glabadanidis was done with shoddy results, and that Glabadanidis8217s results were only reproducible through instituting lookahead bias. The following code shows how to reproduce this lookahead bias. First, the setup of a basic moving average strategy on the SampP 500 index from as far back as Yahoo data will provide. And here is how to institute the lookahead bias. These are the 8220results8221: Of course, this equity curve is of no use, so here8217s one in log scale. As can be seen, lookahead bias makes a massive difference. Here are the numerical results: Again, absolutely ridiculous. Note that when using Return. Portfolio (the function in PerformanceAnalytics), that package will automatically give you the next period8217s return, instead of the current one, for your weights. However, for those writing 8220simple8221 backtests that can be quickly done using vectorized operations, an off-by-one error can make all the difference between a backtest in the realm of reasonable, and pure nonsense. However, should one wish to test for said nonsense when faced with impossible-to-replicate results, the mechanics demonstrated above are the way to do it. Now, onto other news: I8217d like to thank Gerald M for staying on top of one of the Logical Invest strategies8211namely, their simple global market rotation strategy outlined in an article from an earlier blog post. Up until March 2018 (the date of the blog post), the strategy had performed well. However, after said date It has been a complete disaster, which, in hindsight, was evident when I passed it through the hypothesis-driven development framework process I wrote about earlier. So, while there has been a great deal written about not simply throwing away a strategy because of short-term underperformance, and that anomalies such as momentum and value exist because of career risk due to said short-term underperformance, it8217s never a good thing when a strategy creates historically large losses, particularly after being published in such a humble corner of the quantitative financial world. In any case, this was a post demonstrating some mechanics, and an update on a strategy I blogged about not too long ago. Dziękuje za przeczytanie. NOTE: I am always interested in hearing about new opportunities which may benefit from my expertise, and am always happy to network. You can find my LinkedIn profile here . Happy new year. This post will be a quick one covering the relationship between the simple moving average and time series momentum. The implication is that one can potentially derive better time series momentum indicators than the classical one applied in so many papers. Okay, so the main idea for this post is quite simple: I8217m sure we8217re all familiar with classical momentum. That is, the price now compared to the price however long ago (3 months, 10 months, 12 months, etc.). E. G. P(now) 8211 P(10) And I8217m sure everyone is familiar with the simple moving average indicator, as well. E. G. SMA(10). Well, as it turns out, these two quantities are actually related. It turns out, if instead of expressing momentum as the difference of two numbers, it is expressed as the sum of returns, it can be written (for a 10 month momentum) as: MOM10 return of this month return of last month return of 2 months ago 8230 return of 9 months ago, for a total of 10 months in our little example. This can be written as MOM10 (P(0) 8211 P(1)) (P(1) 8211 P(2)) 8230 (P(9) 8211 P(10)). (Each difference within parentheses denotes one month8217s worth of returns.) Which can then be rewritten by associative arithmetic as: (P(0) P(1) 8230 P(9)) 8211 (P(1) P(2) 8230 P(10)). In other words, momentum 8212 aka the difference between two prices, can be rewritten as the difference between two cumulative sums of prices. And what is a simple moving average Simply a cumulative sum of prices divided by however many prices summed over. Here8217s some R code to demonstrate. With the resulting number of times these two signals are equal: In short, every time. Now, what exactly is the punchline of this little example Here8217s the punchline: The simple moving average is8230fairly simplistic as far as filters go. It works as a pedagogical example, but it has some well known weaknesses regarding lag, windowing effects, and so on. Here8217s a toy example how one can get a different momentum signal by changing the filter. With the following results: While the difference of EMA10 strategy didn8217t do better than the difference of SMA10 (aka standard 10-month momentum), that8217s not the point. The point is that the momentum signal is derived from a simple moving average filter, and that by using a different filter, one can still use a momentum type of strategy. Or, put differently, the maingeneral takeaway here is that momentum is the slope of a filter, and one can compute momentum in an infinite number of ways depending on the filter used, and can come up with a myriad of different momentum strategies. Dziękuje za przeczytanie. NOTE: I am currently contracting in Chicago, and am always open to networking. Contact me at my email at ilya. kipnisgmail or find me on my LinkedIn here. This post will outline a first failed attempt at applying the ensemble filter methodology to try and come up with a weighting process on SPY that should theoretically be a gradual process to shift from conviction between a bull market, a bear market, and anywhere in between. This is a follow-up post to this blog post. So, my thinking went like this: in a bull market, as one transitions from responsiveness to smoothness, responsive filters should be higher than smooth filters, and vice versa, as there8217s generally a trade-off between the two. In fact, in my particular formulation, the quantity of the square root of the EMA of squared returns punishes any deviation from a flat line altogether (although inspired by Basel8217s measure of volatility, which is the square root of the 18-day EMA of squared returns), while the responsiveness quantity punishes any deviation from the time series of the realized prices. Whether these are the two best measures of smoothness and responsiveness is a topic I8217d certainly appreciate feedback on. In any case, an idea I had on the top of my head was that in addition to having a way of weighing multiple filters by their responsiveness (deviation from price action) and smoothness (deviation from a flat line), that by taking the sums of the sign of the difference between one filter and its neighbor on the responsiveness to smoothness spectrum, provided enough ensemble filters (say, 101, so there are 100 differences), one would obtain a way to move from full conviction of a bull market, to a bear market, to anything in between, and have this be a smooth process that doesn8217t have schizophrenic swings of conviction. Here8217s the code to do this on SPY from inception to 2003: And here8217s the very underwhelming result: Essentially, while I expected to see changes in conviction of maybe 20 at most, instead, my indicator of sum of sign differences did exactly as I had hoped it wouldn8217t, which is to be a very binary sort of mechanic. My intuition was that between an 8220obvious bull market8221 and an 8220obvious bear market8221 that some differences would be positive, some negative, and that they8217d net each other out, and the conviction would be zero. Furthermore, that while any individual crossover is binary, all one hundred signs being either positive or negative would be a more gradual process. Apparently, this was not the case. To continue this train of thought later, one thing to try would be an all-pairs sign difference. Certainly, I don8217t feel like giving up on this idea at this point, and, as usual, feedback would always be appreciated. Dziękuje za przeczytanie. NOTE: I am currently consulting in an analytics capacity in downtown Chicago. However, I am also looking for collaborators that wish to pursue interesting trading ideas. If you feel my skills may be of help to you, let8217s talk. You can email me at ilya. kipnisgmail, or find me on my LinkedIn here . This review will be about Inovance Tech8217s TRAIDE system. It is an application geared towards letting retail investors apply proprietary machine learning algorithms to assist them in creating systematic trading strategies. Currently, my one-line review is that while I hope the company founders mean well, the application is still in an early stage, and so, should be checked out by potential usersventure capitalists as something with proof of potential, rather than a finished product ready for mass market. While this acts as a review, it8217s also my thoughts as to how Inovance Tech can improve its product. A bit of background: I have spoken several times to some of the company8217s founders, who sound like individuals at about my age level (so, fellow millennials). Ultimately, the selling point is this: Systematic trading is cool. Machine learning is cool. Therefore, applying machine learning to systematic trading is awesome (And a surefire way to make profits, as Renaissance Technologies has shown.) While this may sound a bit snarky, it8217s also, in some ways, true. Machine learning has become the talk of the town, from IBM8217s Watson (RenTec itself hired a bunch of speech recognition experts from IBM a couple of decades back), to Stanford8217s self-driving car (invented by Sebastian Thrun, who now heads Udacity), to the Netflix prize, to god knows what Andrew Ng is doing with deep learning at Baidu. Considering how well machine learning has done at much more complex tasks than 8220create a half-decent systematic trading algorithm8221, it shouldn8217t be too much to ask this powerful field at the intersection of computer science and statistics to help the retail investor glued to watching charts generate a lot more return on his or her investments than through discretionary chart-watching and noise trading. To my understanding from conversations with Inovance Tech8217s founders, this is explicitly their mission. However, I am not sure that Inovance8217s TRAIDE application actually accomplishes this mission in its current state. Here8217s how it works: Users select one asset at a time, and select a date range (data going back to Dec. 31, 2009). Assets are currently limited to highly liquid currency pairs, and can take the following settings: 1 hour, 2 hour, 4 hour, 6 hour, or daily bar time frames. Users then select from a variety of indicators, ranging from technical (moving averages, oscillators, volume calculations, etc. Mostly an assortment of 20th century indicators, though the occasional adaptive moving average has managed to sneak in8211namely KAMA8211see my DSTrading package, and MAMA8211aka the Mesa Adaptive Moving Average, from John Ehlers) to more esoteric ones such as some sentiment indicators. Here8217s where things start to head south for me, however. Namely, that while it8217s easy to add as many indicators as a user would like, there is basically no documentation on any of them, with no links to reference, etc. so users will have to bear the onus of actually understanding what each and every one of the indicators they select actually does, and whether or not those indicators are useful. The TRAIDE application makes zero effort (thus far) to actually get users acquainted with the purpose of these indicators, what their theoretical objective is (measure conviction in a trend, detect a trend, oscillator type indicator, etc.) Furthermore, regarding indicator selections, users also specify one parameter setting for each indicator per strategy. E. G. if I had an EMA crossover, I8217d have to create a new strategy for a 20170 crossover, a 21100 crossover, rather than specifying something like this: short EMA: 20-60 long EMA: 80-200 Quantstrat itself has this functionality, and while I don8217t recall covering parameter robustness checksoptimization (in other words, testing multiple parameter sets8211whether one uses them for optimization or robustness is up to the user, not the functionality) in quantstrat on this blog specifically, this information very much exists in what I deem 8220the official quantstrat manual8221, found here. In my opinion, the option of covering a range of values is mandatory so as to demonstrate that any given parameter setting is not a random fluke. Outside of quantstrat, I have demonstrated this methodology in my Hypothesis Driven Development posts, and in coming up for parameter selection for volatility trading. Where TRAIDE may do something interesting, however, is that after the user specifies his indicators and parameters, its 8220proprietary machine learning8221 algorithms (WARNING: COMPLETELY BLACK BOX) determine for what range of values of the indicators in question generated the best results within the backtest, and assign them bullishness and bearishness scores. In other words, 8220looking backwards, these were the indicator values that did best over the course of the sample8221. While there is definite value to exploring the relationships between indicators and future returns, I think that TRAIDE needs to do more in this area, such as reporting P-values, conviction, and so on. For instance, if you combine enough indicators, your 8220rule8221 is a market order that8217s simply the intersection of all of the ranges of your indicators. For instance, TRAIDE may tell a user that the strongest bullish signal when the difference of the moving averages is between 1 and 2, the ADX is between 20 and 25, the ATR is between 0.5 and 1, and so on. Each setting the user selects further narrows down the number of trades the simulation makes. In my opinion, there are more ways to explore the interplay of indicators than simply one giant AND statement, such as an 8220OR8221 statement, of some sort. (E. G. select all values, put on a trade when 3 out of 5 indicators fall into the selected bullish range in order to place more trades). While it may be wise to filter down trades to very rare instances if trading a massive amount of instruments, such that of several thousand possible instruments, only several are trading at any given time, with TRAIDE, a user selects only one asset class (currently, one currency pair) at a time, so I8217m hoping to see TRAIDE create more functionality in terms of what constitutes a trading rule. After the user selects both a long and a short rule (by simply filtering on indicator ranges that TRAIDE8217s machine learning algorithms have said are good), TRAIDE turns that into a backtest with a long equity curve, short equity curve, total equity curve, and trade statistics for aggregate, long, and short trades. For instance, in quantstrat, one only receives aggregate trade statistics. Whether long or short, all that matters to quantstrat is whether or not the trade made or lost money. For sophisticated users, it8217s trivial enough to turn one set of rules on or off, but TRAIDE does more to hold the user8217s hand in that regard. Lastly, TRAIDE then generates MetaTrader4 code for a user to download. And that8217s the process. In my opinion, while what Inovance Tech has set out to do with TRAIDE is interesting, I wouldn8217t recommend it in its current state. For sophisticated individuals that know how to go through a proper research process, TRAIDE is too stringent in terms of parameter settings (one at a time), pre-coded indicators (its target audience probably can8217t program too well), and asset classes (again, one at a time). However, for retail investors, my issue with TRAIDE is this: There is a whole assortment of undocumented indicators, which then move to black-box machine learning algorithms. The result is that the user has very little understanding of what the underlying algorithms actually do, and why the logic he or she is presented with is the output. While TRAIDE makes it trivially easy to generate any one given trading system, as multiple individuals have stated in slightly different ways before, writing a strategy is the easy part. Doing the work to understand if that strategy actually has an edge is much harder. Namely, checking its robustness, its predictive power, its sensitivity to various regimes, and so on. Given TRAIDE8217s rather short data history (2017 onwards), and coupled with the opaqueness that the user operates under, my analogy would be this: It8217s like giving an inexperienced driver the keys to a sports car in a thick fog on a winding road. Nobody disputes that a sports car is awesome. However, the true burden of the work lies in making sure that the user doesn8217t wind up smashing into a tree. Overall, I like the TRAIDE application8217s mission, and I think it may have potential as something for the retail investors that don8217t intend to learn the ins-and-outs of coding a trading system in R (despite me demonstrating many times over how to put such systems together). I just think that there needs to be more work put into making sure that the results a user sees are indicative of an edge, rather than open the possibility of highly-flexible machine learning algorithms chasing ghosts in one of the noisiest and most dynamic data sets one can possibly find. My recommendations are these: 1) Multiple asset classes 2) Allow parameter ranges, and cap the number of trials at any given point (E. G. 4 indicators with ten settings each 10,000 possible trading systems blow up the servers). To narrow down the number of trial runs, use techniques from experimental design to arrive at decent combinations. (I wish I remembered my response surface methodology techniques from my master8217s degree about now) 3) Allow modifications of order sizing (E. G. volatility targeting, stop losses), such as I wrote about in my hypothesis-driven development posts. 4) Provide some sort of documentation for the indicators, even if it8217s as simple as a link to investopedia (preferably a lot more). 5) Far more output is necessary, especially for users who don8217t program. Namely, to distinguish whether or not there is a legitimate edge, or if there are too few observations to reject the null hypothesis of random noise. 6) Far longer data histories. 2017 onwards just seems too short of a time-frame to be sure of a strategy8217s efficacy, at least on daily data (may not be true for hourly). 7) Factor in transaction costs. Trading on an hourly time frame will mean far less PampL per trade than on a daily resolution. If MT4 charges a fixed ticket price, users need to know how this factors into their strategy. 8) Lastly, dogfooding. When I spoke last time with Inovance Tech8217s founders, they claimed they were using their own algorithms to create a forex strategy, which was doing well in live trading. By the time more of these suggestions are implemented, it8217d be interesting to see if they have a track record as a fund, in addition to as a software provider. If all of these things are accounted for and automated, the product will hopefully accomplish its mission of bringing systematic trading and machine learning to more people. I think TRAIDE has potential, and I8217m hoping that its staff will realize that potential. Dziękuje za przeczytanie. NOTE: I am currently contracting in downtown Chicago, and am always interested in networking with professionals in the systematic trading and systematic asset managementallocation spaces. Find my LinkedIn here. EDIT: Today in my email (Dec. 3, 2018), I received a notice that Inovance was making TRAIDE completely free. Perhaps they want a bunch more feedback on it This post will demonstrate a method to create an ensemble filter based on a trade-off between smoothness and responsiveness, two properties looked for in a filter. An ideal filter would both be responsive to price action so as to not hold incorrect positions, while also be smooth, so as to not incur false signals and unnecessary transaction costs. So, ever since my volatility trading strategy, using three very naive filters (all SMAs) completely missed a 27 month in XIV. I8217ve decided to try and improve ways to create better indicators in trend following. Now, under the realization that there can potentially be tons of complex filters in existence, I decided instead to focus on a way to create ensemble filters, by using an analogy from statisticsmachine learning. In static data analysis, for a regression or classification task, there is a trade-off between bias and variance. In a nutshell, variance is bad because of the possibility of overfitting on a few irregular observations, and bias is bad because of the possibility of underfitting legitimate data. Similarly, with filtering time series, there are similar concerns, except bias is called lag, and variance can be thought of as a 8220whipsawing8221 indicator. Essentially, an ideal indicator would move quickly with the data, while at the same time, not possess a myriad of small bumps-and-reverses along the way, which may send false signals to a trading strategy. So, here8217s how my simple algorithm works: The inputs to the function are the following: A) The time series of the data you8217re trying to filter B) A collection of candidate filters C) A period over which to measure smoothness and responsiveness, defined as the square root of the n-day EMA (2(n1) convention) of the following: a) Responsiveness: the squared quantity of pricefilter 8211 1 b) Smoothness: the squared quantity of filter(t)filter(t-1) 8211 1 (aka R8217s return. calculate) function D) A conviction factor, to which power the errors will be raised. This should probably be between .5 and 3 E) A vector that defines the emphasis on smoothness (vs. emphasis on responsiveness), which should range from 0 to 1. Here8217s the code: This gets SPY data, and creates two utility functions8211xtsApply, which is simply a column-based apply that replaces the original index that using a column-wise apply discards, and sumIsNa, which I use later for counting the numbers of NAs in a given row. It also creates my candidate filters, which, to keep things simple, are just SMAs 2-250. Here8217s the actual code of the function, with comments in the code itself to better explain the process from a technical level (for those still unfamiliar with R, look for the hashtags): The vast majority of the computational time takes place in the two xtsApply calls. On 249 different simple moving averages, the process takes about 30 seconds. Here8217s the output, using a conviction factor of 2: And here is an example, looking at SPY from 2007 through 2017. In this case, I chose to go from blue to green, orange, brown, maroon, purple, and finally red for smoothness emphasis of 0, 5, 25, 50, 75, 95, and 1, respectively. Notice that the blue line is very wiggly, while the red line sometimes barely moves, such as during the 2017 drop-off. One thing that I noticed in the course of putting this process together is something that eluded me earlier8211namely, that naive trend-following strategies which are either fully long or fully short based on a crossover signal can lose money quickly in sideways markets. However, theoretically, by finely varying the jumps between 0 to 100 emphasis on smoothness, whether in steps of 1 or finer, one can have a sort of 8220continuous8221 conviction, by simply adding up the signs of differences between various ensemble filters. In an 8220uptrend8221, the difference as one moves from the most responsive to most smooth filter should constantly be positive, and vice versa. In the interest of brevity, this post doesn8217t even have a trading strategy attached to it. However, an implied trading strategy can be to be long or short the SPY depending on the sum of signs of the differences in filters as you move from responsiveness to smoothness. Of course, as the candidate filters are all SMAs, it probably wouldn8217t be particularly spectacular. However, for those out there who use more complex filters, this may be a way to create ensembles out of various candidate filters, and create even better filters. Furthermore, I hope that given enough candidate filters and an objective way of selecting them, it would be possible to reduce the chances of creating an overfit trading system. However, anything with parameters can potentially be overfit, so that may be wishful thinking. All in all, this is still a new idea for me. For instance, the filter to compute the error terms can probably be improved. The inspiration for an EMA 20 essentially came from how Basel computes volatility (if I recall, correctly, it uses the square root of an 18 day EMA of squared returns), and the very fact that I use an EMA can itself be improved upon (why an EMA instead of some other, more complex filter). In fact, I8217m always open to how I can improve this concept (and others) from readers. Dziękuje za przeczytanie. NOTE: I am currently contracting in Chicago in an analytics capacity. If anyone would like to meet up, let me know. You can email me at ilya. kipnisgmail, or contact me through my LinkedIn here. This post will deal with a quick, finger in the air way of seeing how well a strategy scales8211namely, how sensitive it is to latency between signal and execution, using a simple volatility trading strategy as an example. The signal will be the VIXVXV ratio trading VXX and XIV, an idea I got from Volatility Made Simple8217s amazing blog. particularly this post. The three signals compared will be the 8220magical thinking8221 signal (observe the close, buy the close, named from the ruleOrderProc setting in quantstrat), buy on next-day open, and buy on next-day close. Let8217s get started. So here8217s the run-through. In addition to the magical thinking strategy (observe the close, buy that same close), I tested three other variants8211a variant which transacts the next open, a variant which transacts the next close, and the average of those two. Effectively, I feel these three could give a sense of a strategy8217s performance under more realistic conditions8211that is, how well does the strategy perform if transacted throughout the day, assuming you8217re managing a sum of money too large to just plow into the market in the closing minutes (and if you hope to get rich off of trading, you will have a larger sum of money than the amount you can apply magical thinking to). Ideally, I8217d use VWAP pricing, but as that8217s not available for free anywhere I know of, that means that readers can8217t replicate it even if I had such data. In any case, here are the results. Log scale (for Mr. Tony Cooper and others): My reaction The execute on next day8217s close performance being vastly lower than the other configurations (and that deterioration occurring in the most recent years) essentially means that the fills will have to come pretty quickly at the beginning of the day. While the strategy seems somewhat scalable through the lens of this finger-in-the-air technique, in my opinion, if the first full day of possible execution after signal reception will tank a strategy from a 1.44 Calmar to a .92, that8217s a massive drop-off, after holding everything else constant. In my opinion, I think this is quite a valid question to ask anyone who simply sells signals, as opposed to manages assets. Namely, how sensitive are the signals to execution on the next day After all, unless those signals come at 3:55 PM, one is most likely going to be getting filled the next day. Now, while this strategy is a bit of a tomato can in terms of how good volatility trading strategies can get (they can get a lot better in my opinion), I think it made for a simple little demonstration of this technique. Again, a huge thank you to Mr. Helmuth Vollmeier for so kindly keeping up his dropbox all this time for the volatility data Thanks for reading. NOTE: I am currently contracting in a data science capacity in Chicago. You can email me at ilya. kipnisgmail, or find me on my LinkedIn here. I8217m always open to beers after work if you8217re in the Chicago area. NOTE 2: Today, on October 21, 2018, if you8217re in Chicago, there8217s a Chicago R Users Group conference at Jaks Tap at 6:00 PM. Free pizza, networking, and R, hosted by Paul Teetor, who8217s a finance guy. Mam nadzieję, że się tam zobaczymy. This post deals with an impossible-to-implement statistical arbitrage strategy using VXX and XIV. The strategy is simple: if the average daily return of VXX and XIV was positive, short both of them at the close. This strategy makes two assumptions of varying dubiousness: that one can 8220observe the close and act on the close8221, and that one can short VXX and XIV. So, recently, I decided to play around with everyone8217s two favorite instruments on this blog8211VXX and XIV, with the idea that 8220hey, these two instruments are diametrically opposed, so shouldn8217t there be a stat-arb trade here8221 So, in order to do a lick-finger-in-the-air visualization, I implemented Mike Harris8217s momersion indicator . And then I ran the spread through it. In other words, this spread is certainly mean-reverting at just about all times. And here is the code for the results from 2017 onward, from when the XIV and VXX actually started trading. Here are the equity curves: With the following statistics: In other words, the short side is absolutely amazing as a trade8211except for the one small fact of having it be impossible to actually execute, or at least as far as I8217m aware. Anyhow, this was simply a for-fun post, but hopefully it served some purpose. Dziękuje za przeczytanie. NOTE: I am currently contracting and am looking to network in the Chicago area. You can find my LinkedIn here. Post navigation Categories

No comments:

Post a Comment