//+------------------------------------------------------------------+ //| MTF-HAS-SCALPING.mq4 | //| steinitz@cox.net | //| steinitz@cox.net | //+------------------------------------------------------------------+ #property copyright "steinitz@cox.net" #property link "steinitz@cox.net" #define MagicID 130228 extern string SCALPLING_METHOD="Inventer:DON STEINITZ ;Programmer:LEE YAN"; extern string StartTF ="MN"; extern string EndTF ="M30"; //extern string AlertTFFilter ="FALSE =Ignore the alert on this TF"; //extern bool AlertTFW1On=false; //extern bool AlertTFD1On=true; //extern bool AlertTFH4On=true; //extern bool AlertTFH1On=true; //extern bool AlertTFM30On=true; //extern bool AlertTFM15On=true; //extern bool AlertTFM5On=true; extern double Lots=1; //extern double DoubleLotsWhenFreeMarginXTimes=2; extern string TakeProfitMethod1 ="TPMethodUseATR=true: USE TakeProfitMethod1"; extern bool TPMethodUseATR=false; extern int TPAtrPeriod=14; extern double TPAtrPercentage =0.9; extern string TakeProfitMethod2 ="TPMethodUseATR=false: USE TakeProfitMethod2"; //extern int TakeProfitM1=5;//2007.5.10 extern int TakeProfitW1=100; extern int TakeProfitD1=60; extern int TakeProfitH4=25; extern int TakeProfitH1=25; extern int TakeProfitM30=20; extern int TakeProfitM15=15; extern int TakeProfitM5=8; //extern int TakeProfitMN=200;//2007.5.10 extern string StopLossSET ="---------------------------"; extern int StopLoss=0;// 0= no stoploss extern bool StartTFStopLossOn=true; extern string TrailingStopLossSET ="---------------------------"; extern bool BreakEvenOn=false; extern int BreakEvenTrigger=15; extern bool TrailingStopOn=false; extern int TrailingStopTrigger = 25; extern int TrailingStop = 15; extern string ContackUS ="steinitz@cox.net ; lee_yan_cn@126.com"; extern int MaMetod = 2; extern int MaPeriod = 6; extern int MaMetod2 = 3; extern int MaPeriod2 = 2; bool Scalpling_Method_On =true; bool isFinding=true; int ftf=0,wtf=0,hc1=0,hc2=0,wc=0,fc=0; int htf,ltf; double TakeProfit; // index 0 1 2 3 4 5 6 7 8 int allTF[9]={PERIOD_MN1,PERIOD_W1,PERIOD_D1,PERIOD_H4,PERIOD_H1,PERIOD_M30,PERIOD_M15,PERIOD_M5,PERIOD_M1}; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { htf=txt2tf(StringTrimRight(StringTrimLeft(StartTF))); ltf=txt2tf(StringTrimRight(StringTrimLeft(EndTF))); isFinding=true; if(CheckSet()==true) {Scalpling_Method_On=true;showComment("INITIALIZED SUCCESSFULLY"); start();} else {Scalpling_Method_On=false;showComment("INITIALIZED UNSUCCESSFULLY");} return(0); } int deinit() { return(0); } bool CheckSet() { string str=""; //if(IsDemo()==false) {Alert("This is a demo version! Please use it on a demo account!"); return(false);}//**************** if(htf==0 ){Alert("StartTF is set to wrong value,please recheck it!"); return(false);} if(ltf==0 ){Alert("EndTF is set to wrong value,please recheck it!"); return(false);} if(ltf==1 ){Alert("EndTF must > M1,please recheck it!"); return(false);} if(ltf>=htf ){Alert("EndTF must < StartTF,please recheck it!"); return(false);} //for(int i=0;i<=8;i++)// get the opposite bar's tf // {if(iBars(Symbol(),allTF[i])<100) str=str+tf2txt(allTF[i])+" " ;} //if(str!="") {Alert("There are not enough bars on "+str+",please recheck it!"); return(false);} return(true); } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// int start() { if((Year()==2007 && Month()>6)||(Year()>2007) ) return;//************************** if(Scalpling_Method_On==false||IsTradeAllowed()==false ) return; //************************** Alert(iClose(Symbol(),0,0)); if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(0); else CheckForClose(); return(0); } double getLots() { return(Lots);} int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; for(int i=0;i0) return(buys);else return(-sells); } int CalTPSL(int signal,int tf) { if(TPMethodUseATR==true) TakeProfit=MathRound( iATR(Symbol(),tf,TPAtrPeriod,1)* TPAtrPercentage/Point); else { switch (tf) { //case PERIOD_MN1: TakeProfit=TakeProfitMN; break; //2007.5.10 case PERIOD_W1: TakeProfit=TakeProfitW1 ; break; case PERIOD_D1: TakeProfit=TakeProfitD1 ; break; case PERIOD_H4: TakeProfit=TakeProfitH4 ; break; case PERIOD_H1: TakeProfit=TakeProfitH1 ; break; case PERIOD_M30: TakeProfit=TakeProfitM30 ; break; case PERIOD_M15: TakeProfit=TakeProfitM15 ; break; case PERIOD_M5: TakeProfit=TakeProfitM5 ; break; //case PERIOD_M1: TakeProfit=TakeProfitM1 ; break; //2007.5.10 } } return(0); } void CheckForOpen(int pos) { int res; int signal=ScalplingMethod(pos); CalTPSL(signal,ftf);//wtf-->ftf 2007.5.10 double TPprice,STprice; if (signal==1) {if(TakeProfit!=0) TPprice=Ask+TakeProfit*Point; else TPprice=0; if(StopLoss!=0) STprice=Ask-StopLoss*Point;else STprice=0; } else if (signal==-1) {if(TakeProfit!=0) TPprice=Bid-TakeProfit*Point; else TPprice=0; if(StopLoss!=0) STprice=Bid+StopLoss*Point; else STprice=0; } if (signal==1) {res=OrderSend(Symbol(),OP_BUY,getLots(),Ask,3,STprice,TPprice,"1",MagicID,0,Blue); } else if (signal==-1) {res=OrderSend(Symbol(),OP_SELL,getLots(),Bid,3,STprice,TPprice,"-1",MagicID,0,Red); } if(res<0) {Print("OrderSend failed with error #",GetLastError());} return; } void CheckForClose() { for(int i=0;iPoint*BreakEvenTrigger) &&(OrderStopLoss()Point*BreakEvenTrigger)&&(OrderStopLoss()>OrderOpenPrice()) ) {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);} } if(TrailingStopOn==true) { if(OrderType()==OP_BUY && (Bid-OrderOpenPrice()>Point*TrailingStopTrigger) &&(OrderStopLoss()Point*TrailingStopTrigger)&&(OrderStopLoss()>Ask+Point*TrailingStop) ) {OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);} } if(StartTFStopLossOn==true) { if(OrderType()==OP_BUY && getTFColor(htf,0)!= 1 ) OrderClose(OrderTicket(),OrderLots(),Bid,3,Black); if(OrderType()==OP_SELL&& getTFColor(htf,0)!= -1) OrderClose(OrderTicket(),OrderLots(),Ask,3,Black); } } return; } int ScalplingMethod(int pos) { if(hc1!=getTFColor(htf,pos+1)) isFinding=true; if(isFinding==true) { hc1=getTFColor(htf,pos+1);//+1 closed bar's color ftf=0;wtf=0;isFinding=false; showComment("Trend of closed StartTF is "+ isUporDown(hc1) +", EA is trying to find an opposite trend between "+ tf2txt(htf) +" to "+ tf2txt(ltf) ); } for(int i=0;i<=8;i++)// get the opposite bar's tf {if( allTF[i] =ltf && getTFColor(allTF[i],pos+1)!=hc1) //+1 closed bar's color {ftf=allTF[i];wtf=MathMax(allTF[i+1],PERIOD_M1); break;} if(i==8) {ftf=0;wtf=0; } } if( ftf>0) { showComment("Found opposite trend in " + tf2txt(ftf) +",Waiting for the " +tf2txt(wtf)+" to turn "+isUporDown(hc1)); hc2=getTFColor(htf,pos); fc=getTFColor(ftf,pos); wc=getTFColor(wtf,pos); if(wc==hc1) { isFinding=true; if(hc2==hc1 && fc!=wc)//two filter {showComment("Valid chance appeared"); PlaySound("alert.wav"); return(wc);} else {showComment("Chance appeared but be filtered"); } } } return(0); } int getTFColor(int tf,int index)// return 1 is blue ;-1 is red {double haOpen; double haClose; haOpen = iCustom(NULL,tf,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,index) ; haClose = iCustom(NULL,tf,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,index) ; if (haOpen