robocop

#property copyright "robocop"
#property link      ""
#property version "1.0"
#include <stdlib.mqh>
#include <stderror.mqh>

   enum position
     {
      Yes=1,
      No=2
     };
     
extern string param_broker = "-------- BROKER PARAMETERS --------";
extern string p11 = "Enter the GMT offset of your broker in the winter period (not DST).";
extern int Broker_Server_GMT_Winter = 2;
extern string p12 = "Enter 'yes' if your broker changes the GMT offset, in the DST period.";
input position Broker_Adapt_GMT_On_DST_Period=1;
extern string param_size = "-------- ORDER SIZE PARAMETERS --------";
extern string p21 = "-> Order size in lots. Used if the parameter 'BALANCE_FOR_EVERY_001_Lots' is zero.";
extern double Fixed_Order_Size = 0.1;
extern string p22 = "-> If you set here a value > 0, for example 1000, the size of the order will be 0.01 lots for every 1000 of balance.";
extern double Balance_For_Every_001_Lots = 0.0;
extern string p23 = " -> Define the percentage of your account balance to assign (value between 0-200.0).";
extern double Autolot_Balance_Percentage = 100.0;
extern string param_other = "--------- OTHER PARAMETERS --------";
extern string p41 = "Define the value of the order comment. Special key words: #period #magic";
extern string Order_Comment = "default";

 double LotSize=2;
 int    Slippage=3;
 double StopLoss=0;
 double TakeProfit=100;
 int    GMTShift=0;

int sel;
int clo;


int           ticket;
int           OpenBuyOrders = 0;
int           OpenSellOrders = 0;
int           LastMN=0; 
int           TodayMN=0; 
int           maxloop=25; 
int           MNFactor=100000;
int           MagicNumber=123;
string        EAName="robocop";


int init()
{
  return(0);
}


int deinit()
{
  return(0);
}

int start()
{
  TodayMN = MagicNumber*MNFactor+ (Year() % 100)*10000 + Month()*100 + Day();
  if(DayOfWeek()==0 || DayOfWeek()==6) return(0);
  if(Hour()+GMTShift==22 && Minute()==00) HandleOpenPositions(2);
  
  if(Hour()+GMTShift==0 && Minute()==0 && LastMN!=TodayMN) 
  {
    OpenOrder(OP_BUY,LotSize,Ask,Slippage,StopLoss,TakeProfit,EAName,TodayMN,0,Blue);
    OpenOrder(OP_SELL,LotSize,Bid,Slippage,StopLoss,TakeProfit,EAName,TodayMN,0,Red);
    LastMN=TodayMN;
  }
  HandleOpenPositions(1);
  return(0);
}

int HandleOpenPositions(int id)
{
   int cnt, OrderMN;
   double lotsi;
   
   for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      sel=OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
      lotsi=OrderLots();
      OrderMN=OrderMagicNumber();

      if ( OrderSymbol() != Symbol()) continue;
      if ( OrderMN/MNFactor != MagicNumber)  continue;
      
      if (id==1)
      if (OrderMN<LastMN)
      {
        if (MNProfit(OrderMN)>TakeProfit)
        {
          CloseLongs(OrderMN);
          CloseShorts(OrderMN);
        } else
        if (OrderProfit()>0)
        {
        }
      }

      if (id==2)
      if (OrderMN==LastMN)
      {
        if (OrderType() == OP_BUY && OrderProfit()>0)
        {
          clo=OrderClose(OrderTicket(),lotsi,Bid,Slippage,Blue);
          CloseShorts(OrderMN, lotsi/2);
        }
      
        if (OrderType() == OP_SELL && OrderProfit()>0)
        {
          clo=OrderClose(OrderTicket(),lotsi,Ask,Slippage,Red);
          CloseLongs(OrderMN, lotsi/2);
        }
      }
   }
   return(0);
}

double MNProfit(int MagicNumber)
{
   int cnt;
   double profit=0;
   
   for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      sel=OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
      if ( OrderSymbol() != Symbol()) continue;
      if ( OrderMagicNumber() != MagicNumber)  continue;
      profit=profit+OrderProfit();
   }
   return(profit);
}

double MNDirection(int MagicNumber)
{
   int cnt;
   double lotsi=0;
   
   for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      sel=OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
      if ( OrderSymbol() != Symbol()) continue;
      if ( OrderMagicNumber() != MagicNumber)  continue;
      if ( OrderType()==OP_BUY ) lotsi=lotsi+OrderLots();
      if ( OrderType()==OP_SELL ) lotsi=lotsi-OrderLots();
   }
   return(lotsi);
}


int OpenOrder(int pType,double pLots,double pLevel,int sp, int sl, int tp,string pComment,int pMagic,datetime pExpiration,color pColor)
{
  int ticket=0;
  int err=0;
  int c = 0;
  int NumberOfTries = 10;
  switch (pType)
  {
      case OP_BUY:
         for(c = 0 ; c < NumberOfTries ; c++)
         {  
            RefreshRates();
            ticket=OrderSend(Symbol(),OP_BUY,pLots,Ask,sp,StopLong(Bid,sl),TakeLong(Bid,tp),pComment,pMagic,pExpiration,pColor);
            if (ticket > 0) break;
            err=GetLastError();
            if(err==0)
            { 
               break;
            }
            else
            {
               if(err==4 || err==137 ||err==146 || err==136) 
               {
                  Sleep(5000);
                  continue;
               }
               else 
               {
                  Print("Error Code= ", err);
                  break;
               }  
            }
         } 
         break;
      case OP_SELL:
         for(c = 0 ; c < NumberOfTries ; c++)
         {
            RefreshRates();
            ticket=OrderSend(Symbol(),OP_SELL,pLots,Bid,sp,StopShort(Ask,sl),TakeShort(Ask,tp),pComment,pMagic,pExpiration,pColor);
            if (ticket > 0) break;
            err=GetLastError();
            if(err==0)
            { 
               break;
            }
            else
            {
               if(err==4 || err==137 ||err==146 || err==136) 
               {
                  Sleep(5000);
                  continue;
               }
               else 
               {
                  Print("Error Code= ", err);
                  break;
               }  
            }
         } 
         break;
  } 
  
  return(ticket);
}  



void CloseLongs(int MagicNumber, double clots=0)
{
 int trade;
 for(trade=OrdersTotal()-1;trade>=0;trade--)
 {
  if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false) continue;
  if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber) continue;
  if(OrderType()!=OP_BUY) continue;
   
  if (clots==0) clots=OrderLots();
  clo=OrderClose(OrderTicket(),clots,Bid,Slippage,Blue);
 }
}

void CloseShorts(int MagicNumber, double clots=0)
{
 int trade;
 for(trade=OrdersTotal()-1;trade>=0;trade--)
 {
  if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false) continue;
  if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber) continue;
  if(OrderType()!=OP_SELL) continue;
  
  if (clots==0) clots=OrderLots(); 
  clo=OrderClose(OrderTicket(),clots,Ask,Slippage,Red);
 }
}

double StopLong(double price,int stop)
{
 if(stop==0)
  return(0);
 else
  return(price-(stop*Point));
}

double StopShort(double price,int stop)
{
 if(stop==0)
  return(0);
 else
  return(price+(stop*Point));
}

double TakeLong(double price,int take)
{
 if(take==0)
  return(0);
 else
  return(price+(take*Point));
}

double TakeShort(double price,int take)
{
 if(take==0)
  return(0);
 else
  return(price-(take*Point));
}


int HandleTrailingStop(int type, int ticket, double open_price, double cur_sl, int TrailingStopType, int TrailingStop)
{
	double pt, TS = 0, myAsk, myBid;


	if (type == OP_BUY)
	{
		myBid = MarketInfo(Symbol(),MODE_BID);
		switch (TrailingStopType)
		{
			case 1: 
				pt = Point * StopLoss;
				if (myBid - cur_sl > pt) 
					ModifyStopLoss(ticket, myBid - pt);
				break;
				
			case 2: 
				pt = Point * TrailingStop;
				if (myBid - open_price > pt && (cur_sl < myBid - pt || cur_sl == 0))
					ModifyStopLoss(ticket, myBid - pt);
				break;

		}
		return(0);
	}


	else if (type ==  OP_SELL)
	{
		myAsk = MarketInfo(Symbol(),MODE_ASK);
		switch (TrailingStopType)
		{
			case 1: 
				pt = Point * StopLoss;
				if (cur_sl - myAsk > pt) 
					ModifyStopLoss(ticket, myAsk+pt);
				break;
				
			case 2: 
				pt = Point * TrailingStop;
				if (open_price - myAsk > pt && (cur_sl > myAsk + pt || cur_sl == 0))
					ModifyStopLoss(ticket, myAsk+pt);
				break;
				
		 }
	 }
	 return(0);
}

int ModifyStopLoss(int ticket, double stoploss)
{
    int Cnt, digits, err;
    string symbol;
    double myStopLoss;
    
    sel=OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
    symbol = OrderSymbol();
    digits = MarketInfo(symbol,MODE_DIGITS);
    if (digits > 0)
    {
			 myStopLoss = NormalizeDouble(stoploss,digits);
    }
    Cnt=0;
    while (Cnt < 3)
    {
       if (OrderModify(ticket,OrderOpenPrice(),myStopLoss,OrderTakeProfit(),0,Aqua))
       {
         Cnt = 3;
       }
       else
       {
          err=GetLastError();
          Print(Cnt," Error modifying order : (", err , ") " + ErrorDescription(err));
         if (err>0) Cnt++;
       }
    }
}

本博客所有文章如无特别注明均为原创。作者:天泓评测
分享到:更多

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)