Guys, I have decided that like my previous mean reversion experiments I will happily open source this one onto ASF. Please note the parameters, n=2 and s=120. These are not curve fit or optimised numbers, I simply picked the lowest possible MR frequency and 6 months as a generalised long term measure. You can try other parameters to see where the boundaries of the edge lie.

Code:

//Params
int n=2;
int s=120;
//State
double cmd;
bool newBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
double calculatePosSize()
{
double x = iWPR(NULL,0,s,1);
double y = 0.1;
if(x >= -100 && x < -90) {
y = 0.5;
}
if(x >= -90 && x < -80) {
y = 0.4;
}
if(x >= -80 && x < -70) {
y = 0.3;
}
if(x >= -70 && x < -60) {
y = 0.2;
}
if(x >= -60 && x < -50) {
y = 0.1;
}
if(x >= -50 && x < -40) {
y = 0.1;
}
if(x >= -40 && x < -30) {
y = 0.2;
}
if(x >= -30 && x < -20) {
y = 0.3;
}
if(x >= -20 && x < -10) {
y = 0.4;
}
if(x >= -10 && x < 0) {
y = 0.5;
}
//return(0.1);
return(y);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if(!newBar()) return(0);
//ORDER MAINTENANCE
for(int cnt=OrdersTotal();cnt>=0;cnt--) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() ) {
cmd=OrderType();
//EXIT LONGS
if(cmd==OP_BUY && OrderMagicNumber() == 11){
if(Close[1] > Close[n] || Close[1] < Close[s]) {
OrderClose(OrderTicket(),OrderLots(),Bid,0,Blue);
}
}
//EXIT SHORTS
if(cmd==OP_SELL && OrderMagicNumber() == 21) {
if(Close[1] < Close[n] || Close[1] > Close[s]) {
OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
}
}
}
}
double atr=iATR(NULL,0,10,1);
double atr2=iATR(NULL,0,s,1);
//ORDER ENTRY
if(OrdersTotal() < 1 && atr > atr2){
//BUY
if(Close[1] > Close[s] && Close[1] < Close[n]) {
OrderSend(Symbol(),OP_BUY,calculatePosSize(),Ask,0.3,0,0,NULL,11,0,Blue);
}
//SELL
if(Close[1] < Close[s] && Close[1] > Close[n]) {
OrderSend(Symbol(),OP_SELL,calculatePosSize(),Bid,0.3,0,0,NULL,21,0,Red);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+

Enjoy.

## Bookmarks