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
-
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.
-
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.
- If a Crossover Occurs:
-
-
Looping:
- The
start()
function runs repeatedly as new price data becomes available, constantly monitoring the market and adjusting positions.
- The
/*-----------------------------+
| |
| 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);
}
Comments