// // Regression Channel with variable polynomial degree EA // // original by Boris // www.iticsoftware.com // http://www.mql5.com/en/code/8417 // // V1.1 by graziani: // -> minor changes for MT4 b600 compatibility // #property copyright "© 2008 BJF Trading Group" #property link "www.iticsoftware.com" #property strict #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 #include 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 i,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(i=0; i 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 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=iTime(NULL,TF,0)) orders++; } cnt=OrdersHistoryTotal(); for(i=0; i=iTime(NULL,TF,0)) orders++; } return (orders); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CloseOrders(int type) { int i; int cnt=OrdersTotal(); for(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; iTrailingStart*Point) { if(OrderStopLoss()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=(int) 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=(int) 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=(int) 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); } //+------------------------------------------------------------------+