Author:
Profit factor:
1.17

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
3 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

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%
USD/CAD Oct 2024 - Jan 2025
2.32
Total Trades 37
Won Trades 35
Lost trades 2
Win Rate 94.59 %
Expected payoff 32.32
Gross Profit 2100.27
Gross Loss -904.60
Total Net Profit 1195.67
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.59
Total Trades 29
Won Trades 25
Lost trades 4
Win Rate 86.21 %
Expected payoff -38.97
Gross Profit 1636.00
Gross Loss -2766.00
Total Net Profit -1130.00
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
1.09
Total Trades 44
Won Trades 40
Lost trades 4
Win Rate 90.91 %
Expected payoff 6.20
Gross Profit 3186.00
Gross Loss -2913.00
Total Net Profit 273.00
-100%
-50%
0%
50%
100%
AUD/USD Oct 2024 - Jan 2025
0.73
Total Trades 31
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -30.81
Gross Profit 2634.00
Gross Loss -3589.00
Total Net Profit -955.00
-100%
-50%
0%
50%
100%

Comments