Author: Copyright � 2007, LEGRUPO Version 1.6
Profit factor:
0.00

This software is an automated trading expert designed to manage various positions based on predefined price levels. Instead of making subjective decisions, it follows a systematic approach to identifying and executing trades at specific support and resistance points.

Core Functionality

The script operates by interacting with specific price markers (labeled as S1, S2, S3 for Support and R1, R2, R3 for Resistance) that are placed on the trading chart. Its primary role is to monitor these levels and place orders automatically when the market approaches them.

Key Logic Components

1. Dynamic Order Placement The script constantly evaluates where the current market price sits in relation to your defined support and resistance levels. Depending on this relationship, it intelligently decides how to enter a trade:

  • Limit Orders: If the market is currently above a target price, it places a "limit" order (waiting for the price to drop down to the level).
  • Stop Orders: If the market is currently below a target price, it places a "stop" order (waiting for the price to rise up to the level). This ensures that the script can handle various market conditions and only executes trades when the specific price criteria are met.

2. Automated Risk Management Every trade opened by the script includes built-in safety features:

  • Stop Loss: Every order is automatically assigned a stop-loss value to protect your capital from significant losses.
  • Take Profit: The system uses opposing support/resistance levels as profit targets (for example, a Buy order might use a Resistance level as its exit point).

3. Unique Identification System To keep different trades organized and prevent the script from interfering with other manual trades or other automated tools, it uses a "Magic Number" system. It generates a unique identifier based on:

  • The specific currency pair being traded (e.g., EURUSD, GBPJPY).
  • The timeframe of the chart. This allows the software to distinguish its own positions from everything else in your trading account.

4. Position Monitoring and Management The script includes dedicated functions to scan your active trades. It can identify specifically which positions belong to it (based on the Magic Number) and provides the framework to close all "Buy" or "Sell" positions simultaneously if needed.

5. Visual Information To keep the trader informed, the script displays a real-time data summary on the chart. This includes the current numerical values of all active Support (S1-S3), Resistance (R1-R3), and Pivot points, allowing you to see exactly what price levels the automated system is currently watching.

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
12 Views
0 Downloads
0 Favorites
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);
  }
 }
}

Profitability Reports

PERFORMANCE BY SYMBOL

Expected payoff

Average expected payoff across the available backtests for each symbol.

Positive Negative
1 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%

Comments