Author: Copyright � 2012, Yury V. Reshetov
Profit factor:
0.00

This script is an automated trading system designed for the MetaTrader 4 platform. It utilizes a mathematical approach to analyze market trends and execute trades based on weighted probabilities. Below is a breakdown of how the logic operates in plain language:

Core Trading Logic

The script functions as an automated "Expert Advisor." Its primary goal is to monitor the market, calculate a probability score for price movement, and automatically open buy or sell positions when specific conditions are met.

Key operational rules include:

  • One Position at a Time: The system checks if there are already any active trades open for the specific currency pair. If a position is already active, it will not open a new one.
  • New Bar Execution: To prevent the script from making repetitive decisions every millisecond, it is programmed to perform its primary analysis only once at the start of a new price candle (bar).
  • Safety Features: It includes built-in "Stop Loss" and "Take Profit" levels. These ensure that if a trade moves against your prediction, the system will close the position automatically to limit financial risk.
  • Notifications: Whenever the script successfully opens a trade, it sends an email notification with all the specific details (price, lot size, and exit targets).

Probability Calculation

The "brain" of this script lies in how it decides whether to buy or sell. Instead of looking at just one indicator, it uses a weighted probability model:

  1. Data Collection: The script looks at three different data points from the RSI (Relative Strength Index) indicator. It specifically examines these values across different historical shifts to get a broader view of momentum.
  2. Weighting System: You can provide eight different "weight" values (x0 through x7). These weights determine how much importance the script gives to various combinations of the market data.
  3. The Score: The script processes these inputs through a mathematical formula to produce a probability score between 0 and 1.
    • If the resulting score favors a Short (Sell) position, the system enters a Sell trade.
    • If the score favors a Long (Buy) position, the system enters a Buy trade.

Execution Flow

When the script runs, it follows this sequence:

  1. Verify: It confirms that trading is allowed and that no current positions are open.
  2. Analyze: It gathers the necessary indicator data and feeds it into the probability formula.
  3. Decide: Based on whether the final result is positive or negative, it chooses a direction.
  4. Act: It sends the order to the broker to open the trade with the predefined lot size and protection levels.
  5. Retry Logic: If an error occurs during the trade attempt (such as a connection hiccup), the script includes a "sleep" command to wait 30 seconds before trying again.
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reached
Indicators Used
Relative strength index
Miscellaneous
It sends emails
6 Views
0 Downloads
0 Favorites
RNN_MT4
//+------------------------------------------------------------------+
//|                                                      RNN_MT4.mq4 |
//|                               Copyright © 2012, Yury V. Reshetov |
//|                              http://www.mql5.com/ru/articles/366 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, Yury V. Reshetov"
#property link      "http://www.mql5.com/ru/articles/366"


//---- input parameters
extern int          x0 = 50; // Íàñòðîéêà îò 0 äî 100 ñ øàãîì 1
extern int          x1 = 50; // Íàñòðîéêà îò 0 äî 100 ñ øàãîì 1
extern int          x2 = 50; // Íàñòðîéêà îò 0 äî 100 ñ øàãîì 1
extern int          x3 = 50; // Íàñòðîéêà îò 0 äî 100 ñ øàãîì 1
extern int          x4 = 50; // Íàñòðîéêà îò 0 äî 100 ñ øàãîì 1
extern int          x5 = 50; // Íàñòðîéêà îò 0 äî 100 ñ øàãîì 1 
extern int          x6 = 50; // Íàñòðîéêà îò 0 äî 100 ñ øàãîì 1 
extern int          x7 = 50; // Íàñòðîéêà îò 0 äî 100 ñ øàãîì 1
extern double       sl = 500.0; // Óðîâåðü ñòîïëîññà è òåéêïðîôèòà â ïóíêòàõ
extern double       lots = 1.0;
extern int          mn = 888; // Ìàãè÷åñêèé íîìåð
static int          prevtime = 0; // Âðåìÿ îòêðûòèÿ ïîñëåäíåãî áàðà


int init() {
   prevtime = Time[0];
   return(0);
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
   // Ïðîâåðêà íà âíîâü ñôîðìèðîâàâøèéñÿ áàð
   if (Time[0] == prevtime) {
      return(0);
   }
   prevtime = Time[0];
   
   // Ïðîâåðêà íà ðàçðåøåíèå òîðãîâëè
   if(! IsTradeAllowed()) {
      Print("Trades not allowed");
      again();
      return(0);
   }


   // Ïðîâåðêà îòêðûòûõ ïîçèöèé
   
   // Êîëè÷åñòâî îòêðûòûõ ïîç  
   int total = OrdersTotal(); 
   // Ïåðåáèðàåì îòêðûòûå ïîçèöèè
   for(int i = total - 1; i >= 0; i--) { // Âûäåëÿåì îòêðûòóþ ïîçèöèþ
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES); 
      // Ïðîâåðÿåì íà ñîîòâåòñòâèå ñèìâîëó èíñòðóìåíòà è ìàãè÷åñêîìó íîìåðó
      if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == mn)) {
         // Åñòü îòêðûòàÿ ïîçèöèÿ, âûõîäèì
         return(0); 
      }
   }

   // Âû÷èñëÿåì òîðãîâûé ñèãíàë
   double sp = tradesSingal();

   double stoploss = 0.0;
   double takeprofit = 0.0;
   
   // Çàïðàøèâàåì ñâåæóþ ðûíî÷íóþ èíôîðìàöèþ
   RefreshRates();
   
   string s = "";
   int ticket = -1;
   
   if (sp < 0.0) {
      stoploss = Bid - sl * Point;
      takeprofit = Bid + sl * Point;
      ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 1, stoploss, takeprofit, WindowExpertName(), mn, 0, Blue); 
      if (ticket < 0) {
         Print("OrderSend failed with error #", GetLastError());
         again();      
      } else {
         s = "Open " + Symbol() +  " #" + DoubleToStr(ticket, 0) + " Buy , Price:" + Ask + ", " + DoubleToStr(lots, 2) + " lots, SL: " + DoubleToStr(stoploss, 5) + ", TP: " + DoubleToStr(takeprofit, 5);
         SendMail("Open Buy position of " + Symbol(), s);
      }
   } else {
      stoploss = Ask + sl * Point;
      takeprofit = Ask - sl * Point;
      ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 1, stoploss, takeprofit, WindowExpertName(), mn, 0, Red); 
      if (ticket < 0) {
         Print("OrderSend failed with error #", GetLastError());
         again();
      } else {
         s = "Open " + Symbol() +  " #" + DoubleToStr(ticket, 0) + " Sell , Price:" + Bid + ", " + DoubleToStr(lots, 2) + " lots, SL: " + DoubleToStr(stoploss, 5) + ", TP: " + DoubleToStr(takeprofit, 5);
         SendMail("Open Sell position of " + Symbol(), s);
      }
   }
//-- Exit --
   return(0);
}


// Ïðåîáðàçóåò âåðîÿòíîñòü â òîðãîâûé ñèãíàë

double tradesSingal() {
   // Ñ÷èòûâàåì ïîêàçàíèÿ èíäèêàòîðîâ
   double a1 = input(9, 0);
   double a2 = input(9, 1);
   double a3 = input(9, 2);

   // Âû÷èñëÿåì âåðîÿòíîñòü òîðãîâîãî ñèãíàëà äëÿ êîðîòêîé ïîçèöèè
   double result = getProbability(a1, a2, a3);
   
   string s = Symbol() + ", Probability for Short (Sell) position: " + DoubleToStr(result, 4);

   if (result > 0.5) {
      Print(s);
   } else {
      double r = 1.0 - result;
      Print(s);
      s = Symbol() + ", Probability for Long (Buy) position: " + DoubleToStr(r, 4);
   }
   
   SendMail(s, s);
   // Ëèíåéíûé ñèãìîèä, ïåðåâîäèò çíà÷åíèÿ îò 0 äî 1 â çíà÷åíèÿ îò -1 äî +1
   result = result * 2.0 - 1.0;
   Print("Result = ", result);
   return (result);
}



// Ïîâòîðíûé âûçîâ ôóíêöèè start() íà ïåðâîì òèêå ïîñëå 30 ñåêóíäíîé çàäåðæêè
void again() {
   prevtime = Time[1];
   Sleep(30000);
}

// Âû÷èñëåíèÿ âåðîÿòíîñòè òîðãîâîãî ñèãíàëà äëÿ êîðîòêîé ïîçèöèè
// p1, p2, p3 - ñèãíàëû èíäèêàòîðîâ èëè îñöèëëÿòîðîâ ÒÀ â äèàïàçîíå îò 0 äî 1
double getProbability(double p1, double p2, double p3) {

   double y0 = x0;
   double y1 = x1;
   double y2 = x2;
   double y3 = x3;
   double y4 = x4;
   double y5 = x5;
   double y6 = x6;
   double y7 = x7;

   double pn1 = 1.0 - p1;
   double pn2 = 1.0 - p2;
   double pn3 = 1.0 - p3;

   // Âû÷èñëåíèå âåðîÿòíîñòè â ïðîöåíòàõ
   double probability = 
   pn1 * (pn2 * (pn3 * y0 +
   p3 * y1) +
   p2 * (pn3 * y2 +
   p3 * y3)) +
   p1 * (pn2 * (pn3 * y4 +
   p3 * y5) +
   p2 * (pn3 * y6 +
   p3 * y7)); 

   // Ïåðåâîäèì ïðîöåíòû â âåðîÿòíîñòè
   probability = probability / 100.0;


   return (probability);

}

// Ïîêàçàíèÿ èíäèêàòîðîâ, äîëæíû áûòü â äèàïàçîíå îò 0 äî 1
// p - ïåðèîä èíäèêàòîðà
// shift - ñìåùåíèå âãëóáü èñòîðèè â ïåðèîäàõ èíäèêàòîðà
double input(int p, int shift) { 
  return (iRSI(Symbol(), 0, p, PRICE_OPEN, p * shift) / 100.0);
}


Comments