Author: Copyright � 2007, LEGRUPO Version 1.6
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
Simple S_R
//+------------------------------------------------------------------+
//|                                                       TRO EA.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#include <WinUser32.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>

#property copyright "Copyright © 2007, LEGRUPO Version 1.6"
#property link      "http://www.legrupo.com"

int MagicNumber = 0;
int ticket = 0;
double Price[2];
double buy_sl, sell_sl;
int    giSlippage;
int ticket_r3, ticket_r2, ticket_r1, ticket_s3, ticket_s2, ticket_s1 = 0;
int cmt_r3, cmt_r2, cmt_r1, cmt_s3, cmt_s2, cmt_s1 = 0;
int order_type = -1;

extern int ExpertID = 883377;
extern double StopLoss = 50.0;
extern double LotSize = 0.1;
extern bool useStopLoss = true;
extern int Slippage = 1;
extern color clOpenBuy = CLR_NONE;
extern color clSellBuy = CLR_NONE;
color ExitLongColor = CLR_NONE;
color ExitShortColor = CLR_NONE;
extern string BuyComment = "Simple S&R BUY";
extern string SellComment = "Simple S&R SELL";

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   double s1 = NormalizeDouble(ObjectGet("S1", OBJPROP_PRICE1), Digits);
   double s2 = NormalizeDouble(ObjectGet("S2", OBJPROP_PRICE1), Digits);
   double s3 = NormalizeDouble(ObjectGet("S3", OBJPROP_PRICE1), Digits);
   double r1 = NormalizeDouble(ObjectGet("R1", OBJPROP_PRICE1), Digits);
   double r2 = NormalizeDouble(ObjectGet("R2", OBJPROP_PRICE1), Digits);
   double r3 = NormalizeDouble(ObjectGet("R3", OBJPROP_PRICE1), Digits);
   double pivot = NormalizeDouble(ObjectGet("PIVIOT", OBJPROP_PRICE1), Digits);
   
   if (Ask > r3) {
      order_type = OP_SELLSTOP;
   } else {
      order_type = OP_SELLLIMIT;
   }
   if (OrderSelect(ticket_r3, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_r3 = OrderSend(Symbol(),order_type,LotSize,r3,Slippage,r3+StopLoss*Point,s1,SellComment,MagicNumber,0,clSellBuy);
      if (ticket_r3 < 0) {
         Alert("Open SELL order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_r3, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_r3=OrderCloseTime();
         if(cmt_r3>0) { // checa se a ordem foi fechada
           ticket_r3 = 0;
           cmt_r3 = 0;
         }
   }
   
   //r2
   if (Ask > r2) {
      order_type = OP_SELLSTOP;
   } else {
      order_type = OP_SELLLIMIT;
   }
   if (OrderSelect(ticket_r2, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_r2 = OrderSend(Symbol(),order_type,LotSize,r2,Slippage,r2+StopLoss*Point,s1,SellComment,MagicNumber,0,clSellBuy);
      if (ticket_r2 < 0) {
         Alert("Open SELL order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_r2, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_r2=OrderCloseTime();
         if(cmt_r2>0) { // checa se a ordem foi fechada
           ticket_r2 = 0;
           cmt_r2 = 0;
         }
   }
   
   //r1
   if (Ask > r1) {
      order_type = OP_SELLSTOP;
   } else {
      order_type = OP_SELLLIMIT;
   }
   if (OrderSelect(ticket_r1, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_r1 = OrderSend(Symbol(),order_type,LotSize,r1,Slippage,r1+StopLoss*Point,s1,SellComment,MagicNumber,0,clSellBuy);
      if (ticket_r1 < 0) {
         Alert("Open SELL order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_r1, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_r1=OrderCloseTime();
         if(cmt_r1>0) { // checa se a ordem foi fechada
           ticket_r1 = 0;
           cmt_r1 = 0;
         }
   }
   
   
   //s3
   if (Ask > s3) {
      order_type = OP_BUYLIMIT;
   } else {
      order_type = OP_BUYSTOP;
   }
   if (OrderSelect(ticket_s3, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_s3 = OrderSend(Symbol(),order_type,LotSize,s3,Slippage,s3-StopLoss*Point,r1,BuyComment,MagicNumber,0,clSellBuy);
      if (ticket_s3 < 0) {
         Alert("Open BUY order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_s3, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_s3=OrderCloseTime();
         if(cmt_s3>0) { // checa se a ordem foi fechada
           ticket_s3 = 0;
           cmt_s3 = 0;
         }
   }
   
   //s2
   if (Ask > s2) {
      order_type = OP_BUYLIMIT;
   } else {
      order_type = OP_BUYSTOP;
   }
   if (OrderSelect(ticket_s2, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_s2 = OrderSend(Symbol(),order_type,LotSize,s2,Slippage,s2-StopLoss*Point,r1,BuyComment,MagicNumber,0,clSellBuy);
      if (ticket_s2 < 0) {
         Alert("Open BUY order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_s2, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_s2=OrderCloseTime();
         if(cmt_s2>0) { // checa se a ordem foi fechada
           ticket_s2 = 0;
           cmt_s2 = 0;
         }
   }
   
   //s1
   if (Ask > s1) {
      order_type = OP_BUYLIMIT;
   } else {
      order_type = OP_BUYSTOP;
   }
   if (OrderSelect(ticket_s1, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_s1 = OrderSend(Symbol(),order_type,LotSize,s1,Slippage,s1-StopLoss*Point,r1,BuyComment,MagicNumber,0,clSellBuy);
      if (ticket_s1 < 0) {
         Alert("Open BUY order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_s1, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_s1=OrderCloseTime();
         if(cmt_s1>0) { // checa se a ordem foi fechada
           ticket_s1 = 0;
           cmt_s1 = 0;
         }
   }
   
   MagicNumber = MakeMagicNumber( ExpertID, false );
   
   Comment("S1: ",s1,"\nS2: ",s2,"\nS3: ",s3,"\nPivot: ",pivot,"\nR1: ",r1,"\nR2: ",r2,"\nR3: ",r3);
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Make Magic Number                                                |
//+------------------------------------------------------------------+
int MakeMagicNumber( int ExpertID, bool TimeSpecific )
{
   int SymbolCode  = 0;
   int PeriodCode  = 0;
   int MagicNumber = 0; 
   
   //---- Symbol Code
        if( Symbol() == "AUDCAD" || Symbol() == "AUDCADm" ) { SymbolCode = 1000; }
   else if( Symbol() == "AUDJPY" || Symbol() == "AUDJPYm" ) { SymbolCode = 2000; }
   else if( Symbol() == "AUDNZD" || Symbol() == "AUDNZDm" ) { SymbolCode = 3000; }
   else if( Symbol() == "AUDUSD" || Symbol() == "AUDUSDm" ) { SymbolCode = 4000; }
   else if( Symbol() == "CHFJPY" || Symbol() == "CHFJPYm" ) { SymbolCode = 5000; }
   else if( Symbol() == "EURAUD" || Symbol() == "EURAUDm" ) { SymbolCode = 6000; }
   else if( Symbol() == "EURCAD" || Symbol() == "EURCADm" ) { SymbolCode = 7000; }
   else if( Symbol() == "EURCHF" || Symbol() == "EURCHFm" ) { SymbolCode = 8000; }
   else if( Symbol() == "EURGBP" || Symbol() == "EURGBPm" ) { SymbolCode = 9000; }
   else if( Symbol() == "EURJPY" || Symbol() == "EURJPYm" ) { SymbolCode = 1000; }
   else if( Symbol() == "EURUSD" || Symbol() == "EURUSDm" ) { SymbolCode = 1100; }
   else if( Symbol() == "GBPCHF" || Symbol() == "GBPCHFm" ) { SymbolCode = 1200; }
   else if( Symbol() == "GBPJPY" || Symbol() == "GBPJPYm" ) { SymbolCode = 1300; }
   else if( Symbol() == "GBPUSD" || Symbol() == "GBPUSDm" ) { SymbolCode = 1400; }
   else if( Symbol() == "NZDJPY" || Symbol() == "NZDJPYm" ) { SymbolCode = 1500; }
   else if( Symbol() == "NZDUSD" || Symbol() == "NZDUSDm" ) { SymbolCode = 1600; }
   else if( Symbol() == "USDCAD" || Symbol() == "USDCADm" ) { SymbolCode = 1700; }
   else if( Symbol() == "USDCHF" || Symbol() == "USDCHFm" ) { SymbolCode = 1800; }
   else if( Symbol() == "USDJPY" || Symbol() == "USDJPYm" ) { SymbolCode = 1900; }
                     
   
   //---- Period Code
   if( TimeSpecific )
   {
              if( Period() == 1 )    { PeriodCode = 10; }
         else if( Period() == 5 )    { PeriodCode = 20; }
         else if( Period() == 15 )   { PeriodCode = 30; }
         else if( Period() == 30 )   { PeriodCode = 40; }
         else if( Period() == 60 )   { PeriodCode = 50; }
         else if( Period() == 240 )  { PeriodCode = 60; }
         else if( Period() == 1440 ) { PeriodCode = 70; }
         else if( Period() == 10080 ){ PeriodCode = 80; }
   }
   else
   {
      PeriodCode = 0;
   }
   //---- Calculate MagicNumber
   MagicNumber = ExpertID+SymbolCode+PeriodCode;
   return(MagicNumber);
}
//+------------------------------------------------------------------+
//| Calculate concurrent Long position                               |
//+------------------------------------------------------------------+
int CountLongs(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if( OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
   continue;
   
  if(OrderType()==OP_BUY)
   count++;
 }//for
 return(count);
}
//+------------------------------------------------------------------+
//| Calculate concurrent short position                              |
//+------------------------------------------------------------------+
int CountShorts(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
   continue;
   
  if(OrderType()==OP_SELL)
  count++;
 }//for
 return(count);
}
//+------------------------------------------------------------------+
//| Close Long Position                                              |
//+------------------------------------------------------------------+
void CloseLongs(int MagicNumber)
{
 int i = OrdersTotal();
 
 while( CountLongs(MagicNumber) != 0 && i >= 0 )
 {  
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   
   if( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber )
   {
     i--;
     continue;
   }
   else if(OrderType()==OP_BUY || OrderType()== OP_BUYLIMIT)
   {
     OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,ExitLongColor);
     i--;
   }
 }
}
//+------------------------------------------------------------------+
//| Close Short Position                                             |
//+------------------------------------------------------------------+
void CloseShorts(int MagicNumber)
{
 int i = OrdersTotal();
 
 while( CountShorts(MagicNumber) != 0 && i >= 0 )
 {  
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   
   if( OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber)
   {
     i--;
     continue;
   }
   else if(OrderType()== OP_SELL || OrderType()==OP_SELLLIMIT )
  {
   OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,ExitShortColor);
  }
 }
}

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