escape maxorder SL_nolimit_TP

Profit factor:
0.00
2 Views
0 Downloads
0 Favorites
escape maxorder SL_nolimit_TP
//+------------------------------------------------------------------+
//|                                                      escape.mq4 |
//|                                    Copyright © 2008, OGUZ BAYRAM |
//|                                            es_cape77@hotmail.com |
//+------------------------------------------------------------------+
extern string TradeTime = "0:00-24:00";
extern double    Lot=1.0;
extern double    MaxLot = 10000;
extern bool      LotsOptimized = TRUE;
extern int       Risk = 20;

extern int StopLoss=0;
extern double lTakeProfit = 20;
extern double sTakeProfit = 20;
extern double lTrailingStop = 20;
extern double sTrailingStop = 10;
extern color clOpenBuy = Blue;
extern color clCloseBuy = Aqua;
extern color clOpenSell = Red;
extern color clCloseSell = Violet;
extern color clModiBuy = Blue;
extern color clModiSell = Red;
extern string Name_Expert = "escape";
extern int Slippage = 1;
extern bool UseSound = true;
extern bool FractionalPips = true;
extern string NameFileSound = "Alert.wav";
extern int MaxOrders=100;
extern int Magic=2009;
extern int Spread=6;
void deinit() {
   Comment("");
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){

   if(Bars<100){
      Print("bars less than 100");
      return(0);
   }
   
   if(!IsTradeTime()){
      Print("not the trade time");
      return(0);
   }
   
   if (MarketInfo(Symbol(),MODE_SPREAD)>Spread)return;
   
   /* if (Digits==3 || Digits==5)   
   
   if(lTakeProfit<10){
      Print("TakeProfit less than 10");
      return(0);
   }
   if(sTakeProfit<10){
      Print("TakeProfit less than 10");
      return(0);
   } */

   double diClose0=iClose(NULL,5,0);
   double diMA1=iMA(NULL,5,5,0,MODE_SMA,PRICE_OPEN,0);
   double diClose2=iClose(NULL,5,0);
   double diMA3=iMA(NULL,4,5,0,MODE_SMA,PRICE_OPEN,0);
   double Lots=GetLotSize();

   if(AccountFreeMargin()<(1000*Lots)){
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0);
   }
   if (!ExistPositions()){

      if ((diClose0<diMA1)&& (CountOrders(OP_BUY,Magic)+CountOrders(OP_SELL,Magic))<MaxOrders){
         OpenBuy();
         return(0);
      }

      if ((diClose2>diMA3) && (CountOrders(OP_BUY,Magic)+CountOrders(OP_SELL,Magic))<MaxOrders){
         OpenSell();
         return(0);
      }
   }
   TrailingPositionsBuy(lTrailingStop);
   TrailingPositionsSell(sTrailingStop);
   return (0);
}

bool ExistPositions() {
for (int i=10; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
return(True);
}
} 
} 
return(false);
}
void TrailingPositionsBuy(int trailingStop) { 
   for (int i=10; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol()) { 
            if (OrderType()==OP_BUY) { 
               if (Bid-OrderOpenPrice()>trailingStop*Point) { 
                  if (OrderStopLoss()<Bid-trailingStop*Point) 
                     ModifyStopLoss(Bid-trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void TrailingPositionsSell(int trailingStop) { 
   for (int i=10; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol()) { 
            if (OrderType()==OP_SELL) { 
               if (OrderOpenPrice()-Ask>trailingStop*Point) { 
                  if (OrderStopLoss()>Ask+trailingStop*Point || 
OrderStopLoss()==0)  
                     ModifyStopLoss(Ask+trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void ModifyStopLoss(double ldStopLoss) { 
   bool fm;
   fm = OrderModify(OrderTicket(),OrderOpenPrice
(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE); 
   if (fm && UseSound) PlaySound(NameFileSound); 
} 

void OpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   ldLot = GetLotSize(); 
   if(StopLoss>0){ldStop=Ask-StopLoss*Point;}else {ldStop=0;}
   //ldStop = 0; 
   ldTake = GetTakeProfitBuy(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol
(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,Magic,0,clOpenBuy); 
   if (UseSound) PlaySound(NameFileSound); 
} 
void OpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 

   ldLot = GetLotSize(); 
   if(StopLoss>0){ldStop=Bid+StopLoss*Point;}else {ldStop=0;}
   //ldStop = 0; 
   ldTake = GetTakeProfitSell(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol
(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,Magic,0,clOpenSell); 
   if (UseSound) PlaySound(NameFileSound); 
} 
string GetCommentForOrder() { return(Name_Expert); }
double GetTakeProfitBuy() { return(Ask+lTakeProfit*Point); } 
double GetTakeProfitSell() { return(Bid-sTakeProfit*Point); } 

//|---------count orders

int CountOrders(int Type,int Magic)
{
   int _CountOrd;
   _CountOrd=0;
   for(int i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol())
      {
         if((OrderType()==Type&&(OrderMagicNumber()==Magic)||Magic==0))_CountOrd++;
      }
   }
   return(_CountOrd);
}

double GetLotSize()
{
   int dig = MarketInfo(OrderSymbol(), MODE_DIGITS);//Get digits size
   double MinlotTmp = MarketInfo(Symbol(), MODE_MINLOT); //What's the minimum possible lot size
   double MaxlotTmp = MarketInfo(Symbol(), MODE_MAXLOT); //What's the maximum possible lot size
   double Leverage = AccountLeverage(); //How much can you use depending on your account ballance and what the broker allowes
   double LotSize = MarketInfo(Symbol(), MODE_LOTSIZE); //What is the allowed lot size
   //Print("minimum possible lot size-",MinlotTmp,"/maximum possible lot size-",MaxlotTmp,"/Leverage-",Leverage,"/allowed lot size-",LotSize,"/AccountFreeMargin-",AccountFreeMargin());
   
   double lotsTmp = MathMin(MaxlotTmp, MathMax(MinlotTmp, Lot));
   //Print(lotsTmp);                                                //~500                                                ~0.25
   if (LotsOptimized && Risk > 0.0) lotsTmp = NormalizeDouble(AccountFreeMargin() * Risk / LotSize, dig);//&& AccountFreeMargin() > Ask * lotsTmp * LotSize / Leverage
   else lotsTmp = MinlotTmp;
   //Print(lotsTmp);
   lotsTmp = MathMax(MinlotTmp, MathMin(MaxlotTmp, NormalizeDouble(lotsTmp / MinlotTmp, 0) * MinlotTmp));
   //Print(lotsTmp);
   if (lotsTmp > MaxLot) lotsTmp = MaxLot;
   //Print(lotsTmp);
   return (lotsTmp);
}

bool IsTradeTime()
{
  if (TradeTime == "0:00-24:00") return (true); //Check if it's a round-the-clock time and return true
  
  string timeTmp[]; Split(timeTmp,TradeTime,"&");
  datetime tm0, tm1, tm2;
  string TI[];
  bool isTm = false;
  
  for(int i=0; i<ArraySize(timeTmp);i++)
  {
   Split(TI, timeTmp[i], "-");//Split trade time and make an array
   if (ArraySize(TI) != 2) return (false);//If we don't have two intervals our time setup is wrong. abort
    
   tm0 = TimeCurrent();//get current time
   tm1 = StrToTime(TimeToStr(tm0, TIME_DATE) + " " + TI[0]);//conver the first interval to proper time
   tm2 = StrToTime(TimeToStr(tm0, TIME_DATE) + " " + TI[1]);//conver the second interval to proper time

   if (tm1 <= tm2) 
      isTm = isTm || (tm1 <= tm0 && tm0 < tm2); //Is current time within the time interval?
   else
      isTm = isTm || (tm1 <= tm0 || tm0 < tm2);
  }
  return (isTm);
}

void Split(string& arr[], string str, string sym) //Replacdement for a standard split routine. Split a string into substrings and reformat those into an array
{
  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;
  }
}

Comments