MTF-HAS-SCALPLING-DEMO517

Author: steinitz@cox.net
Price Data Components
Series array that contains close prices for each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself
Indicators Used
Indicator of the average true range
Miscellaneous
It issuies visual alerts to the screenIt plays sound alerts
0 Views
0 Downloads
0 Favorites
MTF-HAS-SCALPLING-DEMO517
//+------------------------------------------------------------------+
//|                                             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;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicID)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
   if(buys>0) 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;i<OrdersTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MagicID || OrderSymbol()!=Symbol()) continue;/////////////////////////
      if(BreakEvenOn==true)
      {
         if(OrderType()==OP_BUY  && (Bid-OrderOpenPrice()>Point*BreakEvenTrigger) &&(OrderStopLoss()<OrderOpenPrice()) ) 
            {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);}
         if(OrderType()==OP_SELL && (OrderOpenPrice()-Ask >Point*BreakEvenTrigger)&&(OrderStopLoss()>OrderOpenPrice()) )  
            {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);}   
       }
      
      if(TrailingStopOn==true)
      {
         if(OrderType()==OP_BUY  && (Bid-OrderOpenPrice()>Point*TrailingStopTrigger) &&(OrderStopLoss()<Bid-Point*TrailingStop) ) 
            {OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Blue);}
         if(OrderType()==OP_SELL && (OrderOpenPrice()-Ask >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] <htf && 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<haClose) return(-1); else return(1); 
 //if ( iCustom(NULL,tf,"N4 TF HAS Bar2",0,index) == EMPTY_VALUE ) return(-1); else return(1);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
string isUporDown(int i)
{
   if(i==1) return ("LONG"); else if (i==-1) return("SHORT");
}

string tf2txt(int tf)
{
   switch(tf)
   {
      case PERIOD_M1: return("M1");
      case PERIOD_M5: return("M5");
      case PERIOD_M15: return("M15");
      case PERIOD_M30: return("M30");
      case PERIOD_H1: return("H1");
      case PERIOD_H4: return("H4");
      case PERIOD_D1: return("D1");
      case PERIOD_W1: return("W1");
      case PERIOD_MN1: return("MN");
   
   }
   return("??");
}

int  txt2tf(string txt)//if wrong input return 0
{
  if(txt=="m1" || txt=="M1") return(PERIOD_M1);
  else if (txt=="m5" || txt=="M5") return(PERIOD_M5);
  else if (txt=="m15" || txt=="M15") return(PERIOD_M15);
  else if (txt=="m30" || txt=="M30") return(PERIOD_M30);
  else if (txt=="h1" || txt=="H1") return(PERIOD_H1);
  else if (txt=="h4" || txt=="H4") return(PERIOD_H4);
  else if (txt=="d1" || txt=="D1") return(PERIOD_D1);
  else if (txt=="w1" || txt=="W1") return(PERIOD_W1);
  else if (txt=="mn" || txt=="MN"|| txt=="mN"|| txt=="Mn") return(PERIOD_MN1);
  else return(0);
}

void showComment(string str)
{
  static string str1t="",str2t="",str3t="",str3="",dots="";
  string mtime=Day()+"/"+Month()+" "+Hour()+":"+Minute()+":"+Seconds()+"  "; 
  string tmpstr;
  Print(str);
  if (str ==str3) 
      {dots=dots+"."; if (dots=="..........") dots="";}
  else 
     {str1t=str2t;str2t=str3t;
      str3t=mtime+str;
      str3=str;dots="";
      }
  tmpstr="MTF HAS SCALPING STATUS:\n"+str1t+"\n"+str2t+"\n"+str3t+dots;
  Comment(tmpstr);
  return;
}




Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---