I'm gonna show you a simple initial approach to the "pattern detection" idea. I will use the open of the last 7 bars to define a "pattern" which will be the relationships between these values. I then look into the past to see if there are enough examples to train my machine learning algo. This is a function to perform this task, it returns 1 if there are enough examples in the historical data, 0 if there are not enough:
If there are enough examples I then build the input/output array using the matching patterns I have located in the past. In this case the inputs are a past number of returns and the outputs are the outcomes of long and short trades managed with a simple trailing stop where the SL is moved to the starting SL value distance from the current open if this stop is more favorable than the present stop:
With these two functions I can build the machine learning functions. This time I decided to use a neural network as the machine learning algo:
In this case the algorithm is retrained and relaunched on every hourly bar where there are enough examples. I then performed an optimization to see if I could find anything worth it on the EUR/USD 1H and found some interesting stuff. Here is a linear system example I found:
starting SL = 150% ATR20
B = 75 (number of examples used per training)
C = 8 (number of input returns used per example)
D = 185 (frontier for the trade output)
This algo does not use any hourly filters on the 1H chart, for this reason it's very different from everything I have shared so far on the 1H. Thoughts, always appreciated...
Inserted Code
int is_viable_examplesByStructure(int examplesNeeded, int frontier){ int tl = frontier; double open1, open2, open3, open4, open5, open6, open7; open1 = iOpen(0,1); open2 = iOpen(0,2); open3 = iOpen(0,3); open4 = iOpen(0,4); open5 = iOpen(0,5); open6 = iOpen(0,6); open7 = iOpen(0,7); int i = 0, j = 0, n=0, m = 0; while(m<examplesNeeded && i+tl+500<iBars(0)){ if ( ((iOpen(0,i+tl+1) > iOpen(0,i+tl+2)) == (open1 > open2)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+3)) == (open1 > open3)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+4)) == (open1 > open4)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+5)) == (open1 > open5)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+6)) == (open1 > open6)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+7)) == (open1 > open7)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+3)) == (open2 > open3)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+4)) == (open2 > open4)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+5)) == (open2 > open5)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+6)) == (open2 > open6)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+7)) == (open2 > open7)) && ((iOpen(0,i+tl+3) > iOpen(0,i+tl+4)) == (open3 > open4)) && ((iOpen(0,i+tl+3) > iOpen(0,i+tl+5)) == (open3 > open5)) && ((iOpen(0,i+tl+3) > iOpen(0,i+tl+6)) == (open3 > open6)) && ((iOpen(0,i+tl+3) > iOpen(0,i+tl+7)) == (open3 > open7)) && ((iOpen(0,i+tl+4) > iOpen(0,i+tl+5)) == (open4 > open5)) && ((iOpen(0,i+tl+4) > iOpen(0,i+tl+6)) == (open4 > open6)) && ((iOpen(0,i+tl+4) > iOpen(0,i+tl+7)) == (open4 > open7)) && ((iOpen(0,i+tl+5) > iOpen(0,i+tl+6)) == (open5 > open6)) && ((iOpen(0,i+tl+5) > iOpen(0,i+tl+7)) == (open5 > open7)) && ((iOpen(0,i+tl+6) > iOpen(0,i+tl+7)) == (open6 > open7)) ) { m++; } i++; } if (m<examplesNeeded){ return(0) ;} else { return(1); } }
If there are enough examples I then build the input/output array using the matching patterns I have located in the past. In this case the inputs are a past number of returns and the outputs are the outcomes of long and short trades managed with a simple trailing stop where the SL is moved to the starting SL value distance from the current open if this stop is more favorable than the present stop:
Inserted Code
RegressionDataset regression_i_simpleReturn_o_tradeOutcome_examplesByStructure(int period, int barsUsed, double initial_SL, int frontier, double minStop){ Data<RealVector> inputs(period,RealVector(barsUsed)); Data<RealVector> labels(period,RealVector(2)); double openPrice; double SL_long, SL_short, new_SL; double atr; int tl = frontier; double open1, open2, open3, open4, open5, open6, open7; open1 = iOpen(0,1); open2 = iOpen(0,2); open3 = iOpen(0,3); open4 = iOpen(0,4); open5 = iOpen(0,5); open6 = iOpen(0,6); open7 = iOpen(0,7); int i = 0, j = 0, n=0, m = 0; while(m<period){ if ( ((iOpen(0,i+tl+1) > iOpen(0,i+tl+2)) == (open1 > open2)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+3)) == (open1 > open3)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+4)) == (open1 > open4)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+5)) == (open1 > open5)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+6)) == (open1 > open6)) && ((iOpen(0,i+tl+1) > iOpen(0,i+tl+7)) == (open1 > open7)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+3)) == (open2 > open3)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+4)) == (open2 > open4)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+5)) == (open2 > open5)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+6)) == (open2 > open6)) && ((iOpen(0,i+tl+2) > iOpen(0,i+tl+7)) == (open2 > open7)) && ((iOpen(0,i+tl+3) > iOpen(0,i+tl+4)) == (open3 > open4)) && ((iOpen(0,i+tl+3) > iOpen(0,i+tl+5)) == (open3 > open5)) && ((iOpen(0,i+tl+3) > iOpen(0,i+tl+6)) == (open3 > open6)) && ((iOpen(0,i+tl+3) > iOpen(0,i+tl+7)) == (open3 > open7)) && ((iOpen(0,i+tl+4) > iOpen(0,i+tl+5)) == (open4 > open5)) && ((iOpen(0,i+tl+4) > iOpen(0,i+tl+6)) == (open4 > open6)) && ((iOpen(0,i+tl+4) > iOpen(0,i+tl+7)) == (open4 > open7)) && ((iOpen(0,i+tl+5) > iOpen(0,i+tl+6)) == (open5 > open6)) && ((iOpen(0,i+tl+5) > iOpen(0,i+tl+7)) == (open5 > open7)) && ((iOpen(0,i+tl+6) > iOpen(0,i+tl+7)) == (open6 > open7)) ){ atr = iAtrWholeDaysSimpleShift(PRIMARY_RATES, 20, i+tl+1); openPrice = cOpen(i+tl); SL_long = openPrice+spread()-atr*initial_SL; SL_short = openPrice+atr*initial_SL; n = 0; while ((SL_long < low(i+tl-n)) && (n<tl)){ n += 1; atr = iAtrWholeDaysSimpleShift(PRIMARY_RATES, 20, i+tl-n+1); new_SL = cOpen(i+tl-n)+spread() - (initial_SL*atr); if(new_SL <= SL_long){ new_SL = cOpen(i+tl-n); } if (cOpen(i+tl-n)-new_SL > minStop){ SL_long = new_SL; } } labels.element(m)[0] = SL_long-(openPrice+spread()); n = 0; while ((SL_short > high(i+tl-n)+spread()) && (n<tl)){ n += 1; atr = iAtrWholeDaysSimpleShift(PRIMARY_RATES, 20, i+tl-n+1); new_SL = cOpen(i+tl-n) + (initial_SL*atr); if(new_SL >= SL_short){ new_SL = cOpen(i+tl-n); } if (new_SL-cOpen(i+tl-n) > minStop){ SL_short = new_SL; } } labels.element(m)[1] = openPrice-SL_short; for(j=0;j<barsUsed;j++){ inputs.element(m)[j] = (iOpen(0,tl+j+i)-iOpen(0,1+tl+j+i))/iOpen(0,1+tl+j+i); } m++; } i++; } RegressionDataset dataset(inputs,labels); return dataset; }
With these two functions I can build the machine learning functions. This time I decided to use a neural network as the machine learning algo:
Inserted Code
double NN_Prediction_i_simpleReturn_o_tradeOutcome_examplesByStructure(int learningPeriod, int barsUsed, double initial_SL, int frontier, double minStop, int trainingEpochs) { int is_viable = is_viable_examplesByStructure(learningPeriod, frontier); if (is_viable == 0) return(0); RegressionDataset dataset = regression_i_simpleReturn_o_tradeOutcome_examplesByStructure(learningPeriod, barsUsed, initial_SL, frontier, minStop); FFNet<FastSigmoidNeuron,FastSigmoidNeuron> network; unsigned numInput=barsUsed; unsigned numHidden=(int)((barsUsed+2)/2); unsigned numOutput=2; unsigned numberOfSteps=trainingEpochs; unsigned step; network.setStructure(numInput, numHidden, numOutput); initRandomUniform(network,-0.5,0.5); SquaredLoss<> loss; ErrorFunction error(dataset, &network,&loss); RpropMinus optimizer; optimizer.init(error); for(step = 0; step < numberOfSteps; ++step){ optimizer.step(error); } network.setParameterVector(optimizer.solution().point); // set weights to weights found by learning Data<RealVector> testOutput; RegressionDataset datasetInput = p_regression_i_simpleReturn(barsUsed); testOutput = network(datasetInput.inputs()); if (testOutput.element(0)[0] > 0 && testOutput.element(0)[1] < 0) return(1); if (testOutput.element(0)[1] > 0 && testOutput.element(0)[0] < 0) return(-1); return(0); }
In this case the algorithm is retrained and relaunched on every hourly bar where there are enough examples. I then performed an optimization to see if I could find anything worth it on the EUR/USD 1H and found some interesting stuff. Here is a linear system example I found:
starting SL = 150% ATR20
B = 75 (number of examples used per training)
C = 8 (number of input returns used per example)
D = 185 (frontier for the trade output)
Attached Image
This algo does not use any hourly filters on the 1H chart, for this reason it's very different from everything I have shared so far on the 1H. Thoughts, always appreciated...