TSD_MT4_MR_Trade_0_101

Download script
Author: Copyright � 2005 Bob O\'Brien / Barcode
Profit factor:
1.10

This automated trading script is designed to manage currency positions by identifying market trends and executing trades based on specific technical conditions. Below is a detailed breakdown of its logic in plain language:

Market Analysis Logic

The script uses a two-step verification process before deciding whether to enter a trade. It looks at two different types of technical indicators:

  1. Trend Direction: The system first determines the "momentum" of the market using either the MACD (Moving Average Convergence Divergence) or the OsMA indicator. By comparing current values against previous values on a weekly timeframe, it decides if the overall trend is moving upward (Bullish) or downward (Bearish).
  2. Entry Filter: Once a trend is established, the script applies a secondary filter to find an optimal entry point. It primarily uses the Williams Percent Range (WPR) indicator. This acts as a "gatekeeper" to ensure that the price isn't overextended before opening a position.

Trade Execution Strategy

The script does not enter the market instantly; instead, it uses Pending Orders (Buy Stops and Sell Stops).

  • Buy Orders: If the trend is upward and the filter gives the "green light," the script places a Buy Stop order. This means the trade will only trigger if the price breaks above a specific high point reached in the previous day's trading session.
  • Sell Orders: If the trend is downward and the filter allows it, the script places a Sell Stop order. This triggers if the price drops below a specific low point from the previous day's session.

Dynamic Trade Management

Once a trade is active or a pending order is waiting, the script actively manages the risk through several automated behaviors:

  • Price Protection: It automatically calculates "Stop Loss" (to limit losses) and "Take Profit" (to secure gains) levels based on recent price extremes. If the market moves significantly against a pending order, the script can delete the order to protect your balance.
  • Dynamic Adjustments: If market conditions change (such as a new daily high or low being formed), the script can modify existing pending orders to ensure they remain relevant to current price action.
  • Trailing Stop: For trades that are already open and moving in a profitable direction, the script employs a "Trailing Stop." This means it follows the price upward (for buys) or downward (for sells), locking in profits as the market moves in your favor while giving the trade room to breathe.

Risk and Money Management

The script includes a built-in calculation engine to determine how much to trade based on your account status:

  • Lot Sizing: It can automatically calculate the number of lots to trade based on your available margin, leverage, and desired risk levels.
  • Safety Limits: It enforces a maximum lot size and checks for minimum margin requirements before opening any position to prevent over-leveraging the account.
  • Spacing: To prevent "over-trading," the script includes a time buffer, ensuring it doesn't execute multiple trades in rapid succession on the same symbol.
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Orders Execution
Checks for the total of open orders
Indicators Used
Larry William percent range indicator
11 Views
0 Downloads
0 Favorites
TSD_MT4_MR_Trade_0_101
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                   TSD_MR_Trade_MACD_WPR_0_10.mq4 |
//|                           Copyright ® 2005 Bob O'Brien / Barcode |
//|             TSD v1.2 rewritten to MQL4 and enhanced by Mindaugas |
//|                   magic number and backtesting mod by Nick Bilak |
//|                                           TSD Trade version 0.10 |
//+------------------------------------------------------------------+
#property copyright "Copyright ® 2005 Bob O\'Brien / Barcode"

#include <stdlib.mqh>

#define MAGICNUM  20050720

#define  DIRECTION_MACD 1
#define  DIRECTION_OSMA 2

#define  FILTER_WPR     1
#define  FILTER_FORCE   2

// which indicators to use
int DirectionMode = DIRECTION_MACD, FilterMode = FILTER_WPR;
// trading periods
int PeriodDirection = PERIOD_W1, PeriodTrade = PERIOD_D1, PeriodTrailing = PERIOD_H4, CandlesTrailing = 3;
// currency pairs to trade

// parameters for iWPR and iForce indicators
int WilliamsP = 24, WilliamsL = -75, WilliamsH = -25;
int ForceP = 2;

int TakeProfit = 100, TrailingStop = 60;
int Slippage = 5, LotsMax = 50;
double Lots = 0.1;
int MM = 0, Leverage = 1, MarginChoke = 200;

string TradeSymbol;
int Pair = 0;
datetime LastTrade = 0;
double Spread, SPoint;

//+------------------------------------------------------------------+

int init()   { return(0); }
int deinit() { return(0); }

//+------------------------------------------------------------------+

int start() {
   int TradesThisSymbol, Direction;
   int i, ticket;
   
   bool okSell, okBuy;
   
   double PriceOpen, Buy_Sl, Buy_Tp, LotMM, WilliamsValue;
   string ValueComment;
   
   if ( (LastTrade + 15) > CurTime() )  return(0);
   
   TradeSymbol = Symbol();
   
   Spread = MarketInfo (TradeSymbol, MODE_SPREAD)*Point;
   SPoint = Point;
   
   TradesThisSymbol = TotalTradesThisSymbol (TradeSymbol);
   
   Direction = Direction (TradeSymbol, PeriodDirection, DirectionMode);
   ValueComment = Filter(TradeSymbol, PeriodTrade, FilterMode, okBuy, okSell);
   
   //Comment ("\nSymbol: ", TradeSymbol, "\nMACD Direction: ", Direction, "\n", ValueComment);

   /////////////////////////////////////////////////
   //  Place new order
   /////////////////////////////////////////////////
   if ( TradesThisSymbol < 1 ) {

      LotMM = CalcMM(MM);
      if ( LotMM < 0 )  return(0);

      ticket = 0;

      if ( Direction == 1 && okBuy ) {
         MarkTrade();
	      //Print ("TSD BuyStop: ", TradeSymbol, " ", LotMM, " ", CalcOpenBuy(), " ", CalcSlBuy(), " ", CalcTpBuy());
         ticket = OrderSend (TradeSymbol, OP_BUYSTOP, LotMM, CalcOpenBuy(), Slippage, CalcSlBuy(), CalcTpBuy(),
		                       "TSD BuyStop", MAGICNUM, 0, Blue);
		}
		   
      if ( Direction == -1 && okSell ) {
         MarkTrade();
	      //Print ("TSD SellStop: ", TradeSymbol, " ", LotMM, " ", CalcOpenSell(), " ", CalcSlSell(), " ", CalcTpSell());
	      ticket = OrderSend (TradeSymbol, OP_SELLSTOP, LotMM, CalcOpenSell(), Slippage, CalcSlSell(), CalcTpSell(),
	                          "TSD SellStop", MAGICNUM, 0, Red);
	   }
 
	   if ( ticket == -1 )  ReportError ();
	   if ( ticket != 0 )   return(0);
	} // End of TradesThisSymbol < 1
		
   /////////////////////////////////////////////////
   //  Pending Order Management
   /////////////////////////////////////////////////
   for (i = 0; i < OrdersTotal(); i++) {
      if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
      if ( OrderSymbol() != TradeSymbol || OrderMagicNumber() != MAGICNUM)  continue;

      if ( OrderType () == OP_BUYSTOP ) {
         if ( Direction != 1 ) {
            MarkTrade();
            OrderDelete ( OrderTicket() );
            return(0);
         }
         if ( iHigh(TradeSymbol, PeriodTrade, 1) < iHigh(TradeSymbol, PeriodTrade, 2) &&
              ( !CompareDoubles (CalcSlBuy(), OrderStopLoss()) ||
                !CompareDoubles (CalcTpBuy(), OrderTakeProfit()) ) ) {
            MarkTrade();
     		   OrderModify (OrderTicket(), CalcOpenBuy(), CalcSlBuy(), CalcTpBuy(), 0, White);
            return(0);
         }
      }

      if ( OrderType () == OP_SELLSTOP ) {
         if ( Direction != -1 ) {
            MarkTrade();
            OrderDelete ( OrderTicket() );
            return(0);
         }
         if ( iLow(TradeSymbol, PeriodTrade, 1) > iLow(TradeSymbol, PeriodTrade, 2) &&
              ( !CompareDoubles (CalcSlSell(), OrderStopLoss()) ||
                !CompareDoubles (CalcTpSell(), OrderTakeProfit()) ) ) {
            MarkTrade();
     		   OrderModify (OrderTicket(), CalcOpenSell(), CalcSlSell(), CalcTpSell(), 0, Gold);
            return(0);
         }
      }
   } // End of Pending Order Management
   
   /////////////////////////////////////////////////
   //  Stop Loss Management
   /////////////////////////////////////////////////
   if ( TrailingStop > 0 ) {
      for (i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != TradeSymbol || OrderMagicNumber() != MAGICNUM )  continue;
         if ( TrailStop (i, TrailingStop) )  return(0);
      }
   }

   return(0);
}
//+------------------------------------------------------------------+
double CalcOpenBuy  () { return (dMax (iHigh(TradeSymbol, PeriodTrade, 1) + 1*SPoint + Spread,
                                       Ask + 16*SPoint)); }
double CalcOpenSell () { return (dMin (iLow(TradeSymbol, PeriodTrade, 1) - 1*SPoint,
                                       Bid - 16*SPoint)); }
double CalcSlBuy  () { return (iLow (TradeSymbol, PeriodTrade, 1) - 1*SPoint); }
double CalcSlSell () { return (iHigh(TradeSymbol, PeriodTrade, 1) + 1*SPoint + Spread); }
double CalcTpBuy  () {
   double PriceOpen = CalcOpenBuy(), SL = CalcSlBuy();
   return (PriceOpen + dMax(TakeProfit*SPoint, (PriceOpen - SL)*2));
}
double CalcTpSell  () {
   double PriceOpen = CalcOpenSell(), SL = CalcSlSell();
   return (PriceOpen - dMax(TakeProfit*SPoint, (SL - PriceOpen)*2));
}
//+------------------------------------------------------------------+
bool TrailStop (int i, int TrailingStop) {
   double StopLoss;

   if ( OrderType() == OP_BUY ) {
      if ( Bid < OrderOpenPrice () )  return;
      StopLoss = iLow(TradeSymbol, PeriodTrailing, Lowest (TradeSymbol, PeriodTrailing, MODE_LOW, CandlesTrailing+1, 0)) - 1*Point;
      StopLoss = dMin (Bid-TrailingStop*SPoint, StopLoss);
      if ( StopLoss > OrderStopLoss() ) {
         MarkTrade();
         OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
         return(true);
      }
   }
   
   if ( OrderType() == OP_SELL ) {
      if ( Ask > OrderOpenPrice () )  return;
      StopLoss = iHigh(TradeSymbol, PeriodTrailing, Highest (TradeSymbol, PeriodTrailing, MODE_HIGH, CandlesTrailing+1, 0)) + 1*Point
                 + Spread;
      StopLoss = dMax (Ask+TrailingStop*SPoint, StopLoss);
      if ( StopLoss < OrderStopLoss() ) {
         MarkTrade();
         OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
         return(true);
      }
   }
}
//+------------------------------------------------------------------+
int Direction (string TradeSymbol, int PeriodDirection, int Mode) {
   double Previous, Previous2;

   if (Mode == DIRECTION_MACD ) {
	   Previous  = iMACD (TradeSymbol, PeriodDirection, 5, 34, 5, PRICE_MEDIAN, MODE_MAIN, 1);
	   Previous2 = iMACD (TradeSymbol, PeriodDirection, 5, 34, 5, PRICE_MEDIAN, MODE_MAIN, 2);
	}
	else {
	   Previous  = iOsMA (TradeSymbol, PeriodDirection, 5, 34, 5, PRICE_MEDIAN, 1);
	   Previous2 = iOsMA (TradeSymbol, PeriodDirection, 5, 34, 5, PRICE_MEDIAN, 2);
	}

   if ( Previous > Previous2 )
      return(1);
   if ( Previous < Previous2 )
      return(-1);
   return(0);
}
//+------------------------------------------------------------------+
string Filter (string TradeSymbol, int PeriodTrade, int Mode, bool &okBuy, bool &okSell) {
   double Value;
   
   okBuy = false; okSell = false;
   
   if (Mode == FILTER_WPR) {
      Value = iWPR(TradeSymbol, PeriodTrade, WilliamsP, 1);
	   if (Value < WilliamsH)  okBuy = true;
   	if (Value > WilliamsL)  okSell = true;
   	return ("iWPR: " + DoubleToStr(Value, 2));
   }
   else if (Mode == FILTER_FORCE) {
      Value = iForce (TradeSymbol, PeriodTrade, ForceP, MODE_EMA, PRICE_CLOSE, 1);
      if (Value < 0)  okBuy = true;
      if (Value > 0)  okSell = true;
   	return ("iForce: " + DoubleToStr(Value, 2));
   }
}
//+------------------------------------------------------------------+
double CalcMM (int MM) {
   double LotMM;

   if ( MM < -1) {
      if ( AccountFreeMargin () < 5 )  return(-1);
		LotMM = MathFloor (AccountBalance()*Leverage/1000);
		if ( LotMM < 1 )  LotMM = 1;
		LotMM = LotMM/100;
   }
	if ( MM == -1 ) {
		if ( AccountFreeMargin() < 50 )  return(-1);
		LotMM = MathFloor(AccountBalance()*Leverage/10000);
		if ( LotMM < 1 )  LotMM = 1;
		LotMM = LotMM/10;
   }
	if ( MM == 0 ) {
		if ( AccountFreeMargin() < MarginChoke ) return(-1); 
		LotMM = Lots;
	}
	if ( MM > 0 ) {
      if ( AccountFreeMargin() < 500 )  return(-1);
		LotMM = MathFloor(AccountBalance()*Leverage/100000);
 		if ( LotMM < 1 )  LotMM = 1;
	}
	if ( LotMM > LotsMax )  LotMM = LotsMax;
	return(LotMM);
}
//+------------------------------------------------------------------+
int TotalTradesThisSymbol (string TradeSymbol) {
   int i, TradesThisSymbol = 0;
   
   for (i = 0; i < OrdersTotal(); i++)
      if ( OrderSelect (i, SELECT_BY_POS) )
         if ( OrderSymbol() == TradeSymbol && OrderMagicNumber() == MAGICNUM)
            TradesThisSymbol++;

   return (TradesThisSymbol);
}

//+------------------------------------------------------------------+
void ReportError () {
   int err = GetLastError();
   Print("Error(",err,"): ", ErrorDescription(err));
}
//+------------------------------------------------------------------+
double dMax (double val1, double val2) {
  if (val1 > val2)  return(val1);
  return(val2);
}
//+------------------------------------------------------------------+
double dMin (double val1, double val2) {
  if (val1 < val2)  return(val1);
  return(val2);
}
//+------------------------------------------------------------------+
void MarkTrade () {
   LastTrade = CurTime();
}

Profitability Reports

PERFORMANCE BY SYMBOL

Expected payoff

Average expected payoff across the available backtests for each symbol.

Positive Negative
30 reports
EUR/USD Oct 2025 - Feb 2026
2.74
Total Trades 17
Won Trades 10
Lost trades 7
Win Rate 58.82 %
Expected payoff 17.40
Gross Profit 466.00
Gross Loss -170.20
Total Net Profit 295.80
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
2.26
Total Trades 10
Won Trades 5
Lost trades 5
Win Rate 50.00 %
Expected payoff 25.61
Gross Profit 459.70
Gross Loss -203.60
Total Net Profit 256.10
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
2.14
Total Trades 8
Won Trades 5
Lost trades 3
Win Rate 62.50 %
Expected payoff 20.77
Gross Profit 311.92
Gross Loss -145.74
Total Net Profit 166.18
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
1.95
Total Trades 8
Won Trades 4
Lost trades 4
Win Rate 50.00 %
Expected payoff 17.17
Gross Profit 282.10
Gross Loss -144.72
Total Net Profit 137.38
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
1.90
Total Trades 24
Won Trades 12
Lost trades 12
Win Rate 50.00 %
Expected payoff 14.26
Gross Profit 723.99
Gross Loss -381.82
Total Net Profit 342.17
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
1.88
Total Trades 18
Won Trades 7
Lost trades 11
Win Rate 38.89 %
Expected payoff 13.95
Gross Profit 536.70
Gross Loss -285.60
Total Net Profit 251.10
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
1.49
Total Trades 23
Won Trades 12
Lost trades 11
Win Rate 52.17 %
Expected payoff 8.91
Gross Profit 619.34
Gross Loss -414.45
Total Net Profit 204.89
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
1.36
Total Trades 9
Won Trades 4
Lost trades 5
Win Rate 44.44 %
Expected payoff 6.73
Gross Profit 228.60
Gross Loss -168.00
Total Net Profit 60.60
-100%
-50%
0%
50%
100%
EUR/USD Jan 2025 - Jul 2025
1.33
Total Trades 30
Won Trades 12
Lost trades 18
Win Rate 40.00 %
Expected payoff 7.67
Gross Profit 926.00
Gross Loss -695.90
Total Net Profit 230.10
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
1.29
Total Trades 14
Won Trades 7
Lost trades 7
Win Rate 50.00 %
Expected payoff 2.82
Gross Profit 175.50
Gross Loss -136.00
Total Net Profit 39.50
-100%
-50%
0%
50%
100%
AUD/USD Oct 2025 - Feb 2026
1.26
Total Trades 20
Won Trades 7
Lost trades 13
Win Rate 35.00 %
Expected payoff 3.65
Gross Profit 354.00
Gross Loss -280.90
Total Net Profit 73.10
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
1.15
Total Trades 10
Won Trades 4
Lost trades 6
Win Rate 40.00 %
Expected payoff 2.93
Gross Profit 229.40
Gross Loss -200.10
Total Net Profit 29.30
-100%
-50%
0%
50%
100%
USD/JPY Jul 2025 - Sep 2025
1.15
Total Trades 16
Won Trades 7
Lost trades 9
Win Rate 43.75 %
Expected payoff 1.98
Gross Profit 241.83
Gross Loss -210.16
Total Net Profit 31.67
-100%
-50%
0%
50%
100%
NZD/USD Jan 2025 - Jul 2025
1.14
Total Trades 25
Won Trades 10
Lost trades 15
Win Rate 40.00 %
Expected payoff 1.68
Gross Profit 352.00
Gross Loss -310.10
Total Net Profit 41.90
-100%
-50%
0%
50%
100%
USD/JPY Jul 2025 - Sep 2025
1.09
Total Trades 15
Won Trades 6
Lost trades 9
Win Rate 40.00 %
Expected payoff 1.41
Gross Profit 247.53
Gross Loss -226.44
Total Net Profit 21.09
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
1.08
Total Trades 18
Won Trades 8
Lost trades 10
Win Rate 44.44 %
Expected payoff 1.52
Gross Profit 382.20
Gross Loss -354.90
Total Net Profit 27.30
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
1.03
Total Trades 15
Won Trades 8
Lost trades 7
Win Rate 53.33 %
Expected payoff 0.24
Gross Profit 134.60
Gross Loss -131.00
Total Net Profit 3.60
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.70
Total Trades 17
Won Trades 7
Lost trades 10
Win Rate 41.18 %
Expected payoff -3.89
Gross Profit 151.80
Gross Loss -217.86
Total Net Profit -66.06
-100%
-50%
0%
50%
100%
GBP/CAD Oct 2025 - Feb 2026
0.66
Total Trades 19
Won Trades 8
Lost trades 11
Win Rate 42.11 %
Expected payoff -6.69
Gross Profit 250.75
Gross Loss -377.94
Total Net Profit -127.19
-100%
-50%
0%
50%
100%
GBP/AUD Jan 2025 - Jul 2025
0.62
Total Trades 33
Won Trades 8
Lost trades 25
Win Rate 24.24 %
Expected payoff -14.41
Gross Profit 790.01
Gross Loss -1265.54
Total Net Profit -475.53
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.56
Total Trades 13
Won Trades 4
Lost trades 9
Win Rate 30.77 %
Expected payoff -6.26
Gross Profit 102.60
Gross Loss -184.00
Total Net Profit -81.40
-100%
-50%
0%
50%
100%
GBP/CAD Jan 2025 - Jul 2025
0.53
Total Trades 28
Won Trades 7
Lost trades 21
Win Rate 25.00 %
Expected payoff -16.92
Gross Profit 534.22
Gross Loss -1007.84
Total Net Profit -473.62
-100%
-50%
0%
50%
100%
USD/CHF Jul 2025 - Sep 2025
0.53
Total Trades 14
Won Trades 3
Lost trades 11
Win Rate 21.43 %
Expected payoff -9.98
Gross Profit 159.00
Gross Loss -298.75
Total Net Profit -139.75
-100%
-50%
0%
50%
100%
USD/CHF Jul 2025 - Sep 2025
0.52
Total Trades 14
Won Trades 3
Lost trades 11
Win Rate 21.43 %
Expected payoff -10.83
Gross Profit 163.13
Gross Loss -314.69
Total Net Profit -151.56
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.51
Total Trades 7
Won Trades 3
Lost trades 4
Win Rate 42.86 %
Expected payoff -10.25
Gross Profit 76.02
Gross Loss -147.74
Total Net Profit -71.72
-100%
-50%
0%
50%
100%
USD/CAD Jan 2025 - Jul 2025
0.48
Total Trades 29
Won Trades 7
Lost trades 22
Win Rate 24.14 %
Expected payoff -11.73
Gross Profit 316.69
Gross Loss -656.76
Total Net Profit -340.07
-100%
-50%
0%
50%
100%
GBP/AUD Oct 2025 - Feb 2026
0.46
Total Trades 14
Won Trades 5
Lost trades 9
Win Rate 35.71 %
Expected payoff -10.00
Gross Profit 119.15
Gross Loss -259.16
Total Net Profit -140.01
-100%
-50%
0%
50%
100%
AUD/USD Jan 2025 - Jul 2025
0.43
Total Trades 26
Won Trades 8
Lost trades 18
Win Rate 30.77 %
Expected payoff -13.30
Gross Profit 262.80
Gross Loss -608.70
Total Net Profit -345.90
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.37
Total Trades 7
Won Trades 3
Lost trades 4
Win Rate 42.86 %
Expected payoff -15.87
Gross Profit 64.49
Gross Loss -175.61
Total Net Profit -111.12
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.27
Total Trades 10
Won Trades 3
Lost trades 7
Win Rate 30.00 %
Expected payoff -7.94
Gross Profit 29.60
Gross Loss -109.00
Total Net Profit -79.40
-100%
-50%
0%
50%
100%

Comments