Author:
Profit factor:
1.77

Okay, here's a breakdown of what this MetaTrader script does, explained without technical jargon:

This script is designed to automatically trade on the Forex market based on a few key ideas:

  1. Trend Following (Weekly Trend): The script first tries to figure out the overall trend of a specific currency pair on a weekly basis. It uses a calculation called MACD to estimate if the price is generally going up (uptrend), going down (downtrend) or moving sideways (neutral).

  2. Money Management: The script uses a money management system to decide how much to invest in each trade. There are several options here:

    • Fixed Lot Size: Use a specific amount for each trade, defined by you.
    • Risk-Based Lot Sizing: Calculate the investment based on how much you are willing to risk on each trade as a percentage of your total account balance. This helps manage risk.
  3. Order Management (Pending Orders): The script manages pending orders, which are instructions to automatically buy or sell when the price reaches a specific level. It will cancel some or modify the price level of the pending orders based on trend.

  4. Trailing Stop: This is a feature to protect profits. If a trade starts making money, the stop-loss order (an automatic exit point to limit losses) is automatically adjusted to lock in some of those profits.

  5. New Order Placement: If there are no open orders for a currency pair, the script will consider placing new pending orders to either buy or sell. The entry price for these new pending orders is based on the previous day's trading range and the current weekly trend, but it will only proceed if the planned order entry price is at least 16 points away from the current price.

In more detail, the script operates as follows:

  • Initialization: When the script starts, it doesn't do much initially. It just prepares itself to run.
  • Main Loop: The script runs repeatedly to check market conditions and manage trades, only every 5 mins after the first run. It performs these actions in each run:
    • Determine Weekly Trend: It calculates the overall trend (up, down, or neutral) for the currency pair.
    • Manage Open Orders: It looks at any pending orders to see if adjustments are needed and delete it if necessary.
    • Apply Trailing Stop: It automatically adjusts stop-loss orders on profitable trades to lock in gains.
    • Open New Orders: If there are no open positions for the currency pair, it decides whether to place a new buy or sell order based on trend indicators and the previous day's price movement.
Price Data Components
Series array that contains open time of each barSeries 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 ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
Indicators Used
MACD HistogramForce index
7 Views
0 Downloads
0 Favorites
TSD v1.1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                     TSD v1-1.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

//---
extern double Lots = 1;
//extern int StopLoss = 0;
extern int TakeProfit = 999;
extern int TrailingStop = 60;
int Slippage = 0;

//---Money Management Parameters
extern int Risk=5;
extern int mm=0;
extern int LiveTrading=0;
extern int AccountIsMini=0;
extern int maxTradesPerPair=1;


datetime NewWeeklyBar;
bool RunOnce=false;
double lotMM=0;
int LotSize=10000,LotMax=50,MarginChoke=500;
int WeeklyDirection;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 
   // Run checks 
   if(RunOnce && MathMod(TimeMinute(CurTime()),5)!=0)return(0); //after the first run of the EA, it only runs every 5 mins
   
   // This function determines the weekly trend
   SetWeeklyTrend();
   
   // Manage current open orders and trades
   RunPendingOrderManagement(Symbol(),"TSD");
   RunTrailingStop(Symbol(),"TSD");
   
   // Open New Orders
   if(!SetLotsMM())return(0);
   RunNewOrderManagement(Symbol(),"TSD");
   
   RunOnce=true;
//----
   return(0);
  }
//+------------------------------------------------------------------+


/////////////////////////////////////////////////
//  WeeklyTrend
/////////////////////////////////////////////////
bool SetWeeklyTrend(){
   double MACD_0,MACD_1,MACD_2;   
   MACD_0   = iMACD(Symbol(),PERIOD_W1,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MACD_1   = iMACD(Symbol(),PERIOD_W1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   MACD_2   = iMACD(Symbol(),PERIOD_W1,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
   if(MACD_1 > MACD_2)  WeeklyDirection = 1;
   if(MACD_1 < MACD_2)  WeeklyDirection = -1;
   if(MACD_1 == MACD_2) WeeklyDirection = 0;

   if(WeeklyDirection==1)  Comment("\nWeekly Trend for "+Symbol()+" = UP TREND");
   if(WeeklyDirection==-1) Comment("\nWeekly Trend for "+Symbol()+" = DOWN TREND");
   if(WeeklyDirection==0)  Comment("\nWeekly Trend for "+Symbol()+" = NEUTRAL TREND");
   
   return(true);
   }
   
/////////////////////////////////////////////////
//  LastOrderCloseTime
/////////////////////////////////////////////////
datetime LastOrderCloseTimeAll(){
   datetime loct;
   for(int x=0;x<HistoryTotal();x++){
      OrderSelect(x, SELECT_BY_POS, MODE_HISTORY);
      if(OrderCloseTime()>loct){
         loct=OrderCloseTime();
         }
      }
   return(loct);
   }
datetime LastOrderCloseTimeBySymbol(string sym){
   datetime loct;
   for(int x=0;x<HistoryTotal();x++){
      OrderSelect(x, SELECT_BY_POS, MODE_HISTORY);
      if(OrderSymbol()==sym){
         if(OrderCloseTime()>loct){
            loct=OrderCloseTime();
            }
         }
      }
   return(loct);
   }


/////////////////////////////////////////////////
//  OpenOrdersBySymbolAndComment
/////////////////////////////////////////////////
int OpenOrdersBySymbolAndComment(string sym, string comm){
   int ofts=0;
   for(int x=0;x<OrdersTotal();x++){
      OrderSelect(x, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==Symbol() && StringSubstr(OrderComment(),0,StringLen(comm))==comm){
         ofts++;
         }
      }
   return(ofts);
   }

/////////////////////////////////////////////////
//  SetLotsMM
/////////////////////////////////////////////////
bool SetLotsMM(){
   double MarginCutoff;

   if(AccountIsMini == 0) MarginCutoff = 1000;
   if(AccountIsMini == 1) MarginCutoff = 100;

   if(AccountFreeMargin() < MarginCutoff)return(false);

   if(mm != 0){
     lotMM = MathCeil(AccountBalance() * Risk / 10000) / 10;

     if(lotMM < 0.1) lotMM = Lots;
     if(lotMM > 1.0) lotMM = MathCeil(lotMM);

     // Enforce lot size boundaries

     if(LiveTrading == 1){
         if(AccountIsMini == 1 )               lotMM = lotMM * 10;
         if(AccountIsMini == 0 && lotMM < 1.0) lotMM = 1.0;
         }

     if(lotMM > 100) lotMM = 100;
   }
   else{
     lotMM = Lots; // Change mm to 0 if you want the Lots parameter to be in effect
     }
   
   return(true);
   }
   
/////////////////////////////////////////////////
//  RunPendingOrderManagement
/////////////////////////////////////////////////
bool RunPendingOrderManagement(string sym, string comm){
   if( OpenOrdersBySymbolAndComment(sym,comm) > 0 ){
      for ( int i = 0; i < OrdersTotal(); i++){
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         //--- LONG TRADES
         if(OrderType() == OP_BUYSTOP && OrderSymbol() == sym && StringSubstr(OrderComment(),0,StringLen(comm))==comm){
            if(WeeklyDirection==-1){
               OrderDelete(OrderTicket());
               continue;
               }
            if( iHigh(Symbol(),PERIOD_D1,1) < iHigh(Symbol(),PERIOD_D1,2) ){
	            if( iHigh(Symbol(),PERIOD_D1,1) > (Ask + 16*Point) ){
	               OrderModify(OrderTicket(),iHigh(Symbol(),PERIOD_D1,1) + 1*Point,iLow(Symbol(),PERIOD_D1,1) - 1*Point,OrderTakeProfit(),OrderExpiration(),Green);
	               continue;
	               }
               }
            }
         //--- SHORT TRADES
         if(OrderType() == OP_SELLSTOP && OrderSymbol() == sym && StringSubstr(OrderComment(),0,StringLen(comm))==comm){
            if(WeeklyDirection==1){
               OrderDelete(OrderTicket());
               continue;
               }
            if( iLow(Symbol(),PERIOD_D1,1) > iLow(Symbol(),PERIOD_D1,2) ){ 
	            if( iLow(Symbol(),PERIOD_D1,1) < (Bid - 16*Point) ){
	               OrderModify(OrderTicket(),iLow(Symbol(),PERIOD_D1,1) - 1*Point,iHigh(Symbol(),PERIOD_D1,1) + 1*Point,OrderTakeProfit(),OrderExpiration(),Blue);
	               continue;
		            }
               }
            }
         }
      }
   }
   
/////////////////////////////////////////////////
//  RunTrailingStop
/////////////////////////////////////////////////
bool RunTrailingStop(string sym, string comm){
   if( OpenOrdersBySymbolAndComment(sym,comm) > 0 ){
      double Buy_Tp,Sell_Tp;
      for ( int i = 0; i < OrdersTotal(); i++){
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         //--- LONG TRADES
         if(OrderType() == OP_BUY && OrderSymbol() == sym && StringSubstr(OrderComment(),0,StringLen(comm))==comm){
            if (Bid - OrderOpenPrice() >= TrailingStop * Point){
               if (OrderStopLoss() < Bid - TrailingStop * Point || OrderStopLoss() == 0){
                  OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * Point, OrderTakeProfit(), Red);
                  }
               }
            }
         //--- SHORT TRADES
         if(OrderType() == OP_SELL && OrderSymbol() == sym && StringSubstr(OrderComment(),0,StringLen(comm))==comm){
            if(OrderOpenPrice() - Ask >= TrailingStop * Point){
               if((OrderStopLoss() > Ask + TrailingStop * Point) || OrderStopLoss() == 0){
                  OrderModify(OrderTicket(), OrderOpenPrice(),Ask + TrailingStop * Point, OrderTakeProfit(), Blue);
                  }
               }
            }
         }
      }
   }

/////////////////////////////////////////////////
//  RunNewOrderManagement
/////////////////////////////////////////////////
bool RunNewOrderManagement(string sym, string comm){
   if( OpenOrdersBySymbolAndComment(sym,comm) <= 0 ){
      double Buy_Tp,Sell_Tp,PriceOpen,NewPrice;
      bool ForcePos = iForce(Symbol(),PERIOD_D1,2,MODE_EMA,PRICE_CLOSE,1)  > 0;
      bool ForceNeg  = iForce(Symbol(),PERIOD_D1,2,MODE_EMA,PRICE_CLOSE,1) < 0;
      
      if( WeeklyDirection == 1 && ForceNeg ){
         PriceOpen = iHigh(Symbol(),PERIOD_D1,1) + 1 * Point;		         // Buy 1 point above high of previous candle
			if( PriceOpen > (Ask + 16 * Point) ){                             // Check If buy price is a least 16 points > Ask
			   if( TakeProfit >= 5 )   Buy_Tp = PriceOpen + TakeProfit * Point;
				if( TakeProfit <  5 )   Buy_Tp = 0;
				OrderSend(sym,OP_BUYSTOP,lotMM,PriceOpen,Slippage,iLow(Symbol(),PERIOD_D1,1) - 1*Point,Buy_Tp,"TSD - "+Symbol()+" Long",0,0,Green);
				return(true);
			   }
			if( PriceOpen <= (Ask + 16 * Point) ){
				NewPrice = Ask + 16 * Point;
				if( TakeProfit >= 5 )   Buy_Tp = NewPrice + TakeProfit * Point;
				if( TakeProfit <  5 )   Buy_Tp = 0;
				OrderSend(sym,OP_BUYSTOP,lotMM,NewPrice,Slippage,iLow(Symbol(),PERIOD_D1,1) - 1*Point,Buy_Tp,"TSD - "+Symbol()+" Long",0,0,Green);
				return(true);
			   }
         }
      if( WeeklyDirection == -1 && ForcePos ){
         PriceOpen = iLow(Symbol(),PERIOD_D1,1) - 1 * Point;		         // Sell 1 point below low of previous candle
			if( PriceOpen < (Bid - 16 * Point) ){                             // Check If sell price is a least 16 points < Bid
			   if( TakeProfit >= 5 )   Sell_Tp = PriceOpen - TakeProfit * Point;
				if( TakeProfit <  5 )   Sell_Tp = 0;
				OrderSend(sym,OP_SELLSTOP,lotMM,PriceOpen,Slippage,iHigh(Symbol(),PERIOD_D1,1) + 1 * Point,Sell_Tp,"TSD - "+Symbol()+" Short",0,0,Red);
				return(true);
			   }
			if( PriceOpen >= (Bid - 16 * Point) ){
				NewPrice = Bid - 16 * Point;
				if( TakeProfit >= 5 )   Sell_Tp = NewPrice - TakeProfit * Point;
				if( TakeProfit <  5 )   Sell_Tp = 0;
				OrderSend(sym,OP_SELLSTOP,lotMM,NewPrice,Slippage,iHigh(Symbol(),PERIOD_D1,1) + 1 * Point,Sell_Tp,"TSD - "+Symbol()+" Short",0,0,Red);
				return(true);
			   }
         }
      }
   return(true);
   }

Profitability Reports

GBP/AUD Jul 2025 - Sep 2025
0.61
Total Trades 30
Won Trades 27
Lost trades 3
Win Rate 90.00 %
Expected payoff -24.99
Gross Profit 1178.69
Gross Loss -1928.46
Total Net Profit -749.77
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
278911.38
Total Trades 420
Won Trades 417
Lost trades 3
Win Rate 99.29 %
Expected payoff 958920.44
Gross Profit 402748038.00
Gross Loss -1444.00
Total Net Profit 402746594.00
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
1.03
Total Trades 24
Won Trades 21
Lost trades 3
Win Rate 87.50 %
Expected payoff 1.29
Gross Profit 1017.00
Gross Loss -986.00
Total Net Profit 31.00
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
8.69
Total Trades 56
Won Trades 55
Lost trades 1
Win Rate 98.21 %
Expected payoff 74.99
Gross Profit 4745.65
Gross Loss -546.22
Total Net Profit 4199.43
-100%
-50%
0%
50%
100%
USD/CAD Jan 2025 - Jul 2025
2.03
Total Trades 75
Won Trades 72
Lost trades 3
Win Rate 96.00 %
Expected payoff 31.21
Gross Profit 4605.31
Gross Loss -2264.80
Total Net Profit 2340.51
-100%
-50%
0%
50%
100%
NZD/USD Jan 2025 - Jul 2025
0.49
Total Trades 62
Won Trades 51
Lost trades 11
Win Rate 82.26 %
Expected payoff -51.84
Gross Profit 3043.00
Gross Loss -6257.00
Total Net Profit -3214.00
-100%
-50%
0%
50%
100%
GBP/USD Jan 2025 - Jul 2025
1.45
Total Trades 81
Won Trades 75
Lost trades 6
Win Rate 92.59 %
Expected payoff 23.28
Gross Profit 6118.00
Gross Loss -4232.00
Total Net Profit 1886.00
-100%
-50%
0%
50%
100%
GBP/CAD Jan 2025 - Jul 2025
0.42
Total Trades 68
Won Trades 57
Lost trades 11
Win Rate 83.82 %
Expected payoff -61.65
Gross Profit 3056.45
Gross Loss -7248.43
Total Net Profit -4191.98
-100%
-50%
0%
50%
100%
GBP/AUD Jan 2025 - Jul 2025
0.60
Total Trades 82
Won Trades 71
Lost trades 11
Win Rate 86.59 %
Expected payoff -47.94
Gross Profit 5911.65
Gross Loss -9842.91
Total Net Profit -3931.26
-100%
-50%
0%
50%
100%
AUD/USD Jan 2025 - Jul 2025
1.10
Total Trades 74
Won Trades 68
Lost trades 6
Win Rate 91.89 %
Expected payoff 5.08
Gross Profit 4223.00
Gross Loss -3847.00
Total Net Profit 376.00
-100%
-50%
0%
50%
100%

Comments