Jump to content

Free EA based on a regression channel


Recommended Posts

I have found this EA on mq4 code base http://codebase.mql4.com/4332.

the concept is sound but for some reason i can't get the expert to function .Could someone who can code have alook at it to see if it can be tweaked....

 

#property copyright "© 2008 BJF Trading Group"
#property link      "www.iticsoftware.com"

#define major   1
#define minor   1

extern string _tmp1_ = " --- Trade params ---";
extern string TradeTime = "3:00-21:20";
extern double Lots = 0.1;
extern int StopLoss = 0;
extern int TakeProfit = 0;
extern int Slippage = 3;
extern int Magic = 20080829;

extern string _tmp2_ = " --- i-Regr ---";
extern int Regr.degree = 3;
extern double Regr.kstd = 1.0;
extern int Regr.bars = 250;
extern int Regr.shift = 0;

extern string _tmp3_ = " --- Trailing ---";
extern bool TrailingOn = false;
extern int TrailingStart = 30;
extern int TrailingSize = 30;

extern string _tmp4_ = " --- Chart ---";
extern color clBuy = DodgerBlue;
extern color clSell = Red;
extern color clModify = Silver;
extern color clClose = Gold;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <stdlib.mqh>
#include <stderror.mqh>

int RepeatN = 3;
int BuyCnt, SellCnt;

void init() 
{
}

void deinit() 
{
}

void start() 
{
 //-----
 
 if (TrailingOn) TrailPositions();  
 
 //-----

 string ind_name = "i-Regr";

 double R.M0 = iCustom(NULL, 0, ind_name,
   Regr.degree, Regr.kstd, Regr.bars, Regr.shift,  
   0, 0);
 double R.M1 = iCustom(NULL, 0, ind_name,
   Regr.degree, Regr.kstd, Regr.bars, Regr.shift,  
   0, 1);
 
 double R.U0 = iCustom(NULL, 0, ind_name,
   Regr.degree, Regr.kstd, Regr.bars, Regr.shift,  
   1, 0);
 double R.U1 = iCustom(NULL, 0, ind_name,
   Regr.degree, Regr.kstd, Regr.bars, Regr.shift,  
   1, 1);

 double R.L0 = iCustom(NULL, 0, ind_name,
   Regr.degree, Regr.kstd, Regr.bars, Regr.shift,  
   2, 0);
 double R.L1 = iCustom(NULL, 0, ind_name,
   Regr.degree, Regr.kstd, Regr.bars, Regr.shift,  
   2, 1);
 
 //-----
 
 if (Bid >= R.M0)
 {
   if (CloseOrders(OP_BUY) > 0) return;
 }

 if (Bid <= R.M0)
 {
   if (CloseOrders(OP_SELL) > 0) return;
 }
 
 //-----
 
 if (!IsTradeTime()) return;

 
if (iHigh(NULL, PERIOD_D1, 1) - iLow(NULL, PERIOD_D1, 1) > 150*Point) 
{
CloseOrders(OP_BUY);
CloseOrders(OP_SELL);
 return;
}

 if (OrdersCountBar0(0) > 0) return;
 
 RecountOrders();
 
 //if (BuyCnt+SellCnt > 0) return;

 //-----
    
 double price, sl, tp;
 int ticket;

 //if (High[0] >= R.M0 && Open[0] < R.M0 && R.M0 > R.M1)
 
 if (Low[0] <= R.L0)
 {
   if (BuyCnt > 0) return;
   //if (CloseOrders(OP_SELL) > 0) return;
   
   //-----    
   
   for (int i=0; i<RepeatN; i++)
   {
     RefreshRates();
     price = Ask;
 
     sl = If(StopLoss > 0, price - StopLoss*Point, 0);
     tp = If(TakeProfit > 0, price + TakeProfit*Point, 0);

     ticket = Buy(Symbol(), GetLots(), price, sl, tp, Magic);
     if (ticket > 0) break;
   }

   return;
 }

 //if (Low[0] <= R.M0 && Open[0] > R.M0 && R.M0 < R.M1)
 if (High[0] >= R.U0)
 {
   if (SellCnt > 0) return;
   //if (CloseOrders(OP_BUY) > 0) return;
   
   //-----    
   
   for (i=0; i<RepeatN; i++)
   {  
     RefreshRates();
     price = Bid;
   
     sl = If(StopLoss > 0, price + StopLoss*Point, 0);
     tp = If(TakeProfit > 0, price - TakeProfit*Point, 0);

     ticket = Sell(Symbol(), GetLots(), price, sl, tp, Magic);
     if (ticket > 0) break;
   }

   return;
 } 
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

double If(bool cond, double if_true, double if_false)
{
 if (cond) return (if_true);
 return (if_false);
}

void split(string& arr[], string str, string sym) 
{
 ArrayResize(arr, 0);
 string item;
 int pos, size;
 
 int len = StringLen(str);
 for (int i=0; i < len;) 
 {
   pos = StringFind(str, sym, i);
   if (pos == -1) pos = len;
   
   item = StringSubstr(str, i, pos-i);
   item = StringTrimLeft(item);
   item = StringTrimRight(item);
   
   size = ArraySize(arr);
   ArrayResize(arr, size+1);
   arr[size] = item;
   
   i = pos+1;
 }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

double GetLots() 
{
 return (Lots);
}

void RecountOrders()
{
 BuyCnt = 0;
 SellCnt = 0;

 int cnt = OrdersTotal();
 for (int i=0; i < cnt; i++) 
 {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol()) continue;
   if (OrderMagicNumber() != Magic) continue;
   
   int type = OrderType();
   if (type == OP_BUY) BuyCnt++;
   if (type == OP_SELL) SellCnt++;
 }
}

bool IsTradeTime()
{
 if (TradeTime == "0:00-24:00") return (true);
 if (TradeTime == "00:00-24:00") return (true);

 datetime tm1, tm2;

 string TI[];
 split(TI, TradeTime, "-");
 if (ArraySize(TI) != 2) return (false);
   
 datetime tm0 = TimeCurrent();
 tm1 = StrToTime(TimeToStr(tm0, TIME_DATE) + " " + TI[0]);
 tm2 = StrToTime(TimeToStr(tm0, TIME_DATE) + " " + TI[1]);

 bool isTm = false; 
 if (tm1 <= tm2) 
   isTm = isTm || (tm1 <= tm0 && tm0 < tm2);
 else
   isTm = isTm || (tm1 <= tm0 || tm0 < tm2);
 
 return (isTm);
}

int OrdersCountBar0(int TF)
{
 int orders = 0;

 int cnt = OrdersTotal();
 for (int i=0; i<cnt; i++) 
 {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol()) continue;
   if (OrderMagicNumber() != Magic) continue;

   if (OrderOpenTime() >= iTime(NULL, TF, 0)) orders++;
 }

 cnt = OrdersHistoryTotal();
 for (i=0; i<cnt; i++) 
 {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
   if (OrderSymbol() != Symbol()) continue;
   if (OrderMagicNumber() != Magic) continue;

   if (OrderOpenTime() >= iTime(NULL, TF, 0)) orders++;
 }

 return (orders);
}

int CloseOrders(int type) 
{  
 int cnt = OrdersTotal();
 for (int i=cnt-1; i >= 0; i--) 
 {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol()) continue;
   if (OrderMagicNumber() != Magic) continue;
   
   if (OrderType() != type) continue;
   
   if (type == OP_BUY) 
   {
     RefreshRates();
     CloseOrder(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID));
     continue;
   }
   
   if (type == OP_SELL) 
   {
     RefreshRates();
     CloseOrder(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK));
     continue;
   }    
 }
 
 int orders = 0;
 cnt = OrdersTotal();
 for (i = 0; i < cnt; i++) 
 {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol()) continue;
   if (OrderMagicNumber() != Magic) continue;
   
   if (OrderType() == type) orders++;
 }
 
 return (orders); 
}

void TrailPositions()
{
 int cnt = OrdersTotal();
 for (int i=0; i<cnt; i++) 
 {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol()) continue;
   if (OrderMagicNumber() != Magic) continue;

   int type = OrderType();
   if (type == OP_BUY) 
   {
     if (Bid-OrderOpenPrice() > TrailingStart*Point) 
     {
       if (OrderStopLoss() < Bid - (TrailingSize+1)*Point) 
       {
         OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingSize*Point, OrderTakeProfit(), 0, clModify);
       }
     }
   }

   if (type == OP_SELL)
   {
     if (OrderOpenPrice()-Ask > TrailingStart*Point) 
     {
       if (OrderStopLoss() > Ask + (TrailingSize+1)*Point || OrderStopLoss() == 0) 
       {
         OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingSize*Point, OrderTakeProfit(), 0, clModify);
       }
     }
   }
 }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int SleepOk = 2000;
int SleepErr = 6000;

int Buy(string symbol, double lot, double price, double sl, double tp, int magic, string comment="") 
{
 int dig = MarketInfo(symbol, MODE_DIGITS);

 price = NormalizeDouble(price, dig);
 sl = NormalizeDouble(sl, dig);
 tp = NormalizeDouble(tp, dig);
   
 string _lot = DoubleToStr(lot, 2);
 string _price = DoubleToStr(price, dig);
 string _sl = DoubleToStr(sl, dig);
 string _tp = DoubleToStr(tp, dig);

 Print("Buy \"", symbol, "\", ", _lot, ", ", _price, ", ", Slippage, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");

 int res = OrderSend(symbol, OP_BUY, lot, price, Slippage, sl, tp, comment, magic, 0, clBuy);
 if (res >= 0) {
   Sleep(SleepOk);
   return (res);
 }     
      
 int code = GetLastError();
 Print("Error opening BUY order: ", ErrorDescription(code), " (", code, ")");
 Sleep(SleepErr);
   
 return (-1);
}

int Sell(string symbol, double lot, double price, double sl, double tp, int magic, string comment="") 
{
 int dig = MarketInfo(symbol, MODE_DIGITS);

 price = NormalizeDouble(price, dig);
 sl = NormalizeDouble(sl, dig);
 tp = NormalizeDouble(tp, dig);
 
 string _lot = DoubleToStr(lot, 2);
 string _price = DoubleToStr(price, dig);
 string _sl = DoubleToStr(sl, dig);
 string _tp = DoubleToStr(tp, dig);

 Print("Sell \"", symbol, "\", ", _lot, ", ", _price, ", ", Slippage, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");
 
 int res = OrderSend(symbol, OP_SELL, lot, price, Slippage, sl, tp, comment, magic, 0, clSell);
 if (res >= 0) {
   Sleep(SleepOk);
   return (res);
 }     
      
 int code = GetLastError();
 Print("Error opening SELL order: ", ErrorDescription(code), " (", code, ")");
 Sleep(SleepErr);
   
 return (-1);
}

bool CloseOrder(int ticket, double lot, double price) 
{
 if (!OrderSelect(ticket, SELECT_BY_TICKET)) return(false);
 if (OrderCloseTime() > 0) return(false);
 
 int dig = MarketInfo(OrderSymbol(), MODE_DIGITS);
 string _lot = DoubleToStr(lot, 2);
 string _price = DoubleToStr(price, dig);

 Print("CloseOrder ", ticket, ", ", _lot, ", ", _price, ", ", Slippage);
 
 bool res = OrderClose(ticket, lot, price, Slippage, clClose);
 if (res) {
   Sleep(SleepOk);
   return (res);
 }     
      
 int code = GetLastError();
 Print("CloseOrder failed: ", ErrorDescription(code), " (", code, ")");
 Sleep(SleepErr);
   
 return (false);
}

 

The indicator it is based on is an i-regr indicator .here is the code for it

 
//----
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Gold
#property indicator_color3 Gold


extern int degree = 3;
extern double kstd = 2.0;
extern int bars = 250;
extern int shift = 0;


//-----
double fx[],sqh[],sql[];

double ai[10,10],b[10],x[10],sx[20];
double sum; 
int ip,p,n,f;
double qq,mm,tt;
int ii,jj,kk,ll,nn;
double sq;

int i0 = 0;

/*  
void clear()
{
 int total = ObjectsTotal();  
 for (int i=total-1; i >= 0; i--) 
 {
   string name = ObjectName(i);
   if (StringFind(name, prefix) == 0) ObjectDelete(name);
 }
}
*/

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
 SetIndexBuffer(0, fx); // ?????? ???????? ??????????
 SetIndexBuffer(1, sqh);
 SetIndexBuffer(2, sql);

 SetIndexStyle(0, DRAW_LINE);
 SetIndexStyle(1, DRAW_LINE);
 SetIndexStyle(2, DRAW_LINE);

 SetIndexEmptyValue(0, 0.0);
 SetIndexEmptyValue(1, 0.0);
 SetIndexEmptyValue(2, 0.0);
 
 SetIndexShift(0, shift);
 SetIndexShift(1, shift);
 SetIndexShift(2, shift);

 return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
 //clear();
 return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
 if (Bars < bars) return;

//---- 
   
 int mi; // ?????????? ?????????????? ?????? ? start
 ip = bars;
 p=ip; // ???? ????????????
 sx[1]=p+1; // ?????????? - [] - ???????? ??????
 nn = degree+1;

 SetIndexDrawBegin(0, Bars-p-1);
 SetIndexDrawBegin(1, Bars-p-1);
 SetIndexDrawBegin(2, Bars-p-1); 
  
//----------------------sx-------------------------------------------------------------------
 for(mi=1;mi<=nn*2-2;mi++) // ?????????????? ????????? - ??? ???? mi ?? 1 ?? nn*2-2 
 {
   sum=0;
   for(n=i0;n<=i0+p;n++)
   {
      sum+=MathPow(n,mi);
   }
   sx[mi+1]=sum;
 }  
 //----------------------syx-----------
 for(mi=1;mi<=nn;mi++)
 {
   sum=0.00000;
   for(n=i0;n<=i0+p;n++)
   {
      if(mi==1) sum+=Close[n];
      else sum+=Close[n]*MathPow(n,mi-1);
   }
   b[mi]=sum;
 } 
//===============Matrix=======================================================================================================
 for(jj=1;jj<=nn;jj++)
 {
   for(ii=1; ii<=nn; ii++)
   {
      kk=ii+jj-1;
      ai[ii,jj]=sx[kk];
   }
 }  
//===============Gauss========================================================================================================
 for(kk=1; kk<=nn-1; kk++)
 {
   ll=0;
   mm=0;
   for(ii=kk; ii<=nn; ii++)
   {
      if(MathAbs(ai[ii,kk])>mm)
      {
         mm=MathAbs(ai[ii,kk]);
         ll=ii;
      }
   }
   if(ll==0) return(0);   
   if (ll!=kk)
   {
      for(jj=1; jj<=nn; jj++)
      {
         tt=ai[kk,jj];
         ai[kk,jj]=ai[ll,jj];
         ai[ll,jj]=tt;
      }
      tt=b[kk];
      b[kk]=b[ll];
      b[ll]=tt;
   }  
   for(ii=kk+1;ii<=nn;ii++)
   {
      qq=ai[ii,kk]/ai[kk,kk];
      for(jj=1;jj<=nn;jj++)
      {
         if(jj==kk) ai[ii,jj]=0;
         else ai[ii,jj]=ai[ii,jj]-qq*ai[kk,jj];
      }
      b[ii]=b[ii]-qq*b[kk];
   }
 }  
 x[nn]=b[nn]/ai[nn,nn];
 for(ii=nn-1;ii>=1;ii--)
 {
   tt=0;
   for(jj=1;jj<=nn-ii;jj++)
   {
      tt=tt+ai[ii,ii+jj]*x[ii+jj];
      x[ii]=(1/ai[ii,ii])*(b[ii]-tt);
   }
 } 
//===========================================================================================================================
 for(n=i0;n<=i0+p;n++)
 {
   sum=0;
   for(kk=1;kk<=degree;kk++)
   {
      sum+=x[kk+1]*MathPow(n,kk);
   }
   fx[n]=x[1]+sum;
 } 
//-----------------------------------Std-----------------------------------------------------------------------------------
 sq=0.0;
 for(n=i0;n<=i0+p;n++)
 {
   sq+=MathPow(Close[n]-fx[n],2);
 }
 sq=MathSqrt(sq/(p+1))*kstd;

 for(n=i0;n<=i0+p;n++)
 {
   sqh[n]=fx[n]+sq;
   sql[n]=fx[n]-sq;
 }

 return(0);
}
//+------------------------------------------------------------------+

 

If the e-regr EA can be tweaked I can post a strategy based on it which recently won a competition winning/ making over 2000% in one month

Link to comment
Share on other sites

  • 2 weeks later...

Re: Free EA based on a regression channel

 

try these professor ryaz:

 

Name: Metatrader Expert Advisor e-Regr and Metatrader Indicator i-Regr

http://codebase.mql4.com/4332

 

Name: Indicator i-Regr H&L. Channel of Regression by High and Low Prices

http://codebase.mql4.com/4404

 

Name: Indicator i-Regr v2. Channel regression

http://russian-forex-indicators.blogspot.com/2009/02/indicator-i-regr-v2-channel-regression.html

"It is inconceivable that anyone will divulge a truly effective get-rich scheme for the price of a book."

Victor Niederhoffer (1943–), US hedge fund manager and statistician

Link to comment
Share on other sites

Re: Free EA based on a regression channel

 

Thanks Stormin for clarifying original post .

Mr. Ryaz ;I had posted the code for both EA and indicator in my first post .

 

I have the winning strategy from surefire that used i-regr as its main indicator and am happy to post it here for your perusal but am unsure how to upload.....I am happy to send via email if you wish or perhaps you could advise how to post for general download...I am thinking that the E-regr Expert could be potentially upgraded/ modified by utilizing the key elements from the surefire strategy or adding a couple of refining/trendchange confirming indicators such as ema crossover , BBmacd or parabolic sar to create a potentially very profitable EA....the concept of entry and exit in the E-regr as outlined in the mq4 codebase link seems very sound,particularly when visually backtested.

note both the I-rgr indicator and E-regr expert allow for use of regression indicator using 1st,2nd,or 3rd polynomial.....,perhaps it just needs a little refinement....Regards BhaiSahib

P.S . I also have the EA creating program..Visual trader system that allows one to build an EA without needing to know mq4 language.More than happy to share for the purpose of building an EA based on the E-regr/i-regr principle

Link to comment
Share on other sites

Re: Free EA based on a regression channel

 

...I am thinking that the E-regr Expert could be potentially upgraded/ modified...

Well the ind needs long time to evaluate so BT is impossible. This is a classical counter trend EA like BBands CT strategy

that work very well on trading markets and very bad on strong trends, so an improvement would be to detect trend strength for ex. use original rules if ADX is below threshold.

regards

Ad augusta per angusta
Link to comment
Share on other sites

Re: Free EA based on a regression channel

 

Thanks Ryaz for your suggestion ..makes a lot of sense. I have contacted a private programmer and will pursue the project privately as there does not seem to be a lot of interest in this forum at this time. I will keep you all updated if I achieve any success in modifying and updating this advisor. I hope that those who downloaded the visual trader"s studio for creating EA's have fun with it

Thankyou all who have contributed....Regards BhaiSahib

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...