Konstruuję strategię handlową i utknąłem w dwóch kluczowych obszarach. Kiedy używam Stoch i MACD w quantmod
, próbuję stworzyć sygnał, gdy wolna stochastyczna przechodzi przez szybką stochastyczną (1), i na odwrót (-1), i płaska, gdy pomiędzy (0). MACD kod jest identyczny z wyjątkiem nazw kolumn MACD i Signal. Wreszcie, staram się połączyć te trzy sygnały do tworzenia sygnału głównego, gdy wszystkie trzy sygnały są równe 1, -1, 0.Tworzenie sygnałów handlowych w R
library(quantmod)
####################
## BOLINGER BANDS ##
####################
getSymbols("SPY", src="yahoo", from="2013-01-01", to="2015-05-01")
x <- na.omit(merge(SPY, BBands(Cl(SPY))))
x$sig <- NA
# Flat where Close crossed the mavg
x$sig[c(FALSE, diff(sign(Cl(x) - x$mavg), na.pad=FALSE) != 0)] <- 0
x$sig[Cl(x) > x$up] <- -1 # short when Close is above up
x$sig[Cl(x) < x$dn] <- 1 # long when Close is below dn
x$sig[1] <- 0 # flat on the first day
x$sig[nrow(x)] <- 0 # flat on the last day
# Fill in the signal for other times
x$sig <- na.locf(x$sig) # wherever sig is NA, copy previous value to next row
# Now Lag your signal to reflect that you can't trade on the same bar that
# your signal fires
x$sig <- Lag(x$sig)
x$sig[1] <- 0 # replace NA with zero position on first row
####################
### STOCHASTICS ####
####################
y <- na.omit(merge(SPY, stoch(Cl(SPY))))
y$sig <- NA
# Flat where between crosses. Not sure how to write
#y$sig[c(FALSE, diff(sign(y$slowD == y$fastD), na.pad=FALSE !=0)] <- 0
y$sig[y$fastD > y$slowD] <- -1 # short when Close is above up
y$sig[y$fastD < y$slowD] <- 1 # long when Close is below dn
y$sig[1] <- 0 # flat on the first day
y$sig[nrow(x)] <- 0 # flat on the last day
# Fill in the signal for other times
y$sig <- na.locf(y$sig) # wherever sig is NA, copy previous value to next row
# Now Lag your signal to reflect that you can't trade on the same bar that
# your signal fires
y$sig <- Lag(y$sig)
y$sig[1] <- 0
####################
###### MACD ########
####################
z <- na.omit(merge(SPY, MACD(Cl(SPY))))
z$sig <- NA
# Flat where between crosses. Not sure how to write
z$sig[c(FALSE, diff(sign(z$signal == z$macd), na.pad=FALSE) != 1)] <- 1
z$sig[z$signal > z$macd] <- -1 # short when Close is above up
z$sig[z$signal < z$macd] <- 1 # long when Close is below dn
z$sig[1] <- 0 # flat on the first day
z$sig[nrow(z)] <- 0 # flat on the last day
# Fill in the signal for other times
z$sig <- na.locf(z$sig) # wherever sig is NA, copy previous value to next row
# Now Lag your signal to reflect that you can't trade on the same bar that
# your signal fires
z$sig <- Lag(z$sig)
z$sig[1] <- 0
# Merge xyz by date and create new signal when all three conditions are met
Co próbuję osiągnąć za pomocą MACD, a STOCH jest sygnałem, gdy linie przecinają się. (1) i wszystko inne (0). Zasadniczo stanowi potwierdzenie band z bollingerami. Punkt, w którym krzyżuje się linia, jest wszystkim, na czym mi zależy. Podstawą są prążki Bollingera, a MACD i STOCH potwierdzają i potwierdzają oryginalny sygnał. jakieś pomysły? – SJSU2013
@ SJSU2013 powinno to rozwiązać twój problem. Punkt, w którym krzyżują się linie, będzie miał wartość 1 lub -1, gdy zastosujesz 'diff' do wektora logicznego, który otrzymasz z' z $ macd> z $ signal'. Podobnie dla stochastycznych. – pbible