Author: Ron Thompson
Profit factor:
0.57

Here's a breakdown of the MQL4 script's logic in plain language, designed for someone who doesn't code.

Overall Goal:

This script is designed to automatically trade in the Forex market based on signals from a technical indicator called the Commodity Channel Index (CCI). It aims to open and close trades hoping to profit from price movements.

Core Concepts

  • Technical Indicator (CCI): Think of this as a mathematical formula applied to price data to generate buy or sell signals. In this case the script use this to help determine possible trend changes in the market.
  • Buy/Sell Orders: These are instructions to your broker to either buy (expecting the price to go up) or sell (expecting the price to go down) a currency pair.
  • Take Profit/Stop Loss: These are safety nets. Take Profit automatically closes a trade when it reaches a certain profit level. Stop Loss automatically closes a trade to limit potential losses.
  • Lot Size: The size of your trade. It represents the amount of currency you are buying or selling.

How the Script Works

  1. Setup (Initialization):

    • When the script starts, it sets up some initial values based on the user-defined settings (input parameters). The interval setting is stored for later use.
  2. Main Trading Logic (The start() Function):

    • Checking Conditions: Before doing anything, the script checks a few things:

      • Account Balance: Does your account have enough money to place a trade with the specified "Lots" size?
      • Data Availability: Has enough price data been loaded for the script to work?
      • Market Movement: Has the price actually changed since the last time the script ran? If not, there is no need to do anything.
    • Calculating Stop Loss and Take Profit:

      • The script figures out where to place the stop loss and take profit orders, based on the user defined pip parameter relative to the current price.
    • Analyzing the CCI Indicator:

      • The script looks at the CCI indicator at the current and previous price bars.
    • Identifying Crossover Signals:

      • The script specifically watches for the CCI to "cross" the zero line.
      • Rising Cross: If the CCI was below zero in the previous bar but is now above zero, this is considered a bullish (buy) signal.
      • Falling Cross: If the CCI was above zero in the previous bar but is now below zero, this is considered a bearish (sell) signal.
    • Opening and Closing Trades:

      • If a Crossover Occurs:
        • The script closes all existing trades for the currency pair.
        • Then, it opens a new trade based on the direction of the crossover: If rising, it opens a buy order. If falling, it opens a sell order. It sets the Stop Loss and Take Profit levels for the new trade.
      • Pyramiding: *The program tries to open more orders in the same direction
        • If there is an order already opened in the current symbol it keeps counting how many bars have passed to open new orders.
        • If the number of bars that have passed reach the configured Interval it will create a new order in the same direction with the same stop loss and take profit.
        • After opening the pyramiding order, the number of bars that have passed is set back to zero to wait until the next Interval is reached.
        • If it open an order, the Stop Loss and Take Profit settings are set.
  3. Looping:

    • The start() function runs repeatedly as new price data becomes available, constantly monitoring the market and adjusting positions.
Price Data Components
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It automatically opens orders when conditions are reached
Indicators Used
Commodity channel index
6 Views
0 Downloads
0 Favorites
zzz004
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//| 1MA Expert                               |
//+------------------------------------------------------------------+
#property copyright "Ron Thompson"
#property link      "http://www.lightpatch.com/forex"

// User Input
extern double Lots = 0.1;
extern int    TakeProfit=90;
extern int    StopLoss=0;
extern int    Interval=5;


// Global scope
double barmove0 = 0;
double barmove1 = 0;
int         itv = 0;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|

int init()
  {
   itv=Interval;
   return(0);
  }


//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {

   bool      found=false;
   bool     rising=false;
   bool    falling=false;
   bool      cross=false;

   double slA=0, slB=0, tpA=0, tpB=0;
   double p=Point();
   
   double cCI0;
   double cCI1;
   
   int      cnt=0;

   // Error checking
   if(AccountFreeMargin()<(1000*Lots))        {Print("-----NO MONEY"); return(0);}
   if(Bars<100)                               {Print("-----NO BARS "); return(0);}
   if(barmove0==Open[0] && barmove1==Open[1]) {                        return(0);}

   // bars moved, update current position
   barmove0=Open[0];
   barmove1=Open[1];

   // interval (bar) counter
   // used to pyramid orders during trend
   itv++;
   
   // since the bar just moved
   // calculate TP and SL for (B)id and (A)sk
   tpA=Ask+(p*TakeProfit);
   slA=Ask-(p*StopLoss);
   tpB=Bid-(p*TakeProfit);
   slB=Bid+(p*StopLoss);
   if (TakeProfit==0) {tpA=0; tpB=0;}           
   if (StopLoss==0)   {slA=0; slB=0;}           
   
   // get CCI based on OPEN
   cCI0=iCCI(Symbol(),0,30,PRICE_OPEN,0);
   cCI1=iCCI(Symbol(),0,30,PRICE_OPEN,1);

   // is it crossing zero up or down
   if (cCI1<0 && cCI0>0) { rising=true; cross=true; Print("Rising  Cross");}
   if (cCI1>0 && cCI0<0) {falling=true; cross=true; Print("Falling Cross");}
   
   // close then open orders based on cross
   // pyramid below based on itv
   if (cross)
     {
      // Close ALL the open orders 
      for(cnt=0;cnt<OrdersTotal();cnt++)
        {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol())
           {
            if (OrderType()==0) {OrderClose(OrderTicket(),Lots,Bid,3,White);}
            if (OrderType()==1) {OrderClose(OrderTicket(),Lots,Ask,3,Red);}
            itv=0;
           }
        }
      // Open new order based on direction of cross
      if (rising)  OrderSend(Symbol(),OP_BUY,Lots,Ask,3,slA,tpA,"ZZZ100",11123,0,White);
      if (falling) OrderSend(Symbol(),OP_SELL,Lots,Bid,3,slB,tpB,"ZZZ100",11321,0,Red);
      
      // clear the interval counter
      itv=0;
     }
   
   // Only pyramid if order already open
   found=false;
   for(cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==Symbol())
        {
         found=true;
         break;
        }
     }


   // Pyramid
   // add an order every
   if (found && itv>=Interval)
     {
      OrderSend(Symbol(),OP_BUY,Lots,Ask,3,slA,tpA,"ZZZ100",11123,0,White);
      itv=0;
     }

   if (found && itv>=Interval)
     {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,3,slB,tpB,"ZZZ100",11321,0,Red);
      itv=0;
     }
   
   
   return(0);
  }

Profitability Reports

USD/CHF Jul 2025 - Sep 2025
0.65
Total Trades 195
Won Trades 106
Lost trades 89
Win Rate 54.36 %
Expected payoff -3.12
Gross Profit 1143.55
Gross Loss -1751.26
Total Net Profit -607.71
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.82
Total Trades 182
Won Trades 108
Lost trades 74
Win Rate 59.34 %
Expected payoff -0.86
Gross Profit 698.06
Gross Loss -855.00
Total Net Profit -156.94
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.73
Total Trades 195
Won Trades 102
Lost trades 93
Win Rate 52.31 %
Expected payoff -1.65
Gross Profit 879.50
Gross Loss -1201.10
Total Net Profit -321.60
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
0.62
Total Trades 136
Won Trades 78
Lost trades 58
Win Rate 57.35 %
Expected payoff -3.07
Gross Profit 676.90
Gross Loss -1094.50
Total Net Profit -417.60
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.45
Total Trades 151
Won Trades 79
Lost trades 72
Win Rate 52.32 %
Expected payoff -4.10
Gross Profit 514.25
Gross Loss -1133.82
Total Net Profit -619.57
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
0.34
Total Trades 176
Won Trades 99
Lost trades 77
Win Rate 56.25 %
Expected payoff -6.47
Gross Profit 586.41
Gross Loss -1725.01
Total Net Profit -1138.60
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.00
Total Trades 21
Won Trades 10
Lost trades 11
Win Rate 47.62 %
Expected payoff -5024.03
Gross Profit 90.00
Gross Loss -105594.60
Total Net Profit -105504.60
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.52
Total Trades 184
Won Trades 92
Lost trades 92
Win Rate 50.00 %
Expected payoff -3.79
Gross Profit 748.40
Gross Loss -1444.90
Total Net Profit -696.50
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
0.93
Total Trades 278
Won Trades 198
Lost trades 80
Win Rate 71.22 %
Expected payoff -0.32
Gross Profit 1199.88
Gross Loss -1288.86
Total Net Profit -88.98
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
0.66
Total Trades 366
Won Trades 201
Lost trades 165
Win Rate 54.92 %
Expected payoff -2.90
Gross Profit 2047.98
Gross Loss -3108.06
Total Net Profit -1060.08
-100%
-50%
0%
50%
100%

Comments