Last active
February 24, 2016 23:38
-
-
Save 54chi/cb6b0e15903b2afc57e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Trend (e.g. moving average crossover) | |
function run() | |
{ | |
vars Price = series(price()); | |
vars Trend = series(LowPass(Price,500)); | |
vars MMI_Raw = series(MMI(Price,300)); | |
vars MMI_Smooth = series(LowPass(MMI_Raw,500)); | |
if(falling(MMI_Smooth)) { | |
if(valley(Trend)) | |
reverseLong(1); | |
else if(peak(Trend)) | |
reverseShort(1); | |
} | |
} | |
// 2. Mean reversion (E.g. Rule #1) | |
function run() | |
{ | |
vars Price = series(price()); | |
vars Filtered = series(HighPass(Price,30)); | |
vars Signal = series(FisherN(Filtered,500)); | |
var Threshold = 1.0; | |
if(Hurst(Price,500) < 0.5) { // do we have mean reversion? | |
if(crossUnder(Signal,-Threshold)) | |
reverseLong(1); | |
else if(crossOver(Signal,Threshold)) | |
reverseShort(1); | |
} | |
} | |
// 3. Statistical Arbitrage (e.g. comparing and ETF with its major stock) | |
require(quantmod) | |
symbols <- c("AAPL", "QQQ") | |
getSymbols(symbols) | |
#define training set | |
startT <- "2007-01-01" | |
endT <- "2009-01-01" | |
rangeT <- paste(startT,"::",endT,sep ="") | |
tAAPL <- AAPL[,6][rangeT] | |
tQQQ <- QQQ[,6][rangeT] | |
#compute price differences on in-sample data | |
pdtAAPL <- diff(tAAPL)[-1] | |
pdtQQQ <- diff(tQQQ)[-1] | |
#build the model | |
model <- lm(pdtAAPL ~ pdtQQQ - 1) | |
#extract the hedge ratio (h1 is assumed 1) | |
h2 <- as.numeric(model$coefficients[1]) | |
#spread price (in-sample) | |
spreadT <- tAAPL - h2 * tQQQ | |
#compute statistics of the spread | |
meanT <- as.numeric(mean(spreadT,na.rm=TRUE)) | |
sdT <- as.numeric(sd(spreadT,na.rm=TRUE)) | |
upperThr <- meanT + 1 * sdT | |
lowerThr <- meanT - 1 * sdT | |
#run in-sample test | |
spreadL <- length(spreadT) | |
pricesB <- c(rep(NA,spreadL)) | |
pricesS <- c(rep(NA,spreadL)) | |
sp <- as.numeric(spreadT) | |
tradeQty <- 100 | |
totalP <- 0 | |
for(i in 1:spreadL) { | |
spTemp <- sp[i] | |
if(spTemp < lowerThr) { | |
if(totalP <= 0){ | |
totalP <- totalP + tradeQty | |
pricesB[i] <- spTemp | |
} | |
} else if(spTemp > upperThr) { | |
if(totalP >= 0){ | |
totalP <- totalP - tradeQty | |
pricesS[i] <- spTemp | |
} | |
} | |
} | |
// 4. Price Constraints (grid trader like the EUR/CHF from back then) | |
// helper function to check if the grid line has no trade | |
bool isFree(var Price,var Grid,bool IsShort) | |
{ | |
for(open_trades) { | |
if(TradeIsShort == IsShort | |
&& between(TradeEntryLimit,Price-Grid/2,Price+Grid/2)) | |
return false; | |
} | |
return true; | |
} | |
// EUR/CHF grid trader main function | |
int run() | |
{ | |
BarPeriod = 60; | |
Hedge = 5; // activate virtual hedging | |
var Grid = 20*PIP; // set grid distance to 20 pips | |
var Close = priceClose(); | |
// place pending trades at 5 grid lines above and below the Close | |
int i; | |
for(i = Close/Grid - 5; i < Close/Grid + 5; i++) | |
{ | |
var Price = i*Grid; | |
// place short trades with profit target below the current price | |
if(Price < Close && isFree(Price,Grid,true)) | |
enterShort(1,Price,0,Grid); | |
// place long trades with profit target above the current price | |
else if(Price > Close && isFree(Price,Grid,false)) | |
enterLong(1,Price,0,Grid); | |
} | |
} | |
// 5. Cycles (sell/buy based on distance from 'fair' price) | |
function run() | |
{ | |
vars Price = series(price()); | |
var Phase = DominantPhase(Price,10); | |
vars Signal = series(sin(Phase+PI/4)); | |
vars Dominant = series(BandPass(Price,rDominantPeriod,1)); | |
ExitTime = 10*rDominantPeriod; | |
var Threshold = 1*PIP; | |
if(Amplitude(Dominant,100) > Threshold) { | |
if(valley(Signal)) | |
reverseLong(1); | |
else if(peak(Signal)) | |
reverseShort(1); | |
} | |
} | |
// 6. Clusters | |
// 7. Curve Patterns (Frechet algorithm) | |
// 8. Seasonality | |
// 9. Gaps (One Night Stand System) | |
// 10. Autoregression and Heteroskedasticity (ARIMA and GARCH) | |
// 11. News | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment