Gerando sinais comerciais em r
GERANDO SINAIS DE NEGOCIAÇÃO NA R.
Robô de opção binária | Os melhores robôs binários e amp; Broker ...
Binary Option Robot é o melhor lugar para os comerciantes que buscam revisões objetivas de robôs e corretores. . com base em sinais de negociação binária, gerados por complexos,.
Análise do Indicador Técnico MACD Sinais de Negociação Forex.
MACD - Convergência média móvel / Divergência Análise técnica Forex e sinais de negociação Forex MACD. Desenvolvido por Gerald Appel, The ...
Binary Options Trading Signals - Aplicativo para Android em ...
Sinais de negociação de opções binárias:. William% R). Esses sinais são criados com binário. Nós executamos esses dados através de nossos algoritmos de negociação gerando sinais binários.
Alguém realmente usa sinais comerciais pagos? • r / Forex.
Regras para o / r / Forex subreddit 1:. Alguém realmente usa sinais comerciais pagos? . gerando um site,.
Sinais de negociação Forex gratuitos - Precisão de 90% - Grátis ...
04.05.2018 & # 0183; & # 32; Sinais de negociação de Forex gratuitos. dinheiro é necessário e você não está gerando dinheiro de pessoas que comercializam estes. sinais, mas inclua TP, SL, R.
Definição Estratégia Forex Trading | Investopedia.
11.05.2008 & # 0183; & # 32; Estratégias de negociação Forex podem ser métodos manuais ou automatizados para gerar sinais de negociação. . procurando sinais comerciais,. um Forex Trading.
Forex Trading Signals Ranking da aplicação e dados da loja | App Annie.
Confira o ranking diário do aplicativo, o histórico de classificação, as classificações, os recursos e as avaliações de melhores aplicativos como Forex Trading Signals na Google Play Store.
Melhor software de análise técnica com venda de compra precisa.
A melhor análise técnica e amp; comprar software de geração de sinal de venda. na sua negociação. Trade Smooth Trading. e nossa equipe de R & amp; D desenvolverá novas negociações.
App Shopper: sinais de negociação de opções binárias (Finanças)
Sinais de negociação de opções binárias. através de nossos algoritmos de negociação gerando binário. divergências de baixa em esses sinais de opções de estoque. - William% R.
Opções de negociação: mariposa mestra se espalha e outros ...
Opções de Sinais de Negociação. Fazendo tempo trabalhar. dando-lhes a menor quantidade de risco e a maior probabilidade de geração. Boa negociação. . . . - Bill R. -
Gerando sinais comerciais em r
Modelagem Financeira Quantitativa & amp; Quadro de negociação para R.
Apresentando quantmod:
É possível com uma função quantmod carregar dados de uma variedade de fontes, incluindo. Yahoo! Finanças (dados da OHLC) Banco da Reserva Federal de St. Louis FRED & # 174 (11,000 séries econômicas) Google Finance (dados OHLC) Oanda, o Site Moeda (FX e Metals) Bases de dados MySQL (seus dados locais) R formatos binários (.RData e. rda) Arquivos de valores separados por vírgulas (.csv) Mais para vir incluindo (RODBC, economagic, Rbloomberg.) Como você pergunta?
Obtendo dados.
& gt; getSymbols ("GOOG", src = "quantmod / examples / intro / yahoo") # do yahoo finance.
& gt; getSymbols ("DEXJPUS", src = "quantmod / examples / intro / FRED") # Taxas de FX de FRED.
& gt; getSymbols ("XPT / USD", src = "quantmod / examples / intro / Oanda") # Platinum from Oanda.
[1] "XPTUSD" Cada chamada resulta em dados que estão sendo carregados diretamente no seu espaço de trabalho, com o nome do objeto retornado da chamada. Um pouco útil, mas fica melhor. & gt; # Especificar parâmetros de pesquisa e salvar para futuras sessões.
& gt; # novas sessões chamada loadSymbolLookup (file = "mysymbols. rda")
[1] "YHOO" "GOOG" "DEXJPUS" "XPTUSD" Agora é fácil carregar dados de diferentes fontes em seu espaço de trabalho (ou qualquer outro ambiente) sem exigir explicitamente a atribuição ou lembrar / especificar constantemente os parâmetros de conexão. Pense nisso como um comando de carga que pode buscar dados de quase qualquer lugar. Experimente você mesmo, getdata. R.
Charting com quantmod.
Agora que temos alguns dados, podemos querer olhar para ele. Digite a nova função chartSeries. No momento, esta é uma ótima ferramenta para visualizar as séries temporais financeiras de uma forma que muitos praticantes estão familiarizados com gráficos de linha, bem como cartas de barra e vela de OHLC. Existem wrappers de conveniência para esses estilos diferentes (lineChart, barChart e candleChart), embora o chartSeries faça um pouco para manipular automaticamente os dados da maneira mais apropriada.
Um rápido olhar sobre como criar alguns gráficos, incluindo alguns recursos e um olhar sobre o que vem em lançamentos futuros. & gt; # Especificar parâmetros de pesquisa e salvar para futuras sessões.
& gt; # Adicione multi-coloring e mude o fundo para o branco.
& gt; chartSeries (XPTUSD, name = "Platinum (.oz) em $ USD")
Ferramentas de gráficos de análise técnica.
Usando os dados para gerar sinais.
Os modelos de construção serão principalmente deixados para uma série de exemplo posterior, mas para aqueles ansiosos para continuar a perder uma tarde de sexta-feira no trabalho (quando a maioria dos meus visitantes parecem aparecer), vou continuar.
Modelar em R é o que é R. Os dados são alimentados nesta discussão com a maior preavaliação devido ao fato de que muitos dados financeiros não estão contidos em objetos de dados únicos. Muito, se não tudo, tem que ser coletado e agregado por você, o modelador.
É aqui que as fontes de dados e os parâmetros de conexão pré-especificados são úteis. setSymbolLookup permite ao modelador a oportunidade de instruir quantmod aos dados de origem - dado um símbolo específico - de uma maneira particular. Ao construir modelos em R, muitas vezes uma fórmula é passada para a função de ajuste juntamente com o objeto de dados apropriado para pesquisar.
Para lidar com muitas fontes diferentes, é necessário criar um objeto de dados com todas as colunas pré-especificadas, OU para usar objetos visíveis no ambiente do usuário. Ambos têm desvantagens óbvias - não menos importante, dependendo do modelador ter carregado e alinhado manualmente a série em questão.
No melhor, isso é demorado e certamente não é muito esclarecedor. Na pior das hipóteses, pode ser perigoso, pois o tratamento de dados é inerentemente propenso a erros. Os erros de dados na pesquisa podem ser dispendiosos, os erros de dados na negociação podem levar rapidamente a uma nova carreira. Dito isto, vou ressaltar os termos da LICENÇA indicando a GARANTIA COMPLETA DE GARANTIA em relação a este software e a todos os R para esse assunto. Usuário cuidado!
Para facilitar esse problema de dados relativamente exclusivo, o quantmod cria dinamicamente objetos de dados para uso no processo de modelagem, criando um quadro modelo internamente depois de passar por uma série de etapas para identificar as fontes de dados necessárias - carregando, se necessário. specifModel é a função de cavalo-de-obra para lidar com todos os problemas de dados, e o arquivo de ajuda deve ser lido para entender completamente o que está acontecendo internamente. Para os nossos propósitos aqui, basta saber que se pode especificar QUALQUER dado dentro da chamada para especificarModel, e o quantmod irá lidar com a pesquisa e a agregação de dados para você. É claro que os dados devem ser localizáveis e únicos, mas isso provavelmente foi suspeitado.
Vamos dar uma olhada em um exemplo de especificarModel: & gt; # Crie um objeto quantmod para uso em.
& gt; # na montagem modelo posterior. Observe que existe.
& gt; # não precisa carregar os dados antes de sua mão.
mm é agora um objeto quantmod que contém a fórmula do modelo e a estrutura de dados que implica que o próximo (próximo) período aberto ao fechamento do S & amp; P 500 ETF (OpCl (SPY)) é modelado como uma fucntion do período atual aberto para fechar e o fechamento atual do VIX (Cl (VIX)).
A chamada para modelData extrai o conjunto de dados relevantes, com transformações aplicadas magicamente. Você pode levar os dados e fazer com ele como você gostaria. Uma função mais direta para realizar o mesmo final é buildData.
Qual é o próximo?
Como sobre alguns exemplos de manipulação de dados do quantmod.
Extração de sinal híbrido, previsão e negociação financeira.
iMetrica: econometria e estratégias de negociação financeira.
Tagged com negociação de alta freqüência em R.
Negociação financeira de alta freqüência em futuros de índices com MDFA e R: um exemplo com o EURO STOXX50.
Figura 1: Desempenho na amostra e fora da amostra (observações 240-457) do sinal de negociação para os futuros do índice Euro Stoxx50 com vencimento em 18 de março (STXE H3) durante o período de 1-9-2018 e 2-1 -2018, usando log-returns de 15 minutos. As linhas pontilhadas pretas indicam um sinal de compra / longo e as linhas pontilhadas azuis indicam uma venda / curta (parte superior).
Neste segundo tutorial sobre a construção de sinais de negociação financeira de alta freqüência usando a abordagem de filtro direto multivariante em R, foco no primeiro exemplo do meu artigo anterior sobre engenharia de sinal na negociação de alta freqüência de futuros de índices financeiros, onde considero registro de 15 minutos - retornos dos futuros do índice Euro STOXX50 com vencimento em 18 de março de 2018 (STXE H3). Como mencionei na introdução, adicionei um passo um pouco novo na minha abordagem para a construção de sinais para observações intradias, pois estava estudando o problema de variações próximas ao domínio de freqüência. Com dados de log-retorno de 15 minutos, vejo a estrutura de freqüência relacionada à variação de fechar-abrir no preço, ou seja, quando o preço ao fechamento das horas de mercado difere significativamente do preço aberto, um efeito I & # 8217 Eu mencionei nos meus dois artigos anteriores sobre os dados de retorno de log intradía. Eu vou mostrar (desta vez em R) como o MDFA pode aproveitar esta variação de preço e lucro de cada um por # 8216; previsão & # 8217; com o sinal extraído o salto ou queda no preço no aberto do próximo dia de negociação. Parece bom para ser verdade, certo? Eu demonstrei neste artigo como é possível.
O primeiro passo depois de analisar o preço do log e os dados de log-return do objeto que está sendo negociado é construir o periodograma dos dados na amostra que estão sendo comercializados. Neste exemplo, eu trabalho com o mesmo período de tempo que fiz com o meu tutorial R anterior, considerando a parte da amostra em meus dados de 1-4-2018 para 1-23-2018, com a minha saída de amostra O intervalo de dados é de 1-23-2018 a 2-1-2018, que será usado para analisar o verdadeiro desempenho do sinal de negociação. Os dados STXE e as séries explicativas do EURO STOXX50 são primeiro carregados em R e, em seguida, o periodograma é calculado da seguinte forma.
Você notará no periodograma dos log-return STXE na amostra que eu identifiquei um pico espectral entre duas linhas tracejadas azuis. Esse pico corresponde a um ciclo intrinsecamente importante nos log-returns de 15 minutos de futuros de índice que dá acesso à previsão da variação do preço próximo ao aberto. Como você verá, o ciclo flui fluentemente através dos 26 intervalos de 15 minutos durante cada dia de negociação e cruzará zero em (geralmente) um a dois pontos durante cada dia de negociação para indicar se vai longo ou curto índice para o próximo dia. Eu deduzi esta faixa de freqüência ótima em uma análise prévia desses dados que fiz usando o meu kit de ferramentas de filtro de destino no iMetrica (ver artigo anterior). Essa faixa de freqüência dependerá da freqüência das observações intradias e também pode depender do índice (mas em meus experimentos, esse intervalo geralmente é consistente entre 0,23 e 0,32 para a maioria dos futuros de índice usando observações de 15 minutos). Assim, no código R acima, eu definei um ponto de corte de freqüência em .32 e pontos de passagem de banda superior e inferior em .32 e .23, respectivamente.
Figura 2: Periodograma dos dados STXE log-return. O pico espectral é extraído e destacado entre as duas linhas tracejadas vermelhas.
Nesta primeira parte do tutorial, eu extrai esse ciclo responsável por marcar as variações de fechar-abrir e mostrar o quão bom ele pode executar. Como eu mencionei em meus artigos anteriores sobre a extração de sinal de negociação, eu gostaria de começar com a solução do meio-quadrado (ou seja, sem personalização ou regularização) para o problema de extração para ver exatamente qual o tipo de parametrização que talvez eu precise. Para produzir a solução do quadrado médio da baunilha, configurei todos os parâmetros para 0,0 e, em seguida, computei o filtro chamando a função MDFA principal (mostrado abaixo). A função IMDFA retorna um objeto com os coeficientes de filtro e o sinal na amostra. Ele também traça a função de transferência simultânea para ambos os filtros, juntamente com os coeficientes de filtro para aumentar o atraso, mostrado na Figura 3.
Figura 3: Funções de transferência simultânea para as séries STXE (vermelho) e explicativas (ciano) (topo). Coeficientes para STXE e séries explicativas (inferior).
Observe o vazamento de ruído após a faixa de parada no filtro concorrente e a rugosidade de ambos os conjuntos de coeficientes de filtro (devido à superposição). Gostaríamos de suavizar ambos os casos, além de permitir que os coeficientes do filtro diminuam à medida que o atraso aumenta. Isso garante propriedades mais consistentes na amostra e fora da amostra do filtro. Primeiro, aplico um pouco de suavização para o stopband aplicando um parâmetro de peso do valor de 16 e, para compensar ligeiramente essa suavidade aprimorada, aperfeiçoo a pontualidade ajustando o parâmetro lambda para 1. Depois de perceber a melhora na suavidade dos coeficientes de filtro, eu então prosseguir com a regularização e concluir com os seguintes parâmetros.
Figura 4: Transferir funções e coeficientes após suavização e regularização.
Uma grande melhoria em relação à solução do meio-quadrado. Praticamente nenhum vazamento de ruído na banda de parada passou e os coeficientes se deterioram lindamente com a suavidade perfeita alcançada. Observe as duas funções de transferência escolhendo perfeitamente o pico espectral que é intrínseco ao ciclo de fechamento aberto que mencionei estava entre 0,23 e 0,32. Para verificar que esses coeficientes de filtro conseguem a extração do ciclo de fechamento para abrir, computo o sinal de negociação do objeto imdfa e depois traço-o contra os retornos de log da STXE. Eu então calculo os negócios na amostra usando o sinal e o preço de log da STXE. O código R está abaixo e as parcelas são mostradas nas Figuras 5 e 6.
Figura 5: O sinal na amostra e os retornos de registro do SXTE em observações de 15 minutos de 1-9-2018 para 1-23-2018.
A Figura 5 mostra os dados de retorno de log e o sinal de negociação extraído dos dados. Os picos nos dados de retorno de registro representam os saltos abertos no contrato de futuros do índice STOXX Europe 50, ocorrendo a cada 27 observações. Mas observe como o sinal é regular, e quão consistente esta faixa de freqüência é encontrada nos dados de registro-retorno, quase como uma onda sinusoidal perfeita, com um ciclo completo ocorrendo quase todas as 27 observações. Este sinal desencadeia negócios que são mostrados na Figura 6, onde as linhas pontilhadas pretas são compradas / longas e as linhas pontilhadas azuis são vendidas / shorts. O sinal é extremamente consistente em encontrar os momentos oportunos para comprar e vender nos picos quase ótimos, como nas observações 140, 197 e 240. Ele também "prediz" # 8217; o salto ou queda do futuro do índice EuroStoxx50 para o próximo dia de negociação, desencadeando o sinal de compra / venda necessário, como nas observações 19, 40, 51, 99, 121, 156 e, 250. O desempenho dessa negociação, A amostra é mostrada na Figura 7.
Figura 6: Os negócios na amostra. As linhas pontilhadas pretas são compradas / longas e as linhas pontilhadas azuis são vendidas / curtas.
Figura 7: O desempenho na amostra do sinal de negociação.
Agora, para o verdadeiro teste decisivo no desempenho deste sinal extraído, precisamos aplicar o filtro fora de amostra para verificar a consistência, não só no desempenho, mas também nas características comerciais. Para fazer isso em R, vinculamos os dados na amostra e fora da amostra juntos e, em seguida, aplicamos o filtro no conjunto fora da amostra (precisando as observações L-1 finais da porção na amostra). O sinal resultante mostrado na Figura 8.
O sinal e os dados de retorno de registro Observe que o sinal executa consistentemente fora de amostra até à direita em torno da observação 170 quando os retornos de registro se tornam cada vez mais voláteis. O ciclo intrínseco entre as frequências .23 e .32 foi desacelerado devido a esta volatilidade aumentada e pode afetar o desempenho comercial.
Figura 8: Sinal produzido fora da amostra em 210 observações e dados de retorno de registro de STXE.
O desempenho de negociação total na amostra mais fora de amostra é mostrado na Figura 9 e 10, sendo os 210 pontos finais fora de amostra. O desempenho fora da amostra é muito parecido com o desempenho em amostra que tivemos, com uma negociação sistemática muito clara exposta pela & # 8216; previsão & # 8217; No próximo dia, o salto ou a queda do próximo-a-aberto, de forma consistente, desencadeando o sinal de compra / venda necessário, como nas observações 310, 363, 383 e 413, com apenas uma perda até o dia final de negociação. A maior volatilidade durante o último dia do período de amostragem prejudica o sinal cíclico e falha ao comércio de forma sistemática como ocorreu durante as primeiras 420 observações.
Figura 9: O total na amostra mais fora de amostra compra e vende.
Figura 10: Desempenho total em períodos in-sample e out-of-sample.
Com este tipo de desempenho, tanto na amostra como fora da amostra, e os padrões de negociação bastante consistentes e metodológicos que este sinal fornece, parece que tentar melhorar, seria uma tarefa inútil. Por que tentar consertar o que # 8217; s não & # 8220; quebrado & # 8221 ;. Mas, sendo o perfeccionista que sou, luto por um "melhor" e # 8221; filtro. Se houvesse apenas uma maneira de 1) manter os efeitos de negociação cíclicos consistentes como antes 2) & # 8216; prever & # 8217; no próximo dia, o salto / queda do futuro do Euro Stoxx50 como antes, e 3) evitar períodos voláteis para eliminar o comércio errado, onde o sinal foi pior. Depois de horas passadas na iMetrica, imaginei como fazê-lo. É aqui que a engenharia avançada de sinais comerciais entra em jogo.
O primeiro passo foi incluir todas as freqüências mais baixas abaixo de .23, que não estavam incluídas no meu sinal de negociação anterior. Devido à baixa quantidade de atividade nessas freqüências mais baixas, isso só deve fornecer o efeito ou um aumento & # 8217; ou um & # 8216; push & # 8217; ou o sinal local, enquanto ainda mantém o componente cíclico. Então, depois de mudar meu filtro de passagem baixa com o ponto de corte ajustado, eu computei o filtro com o novo design de passagem baixa. As funções de transferência para os coeficientes do filtro são mostradas abaixo na Figura 11, com o gráfico colorido vermelho a função de transferência para o STXE. Observe que a função de transferência para a série explicativa ainda privilegia o pico espectral entre 0,23 e 0,32, com apenas um ligeiro aumento na freqüência zero (compare isso com o design de passagem de banda na Figura 4, não mudou muito). O problema é que o pico excede 1,0 na banda passante, e isso ampliará o componente cíclico extraído do log-return. Pode ser bom, comercializado, mas não o que eu procuro fazer. Para o filtro STXE, obtemos um pouco mais de elevação na freqüência zero, no entanto, isso foi compensado com uma diminuição da extração do ciclo entre as freqüências .23 e .32. Além disso, uma pequena quantidade de ruído entrou no stopband, outro fator que devemos acalmar.
Figura 11: As funções de transferência simultânea depois de mudar para filtro de passagem baixa.
Para melhorar as propriedades de filtro simultâneas para ambos, eu aumento o tempo de limpeza de suavização para 26, o que afetará o lambda_smooth, portanto, diminuo para .70. Isso me dá um par de funções de transferência muito melhor, mostrado na Figura 12. Observe que o pico na função de transferência de série explicativa está agora muito mais próximo de 1,0, exatamente o que queremos.
Figura 12: As funções de transferência simultânea depois de mudar para o filtro de passagem baixa, aumentando o peso do ventilador para 26 e diminuindo lambda_smooth para .70.
Eu ainda não estou satisfeito com o elevador na freqüência zero para a série STXE. Em aproximadamente .5 na freqüência zero, o filtro pode não fornecer empurrar ou puxar o que preciso. A única maneira de garantir uma elevação garantida na série STXE log-return é empregar restrições nos coeficientes de filtro, de modo que a função de transferência seja uma na freqüência zero. Isso pode ser conseguido definindo i1 como verdadeiro na chamada de função IMDFA, o que efetivamente garante que a soma dos coeficientes de filtro seja uma. Depois de fazer isso, recebo as seguintes funções de transferência e os respectivos coeficientes de filtro.
Figura 13: função de transferência e coeficientes de filtro depois de definir a restrição de coeficiente i1 como verdadeira.
Agora, isso é exatamente o que eu estava procurando. Não só a função de transferência para a série explicativa mantém intacto o importante ciclo de fechamento aberto, mas também apliquei o elevador que preciso para a série STXE. Os coeficientes ainda permanecem suaves com uma boa propriedade decadente no final. Com os novos coeficientes de filtro, eu os apliquei nos dados tanto na amostra como fora da amostra, obtendo o sinal comercial mostrado na Figura 14. Possui exatamente as propriedades que eu estava procurando. O componente cíclico próximo a aberto ainda está sendo extraído (graças, em parte, à série explicativa), e ainda é relativamente consistente, embora não tanto quanto o design de passagem de banda pura. O recurso que eu gosto é o seguinte: quando os dados de log-return divergem do componente cíclico, com o aumento da volatilidade, o filtro STXE reage pressionando o sinal para evitar qualquer negociação errônea. Isso pode ser visto nas observações 100 a 120 e depois nas observações 390 até o final da negociação. A Figura 15 (da mesma forma que a Figura 1 na parte superior do artigo) mostra os negócios e o desempenho resultantes produzidos na amostra e fora da amostra por este sinal. Esta é a arte do pessoal meticuloso de engenharia de sinais.
Figura 14: Sinal na amostra e fora da amostra produzido a partir da passagem baixa com restrições de coeficientes i1.
Com apenas duas perdas sofridas fora da amostra durante os aproximadamente 9 dias de negociação, o filtro é muito mais metodologicamente do que antes. Aviso durante os últimos dois dias de negociação, quando a volatilidade apanhou, o sinal deixa de ser comercializado, pois está sendo pressionado. Até continua a prever & # 8217; O salto / queda do close-to-open corretamente, como nas observações 288, 321 e 391. O último comércio feito foi uma posição de venda / venda curta, com o sinal tendendo para baixo no final. O filtro está em posição de fazer um enorme ganho com esta sinalização oportuna de uma posição curta em 391, determinando corretamente uma grande queda no próximo dia de negociação, e depois aguardando a negociação volátil. O ganho deve ser grande, não importa o que aconteça.
Figura 15: Desempenho na amostra e fora da amostra do design do filtro constrangido i1.
Uma coisa que mencionei antes de concluir é que fiz um pequeno ajuste ao design do filtro depois de empregar a restrição i1 para obter os resultados mostrados na Figura 13-15. Eu deixarei isso como um exercício para o leitor deduzir o que fiz. Dica: veja os graus de liberdade congelados antes e depois de aplicar a restrição i1. Se você ainda tem dificuldade em encontrar o que eu fiz, envie-me um e-mail e eu irei dar mais sugestões.
Conclusão.
O desempenho geral do primeiro filtro construído, em relação ao retorno total do investimento fora da amostra, foi superior ao segundo. No entanto, esse desempenho superior vem apenas com a suposição de que o componente do ciclo definido entre as freqüências .23 e .32 continuará presente nas futuras observações da STXE até a expiração. Se a volatilidade aumentar e esse ciclo intrínseco deixa de existir nos dados de log-return, o desempenho irá deteriorar-se.
Para uma abordagem melhor e mais confortável que lida com a mudança de condições de índice volátil, eu optaria por garantir que o viés local esteja presente no sinal. Isto irá efetivamente empurrar ou puxar o sinal para baixo ou para cima quando o ciclo intrínseco é fraco no aumento volatilidade, resultando em uma retração na atividade de negociação.
Como antes, você pode adquirir os dados de alta freqüência usados neste tutorial, solicitando-o por e-mail.
Negociação financeira de alta freqüência no FOREX com MDFA e R: um exemplo com o iene japonês.
Figura 1: In-sample (observações 1-250) e desempenho fora da amostra do sinal de negociação incorporado neste tutorial usando MDFA. (Top) O preço de registro do Yen (FXY) em intervalos de 15 minutos e os negócios gerados pelo sinal de negociação. Aqui a linha preta é uma compra (longa), o azul é vendido (posição curta). (Fundo) Os retornos acumulados (caixa) gerados pela negociação, em porcentagem obtida ou perdida.
No meu artigo anterior sobre o comércio de alta frequência no iMetrica no FOREX / GLOBEX, introduzi algumas estratégias robustas de extração de sinal no iMetrica usando a abordagem de filtro direto multidimensional (MDFA) para gerar sinais de alto desempenho para negociação no mercado de câmbio e Futuros . Neste artigo, faço uma breve falta de ausência do meu mundo de desenvolvimento de sinais de negociação financeira no iMetrica e migre para uma linguagem uber-popular usada em finanças devido à sua exuberante variedade de pacotes, gerenciamento rápido de dados e manipulação de gráficos e de Claro que é livre (como na fala e cerveja) em quase qualquer plataforma de computação do mundo.
Este artigo oferece um tutorial de introdução no uso de R para negociação de alta freqüência no mercado FOREX usando o pacote R para MDFA (oferecido por Herr Doktor Marc Wildi von Bern) e algumas estratégias desenvolvidas pela I & # 8217 para gerar sinais de negociação financeiramente robustos . Para este tutorial, considero o segundo exemplo dado no meu artigo anterior, onde criei um sinal comercial para logaritmos de 15 minutos do iene japonês (de abertura de sino a mercado fechado EST). Isso apresentou desafios um pouco novos do que antes, pois as variações de salto fechadas para abrir são muito maiores que as geradas por retornos horários ou diários. Mas, como mostrei, essas variações maiores no preço fechado aberto não representavam problemas para o MDFA. Na verdade, explorou esses saltos e ganhou grandes lucros ao prever a direção do salto. A Figura 1 na parte superior deste artigo mostra o desempenho na amostra (observações 1-250) e fora da amostra (observações 251 em diante) do filtro que vou construir na primeira parte deste tutorial.
Ao longo deste tutorial, eu tento replicar esses resultados que eu construí no iMetrica e expandi-los um pouco usando o idioma R e a implementação do MDFA disponível aqui. Os dados que consideramos são log-devoluções de 15 minutos do iene de 4 de janeiro a 17 de janeiro e eu os guardo como um arquivo. RData dado por ld_fxy_insamp. Eu tenho uma série explicativa adicional embutida no arquivo. RData que I & # 8217; m usando para prever o preço do iene. Além disso, eu também usarei price_fxy_insamp qual é o preço de registro do iene, usado para calcular o desempenho (compra / venda) do sinal de negociação. O ld_fxy_insamp será usado como dados na amostra para construir o filtro eo sinal de negociação para FXY. Para obter esses dados para que você possa executar esses exemplos em casa, envie-me um e-mail e I & # 8217; envie-lhe todos os arquivos. RData necessários (os dados na amostra e fora da amostra) em um arquivo. zip. Olhando rapidamente nos dados do ld_fxy_insamp, veremos os retornos de log do iene a cada 15 minutos começando no mercado aberto (UTC). Os dados de destino (Ien) estão na primeira coluna, juntamente com as duas séries explicativas (Yen e outro recurso co-integrado com movimento de ienes).
2018-01-04 13:30:00 0.000000e + 00 0.000000e + 00 0.0000000000.
2018-01-04 13:45:00 4.763412e-03 4.763412e-03 0.0033465833.
2018-01-04 14:00:00 -8.966599e-05 -8.966599e-05 0.0040635638.
2018-01-04 14:15:00 2.597055e-03 2.597055e-03 -0.0008322064.
2018-01-04 14:30:00 -7.157556e-04 -7.157556e-04 0.0020792190.
2018-01-04 14:45:00 -4.476075e-04 -4.476075e-04 -0.0014685198.
Iniciando, para começar a construir o primeiro sinal de negociação para o iene, começamos por carregar os dados no nosso ambiente R, definimos alguns parâmetros iniciais para a chamada de função MDFA e, em seguida, calculamos os DFTs e o periodograma para o iene.
Como mencionei nos meus artigos anteriores, minha estratégia passo-a-passo para a construção de sinais comerciais sempre começa por uma rápida análise do periodograma do objeto a ser negociado. Segurando a chave para fornecer informações sobre as características de como o recurso é comercializado, o periodograma é uma ferramenta essencial para navegar como o extractor é escolhido. Aqui, procuro os principais picos espectrales que correspondem no domínio do tempo a como e onde o meu sinal irá desencadear negócios de compra / venda. A Figura 2 mostra o periodograma dos retornos de log de 15 minutos do iene japonês durante o período in-sample de 4 de janeiro a 17 de janeiro de 2018. As setas apontam para os principais picos espectrales que procuro e fornece um guia sobre como eu irá definir a minha função. As linhas pontilhadas pretas indicam os dois cortes de freqüência que considerarei neste exemplo, o primeiro ser e o segundo em. Observe que ambos os pontos de corte são definidos diretamente após um pico espectral, algo que eu recomendo. Na negociação de alta freqüência no FOREX usando o MDFA, como veremos, o truque é buscar o pico espectral que explica a variação do fechamento no preço da moeda estrangeira. Queremos aproveitar esse pico espectral, pois é aqui que os grandes ganhos em troca de moeda estrangeira usando o MDFA ocorrerão.
Figura 2: Periodograma de FXY (Yen japonês), juntamente com picos espectrales e dois pontos de corte de freqüência diferentes.
Em nosso primeiro exemplo, consideramos a maior freqüência como o ponto de corte para o ajuste (a linha mais direita na figura do periodograma). Em seguida, inicialmente configurei os parâmetros de tempo e suavidade, e expweight para 0 juntamente com a configuração de todos os parâmetros de regularização para 0 também. Isso me dará um barómetro para onde e quanto ajustar os parâmetros do filtro. Ao selecionar o comprimento do filtro, meus estudos empíricos sobre inúmeras experiências na construção de sinais comerciais usando o iMetrica demonstraram que um & # 8216; good & # 8217; A escolha é em qualquer lugar entre 1/4 e 1/5 do comprimento total da amostra dos dados da série temporal. Claro, o comprimento depende da frequência das observações de dados (isto é, 15 minutos, hora, diária, etc.), mas, em geral, você provavelmente nunca precisará mais do que ser maior que 1/4 no tamanho da amostra. Caso contrário, a regularização pode tornar-se demasiado pesada para lidar de forma eficaz. Neste exemplo, o comprimento total na amostra é 335 e, portanto, eu configurei o que eu farei para o restante deste tutorial. Em qualquer caso, o comprimento do filtro não é o parâmetro mais importante a considerar na construção de bons sinais comerciais. Para uma boa seleção robusta dos pares de parâmetros de filtro com séries explicativas apropriadas, os resultados do sinal de negociação em comparação com, digamos, dificilmente devem diferir. Se o fizerem, a parametrização não é suficientemente robusta.
Depois de carregar os dados de registro-retorno na amostra juntamente com o preço de registro correspondente do iene para calcular o desempenho da negociação, procedemos em R para definir as configurações de filtro inicial para a rotina de MDFA e, em seguida, computa o filtro usando a função IMDFA_comp. Isso retorna o i_mdfa & amp; coeficientes de retenção de objetos, funções de resposta de freqüência e estatísticas de filtro, juntamente com o sinal produzido para cada série explicativa. Combinamos esses sinais para obter o sinal comercial final na amostra. Tudo isso é feito em R da seguinte maneira:
As funções de resposta de frequência resultantes do filtro e os coeficientes são traçados na figura abaixo.
Figura 3: As funções de resposta de frequência do filtro (superior) e os coeficientes de filtro (abaixo)
Observe que a abundância de ruído ainda presente passou a freqüência de corte. Isso é melhorado pelo aumento do parâmetro de suavidade do tempo de espera. Os coeficientes para cada série explicativa mostram alguma correlação em seu movimento à medida que os atrasos aumentam. No entanto, a suavidade e deterioração dos coeficientes deixa muito a desejar. Vamos remediar isso introduzindo parâmetros de regularização. Plots of the in-sample trading signal and the performance in-sample of the signal are shown in the two figures below. Notice that the trading signal behaves quite nicely in-sample. However, looks can be deceiving. This stellar performance is due in large part to a filtering phenomenon called overfitting. One can deduce that overfitting is the culprit here by simply looking at the nonsmoothness of the coefficients along with the number of freezed degrees of freedom, which in this example is roughly 174 (out of 174), way too high. We would like to get this number at around half the total amount of degrees of freedom (number of explanatory series x L).
Figure 4: The trading signal and the log-return data of the Yen.
The in-sample performance of this filter demonstrates the type of results we would like to see after regularization is applied. But now comes for the sobering effects of overfitting. We apply these filter coeffcients to 200 15-minute observations of the Yen and the explanatory series from January 18 to February 1 2018 and compare with the characteristics in-sample. To do this in R, we first load the out-of-sample data into the R environment, and then apply the filter to the out-of-sample data that I defined as x_out.
The plot in Figure 5 shows the out-of-sample trading signal. Notice that the signal is not nearly as smooth as it was in-sample. Overshooting of the data in some areas is also obviously present. Although the out-of-sample overfitting characteristics of the signal are not horribly suspicious, I would not trust this filter to produce stellar returns in the long run.
Figure 5 : Filter applied to 200 15 minute observations of Yen out-of-sample to produce trading signal (shown in blue)
Following the previous analysis of the mean-squared solution (no customization or regularization), we now proceed to clean up the problem of overfitting that was apparent in the coefficients along with mollifying the noise in the stopband (frequencies after ). In order to choose the parameters for smoothing and regularization, one approach is to first apply the smoothness parameter first, as this will generally smooth the coefficients while acting as a ‘pre’-regularizer, and then advance to selecting appropriate regularization controls. In looking at the coefficients (Figure 3), we can see that a fair amount of smoothing is necessary, with only a slight touch of decay. To select these two parameters in R, one option is to use the Troikaner optimizer (found here) to find a suitable combination (I have a secret sauce algorithmic approach I developed for iMetrica for choosing optimal combinations of parameters given an extractor and a performance indicator, although it’s lengthy (even in GNU C) and cumbersome to use, so I typically prefer the strategy discussed in this tutorial). In this example, I began by setting the lambda_smooth to .5 and the decay to (.1,.1) along with an expweight smoothness parameter set to 8.5. After viewing the coefficients, it still wasn’t enough smoothness, so I proceeded to add more finally reaching .63, which did the trick. I then chose lambda to balance the effects of the smoothing expweight (lambda is always the last resort tweaking parameter).
Figure 6 shows the resulting frequency response function for both explanatory series (Yen in red). Notice that the largest spectral peak found directly before the frequency cutoff at is being emphasized and slightly mollified (value near .8 instead of 1.0). The other spectral peaks below are also present. For the coefficients, just enough smoothing and decay was applied to keep the lag, cyclical, and correlated structure of the coefficients intact, but now they look much nicer in their smoothed form. The number of freezed degrees of freedom has been reduced to approximately 102.
Figure 6: The frequency response functions and the coefficients after regularization and smoothing have been applied (top). The smoothed coefficients with slight decay at the end (bottom). Number of freezed degrees of freedom is approximately 102 (out of 172).
Along with an improved freezed degrees of freedom and no apparent havoc of overfitting, we apply this filter out-of-sample to the 200 out-of-sample observations in order to verify the improvement in the structure of the filter coefficients (shown below in Figure 7). Notice the tremendous improvement in the properties of the trading signal (compared with Figure 5). The overshooting of the data has be eliminated and the overall smoothness of the signal has significantly improved. This is due to the fact that we’ve eradicated the presence of overfitting.
Figure 7: Out-of-sample trading signal with regularization.
With all indications of a filter endowed with exactly the characteristics we need for robustness, we now apply the trading signal both in-sample and out of sample to activate the buy/sell trades and see the performance of the trading account in cash value. When the signal crosses below zero, we sell (enter short position) and when the signal rises above zero, we buy (enter long position).
The top plot of Figure 8 is the log price of the Yen for the 15 minute intervals and the dotted lines represent exactly where the trading signal generated trades (crossing zero). The black dotted lines represent a buy (long position) and the blue lines indicate a sell (and short position). Notice that the signal predicted all the close-to-open jumps for the Yen (in part thanks to the explanatory series). This is exactly what we will be striving for when we add regularization and customization to the filter. The cash account of the trades over the in-sample period is shown below, where transaction costs were set at .05 percent. In-sample, the signal earned roughly 6 percent in 9 trading days and a 76 percent trading success ratio.
Figure 8: In-sample performance of the new filter and the trades that are generated.
Now for the ultimate test to see how well the filter performs in producing a winning trading signal, we applied the filter to the 200 15-minute out-of-sample observation of the Yen and the explanatory series from Jan 18th to February 1st and make trades based on the zero crossing. The results are shown below in Figure 9. The black lines represent the buys and blue lines the sells (shorts). Notice the filter is still able to predict the close-to-open jumps even out-of-sample thanks to the regularization. The filter succumbs to only three tiny losses at less than .08 percent each between observations 160 and 180 and one small loss at the beginning, with an out-of-sample trade success ratio hitting 82 percent and an ROI of just over 4 percent over the 9 day interval.
Figure 9: Out-of-sample performance of the regularized filter on 200 out-of-sample 15 minute returns of the Yen. The filter achieved 4 percent ROI over the 200 observations and an 82 percent trade success ratio.
Compare this with the results achieved in iMetrica using the same MDFA parameter settings. In Figure 10, both the in-sample and out-of-sample performance are shown. The performance is nearly identical.
Figure 10: In-sample and out-of-sample performance of the Yen filter in iMetrica. Nearly identical with performance obtained in R.
Now we take a stab at producing another trading filter for the Yen, only this time we wish to identify only the lowest frequencies to generate a trading signal that trades less often, only seeking the largest cycles. As with the performance of the previous filter, we still wish to target the frequencies that might be responsible to the large close-to-open variations in the price of Yen. To do this, we select our cutoff to be which will effectively keep the largest three spectral peaks intact in the low-pass band of .
For this new filter, we keep things simple by continuing to use the same regularization parameters chosen in the previous filter as they seemed to produce good results out-of-sample. The and expweight customization parameters however need to be adjusted to account for the new noise suppression requirements in the stopband and the phase properties in the smaller passband. Thus I increase the smoothing parameter and decreased the timeliness parameter (which only affects the passband) to account for this change. The new frequency response functions and filter coefficients for this smaller lowpass design are shown below in Figure 11. Notice that the second spectral peak is accounted for and only slightly mollified under the new changes. The coefficients still have the noticeable smoothness and decay at the largest lags.
Figure 11: Frequency response functions of the two filters and their corresponding coefficients.
To test the effectiveness of this new lower trading frequency design, we apply the filter coefficients to the 200 out-of-sample observations of the 15-minute Yen log-returns. The performance is shown below in Figure 12. In this filter, we clearly see that the filter still succeeds in predicting correctly the large close-to-open jumps in the price of the Yen. Only three total losses are observed during the 9 day period. The overall performance is not as appealing as the previous filter design as less amount of trades are made, with a near 2 percent ROI and 76 percent trade success ratio. However, this design could fit the priorities for a trader much more sensitive to transaction costs.
Figure 12: Out-of-sample performance of filter with lower cutoff.
Conclusão.
Verification and cross-validation is important, just as the most interesting man in the world will tell you.
The point of this tutorial was to show some of the main concepts and strategies that I undergo when approaching the problem of building a robust and highly efficient trading signal for any given asset at any frequency. I also wanted to see if I could achieve similar results with the R MDFA package as my iMetrica software package. The results ended up being nearly parallel except for some minor differences. The main points I was attempting to highlight were in first analyzing the periodogram to seek out the important spectral peaks (such as ones associate with close-to-open variations) and to demonstrate how the choice of the cutoff affects the systematic trading. Here’s a quick recap on good strategies and hacks to keep in mind.
Summary of strategies for building trading signal using MDFA in R:
As I mentioned before, the periodogram is your best friend. Apply the cutoff directly after any range of spectral peaks that you want to consider. These peaks are what generate the trades. Utilize a choice of filter length no greater than 1/4. Anything larger is unnecessary. Begin by computing the filter in the mean-square sense, namely without using any customization or regularization and see exactly what needs to be approved upon by viewing the frequency response functions and coefficients for each explanatory series. Good performance of the trading signal in-sample (and even out-of-sample in most cases) is meaningless unless the coefficients have solid robust characteristics in both the frequency domain and the lag domain. I recommend beginning with tweaking the smoothness customization parameter expweight and the lambda_smooth regularization parameters first. Then proceed with only slight adjustments to the lambda_decay parameters. Finally, as a last resort, the lambda customization. I really never bother to look at lambda_cross. It has seldom helped in any significant manner. Since the data we are using to target and build trading signals are log-returns, no need to ever bother with i1 and i2. Those are for the truly advanced and patient signal extractors, and should only be left for those endowed with iMetrica 😉
If you have any questions, or would like the high-frequency Yen data I used in these examples, feel free to contact me and I’ll send them to you. Until next time, happy extracting!
Generating trading signals in r
Obter através da App Store Leia esta publicação em nosso aplicativo!
Creating Trading Signals in R.
I'm constructing a trading strategy and am stuck at two key areas. When using Stoch and MACD in quantmod , I am trying to create a signal when the slow stochastic crosses over the fast stochastic (1), and visa-versa(-1), and flat when in between (0). MACD the code is identical except with the column names MACD and Signal. Lastly, I am trying to merge the three signals to create a master signal when all three signals equal 1, -1, 0.
Update : I fixed all the nasty loops using a diff instead after this answer.
This is how I would approach this problem. You are calculating all position that have the desired relationships. You only want the first position that satisfies the trading signal to act on it as soon as possible.
I would set up the Bollinger band signal like this:
I would create the stochastic signal like this:
Once you calculate the difference, you want to find the first crossover where one is higher than the other so you need to consider the i th and i-1 th positions. Also the signal will be stronger if you are in overbought or oversold territory (0.8 or 0.2).
Similarly for MACD:
Now we merge them and calculate the combine signal:
If it were me, I would rather have a sum of the signals because it will tell you how trust worthy each signal is. If you have a 3, that is stong but a 1 or 2 not as strong. So I would go with the sum as the combined signal.
Now all is a matrix with all the signals and the last column is the combined signal strength.
Also think about how this may not give you a good signal. Using the approach for this chart, The strongest signals I get are -2, and I only get 5 occasions. Kind of odd since the chart goes straight up but there are no strong buys.
These sell signals only give a short downside and then the chart rockets higher. Of course it all depends on the stock etc.
You also get situations like this:
Some indicators are faster or slower than others. This would be my approach, but you should do broad based tests and determine if you think these will be actionable trades and if you would make any money acting on them minus commission and hold duration.
QuantStrat TradeR.
Trading, QuantStrat, R, and more.
Replicating Volatiltiy ETN Returns From CBOE Futures.
This post will demonstrate how to replicate the volatility ETNs (XIV, VXX, ZIV, VXZ) from CBOE futures, thereby allowing any individual to create synthetic ETF returns from before their inception, free of cost.
So, before I get to the actual algorithm, it depends on an update to the term structure algorithm I shared some months back.
In that algorithm, mistakenly (or for the purpose of simplicity), I used calendar days as the time to expiry, when it should have been business days, which also accounts for weekends, and holidays, which are an irritating artifact to keep track of.
So here’s the salient change, in the loop that calculates times to expiry:
The one salient line in particular, is this:
What is this bizdays function? It comes from the bizdays package in R.
There’s also the tradingHolidays. R script, which makes further use of the bizdays package. Here’s what goes on under the hood in tradingHolidays. R, for those that wish to replicate the code:
There are two CSVs that I manually compiled, but will share screenshots of–they are the easter holidays (because they have to be adjusted for turning Sunday to Friday because of Easter Fridays), and the rest of the national holidays.
Here is what the easters csv looks like:
And the nonEasterHolidays, which contains New Year’s Day, MLK Jr. Day, President’s Day, Memorial Day, Independence Day, Labor Day, Thanksgiving Day, and Christmas Day (along with their observed dates) CSV:
Furthermore, we need to adjust for the two days that equities were not trading due to Hurricane Sandy.
So then, the list of holidays looks like this:
So once we have a list of holidays, we use the bizdays package to set the holidays and weekends (Saturday and Sunday) as our non-business days, and use that function to calculate the correct times to expiry.
So, now that we have the updated expiry structure, we can write a function that will correctly replicate the four main volatility ETNs–XIV, VXX, ZIV, and VXZ.
Here’s the English explanation:
VXX is made up of two contracts–the front month, and the back month, and has a certain number of trading days (AKA business days) that it trades until expiry, say, 17. During that timeframe, the front month (let’s call it M1) goes from being the entire allocation of funds, to being none of the allocation of funds, as the front month contract approaches expiry. That is, as a contract approaches expiry, the second contract gradually receives more and more weight, until, at expiry of the front month contract, the second month contract contains all of the funds–just as it *becomes* the front month contract. So, say you have 17 days to expiry on the front month. At the expiry of the previous contract, the second month will have a weight of 17/17–100%, as it becomes the front month. Then, the next day, that contract, now the front month, will have a weight of 16/17 at settle, then 15/17, and so on. That numerator is called dr, and the denominator is called dt.
However, beyond this, there’s a second mechanism that’s responsible for the VXX looking like it does as compared to a basic futures contract (that is, the decay responsible for short volatility’s profits), and that is the “instantaneous” rebalancing. That is, the returns for a given day are today’s settles multiplied by yesterday’s weights, over yesterday’s settles multiplied by yesterday’s weights, minus one. That is, (S_1_t * dr/dt_t-1 + S_2_t * 1-dr/dt_t-1) / (S_1_t-1 * dr/dt_t-1 + S_2_t-1 * 1-dr/dt_t-1) – 1 (I could use a tutorial on LaTeX). So, when you move forward a day, well, tomorrow, today’s weights become t-1. Yet, when were the assets able to be rebalanced? Well, in the ETNs such as VXX and VXZ, the “hand-waving” is that it happens instantaneously. That is, the weight for the front month was 93%, the return was realized at settlement (that is, from settle to settle), and immediately after that return was realized, the front month’s weight shifts from 93%, to, say, 88%. So, say Credit Suisse (that issues these ETNs ), has $10,000 (just to keep the arithmetic and number of zeroes tolerable, obviously there are a lot more in reality) worth of XIV outstanding after immediately realizing returns, it will sell $500 of its $9300 in the front month, and immediately move them to the second month, so it will immediately go from $9300 in M1 and $700 in M2 to $8800 in M1 and $1200 in M2. When did those $500 move? Immediately, instantaneously, and if you like, you can apply Clarke’s Third Law and call it “magically”.
The only exception is the day after roll day, in which the second month simply becomes the front month as the previous front month expires, so what was a 100% weight on the second month will now be a 100% weight on the front month, so there’s some extra code that needs to be written to make that distinction.
That’s the way it works for VXX and XIV. What’s the difference for VXZ and ZIV? It’s really simple–instead of M1 and M2, VXZ uses the exact same weightings (that is, the time remaining on front month vs. how many days exist for that contract to be the front month), uses M4, M5, M6, and M7, with M4 taking dr/dt, M5 and M6 always being 1, and M7 being 1-dr/dt.
In any case, here’s the code.
So, a big thank you goes out to Michael Kapler of Systematic Investor Toolbox for originally doing the replication and providing his code. My code essentially does the same thing, in, hopefully a more commented way.
So, ultimately, does it work? Well, using my updated term structure code, I can test that.
While I’m not going to paste my entire term structure code (again, available here, just update the script with my updates from this post), here’s how you’d run the new function:
And since it returns both the vxx returns and the vxz returns, we can compare them both.
With the result:
Basically, a perfect match.
Let’s do the same thing, with ZIV.
So, rebuilding from the futures does a tiny bit better than the ETN. But the trajectory is largely identical.
That concludes this post. I hope it has shed some light on how these volatility ETNs work, and how to obtain them directly from the futures data published by the CBOE, which are the inputs to my term structure algorithm.
This also means that for institutions interested in trading my strategy, that they can obtain leverage to trade the futures-composite replicated variants of these ETNs, at greater volume.
Thanks for reading.
NOTES: For those interested in a retail subscription strategy to trading volatility, do not hesitate to subscribe to my volatility-trading strategy. For those interested in employing me full-time or for long-term consulting projects, I can be reached on my LinkedIn, or my email: ilya. kipnis@gmail.
(Don’t Get) Contangled Up In Noise.
This post will be about investigating the efficacy of contango as a volatility trading signal.
For those that trade volatility (like me), a term you may see that’s somewhat ubiquitous is the term “contango”. What does this term mean?
Well, simple: it just means the ratio of the second month of VIX futures over the first. The idea being is that when the second month of futures is more than the first, that people’s outlook for volatility is greater in the future than it is for the present, and therefore, the futures are “in contango”, which is most of the time.
Furthermore, those that try to find decent volatility trading ideas may have often seen that futures in contango implies that holding a short volatility position will be profitable.
Is this the case?
Well, there’s an easy way to answer that.
First off, refer back to my post on obtaining free futures data from the CBOE.
Using this data, we can obtain our signal (that is, in order to run the code in this post, run the code in that post).
Now, let’s get our XIV data (again, big thanks to Mr. Helmuth Vollmeier for so kindly providing it.
Now, here’s how this works: as the CBOE doesn’t update its settles until around 9:45 AM EST on the day after (EG a Tuesday’s settle data won’t release until Wednesday at 9:45 AM EST), we have to enter at close of the day after the signal fires. (For those wondering, my subscription strategy uses this mechanism, giving subscribers ample time to execute throughout the day.)
So, let’s calculate our backtest returns. Here’s a stratStats function to compute some summary statistics.
With the following results:
So, this is obviously a disaster. Visual inspection will show devastating, multi-year drawdowns. Using the table. Drawdowns command, we can view the worst ones.
So, the top 3 are horrendous, and then anything above 30% is still pretty awful. A couple of those drawdowns lasted multiple years as well, with a massive length to the trough. 458 trading days is nearly two years, and 364 is about one and a half years. Imagine seeing a strategy be consistently on the wrong side of the trade for nearly two years, and when all is said and done, you’ve lost three-fourths of everything in that strategy.
There’s no sugar-coating this: such a strategy can only be called utter trash.
Let’s try one modification: we’ll require both contango (C2 > C1), and that contango be above its 60-day simple moving average, similar to my VXV/VXMT strategy.
With the results:
So, a Calmar still safely below 1, an Ulcer Performance Index still in the basement, a maximum drawdown that’s long past the point that people will have abandoned the strategy, and so on.
So, even though it was improved, it’s still safe to say this strategy doesn’t perform too well. Even after the large 2007-2008 drawdown, it still gets some things pretty badly wrong, like being exposed to all of August 2017.
While I think there are applications to contango in volatility investing, I don’t think its use is in generating the long/short volatility signal on its own. Rather, I think other indices and sources of data do a better job of that. Such as the VXV/VXMT, which has since been iterated on to form my subscription strategy.
Thanks for reading.
NOTE: I am currently seeking networking opportunities, long-term projects, and full-time positions related to my skill set. My linkedIn profile can be found here.
Comparing Some Strategies from Easy Volatility Investing, and the Table. Drawdowns Command.
This post will be about comparing strategies from the paper “Easy Volatility Investing”, along with a demonstration of R’s table. Drawdowns command.
First off, before going further, while I think the execution assumptions found in EVI don’t lend the strategies well to actual live trading (although their risk/reward tradeoffs also leave a lot of room for improvement), I think these strategies are great as benchmarks.
So, some time ago, I did an out-of-sample test for one of the strategies found in EVI, which can be found here.
Using the same source of data, I also obtained data for SPY (though, again, AlphaVantage can also provide this service for free for those that don’t use Quandl).
Here’s the new code.
So, an explanation: there are four return streams here–buy and hold XIV, the DDN momentum from a previous post, and two other strategies.
The simpler one, called the VRatio is simply the ratio of the VIX over the VXV. Near the close, check this quantity. If this is less than one, buy XIV, otherwise, buy VXX.
The other one, called the Volatility Risk Premium strategy (or VRP for short), compares the 10 day historical volatility (that is, the annualized running ten day standard deviation) of the S&P 500, subtracts it from the VIX, and takes a 5 day moving average of that. Near the close, when that’s above zero (that is, VIX is higher than historical volatility), go long XIV, otherwise, go long VXX.
Again, all of these strategies are effectively “observe near/at the close, buy at the close”, so are useful for demonstration purposes, though not for implementation purposes on any large account without incurring market impact.
Here are the results, since 2018 (that is, around the time of XIV’s actual inception):
To note, both the momentum and the VRP strategy underperform buying and holding XIV since 2018. The VRatio strategy, on the other hand, does outperform.
Here’s a summary statistics function that compiles some top-level performance metrics.
To note, all of the benchmark strategies suffered very large drawdowns since XIV’s inception, which we can examine using the table. Drawdowns command, as seen below:
Note that the table. Drawdowns command only examines one return stream at a time. Furthermore, the top argument specifies how many drawdowns to look at, sorted by greatest drawdown first.
One reason I think that these strategies seem to suffer the drawdowns they do is that they’re either all-in on one asset, or its exact opposite, with no room for error.
One last thing, for the curious, here is the comparison with my strategy since 2018 (essentially XIV inception) benchmarked against the strategies in EVI (which I have been trading with live capital since September, and have recently opened a subscription service for):
Thanks for reading.
NOTE: I am currently looking for networking and full-time opportunities related to my skill set. My LinkedIn profile can be found here.
Launching My Subscription Service.
After gauging interest from my readers, I’ve decided to open up a subscription service. I’ll copy and paste the FAQs, or my best attempt at trying to answer as many questions as possible ahead of time, and may answer more in the future.
I’m choosing to use Patreon just to outsource all of the technicalities of handling subscriptions and creating a centralized source to post subscription-based content.
FAQs (copied from the subscription page):
Obrigado pela visita. After gauging interest from my readership on my main site (quantstrattrader. wordpress), I created this as a subscription page for quantitative investment strategies, with the goal of having subscribers turn their cash into more cash, net of subscription fees (hopefully). The systems I develop come from a background of learning from experienced quantitative trading professionals, and senior researchers at large firms. The current system I initially published a prototype for several years back and watched it being tracked, before finally starting to deploy my own capital earlier this year, and making the most recent modifications even more recently.
And while past performance doesn’t guarantee future results and the past doesn’t repeat itself, it often rhymes, so let’s turn money into more money.
Some FAQs about the strategy:
What is the subscription price for this strategy?
Currently, after gauging interest from readers and doing research based on other sites, the tentative pricing is $50/month. As this strategy builds a track record, that may be subject to change in the future, and notifications will be made in such an event.
What is the description of the strategy?
The strategy is mainly a short volatility system that trades XIV, ZIV, and VXX. As far as volatility strategies go, it’s fairly conservative in that it uses several different checks in order to ensure a position.
What is the strategy’s edge?
In two words: risk management. Essentially, there are a few separate criteria to select an investment, and the system spends a not-insignificant time with no exposure when some of these criteria provide contradictory signals. Furthermore, the system uses disciplined methodologies in its construction in order to avoid unnecessary free parameters, and to keep the strategy as parsimonious as possible.
Do you trade your own capital with this strategy?
When was the in-sample training period for this system?
A site that no longer updates its blog (volatility made simple) once tracked a more rudimentary strategy that I wrote about several years ago. I was particularly pleased with the results of that vetting, and recently have received input to improve my system to a much greater degree, as well as gained the confidence to invest live capital into it.
How many trades per year does the system make?
In the backtest from April 20, 2008 through the end of 2018, the system made 187 transactions in XIV (both buy and sell), 160 in ZIV, and 52 in VXX. Meaning over the course of approximately 9 years, there was on average 43 transactions per year. In some cases, this may simply be switching from XIV to ZIV or vice versa. In other words, trades last approximately a week (some may be longer, some shorter).
When will signals be posted?
Signals will be posted sometime between 12 PM and market close (4 PM EST). In backtesting, they are tested as market on close orders, so individuals assume any risk/reward by executing earlier.
How often is this system in the market?
About 56%. However, over the course of backtesting (and live trading), only about 9% of months have zero return.
What are the distribution of winning, losing, and zero return months?
As of late October 2017, there have been about 65% winning months (with an average gain of 12.8%), 26% losing months (with an average loss of 4.9%), and 9% zero months.
What are some other statistics about the strategy?
Since 2018 (around the time that XIV officially came into inception as opposed to using synthetic data), the strategy has boasted an 82% annualized return, with a 24.8% maximum drawdown and an annualized standard deviation of 35%. This means a Sharpe ratio (return to standard deviation) higher than 2, and a Calmar ratio higher than 3. It also has an Ulcer Performance Index of 10.
What are the strategy’s worst drawdowns?
Since 2018 (again, around the time of XIV’s inception), the largest drawdown was 24.8%, starting on October 31, 2018, and making a new equity high on January 12, 2018. The longest drawdown started on August 21, 2018 and recovered on April 10, 2018, and lasted for 160 trading days.
Will the subscription price change in the future?
If the strategy continues to deliver strong returns, then there may be reason to increase the price so long as the returns bear it out.
Can a conservative risk signal be provided for those who might not be able to tolerate a 25% drawdown?
A variant of the strategy that targets about half of the annualized standard deviation of the strategy boasts a 40% annualized return for about 12% drawdown since 2018. Overall, this has slightly higher reward to risk statistics, but at the cost of cutting aggregate returns in half.
Can’t XIV have a termination event?
This refers to the idea of the XIV ETN terminating if it loses 80% of its value in a single day. To give an idea of the likelihood of this event, using synthetic data, the XIV ETN had a massive drawdown of 92% over the course of the 2008 financial crisis. For the history of that synthetic (pre-inception) and realized (post-inception) data, the absolute worst day was a down day of 26.8%. To note, the strategy was not in XIV during that day.
What was the strategy’s worst day?
On September 16, 2018, the strategy lost 16% in one day. This was at the tail end of a stretch of positive days that made about 40%.
What are the strategy’s risks?
The first risk is that given that this strategy is naturally biased towards short volatility, that it can have potential for some sharp drawdowns due to the nature of volatility spikes. The other risk is that given that this strategy sometimes spends its time in ZIV, that it will underperform XIV on some good days. This second risk is a consequence of additional layers of risk management in the strategy.
How complex is this strategy?
Not overly. It’s only slightly more complex than a basic momentum strategy when counting free parameters, and can be explained in a couple of minutes.
Does this strategy use any complex machine learning methodologies?
No. The data requirements for such algorithms and the noise in the financial world make it very risky to apply these methodologies, and research as of yet did not bear fruit to justify incorporating them.
Will instrument volume ever be a concern (particularly ZIV)?
According to one individual who worked on the creation of the original VXX ETN (and by extension, its inverse, XIV), new shares of ETNs can be created by the issuer (in ZIV’s case, Credit Suisse) on demand. In short, the concern of volume is more of a concern of the reputability of the person making the request. In other words, it depends on how well the strategy does.
Can the strategy be held liable/accountable/responsible for a subscriber’s loss/drawdown?
Let this serve as a disclaimer: by subscribing, you agree to waive any legal claim against the strategy, or its creator(s) in the event of drawdowns, losses, etc. The subscription is for viewing the output of a program, and this service does not actively manage a penny of subscribers’ actual assets. Subscribers can choose to ignore the strategy’s signals at a moment’s notice at their discretion. The program’s output should not be thought of as the investment advice coming from a CFP, CFA, RIA, etc.
Why should these signals be trusted?
Because my work on other topics has been on full, public display for several years. Unlike other websites, I have shown “bad backtests”, thus breaking the adage of “you’ll never see a bad backtest”. I have shown thoroughness in my research, and the same thoroughness has been applied towards this system as well. Until there is a longer track record such that the system can stand on its own, the trust in the system is the trust in the system’s creator.
Who is the intended audience for these signals?
The intended audience is individual, retail investors with a certain risk tolerance, and is priced accordingly.
Isn’t volatility investing very risky?
It’s risky from the perspective of the underlying instrument having the capacity to realize very large drawdowns (greater than 60%, and even greater than 90%). However, from a purely numerical standpoint, the company taking over so much of shopping, Amazon, since inception has had a 37.1% annualized rate of return, a standard deviation of 61.5%, a worst drawdown of 94%, and an Ulcer Performance Index of 0.9. By comparison, XIV, from 2008 (using synthetic data), has had a 35.5% annualized rate of return, a standard deviation of 57.7%, a worst drawdown of 92%, and an Ulcer Performance Index of 0.6. If Amazon is considered a top-notch asset, then from a quantitative comparison, a system looking to capitalize on volatility bets should be viewed from a similar perspective. To be sure, the strategy’s performance vastly outperforms that of buying and holding XIV (which nobody should do). However, the philosophy of volatility products being much riskier than household tech names just does not hold true unless the future wildly differs from the past.
Is there a possibility for collaborating with other strategy creators?
Feel free to contact me at my email ilya. kipnis@gmail to discuss that possibility. I request a daily stream of returns before starting any discussion.
Because past all the artsy-craftsy window dressing and interesting choice of vocabulary, Patreon is simply a platform that processes payments and creates a centralized platform from which to post subscription-based content, as opposed to maintaining mailing lists and other technical headaches. Essentially, it’s simply a way to outsource the technical end of running a business, even if the window dressing is a bit unorthodox.
Thanks for reading.
NOTE: I am currently interested in networking and full-time roles based on my skills. My LinkedIn profile can be found here.
The Return of Free Data and Possible Volatility Trading Subscription.
This post will be about pulling free data from AlphaVantage, and gauging interest for a volatility trading subscription service.
So first off, ever since the yahoos at Yahoo decided to turn off their free data, the world of free daily data has been in somewhat of a dark age. Well, thanks to blog. fosstrading/2017/10/getsymbols-and-alpha-vantage. html#gpluscommentsJosh Ulrich, Paul Teetor, and other R/Finance individuals, the latest edition of quantmod (which can be installed from CRAN) now contains a way to get free financial data from AlphaVantage since the year 2000, which is usually enough for most backtests, as that date predates the inception of most ETFs.
Here’s how to do it.
Once you do that, downloading data is simple, if not slightly slow. Here’s how to do it.
And the results:
Which means if any one of my old posts on asset allocation has been somewhat defunct thanks to bad yahoo data, it will now work again with a slight modification to the data input algorithms.
Beyond demonstrating this routine, one other thing I’d like to do is to gauge interest for a volatility signal subscription service, for a system I have personally started trading a couple of months ago.
Simply, I have seen other websites with subscription services with worse risk/reward than the strategy I currently trade, which switches between XIV, ZIV, and VXX. Currently, the equity curve, in log 10, looks like this:
That is, $1000 in 2008 would have become approximately $1,000,000 today, if one was able to trade this strategy since then.
Since 2018 (around the time of inception for XIV), the performance has been:
Considering that some websites out there charge upwards of $50 a month for either a single tactical asset rotation strategy (and a lot more for a combination) with inferior risk/return profiles, or a volatility strategy that may have had a massive and historically record-breaking drawdown, I was hoping to gauge a price point for what readers would consider paying for signals from a better strategy than those.
Thanks for reading.
NOTE: I am currently interested in networking and am seeking full-time opportunities related to my skill set. My LinkedIn profile can be found here.
The Kelly Criterion — Funciona?
This post will be about implementing and investigating the running Kelly Criterion — that is, a constantly adjusted Kelly Criterion that changes as a strategy realizes returns.
For those not familiar with the Kelly Criterion, it’s the idea of adjusting a bet size to maximize a strategy’s long term growth rate. Both https://en. wikipedia/wiki/Kelly_criterionWikipedia and Investopedia have entries on the Kelly Criterion. Essentially, it’s about maximizing your long-run expectation of a betting system, by sizing bets higher when the edge is higher, and vice versa.
There are two formulations for the Kelly criterion: the Wikipedia result presents it as mean over sigma squared. The Investopedia definition is P-[(1-P)/winLossRatio], where P is the probability of a winning bet, and the winLossRatio is the average win over the average loss.
In any case, here are the two implementations.
Let’s try this with some data. At this point in time, I’m going to show a non-replicable volatility strategy that I currently trade.
For the record, here are its statistics:
Now, let’s see what the Wikipedia version does:
The results are simply ridiculous. And here would be why: say you have a mean return of .0005 per day (5 bps/day), and a standard deviation equal to that (that is, a Sharpe ratio of 1). You would have 1/.0005 = 2000. In other words, a leverage of 2000 times. This clearly makes no sense.
The other variant is the more particular Investopedia definition.
Looks a bit more reasonable. However, how does it stack up against not using it at all?
Turns out, the fabled Kelly Criterion doesn’t really change things all that much.
For the record, here are the statistical comparisons:
Thanks for reading.
NOTE: I am currently looking for my next full-time opportunity, preferably in New York City or Philadelphia relating to the skills I have demonstrated on this blog. My LinkedIn profile can be found here. If you know of such opportunities, do not hesitate to reach out to me.
Leverage Up When You’re Down?
This post will investigate the idea of reducing leverage when drawdowns are small, and increasing leverage as losses accumulate. It’s based on the idea that whatever goes up must come down, and whatever comes down generally goes back up.
I originally came across this idea from this blog post.
So, first off, let’s write an easy function that allows replication of this idea. Essentially, we have several arguments:
One: the default leverage (that is, when your drawdown is zero, what’s your exposure)? For reference, in the original post, it’s 10%.
Next: the various leverage levels. In the original post, the leverage levels are 25%, 50%, and 100%.
And lastly, we need the corresponding thresholds at which to apply those leverage levels. In the original post, those levels are 20%, 40%, and 55%.
So, now we can create a function to implement that in R. The idea being that we have R compute the drawdowns, and then use that information to determine leverage levels as precisely and frequently as possible.
Here’s a quick piece of code to do so:
So, let’s replicate some results.
And our results look something like this:
That said, what would happen if one were to extend the data for all available XIV data?
A different story.
In this case, I think the takeaway is that such a mechanism does well when the drawdowns for the benchmark in question occur sharply, so that the lower exposure protects from those sharp drawdowns, and then the benchmark spends much of the time in a recovery mode, so that the increased exposure has time to earn outsized returns, and then draws down again. When the benchmark continues to see drawdowns after maximum leverage is reached, or continues to perform well when not in drawdown, such a mechanism falls behind quickly.
As always, there is no free lunch when it comes to drawdowns, as trying to lower exposure in preparation for a correction will necessarily mean forfeiting a painful amount of upside in the good times, at least as presented in the original post.
Thanks for reading.
NOTE: I am currently looking for my next full-time opportunity, preferably in New York City or Philadelphia relating to the skills I have demonstrated on this blog. My LinkedIn profile can be found here. If you know of such opportunities, do not hesitate to reach out to me.
Let’s Talk Drawdowns (And Affiliates)
This post will be directed towards those newer in investing, with an explanation of drawdowns–in my opinion, a simple and highly important risk statistic.
Would you invest in this?
As it turns out, millions of people do, and did. That is the S&P 500, from 2000 through 2018, more colloquially referred to as “the stock market”. Plenty of people around the world invest in it, and for a risk to reward payoff that is very bad, in my opinion. This is an investment that, in ten years, lost half of its value–twice!
At its simplest, an investment–placing your money in an asset like a stock, a savings account, and so on, instead of spending it, has two things you need to look at.
First, what’s your reward? If you open up a bank CD, you might be fortunate to get 3%. If you invest it in the stock market, you might get 8% per year (on average) if you held it for 20 years. In other words, you stow away $100 on January 1st, and you might come back and find $108 in your account on December 31st. This is often called the compound annualized growth rate (CAGR)–meaning that if you have $100 one year, earn 8%, you have 108, and then earn 8% on that, and so on.
The second thing to look at is the risk. What can you lose? The simplest answer to this is “the maximum drawdown”. If this sounds complicated, it simply means “the biggest loss”. So, if you had $100 one month, $120 next month, and $90 the month after that, your maximum drawdown (that is, your maximum loss) would be 1 – 90/120 = 25%.
When you put the reward and risk together, you can create a ratio, to see how your rewards and risks line up. This is called a Calmar ratio, and you get it by dividing your CAGR by your maximum drawdown. The Calmar Ratio is a ratio that I interpret as “for every dollar you lose in your investment’s worst performance, how many dollars can you make back in a year?” For my own investments, I prefer this number to be at least 1, and know of a strategy for which that number is above 2 since 2018, or higher than 3 if simulated back to 2008.
Most stocks don’t even have a Calmar ratio of 1, which means that on average, an investment makes more than it can possibly lose in a year. Even Amazon, the company whose stock made Jeff Bezos now the richest man in the world, only has a Calmar Ratio of less than 2/5, with a maximum loss of more than 90% in the dot-com crash. The S&P 500, again, “the stock market”, since 1993, has a Calmar Ratio of around 1/6. That is, the worst losses can take *years* to make back.
A lot of wealth advisers like to say that they recommend a large holding of stocks for young people. In my opinion, whether you’re young or old, losing half of everything hurts, and there are much better ways to make money than to simply buy and hold a collection of stocks.
For those with coding skills, one way to gauge just how good or bad an investment is, is this:
An investment has a history–that is, in January, it made 3%, in February, it lost 2%, in March, it made 5%, and so on. By shuffling that history around, so that say, January loses 2%, February makes 5%, and March makes 3%, you can create an alternate history of the investment. It will start and end in the same place, but the journey will be different. For investments that have existed for a few years, it is possible to create many different histories, and compare the Calmar ratio of the original investment to its shuffled “alternate histories”. Ideally, you want the investment to be ranked among the highest possible ways to have made the money it did.
To put it simply: would you rather fall one inch a thousand times, or fall a thousand inches once? Well, the first one is no different than jumping rope. The second one will kill you.
Here is some code I wrote in R (if you don’t code in R, don’t worry) to see just how the S&P 500 (the stock market) did compared to how it could have done.
This is the resulting plot:
That red line is the actual performance of the S&P 500 compared to what could have been. And of the 1000 different simulations, only 91 did worse than what happened in reality.
This means that the stock market isn’t a particularly good investment, and that you can do much better using tactical asset allocation strategies.
One site I’m affiliated with, is AllocateSmartly. It is a cheap investment subscription service ($30 a month) that compiles a collection of asset allocation strategies that perform better than many wealth advisers. When you combine some of those strategies, the performance is better still. To put it into perspective, one model strategy I’ve come up with has this performance:
In this case, the compound annualized growth rate is nearly double that of the maximum loss. For those interested in something a bit more aggressive, this strategy ensemble uses some fairly conservative strategies in its approach.
In conclusion, when considering how to invest your money, keep in mind both the reward, and the risk. One very simple and important way to understand risk is how much an investment can possibly lose, from its highest, to its lowest value following that peak. When you combine the reward and the risk, you can get a ratio that tells you about how much you can stand to make for every dollar lost in an investment’s worst performance.
Thanks for reading.
NOTE: I am interested in networking opportunities, projects, and full-time positions related to my skill set. If you are looking to collaborate, please contact me on my LinkedIn here.
An Out of Sample Update on DDN’s Volatility Momentum Trading Strategy and Beta Convexity.
The first part of this post is a quick update on Tony Cooper’s of Double Digit Numerics’s volatility ETN momentum strategy from the volatility made simple blog (which has stopped updating as of a year and a half ago). The second part will cover Dr. Jonathan Kinlay’s Beta Convexity concept.
So, now that I have the ability to generate a term structure and constant expiry contracts, I decided to revisit some of the strategies on Volatility Made Simple and see if any of them are any good (long story short: all of the publicly detailed ones aren’t so hot besides mine–they either have a massive drawdown in-sample around the time of the crisis, or a massive drawdown out-of-sample).
Why this strategy? Because it seemed different from most of the usual term structure ratio trades (of which mine is an example), so I thought I’d check out how it did since its first publishing date, and because it’s rather easy to understand.
Here’s the strategy:
Take XIV, VXX, ZIV, VXZ, and SHY (this last one as the “risk free” asset), and at the close, invest in whichever has had the highest 83 day momentum (this was the result of optimization done on volatilityMadeSimple).
Here’s the code to do this in R, using the Quandl EOD database. There are two variants tested–observe the close, buy the close (AKA magical thinking), and observe the close, buy tomorrow’s close.
Aqui estão os resultados.
Looks like this strategy didn’t pan out too well. Just a daily reminder that if you’re using fine grid-search to select a particularly good parameter (EG n = 83 days? Maybe 4 21-day trading months, but even that would have been n = 82), you’re asking for a visit from, in the words of Mr. Tony Cooper, a visit from the grim reaper.
Moving onto another topic, whenever Dr. Jonathan Kinlay posts something that I think I can replicate that I’d be very wise to do so, as he is a very skilled and experienced practitioner (and also includes me on his blogroll).
A topic that Dr. Kinlay covered is the idea of beta convexity–namely, that an asset’s beta to a benchmark may be different when the benchmark is up as compared to when it’s down. Essentially, it’s the idea that we want to weed out firms that are what I’d deem as “losers in disguise”–I. E. those that act fine when times are good (which is when we really don’t care about diversification, since everything is going up anyway), but do nothing during bad times.
The beta convexity is calculated quite simply: it’s the beta of an asset to a benchmark when the benchmark has a positive return, minus the beta of an asset to a benchmark when the benchmark has a negative return, then squaring the difference. That is, (beta_bench_positive – beta_bench_negative) ^ 2.
Here’s some R code to demonstrate this, using IBM vs. the S&P 500 since 1995.
Thanks for reading.
NOTE: I am always looking to network, and am currently actively looking for full-time opportunities which may benefit from my skill set. If you have a position which may benefit from my skills, do not hesitate to reach out to me. My LinkedIn profile can be found here.
Testing the Hierarchical Risk Parity algorithm.
This post will be a modified backtest of the Adaptive Asset Allocation backtest from AllocateSmartly, using the Hierarchical Risk Parity algorithm from last post, because Adam Butler was eager to see my results. On a whole, as Adam Butler had told me he had seen, HRP does not generate outperformance when applied to a small, carefully-constructed, diversified-by-selection universe of asset classes, as opposed to a universe of hundreds or even several thousand assets, where its theoretically superior properties result in it being a superior algorithm.
First off, I would like to thank one Matthew Barry, for helping me modify my HRP algorithm so as to not use the global environment for recursion. You can find his github here.
Here is the modified HRP code.
With covMat and corMat being from the last post. In fact, this function can be further modified by encapsulating the clustering order within the getRecBipart function, but in the interest of keeping the code as similar to Marcos Lopez de Prado’s code as I could, I’ll leave this here.
Anyhow, the backtest will follow. One thing I will mention is that I’m using Quandl’s EOD database, as Yahoo has really screwed up their financial database (I. E. some sector SPDRs have broken data, dividends not adjusted, etc.). While this database is a $50/month subscription, I believe free users can access it up to 150 times in 60 days, so that should be enough to run backtests from this blog, so long as you save your downloaded time series for later use by using write. zoo.
This code needs the tseries library for the portfolio. optim function for the minimum variance portfolio (Dr. Kris Boudt has a course on this at datacamp), and the other standard packages.
A helper function for this backtest (and really, any other momentum rotation backtest) is the appendMissingAssets function, which simply adds on assets not selected to the final weighting and re-orders the weights by the original ordering.
Next, we make the call to Quandl to get our data.
While Josh Ulrich fixed quantmod to actually get Yahoo data after Yahoo broke the API, the problem is that the Yahoo data is now garbage as well, and I’m not sure how much Josh Ulrich can do about that. I really hope some other provider can step up and provide free, usable EOD data so that I don’t have to worry about readers not being able to replicate the backtest, as my policy for this blog is that readers should be able to replicate the backtests so they don’t just nod and take my word for it. If you are or know of such a provider, please leave a comment so that I can let the blog readers know all about you.
Next, we initialize the settings for the backtest.
While the AAA backtest actually uses a 126 day lookback instead of a 6 month lookback, as it trades at the end of every month, that’s effectively a 6 month lookback, give or take a few days out of 126, but the code is less complex this way.
Next, we have our actual backtest.
In a few sentences, this is what happens:
The algorithm takes a subset of the returns (the past six months at every month), and computes absolute momentum. It then ranks the ten absolute momentum calculations, and selects the intersection of the top 5, and those with a return greater than zero (so, a dual momentum calculation).
If no assets qualify, the algorithm invests in nothing. If there’s only one asset that qualifies, the algorithm invests in that one asset. If there are two or more qualifying assets, the algorithm computes a covariance matrix using 20 day volatility multiplied with a 126 day correlation matrix (that is, sd_20′ %*% sd_20 * (elementwise) cor_126. It then computes normalized inverse volatility weights using the volatility from the past 20 days, a minimum variance portfolio with the portfolio. optim function, and lastly, the hierarchical risk parity weights using the HRP code above from Marcos Lopez de Prado’s paper.
Lastly, the program puts together all of the weights, and adds a cash investment for any period without any investments.
Aqui estão os resultados:
In short, in the context of a small, carefully-selected and allegedly diversified (I’ll let Adam Butler speak for that one) universe dominated by the process of which assets to invest in as opposed to how much, the theoretical upsides of an algorithm which simultaneously exploits a covariance structure without needing to invert a covariance matrix can be lost.
However, this test (albeit from 2007 onwards, thanks to ETF inception dates combined with lookback burn-in) confirms what Adam Butler himself told me, which is that HRP hasn’t impressed him, and from this backtest, I can see why. However, in the context of dual momentum rank selection, I’m not convinced that any weighting scheme will realize much better performance than any other.
Thanks for reading.
NOTE: I am always interested in networking and hearing about full-time opportunities related to my skill set. My linkedIn profile can be found here.
Comments
Post a Comment