function tradeMeanReversion() {
int startHour = optimize(8, 0, 23, 1);
int openingWindow = optimize(8, 1, 23, 1); // max 24 hours
vars Prices = series((priceH()+priceL())/2); // Series of **average** price data
vars Cycles = series(StochRSI(Prices, 14, 10, 5, MAType_SMA)); // StochRSI cycles
vars Ratios = series(ER(Prices, 20));
var RatioThreshold = optimize(0.4, 0.2, 0.8, 0.1);
var Threshold = optimize(15, 5, 25, 5); // threshold for entry
var Exit_Threshold = 50; // threshold for exit
Trail = Stop = optimize(10, 4, 20, 2)*ATR(100);
LifeTime = 150; // set max lifetime just incase
MaxLong = MaxShort = -1; // one open trade at a time
Spread = marketVal();
Slippage = 1;
//RollLong = RollShort = 0;
Commission = 0.35; // Set costs to FX Prop firm highest rate for testing
// close all position at 16:15 local time
//if(at(1759)) {
// exitLong("**");
// exitShort("**");
//}
// move the starting hour to 0, any time before the starting hour needs to have 24h added to it before,
var hour24 = hour();
if(hour24 < startHour)
hour24 += 24;
// Time of day opening filter
if((hour24 >= startHour) && (hour24 <= startHour + openingWindow)) {
// noise filter
if(Ratios[0] > RatioThreshold)
{
// SHORT Condition
if(crossOver(Cycles, 100-Threshold)){
enterShort();
}
// LONG Condition
if(crossUnder(Cycles, Threshold)){
enterLong();
}
}
}
if(crossUnder(Cycles, Threshold)){
exitShort();
}
if(crossOver(Cycles, 100-Threshold)){
exitLong();
}
plot("Cycles", Cycles, LINE+NEW, RED);
plot("Threshold+", 100-Threshold, 0, GREY);
plot("Threshold-", Threshold, 0, GREY);
plot("Exit Threshold", Exit_Threshold, 0, GREY);
plot("Efficiency Ratio", Ratios, NEW, RED);
plot("Threshold Ratio", RatioThreshold, 0, GREY);
}
function run()
{
NumCores = -2;
set(LOGFILE+PLOTNOW+PARAMETERS+FACTORS+TESTNOW+ALLCYCLES); // Add factors to the list to generate a FACTORS file
// setf(TrainMode, BRUTE);
BarPeriod = 60;
LookBack = 150; // set lookback explicitly so all optimised strategies start at the same time
assetList("AssetsCur.csv");
if(Train) Detrend = TRADES; // equalise long and short profit for training
StartDate = 2010;
EndDate = 2018;
NumSampleCycles = 4; // oversample the price curve
NumWFOCycles = 10;
NumCores = -2;
ReTrainDays = 147;
if(ReTrain) { // true when training started during live trading
UpdateDays = -1; // Always download price data from broker
SelectWFO = -1; // only retrain the last cycle
}
while(asset(loop("EUR/USD", "GBP/USD")))
{
tradeMeanReversion();
}
}