Donchain counter-channel system

Author: by Michal Rutka
Profit factor:
9.10

Okay, here's a breakdown of what this trading script does, explained in a way that doesn't require any programming knowledge:

Overall Goal:

This script is designed to automatically trade based on a specific set of rules described in an article about the "Donchian counter-channel" system. The goal is to identify potential buy or sell opportunities based on how the price interacts with Donchian Channels.

Key Concepts:

  • Donchian Channels: Imagine drawing two lines on a price chart. The upper line represents the highest price reached in the last 20 days (or whatever period you choose), and the lower line represents the lowest price reached in the last 20 days. These lines form a channel around the price. This script requires an indicator, which is a bit of code from other person, to calculate the Donchian Channels.
  • Upturn/Downturn: The script looks at the direction of the Donchian Channel lines. An "upturn" means the lower line is starting to rise, and a "downturn" means the upper line is starting to fall.
  • Long/Short: "Long" means buying, hoping the price will go up. "Short" means selling, hoping the price will go down (and then you buy back at a lower price to profit).
  • Stop Loss: A stop loss is a price level you set to automatically close a trade if the price moves against you too much. It's like an emergency exit to limit your potential losses.

How the Script Works:

  1. Setup: The script starts by defining some settings that you can adjust, like the amount of money to trade with (Lots), how much price slippage to allow (Slippage), a unique identification number (Magic) for the script's trades, the period used for Donchian Channels (ChannelPeriod), and the timeframe to get the data (TimeFrame).
  2. Checking Existing Trades: Before doing anything, the script checks if it already has any open trades related to its system. This is to not make same trade multiple times. It keeps track of whether it's currently in a "long" (buy) or "short" (sell) position.
  3. Adjusting Stop Losses: If the script is in a trade, it checks if the stop loss needs to be adjusted based on the current Donchian Channel lines. The stop loss will be automatically updated.
  4. Looking for New Opportunities:
    • The script determines if the Donchian Channel lines are turning up or down, according to the indicator.
    • If the lower Donchian Channel line turns up, it signals a potential "long" (buy) opportunity.
    • If the upper Donchian Channel line turns down, it signals a potential "short" (sell) opportunity.
  5. Entering a Trade:
    • If the script sees a "long" opportunity and it's not already in a trade, it automatically places a "buy" order.
    • If the script sees a "short" opportunity and it's not already in a trade, it automatically places a "sell" order.
    • When entering a trade, the script sets a stop loss based on the Donchian Channel line.
  6. Daily trade limit: To not execute trades constantly, the script only makes one trade in a day.
  7. Reporting Profit: The script report how much profits or losses the strategy have made.

In simple terms:

The script watches the Donchian Channels. When the lower line starts rising, it buys. When the upper line starts falling, it sells. It uses the opposite Donchian Channel line as a stop loss to protect against losses.

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
Miscellaneous
It issuies visual alerts to the screen
4 Views
0 Downloads
0 Favorites
Donchain counter-channel system
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                              Donchain counter-channel system.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "by Michal Rutka"
#property link      ""

/*
  System 'Donchain counter-channel' was described in the Currency Trader
  Magazine, Octobre 2005, p.32.
  
  Rules:
    1. Go long next day at market if the 20-day
       lower Donchian channel line turns up.
    2. Go short next day at market if the upper 20-day
       Donchian channel line turns down.
    3. Exit long with a stop at the lower 20-day Donchian
       channel line.
    4. Exit short with a stop at the upper 20-day
       Donchian channel line.
       
  Strategy uses a custom indicator "Donchain Channels - Generalized version.mq4", which must be put in the indicators 
  subfolder.

    Version     Date         Comment
    1.0         9 Oct 2005   First version based on the article from the Currency Trader magazine.

*/

extern double Lots     = 1.0;       // MM will come when strategy proved 
extern int    Slippage = 3;         // Change it for your brooker
extern int    Magic    = 20051006;  // Magic number of the strategy

// Optimalization parameters:
extern int    ChannelPeriod = 20;   // Original channel period from th article.
extern int    TimeFrame     = PERIOD_D1; // Time frame of the Donchain indicator.

// Privete variables
datetime last_trade_time;           // in order to execute maximum only one trade a day.

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }

void ReportStrategy(int Magic)
{
  int    totalorders = HistoryTotal();
  double StrategyProfit = 0.0;
  int    StrategyOrders = 0;
  
  for(int j=0; j<totalorders;j++)
  { if(OrderSelect(j, SELECT_BY_POS, MODE_HISTORY) &&
       (OrderMagicNumber() == Magic))
    {
      if((OrderType() == OP_BUY) ||
         (OrderType() == OP_SELL))
      {
        StrategyOrders++;
        StrategyProfit += OrderProfit();
      }
    }
  }
  
  totalorders = OrdersTotal();
  for(j=0; j<totalorders;j++)
  { if(OrderSelect(j, SELECT_BY_POS, MODE_TRADES) &&
       (OrderMagicNumber() == Magic))
    {
      if((OrderType() == OP_BUY) ||
         (OrderType() == OP_SELL))
      {
        StrategyOrders++;
        StrategyProfit += OrderProfit();
      }
    }
  }
  
  
  Comment("Executed ", StrategyOrders, " trades with ", StrategyProfit," of profit");
  return;
}


//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
  double stop_short;
  double stop_long;
  
  bool entry_short;
  bool entry_long;
  bool are_we_long     = false;
  bool are_we_short    = false;
  int  orders_in_trade = 0;
  int  active_order_ticket;
  
//---- 
  ReportStrategy(Magic);

  // Check current trades:
  int TotalOrders = OrdersTotal();
  for(int j=0;j<TotalOrders;j++){
    OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
    if(OrderSymbol()==Symbol() && 
	    OrderMagicNumber() == Magic){
	      are_we_long  = are_we_long  || OrderType() == OP_BUY;
	      are_we_short = are_we_short || OrderType() == OP_SELL;
	      orders_in_trade++;
	      active_order_ticket = OrderTicket();
    }
  }
  
  if(orders_in_trade > 1){
    Alert("More than one active trade! Please close the wrong one.");
    return(-1);
  }
   
  // Lower channel:
  stop_long = iCustom(NULL, TimeFrame, "Donchian Channels - Generalized version",ChannelPeriod,1,0,0,0,0,0);

  // Upper channel:
  stop_short = iCustom(NULL, TimeFrame, "Donchian Channels - Generalized version",ChannelPeriod,1,0,0,0,1,0);
  
  if(are_we_long){
    // We have long position. Check stop loss:
    OrderSelect(active_order_ticket, SELECT_BY_TICKET);
    if(OrderStopLoss() < stop_long)
      OrderModify(active_order_ticket,
                  OrderOpenPrice(),
                  stop_long,
                  OrderTakeProfit(),
                  0,
                  Blue);
    
    return(0);
  }

  if(are_we_short){
    // We have long position. Check stop loss:
    OrderSelect(active_order_ticket, SELECT_BY_TICKET);
    if(OrderStopLoss() > stop_short)
      OrderModify(active_order_ticket,
                  OrderOpenPrice(),
                  stop_short,
                  OrderTakeProfit(),
                  0,
                  Blue);
    
    return(0);
  }

  // Do not execute new trade for a next 24 hours.
  
  if((CurTime() - last_trade_time)<24*60*60) return(0);
  
  // Lower channel:
  entry_long = iCustom(NULL, TimeFrame, "Donchian Channels - Generalized version",ChannelPeriod,1,0,0,0,0,1) > 
               iCustom(NULL, TimeFrame, "Donchian Channels - Generalized version",ChannelPeriod,1,0,0,0,0,2);

  // Upper channel:
  entry_short = iCustom(NULL, TimeFrame, "Donchian Channels - Generalized version",ChannelPeriod,1,0,0,0,1,1) < 
                iCustom(NULL, TimeFrame, "Donchian Channels - Generalized version",ChannelPeriod,1,0,0,0,1,2);
  
  if(entry_long && entry_short)
    Alert("Short and long entry. Probably one of the is wrong.");
  
  if(entry_long){
    OrderSend(Symbol(),
              OP_BUY,
              Lots,
              Ask,
              Slippage,
              stop_long,
              0,
              "",
              Magic,
              0,
              FireBrick);
    last_trade_time = CurTime();
  }

  if(entry_short){
    OrderSend(Symbol(),
              OP_SELL,
              Lots,
              Bid,
              Slippage,
              stop_short,
              0,
              "",
              Magic,
              0,
              FireBrick);
    last_trade_time = CurTime();
  }

//----
   return(0);
  }
//+------------------------------------------------------------------+

Profitability Reports

EUR/USD Jan 2025 - Jul 2025
4.17
Total Trades 6
Won Trades 2
Lost trades 4
Win Rate 33.33 %
Expected payoff 1612.67
Gross Profit 12725.00
Gross Loss -3049.00
Total Net Profit 9676.00
-100%
-50%
0%
50%
100%
AUD/USD Jan 2025 - Jul 2025
0.00
Total Trades 6
Won Trades 0
Lost trades 6
Win Rate 0.00 %
Expected payoff -543.50
Gross Profit 0.00
Gross Loss -3261.00
Total Net Profit -3261.00
-100%
-50%
0%
50%
100%
USD/CAD Oct 2024 - Jan 2025
0.00
Total Trades 1
Won Trades 1
Lost trades 0
Win Rate 100.00 %
Expected payoff 3653.77
Gross Profit 3653.77
Gross Loss 0.00
Total Net Profit 3653.77
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.00
Total Trades 1
Won Trades 1
Lost trades 0
Win Rate 100.00 %
Expected payoff 3426.00
Gross Profit 3426.00
Gross Loss 0.00
Total Net Profit 3426.00
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
33.37
Total Trades 2
Won Trades 1
Lost trades 1
Win Rate 50.00 %
Expected payoff 2363.00
Gross Profit 4872.00
Gross Loss -146.00
Total Net Profit 4726.00
-100%
-50%
0%
50%
100%
AUD/USD Oct 2024 - Jan 2025
17.08
Total Trades 3
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff 1093.33
Gross Profit 3484.00
Gross Loss -204.00
Total Net Profit 3280.00
-100%
-50%
0%
50%
100%

Comments