Author: Copyright � 2009, MetaQuotes Software Corp.
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 strategy
0 Views
0 Downloads
0 Favorites
WoodyBcp
//+------------------------------------------------------------------+
//|                                                  laguerrebot.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

datetime lastOrder = 0;
int MAGIC = 40;
int ticket;
datetime PreviousBar;      // record the candle/bar time
double lots = 0.3;


bool hasHedge = false;
bool hasHedgeWithStop = false;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

bool canTrade() {

   if ( Hour() >= 1 && Hour() < 2 ) return (false);
   
   if ( Hour() >= 9 && Hour() < 10 ) return (false);
   
   if ( Hour() >= 13 && Hour() < 14 ) return (false);
   
   return (true);

}

int getTrades() {


   int z = OrdersTotal();
   
   for(int i=0;i<z;i++) {
   
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if ( OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC ) return (OrderTicket());
   
   }
   return(-1);

}

int getHedgeTrades() {


   int z = OrdersTotal();
   
   for(int i=0;i<z;i++) {
   
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if ( OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC && OrderComment() == "hedge" ) return (OrderTicket());
      
          // Print("fodaase ". OrderComment()); // && OrderComment() == "hedge"  ) return (OrderTicket());
      //}
   
   }
   
   hasHedge = false;
   hasHedgeWithStop = false;
   
   
   return(-1);

}



void checkProfit() {

   double currentProfit = OrderProfit();
   
   int orderType = OrderType();
   
   int hedge = getHedgeTrades() ;
   
   if ( hedge == -1 && currentProfit < -100 ) {
      //vou abrir um hedge 
      
      if ( orderType == 0 ) {
         // é um buy vou abrir um sell */
         OrderSend(Symbol(),OP_SELL, lots, Bid, 3, 0, 0,"hedge", MAGIC, 0, Red);
      }
      else {
      
         OrderSend(Symbol(),OP_BUY, lots, Ask, 3, 0, 0,"hedge", MAGIC, 0, Red); 
      }
      hasHedge = true;
      
   
   }
   if ( hedge > 0 && !hasHedgeWithStop) {
   
         // ja tenho um edge e agora que faco com ele 
         bool res = false;
         if ( OrderType() == 0 ) {
            
            res = OrderModify(hedge,Ask,OrderOpenPrice(),0,0,Blue);
            
         
         }
         else {
            
           res =  OrderModify(hedge,Bid,OrderOpenPrice(),0,0,Blue);
           
         
         }
         if ( res ) {
         
            Print(" modifiquei no hedge" . hedge);
            hasHedgeWithStop = true;
         }
   
   }
   

}

bool NewBar()
{
   if(PreviousBar<Time[0])
   {
      PreviousBar = Time[0];
      return(true);
   }
   else
   {
      return(false);
   }
   return(false);    // in case if - else statement is not executed
}
  

int start()
  {

      int tik = getTrades();
      
      if ( tik != -1 ) {
      
         checkProfit();
         return(0);
      }

         if ( !canTrade()  ) {
         
            Comment("no trade time");
            return(0);
         }
         else Comment("");
         
          if(NewBar() == false ) return(0);
         
         double y1 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",0,0);
         double y2 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",1,0);
         double y3 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",2,0);
         double y4 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",3,0);
         double y5 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",5,0);
         double y6 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",5,1);
         
         double y7 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",5,2);
         
/*
         Comment("value laguerre " , y1 , " ", y2, " " , y3, " " , y4 , " " , y5);
         string valores = "";
         bool found = false;
         
         
         int safe =0;
         int unsafe = 0;
         for(int i=0;i<10;i++) {
         
             double x0 = iCustom(NULL, 0, "laguerre",0,i);
             double x1 = iCustom(NULL, 0, "laguerre",0,i-1);
             
             if ( x0 > 0.15 && x1 < 0.15 ) {
                  
                  found = true;
                  continue;         
                         
             }
             if ( found  ) {
                  
                  if ( x0 < 0.15 ) safe++;
                  else unsafe++;
                  continue;
                      
             }
             
             
             
           // valores = StringConcatenate( valores , " ,  " ,   laguerre ) ;
         
         }

        //if ( found ) Print( safe , " " , unsafe );
         
        // if ( found && iCustom(NULL, 0, "laguerre",0,0) <0.16 &&  iCustom(NULL, 0, "laguerre",0,0)> 0.15  && OrdersTotal() == 0  && (TimeCurrent() - lastOrder ) > (60*60*3 ) )  {
        */
        if ( y7 < 0 &&  y6 > 0 && y5 > 0  && y5 > y6 && y5 > 100 && OrdersTotal() == 0 && (TimeCurrent() - lastOrder ) > (60*60*3 ) ) {
               Print( "buy " , y7 , " " , y6 , " ", y5 ,  " " , y1 );
               
               

                      ticket = OrderSend(Symbol(),OP_BUY, lots, Ask, 3, 0, Ask+65*Point,"", MAGIC, 0, Red); 
                      
                      lastOrder = TimeCurrent();
              

         }
         
         else if ( y7 > 0 && y6 < 0 && y5 < 0 && OrdersTotal() == 0  && (TimeCurrent() - lastOrder ) > (60*60*3 )) {
         Print( "sell " , y7 , " " , y6 , " ", y5 , " " , y1 );
             ticket =  OrderSend(Symbol(),OP_SELL, lots, Bid, 3, 0, Bid-65*Point,"", MAGIC, 0, Red); 
               
                        if ( ticket > 0 ) {
                               lastOrder = TimeCurrent();
                            
                        }
         
         }
         

/*
         for(int i=0;i<10;i++) {
         
             double x0 = iCustom(NULL, 0, "laguerre",0,i);
             double x1 = iCustom(NULL, 0, "laguerre",0,i-1);
             
             if ( x0 < 0.15 && x1 < 0.15 ) {
                  
                  found = true;
                  continue;         
                         
             }
             if ( found  ) {
                  
                  if ( x0 < 0.15 ) safe++;
                  else unsafe++;
                  continue;
                      
             }
             
             
             
           // valores = StringConcatenate( valores , " ,  " ,   laguerre ) ;
         
         }
*/


   return(0);
  }
//+------------------------------------------------------------------+

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 ---