Author: Copyright 2010, Alf.
Profit factor:
0.00

This automated trading script is designed to execute trades by identifying specific momentum shifts and price trends in the financial markets. It combines three primary technical indicators—two Moving Averages and a Relative Strength Index (RSI)—to make high-level decisions on when to enter the market.

Core Trading Logic

The script operates by monitoring three distinct layers of data simultaneously:

1. Trend Identification (Moving Averages) The system uses two Exponential Moving Averages (EMA) with different timeframes (a "fast" one and a "slow" one). These act as a filter to determine the direction of the price flow:

  • When the fast average moves from below to above the slow average, it signals an upward momentum shift.
  • When the fast average moves from above to below the slow average, it signals a downward momentum shift.

2. Strength Verification (RSI) To prevent entering trades during "noisy" or weak market movements, the script uses the Relative Strength Index (RSI). This acts as a confirmation tool:

  • It looks for specific conditions where the price is either oversold (low strength) or overbought (high strength) to ensure it isn't buying at the very top or selling at the very bottom of a move.

3. Execution Strategy The bot will only initiate a trade when both the trend and the strength conditions align perfectly:

  • Buy Orders: Triggered when the fast moving average crosses upward over the slow one, provided that the RSI indicates the market is not currently overbought.
  • Sell Orders: Triggered when the fast moving average crosses downward under the slow one, provided that the RSI confirms the market is not currently oversold.

Trade Management and Risk Control

The script includes several automated features to manage risk and position sizing:

  • Dynamic Lot Sizing: It utilizes a "Martingale" logic system. This means it can automatically adjust the size of subsequent trades based on the outcome of previous ones, attempting to recover losses by scaling the volume of new positions.
  • Automated Protection: Every trade is strictly governed by predefined Take Profit (TP) and Stop Loss (SL) levels. These are calculated automatically based on the price at the moment of entry to ensure that no single trade can exceed a specific risk threshold.
  • Position Awareness: The script is designed to be "aware" of its current status; it checks if a position is already open before looking for new opportunities, ensuring it doesn't over-leverage the account by opening multiple redundant trades at once.

Summary of Operation

In simple terms, the script acts as an automated observer that waits for a specific "crossover" in price trends. Once it sees a trend change, it checks the market strength to see if it is a high-probability move. If both conditions are met, it enters a trade with a pre-calculated risk limit and uses a systematic approach to manage the volume of those trades over time.

Price Data Components
Series array that contains open prices of each bar
Orders Execution
It automatically opens orders when conditions are reached
Indicators Used
Moving average indicatorRelative strength index
15 Views
0 Downloads
0 Favorites
2ma_rsi_v1
//+------------------------------------------------------------------+
//|                                                      2MA_RSI.mq5 |
//|                                             Copyright 2010, Alf. |
//|                                      http://forum.liteforex.org/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, Alf."
#property link      "http://forum.liteforex.org/"
#property version   "1.00"

#include <OnTesterFunctions.mqh>
#include <Martingail.mqh>
//--- input parameters
input double   DML=1000;
input int      Ud=1;
input int      Stop=500;
input int      Tp=1500;
input int      Slipage=50;
input int      PeriodFast=5;
input int      PeriodSlow=20;
input int      ShiftFast=0;
input int      ShiftSlow=0;
input int      RSI=14;
input double   Perekup=70;
input double   Pereprod=30;

int m1=0;
int m2=0;
int r=0;

Martingail lt;
//+------------------------------------------------------------------+
//| Open                                                             |
//+------------------------------------------------------------------+
void Open()
  {
   double ma1[2];
   double ma2[2];
   double rsi[1];
   if(CopyBuffer(m1,0,1,2,ma1)<0) return;
   if(CopyBuffer(m2,0,1,2,ma2)<0) return;
   if(CopyBuffer(r,0,1,1,rsi)<0)  return;
   MqlTick last_tick;
   SymbolInfoTick(_Symbol,last_tick);
   MqlTradeResult result;
   MqlTradeRequest request;
   ZeroMemory(request);

   request.symbol=_Symbol;
   request.magic=777;
   request.deviation=50;
   request.action=TRADE_ACTION_DEAL;
   request.type_filling=ORDER_FILLING_FOK;
   Comment("Ëîò=",lt.Lot());
   if(ma1[1]<ma2[1] && ma1[0]>ma2[0] && rsi[0]<Pereprod)
     {
      request.volume=lt.Lot();
      request.price=last_tick.ask;
      request.type=ORDER_TYPE_BUY;
      request.sl=last_tick.bid-Stop*Point();
      request.tp=last_tick.ask+Tp*Point();
      OrderSend(request,result);
     }
   if(ma1[1]>ma2[1] && ma1[0]<ma2[0] && rsi[0]>Perekup)
     {
      request.volume=lt.Lot();
      request.price=last_tick.bid;
      request.type=ORDER_TYPE_SELL;
      request.sl=last_tick.ask+Stop*Point();
      request.tp=last_tick.bid-Tp*Point();
      OrderSend(request,result);
     }
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   m1=iMA(_Symbol,0,PeriodFast,ShiftFast,MODE_EMA,PRICE_MEDIAN);
   m2=iMA(_Symbol,0,PeriodSlow,ShiftSlow,MODE_EMA,PRICE_MEDIAN);
   r=iRSI(_Symbol,0,RSI,PRICE_MEDIAN);
   lt.GVarName="MG_1";
   lt.Shape=DML;
   lt.DoublingCount=Ud;
   lt.GVarGet();
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   lt.GVarSet();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlRates rt[1];
   if(CopyRates(Symbol(),_Period,0,1,rt)<0) return;
   if(rt[0].tick_volume>1) return;
   if(!PositionSelect(_Symbol))
     {
      Print("OK");
      Open();
     }
  }
//+------------------------------------------------------------------+
double OnTester()
  {
   double p=profitc_divide_lossc();
   double s=max_series_loss();
   if(Tp<Stop) return 0;
   s=s+1;
   return p/s;
  }
//+------------------------------------------------------------------+

Comments