VolumeWatcher

Author: Copyright � 2009, MetaQuotes Software Corp.
Orders Execution
It Closes Orders by itself It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategyChecks for the total of open orders
Indicators Used
Money flow index
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
1.00 %
Total Trades 2
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -308.80
Gross Profit 5.00
Gross Loss -622.60
Total Net Profit -617.60
-100%
-50%
0%
50%
100%
GBP/CAD Oct 2024 - Jan 2025
0.00 %
Total Trades 12
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff 3.61
Gross Profit 43.35
Gross Loss 0.00
Total Net Profit 43.35
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
18.00 %
Total Trades 15
Won Trades 14
Lost trades 1
Win Rate 0.93 %
Expected payoff -21.48
Gross Profit 70.00
Gross Loss -392.20
Total Net Profit -322.20
-100%
-50%
0%
50%
100%
VolumeWatcher
//+------------------------------------------------------------------+
//|                                                VolumeWatcher.mq4 |
//|                                              @2009 Desynced Tech |
//|                                     http://desynced.no-ip.org/fx |
//+------------------------------------------------------------------+
#property copyright "@2009 Desynced Tech"
#property link      "http://desynced.no-ip.org/fx"

//+------------------------------------------------------------------+
//|                                                VolumeWatcher.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double  lots = 0.1;
datetime lastOrder =0;

int orderTik =0;
int hedgeTik = 0 ;

extern int MAGIC = 55;
int ticket;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+



int closeOrder(int ticket,int orderType, double lts) {

if ( orderType == OP_BUY ) {

   OrderClose(ticket,lts,Bid,3,Green);
   return(0);
}
else {
   OrderClose(ticket, lts, Ask, 3, Green);
    return(0);

}
}


void openHedge(int currentTicket, int currentOrderType,double hLots ) {

   if ( currentOrderType == 0  ) {
         // é um buy vou abrir um sell */
         OrderSend(Symbol(),OP_SELL, hLots, Bid, 3,0 /*Ask+200*Point*/, 0,"hedge", MAGIC, 0, Red);
         
         OrderModify(currentTicket,Ask,0,0,0,Blue);
         /*
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
                                                                                                                                              insertOrderDb(ticket,Symbol(),Bid,0, 0,MAGIC,"woodyEA-HEDGER",OP_SELL, TimeCurrent());
                                                                                                                                             mysql_close(mysql);
                        
         */
      //   hasHedgeWithStop = true;
             
      }
      else if ( currentOrderType == 1  ) {
      
        OrderSend(Symbol(),OP_BUY, hLots, Ask, 3, 0/*Bid-200*Point*/, 0,"hedge", MAGIC, 0, Red); 
        OrderModify(currentTicket,Bid,0,0,0,Blue);
      //   hasHedgeWithStop = true;
      }



}


void checkProfit(double currentProfit , int currentTicket , int currentOrderType , double currentLots , double hedgeProfit , int hedgeTicket , int  hedgeType , double hedgeLots,  int hedgeLoss, bool hasHedgeWithStop ) {

/*
   double currentProfit = OrderProfit();
   int orderTicket = OrderTicket();
   int orderType = OrderType();
  */ 
  // double hLots = OrderLots();
   double hLots = currentLots*2;
   
  
   
   if ( hedgeTicket > 0 ) {
   
      if ( currentProfit > 0 && hedgeProfit <0  ) {
         // esta na altura de fechar a original e abrir um hedge do hedge  
         Print (" deu para o torto  fechei" ,currentProfit , " " , hedgeProfit);
         
         closeOrder(currentTicket,currentOrderType,currentLots);
         
         openHedge(hedgeTicket, hedgeType , hedgeLots *2);
         
         return(0);
         
         
         
         
      
      }
   
   }
   if ( hedgeTicket == -1 && currentProfit > hedgeLoss && currentProfit < hedgeLoss -100  ) {
      
     // int decision = stochasticAlert();
      
      if ( currentOrderType == 0  ) {
         // é um buy vou abrir um sell */
         OrderSend(Symbol(),OP_SELL, hLots, Bid, 3,0 /*Ask+200*Point*/, 0,"hedge", MAGIC, 0, Red);
         
         OrderModify(currentTicket,Ask,0,0,0,Blue);
         /*
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
                                                                                                                                              insertOrderDb(ticket,Symbol(),Bid,0, 0,MAGIC,"woodyEA-HEDGER",OP_SELL, TimeCurrent());
                                                                                                                                             mysql_close(mysql);
                        
         */
         hasHedgeWithStop = true;
             
      }
      else if ( currentOrderType == 1  ) {
      
        OrderSend(Symbol(),OP_BUY, hLots, Ask, 3, 0/*Bid-200*Point*/, 0,"hedge", MAGIC, 0, Red); 
        OrderModify(currentTicket,Bid,0,0,0,Blue);
         hasHedgeWithStop = true;
      }
     
      return(0);
   
   }
   else if ( hedgeTicket > 0 && !hasHedgeWithStop) {
   
         // ja tenho um edge e agora que faco com ele 
         bool res = false;
         double point = 0 ;
         if ( hedgeType == OP_BUY ) {
            
            res = OrderModify(hedgeTicket,Ask,OrderOpenPrice()+100*Point,0,0,Blue);
            
            point = MathAbs((Ask-OrderOpenPrice()));
         
         }
         else {
            
           res =  OrderModify(hedgeTicket,Bid,OrderOpenPrice()-100*Point,0,0,Blue);
           
             point = MathAbs((Bid-OrderOpenPrice()));
         }
         if ( res ) {
         
           // Print(" modifiquei no hedge" , hedge);
            
         }
         else {
              // edgeTries++;
              // Print(" modifiquei no hedge" , edgeTries);
         }
   
   }
   else if  ( hedgeTicket > 0 && hasHedgeWithStop ) {
      
      if ( currentProfit + hedgeProfit > 0 ) {
         
               if ( currentProfit + OrderProfit() > 0 )  {
                  closeOrder(hedgeTicket,hedgeType,hedgeLots);
                  closeOrder(currentTicket,currentOrderType,currentLots);
                  
               }
               Print("tenho aqui um ponto de lucro " , currentProfit + hedgeProfit);
         }
   
   
   }
   else if ( hedgeTicket < 0 ) {
   
      //   getNumberTrades();
         
        // if ( OrderTakeProfit() == 0 ) {
         
            OrderSelect(orderTik,SELECT_BY_TICKET,MODE_TRADES);
            if ( OrderType() == OP_BUY ) {
            
             //     Print("aqui");
                if ( OrderTakeProfit() == 0 )   OrderModify(OrderTicket(),Ask,0,OrderOpenPrice()+50*Point,0,Blue);
                  
            }
            else  {
            //Print("aqui");
            if ( OrderTakeProfit() == 0 ) OrderModify(OrderTicket(),Bid,0,Bid-50*Point,0,Blue);
            }
         
         
   //      }
   
   }
   

}



void  getTrades() {


    int total = OrdersTotal();
   int numords = 0;
   
   int tickets[10];
   int ticketsFound = 0; 
     
   for(int cnt=0; cnt<total; cnt++) 
   {        
   OrderSelect(cnt, SELECT_BY_POS);            
   if(OrderSymbol() == Symbol() &&  OrderMagicNumber() == MAGIC) 
   
      tickets[ticketsFound] = OrderTicket();
      ticketsFound++;
   }
   
   if ( ticketsFound == 2 ) {
   
      if ( tickets[0] < tickets[1] ) {
      
         orderTik = tickets[0];
         hedgeTik = tickets[1];
         
        // Print("orignal é " , tickets[0] , " hedge " , tickets[1] ) ;
      
      }
      else {
         //Print("orignal é " , tickets[1] , " hedge " , tickets[0] ) ;
         
         orderTik = tickets[1];
         hedgeTik = tickets[0];
      
      }
   
   
   }
   else if ( ticketsFound == 1  ) {
   
         orderTik = tickets[0];
          
         hedgeTik = -1;
   }
   else if ( ticketsFound < 1 ) {
   
      orderTik = -1;
      hedgeTik = -1;
   }
   else if ( ticketsFound > 2 ) {
   
      Print("FODDASE NAO PODE HAVER MAIS QUE 2 ");
   }
}






int start()
  {
 //double y0 = iCustom(NULL, 0, "Indraws Volume Analysis 3",0,0);
 //double y1 = iCustom(NULL, 0, "Indraws Volume Analysis 3",0,0);
 
orderTik = -1;
      hedgeTik = -1;
      getTrades();
      
      
      if ( orderTik != -1 )  {
      
         double currentProfit;     
         int currentOrderType;
         double currentLots;
         
         
         double hedgeProfit = 0;
         
         int  hedgeType = -1;
         double hedgeLots= 0 ;
         int hedgeLoss = -150;
         bool hasHedgeWithStop= false;
         
         
         //void checkProfit(double currentProfit , int currentTicket , int currentOrderType , double currentLots , double hedgeProfit , int hedgeTicket , int  hedgeType , double hedgeLots,  int hedgeLoss, bool hasHedgeWithStop ) {
           
            OrderSelect(orderTik,SELECT_BY_TICKET,MODE_TRADES);
            
            currentProfit = OrderProfit();
            currentOrderType = OrderType();
            currentLots = OrderLots();
            
            if ( hedgeTik != -1 ) {
               
                  OrderSelect(hedgeTik,SELECT_BY_TICKET,MODE_TRADES);
                  hedgeProfit = OrderProfit();
                  hedgeType = OrderType();
                  hedgeLots = OrderLots();
                  hasHedgeWithStop = OrderStopLoss() != 0;
                  
            }
            checkProfit(currentProfit,orderTik,currentOrderType,currentLots,hedgeProfit,hedgeTik,hedgeType,hedgeLots,hedgeLoss , hasHedgeWithStop );

         
         
        // Print(" tenho ordem Activa ") ;
         
         return(0);
      }

 
 double y0 = iMFI(NULL,0,14,0);
 double y1 =iMFI(NULL,0,14,1);
 
 Comment(Ask - Bid );

 
 int decision = 0;
 if ( y1 > 80 && y0 < 80 ) decision = -1;
 
 if ( y1 < 20 && y0 > 20 ) decision = 1;
 
 
 if ( decision == 1 ) {
              
               
                if ( (TimeCurrent() - lastOrder ) > (60*60*3 ) ) {
                     Print("buy " , y0 , " " , y1);
                      ticket = OrderSend(Symbol(),OP_BUY, lots, Ask, 3, 0, Ask+50*Point,"", MAGIC, 0, Red); 
                      
                      /*
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
                                                                                                                                              insertOrderDb(ticket,Symbol(),,0, Ask+60*Point,MAGIC,"woodyEA",OP_BUY, TimeCurrent());
                                                                                                                                             mysql_close(mysql);
                        
                      */
                      lastOrder = TimeCurrent();
               }

         }
         
         else if ( decision == -1 ) {
             
             if ( (TimeCurrent() - lastOrder ) > (60*60*3 ) ) {
             ticket =  OrderSend(Symbol(),OP_SELL, lots, Bid, 3, 0, Bid-50*Point,"", MAGIC, 0, Red); 
                           Print("sell " , y0 , " " , y1);
                        if ( ticket > 0 ) {
                        
                         /*
                        
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
                                                                                                                                              insertOrderDb(ticket,Symbol(),Bid,0, Bid-60*Point,MAGIC,"woodyEA",OP_SELL, TimeCurrent());
                                                                                                                                             mysql_close(mysql);
                      */
                               lastOrder = TimeCurrent();
                            
                        }
            }
         }


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