TSD_MT4_MR_Trade_0_11
Download scriptThis automated trading script is designed to manage a diverse portfolio of currency pairs by executing trades based on specific technical indicators and strict risk management rules. It operates autonomously, cycling through different currency pairs to identify opportunities while ensuring that multiple positions are not opened simultaneously for the same symbol.
Core Trading Logic
The script determines whether to enter a trade by combining two main analytical components: direction and filtering.
1. Directional Analysis The system first identifies the "trend" or general direction of the market using momentum indicators (specifically MACD or OSMA). It compares the current state of the market against the previous state to decide if the price movement is trending upward or downward. This ensures that the script only looks for "Buy" opportunities in an up-trend and "Sell" opportunities in a down-trend.
2. Market Filtering To refine its accuracy, the script passes the directional data through a secondary filter (specifically the Williams %R indicator). This acts as a confirmation step:
- It only confirms a Buy signal if the market is sufficiently oversold or at a favorable level.
- It only confirms a Sell signal if the market is sufficiently overbought or at a favorable level.
Trade Execution and Management
Rather than entering the market immediately, the script utilizes "Pending Orders." This means it places orders to be triggered only when the price reaches specific levels (Buy Stops or Sell Stops).
- Order Placement: Once a trend and filter match, the script calculates the best entry points based on recent high/low prices and sets the trade with predefined profit targets and stop-loss limits.
- Dynamic Adjustment: The script constantly monitors these pending orders. If the market trend changes before the order is triggered (e.g., a "Buy" signal disappears), it will automatically delete the pending order to prevent unnecessary risk. It also adjusts the entry levels if the recent price highs or lows shift, keeping the trade setup relevant to current market conditions.
- Trailing Stop-Loss: For active trades that move into a profitable state, the script employs a "Trailing Stop" mechanism. This moves the stop-loss level automatically in favor of the trader as the price moves correctly. It uses recent price history (lows for buys, highs for sells) to ensure the protection follows the price closely, aiming to lock in profits while giving the trade room to breathe.
Risk and Money Management
The script includes a sophisticated automated money management system that determines the size of each trade based on your account balance and available margin. It offers different modes:
- Fixed Lot: Trades a set amount regardless of balance.
- Proportional Scaling: Automatically calculates trade sizes based on leverage and available funds, ensuring the risk is scaled relative to the account size.
- Safety Constraints: It includes "Margin Choke" limits and maximum lot caps to prevent the system from over-leveraging the account or placing trades that exceed specific safety thresholds.
Summary of Workflow
- Scan: Cycles through a list of 11 currency pairs.
- Analyze: Checks the current trend (Direction) and confirms with a technical filter.
- Position: Places a pending order at a calculated price point if the conditions are met.
- Monitor: Continuously updates or deletes pending orders based on shifting market highs/lows.
- Protect: Applies a trailing stop to active trades to secure profits as the market moves in the desired direction.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| TSD_MR_Trade_MACD_WPR_0_11.mq4 |
//| Copyright ® 2005 Bob O'Brien / Barcode |
//| TSD v1.2 rewritten to MQL4 and enhanced by Mindaugas |
//| TSD Trade version 0.11 |
//+------------------------------------------------------------------+
#property copyright "Copyright ® 2005 Bob O'Brien / Barcode"
#include <stdlib.mqh>
#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
string pairs[] = { "AUDUSD", "EURCHF", "EURGBP", "EURJPY", "EURUSD", "GBPCHF", "GBPJPY", "GBPUSD",
"USDCAD", "USDCHF", "USDJPY" };
// parameters for iWPR and iForce indicators
int WilliamsP = 24, WilliamsL = -75, WilliamsH = -25;
int ForceP = 2;
int MagicNumber = 2005072001;
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, SDigits;
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);
Pair = (Pair+1) % ArraySize(pairs);
TradeSymbol = pairs[Pair];
Spread = MarketInfo (TradeSymbol, MODE_ASK) - MarketInfo (TradeSymbol, MODE_BID);
SPoint = MarketInfo (TradeSymbol, MODE_POINT);
SDigits = MarketInfo (TradeSymbol, MODE_DIGITS);
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", MagicNumber, 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", MagicNumber, 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() != MagicNumber) continue;
if ( OrderType () == OP_BUYSTOP ) {
if ( Direction != 1 ) {
MarkTrade();
OrderDelete ( OrderTicket() );
return(0);
}
if ( iHigh(TradeSymbol, PeriodTrade, 1) < iHigh(TradeSymbol, PeriodTrade, 2) &&
!ComparePrices (CalcSlBuy(), OrderStopLoss()) ) {
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) &&
!ComparePrices (CalcSlSell(), OrderStopLoss()) ) {
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() != MagicNumber) continue;
if ( TrailStop (i, TrailingStop) ) return(0);
}
}
return(0);
}
//+------------------------------------------------------------------+
double CalcOpenBuy () { return (dMax (iHigh(TradeSymbol, PeriodTrade, 1) + 1*SPoint + Spread,
MarketInfo(TradeSymbol, MODE_ASK) + 16*SPoint)); }
double CalcOpenSell () { return (dMin (iLow(TradeSymbol, PeriodTrade, 1) - 1*SPoint,
MarketInfo(TradeSymbol, MODE_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 ( MarketInfo (TradeSymbol, MODE_BID) < OrderOpenPrice () ) return;
StopLoss = iLow(TradeSymbol, PeriodTrailing, Lowest (TradeSymbol, PeriodTrailing, MODE_LOW, CandlesTrailing+1, 0)) - 1*SPoint;
StopLoss = dMin (MarketInfo (TradeSymbol, MODE_BID)-TrailingStop*SPoint, StopLoss);
if ( StopLoss > OrderStopLoss() ) {
MarkTrade();
OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
return(true);
}
}
if ( OrderType() == OP_SELL ) {
if ( MarketInfo (TradeSymbol, MODE_ASK) > OrderOpenPrice () ) return;
StopLoss = iHigh(TradeSymbol, PeriodTrailing, Highest (TradeSymbol, PeriodTrailing, MODE_HIGH, CandlesTrailing+1, 0)) + 1*SPoint
+ Spread;
StopLoss = dMax (MarketInfo (TradeSymbol, MODE_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 if (Mode == DIRECTION_OSMA) {
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() == MagicNumber )
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();
}
//+------------------------------------------------------------------+
bool ComparePrices (double Price1, double Price2) {
return ( NormalizeDouble (Price1, SDigits) == NormalizeDouble (Price2, SDigits) );
}
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments