- ago
I’m currently looking for introductory material on quantitative trading, mainly focused on trend following and mean reversion strategies. My goal is to study both the theoretical foundations and practical applications of these models. I already work in the financial markets, so I’m especially interested in content that is more applied and market-oriented rather than purely academic.
0
90
5 Replies

Reply

Bookmark

Sort
- ago
#1
CODE:
# FIRST CODE OF MY LIFE========================================================= # TURTLE TRADER ORIGINAL (SPY) # TREND FOLLOWING SYSTEM # DADOS DESDE 2023 # CAPITAL INICIAL = $1000 # 10% DO PATRIMÔNIO POR TRADE # ========================================================= # ========================================================= # BIBLIOTECAS # ========================================================= library(quantmod) library(TTR) library(PerformanceAnalytics) library(xts) # ========================================================= # DOWNLOAD DOS DADOS # ========================================================= getSymbols( "SPY", src = "yahoo", from = "2023-01-01" ) data <- na.omit(SPY) # ========================================================= # VISUALIZAR DADOS # ========================================================= View(data) # ========================================================= # PARÂMETROS # ========================================================= # Canal de entrada ChannelUp <- 20 ChannelDn <- 20 # Canal de saída LongExit <- 10 ShortExit <- 10 # Canal fail-safe FailsafeUp <- 55 FailsafeDn <- 55 # ATR ATRParam <- 20 # Tick mínimo Tick <- 0.01 # Capital inicial initialCapital <- 1000 # Percentual do patrimônio usado por trade riskPerTrade <- 0.10 # ========================================================= # INDICADORES # ========================================================= # Breakouts channelHigh <- runMax(Hi(data), ChannelUp) channelLow <- runMin(Lo(data), ChannelDn) # Fail-safe failsafeHigh <- runMax(Hi(data), FailsafeUp) failsafeLow <- runMin(Lo(data), FailsafeDn) # Exits longExitLine <- runMin(Lo(data), LongExit) shortExitLine <- runMax(Hi(data), ShortExit) # ATR ("N" do Turtle) N <- ATR(HLC(data), n = ATRParam)$atr # ========================================================= # VARIÁVEIS # ========================================================= n <- NROW(data) position <- rep(0, n) units <- rep(0, n) strategyRet <- rep(0, n) # Equity inicializado corretamente equity <- rep(initialCapital, n) # ========================================================= # ESTADO DA ESTRATÉGIA # ========================================================= currentPos <- 0 currentUnits <- 0 currentEntry <- NA shares <- 0 # ========================================================= # START INDEX # ========================================================= startIndex <- max( ChannelUp, ChannelDn, LongExit, ShortExit, FailsafeUp, FailsafeDn, ATRParam ) + 1 # ========================================================= # LOOP PRINCIPAL # ========================================================= for(i in startIndex:n) { # ===================================================== # PREÇOS # ===================================================== closePrice <- as.numeric(Cl(data)[i]) highPrice <- as.numeric(Hi(data)[i]) lowPrice <- as.numeric(Lo(data)[i]) atr <- as.numeric(N[i]) # Equity anterior currentEquity <- equity[i-1] # ===================================================== # CANAIS # ===================================================== # Breakouts L1 <- as.numeric(channelHigh[i-1]) S1 <- as.numeric(channelLow[i-1]) # Fail-safe FU <- as.numeric(failsafeHigh[i-1]) FD <- as.numeric(failsafeLow[i-1]) # Exits LE <- as.numeric(longExitLine[i-1]) - Tick SE <- as.numeric(shortExitLine[i-1]) + Tick # ===================================================== # PIRAMIDAGEM # ===================================================== # Long L2 <- L1 + 0.5 * atr L3 <- L1 + 1.0 * atr L4 <- L1 + 1.5 * atr # Short S2 <- S1 - 0.5 * atr S3 <- S1 - 1.0 * atr S4 <- S1 - 1.5 * atr # ===================================================== # ENTRADAS # ===================================================== if(currentPos == 0) { # Usa 10% do patrimônio allocation <- currentEquity * riskPerTrade # ================================================= # LONG ENTRY # ================================================= if(highPrice >= L1) { currentPos <- 1 currentEntry <- L1 currentUnits <- 1 # Fractional shares shares <- allocation / currentEntry } # ================================================= # SHORT ENTRY # ================================================= else if(lowPrice <= S1) { currentPos <- -1 currentEntry <- S1 currentUnits <- 1 # Fractional shares shares <- allocation / currentEntry } } # ===================================================== # PIRAMIDAGEM LONG # ===================================================== if(currentPos == 1) { if(currentUnits == 1 && highPrice >= L2) currentUnits <- 2 if(currentUnits == 2 && highPrice >= L3) currentUnits <- 3 if(currentUnits == 3 && highPrice >= L4) currentUnits <- 4 } # ===================================================== # PIRAMIDAGEM SHORT # ===================================================== if(currentPos == -1) { if(currentUnits == 1 && lowPrice <= S2) currentUnits <- 2 if(currentUnits == 2 && lowPrice <= S3) currentUnits <- 3 if(currentUnits == 3 && lowPrice <= S4) currentUnits <- 4 } # ===================================================== # STOPS LONG # ===================================================== if(currentPos == 1) { stopPrice <- currentEntry - 2 * atr if(lowPrice <= stopPrice || lowPrice <= LE) { currentPos <- 0 currentUnits <- 0 currentEntry <- NA shares <- 0 } } # ===================================================== # STOPS SHORT # ===================================================== if(currentPos == -1) { stopPrice <- currentEntry + 2 * atr if(highPrice >= stopPrice || highPrice >= SE) { currentPos <- 0 currentUnits <- 0 currentEntry <- NA shares <- 0 } } # ===================================================== # RETORNO DIÁRIO # ===================================================== ret <- as.numeric( dailyReturn(Cl(data))[i] ) # ===================================================== # PNL # ===================================================== pnl <- ret * currentPos * currentUnits * shares * closePrice # ===================================================== # SALVA RETORNO # ===================================================== strategyRet[i] <- pnl # ===================================================== # ATUALIZA EQUITY # ===================================================== equity[i] <- equity[i-1] + pnl # ===================================================== # SALVA ESTADO # ===================================================== position[i] <- currentPos units[i] <- currentUnits } # ========================================================= # CONVERTE PARA XTS # ========================================================= strategyRet <- xts( strategyRet, order.by = index(data) ) strategyRet[is.na(strategyRet)] <- 0 equity <- xts( equity, order.by = index(data) ) # ========================================================= # RETORNOS % # ========================================================= returnsPct <- Return.calculate(equity) returnsPct[is.na(returnsPct)] <- 0 # ========================================================= # MÉTRICAS # ========================================================= cat("\n==============================\n") cat("CAPITAL FINAL\n") cat("==============================\n") print(last(equity)) cat("\n==============================\n") cat("RETORNO TOTAL (%)\n") cat("==============================\n") totalReturn <- ( as.numeric(last(equity)) / initialCapital - 1 ) * 100 print(round(totalReturn, 2)) cat("\n==============================\n") cat("ANNUALIZED RETURNS\n") cat("==============================\n") print( table.AnnualizedReturns( returnsPct ) ) cat("\n==============================\n") cat("SHARPE RATIO\n") cat("==============================\n") print( SharpeRatio.annualized( returnsPct ) ) cat("\n==============================\n") cat("MAX DRAWDOWN\n") cat("==============================\n") print( maxDrawdown( returnsPct ) ) # ========================================================= # EQUITY CURVE # ========================================================= charts.PerformanceSummary( returnsPct ) # ========================================================= # GRÁFICO # ========================================================= chartSeries( data, theme = chartTheme("white"), name = "Turtle Trader - SPY", TA = NULL ) # Canal breakout addTA( channelHigh, col = "green" ) addTA( channelLow, col = "red" ) # Fail-safe addTA( failsafeHigh, col = "blue" ) addTA( failsafeLow, col = "purple" )
0
- ago
#2
There are several books on quantitative trading that are well worth reading. The books by Perry Kaufmann, Brent Penfold, Robert Carver, and Laurens Bensdorp, for example, would be a good place to start. You can also find videos on YouTube by these authors and others who haven’t written books—such as Adrian Reid or Martyn Tinsley—on the subject. This is merely an illustrative overview of a few “knowledge providers” in the field of quantitative system development. The world of quants is diverse, but it's definitely worth diving into.
1
- ago
#3
Looks like R code, yes?
0
- ago
#4
Yes, my first code
1
- ago
#5
Below is a very informative link that cites excellent references for learning more about quantitative trading. I would scroll down towards the bottom of this link to reach all the useful references. I think you'll find them interesting, especially if you're into statistical analysis.
https://www.quantstart.com/articles/Self-Study-Plan-for-Becoming-a-Quantitative-Trader-Part-I/
0

Reply

Bookmark

Sort