Okay, here's a breakdown of what the MetaTrader script does, explained in a way that someone who doesn't write code can understand:
The script is designed to automatically trade on the Forex market, trying to make a profit by opening and closing buy and sell orders based on certain price movements. It's like a robot trader that follows a set of rules.
Here's the core idea:
The script aims to capitalize on small price fluctuations. It identifies potential "high" and "low" price points (represented by GLBmax
and GLBmin
) and places orders accordingly. Think of it like setting up a system to buy low and sell high.
Here's a step-by-step explanation:
-
Setup & Configuration: The script starts by reading a set of instructions from the user (or from previous settings). These instructions specify things like:
Lots
: The size of each trade (how much money to risk on each trade).maxcount
: The maximum number of open trades the script is allowed to have at any given time.pipstep
: The distance (in pips, a standard Forex unit) the price needs to move away from a high or low point before the script will open a new trade in the opposite direction. Essentially, this is how far the price needs to move before the robot thinks it's found a new opportunity.backstep
: The distance the price needs to move back, after a trade has been opened, to trigger closing of the opposite trades.use_sessionTP
andsessionTP
: Whether to use a profit target, and how much profit (in dollars per lot) the robot should try to achieve before closing all open trades.use_sessionSL
andsessionSL
: Whether to use a stop loss, and how much loss (in dollars per lot) the robot should tolerate before closing all open trades.- Manual Controls: The script also includes manual controls, so that the user can manually control whether the bot executes operations or not.
-
Initialization: When the script starts, it sets initial values for its internal tracking. The most important of these are
GLBmax
andGLBmin
. -
Main Trading Loop: The script then enters a continuous loop (it runs repeatedly). In each iteration of this loop, it does the following:
- Check for User Commands: Checks if the user has told it to:
- Stop: Quit trading entirely.
- Close All: Immediately close all open trades.
- Close Buy/Sell: Close all Buy or Sell orders individually.
- Check for Trading Conditions: If no user commands are active, the script checks if it should open new trades.
- Manage Existing Trades: If there are existing trades open, the script calculates the total profit or loss on those trades. It then checks if it has hit its profit target (
sessionTP
) or its stop loss limit (sessionSL
). If either of these is reached, it closes all open trades. - New Trade Logic:
- The script constantly monitors the price, looking for opportunities to buy or sell.
- The script trades between max and min values that are constantly updated based on changes in the price of the asset.
- When the price goes up after a buy order, max and min are updated, the direction is set as Forward, and the bot is looking to execute sell orders in the same cycle.
- When the price goes down after a sell order, max and min are updated, the direction is set as Forward, and the bot is looking to execute buy orders in the same cycle.
- When the price goes down after a buy order, the direction is set to Backward, it closes all buy orders, opens a sell order, and updates the profit target.
- When the price goes up after a sell order, the direction is set to Backward, it closes all sell orders, opens a buy order, and updates the profit target.
- "Rubber Band" Analogy: The
pipstep
andbackstep
values, along with theGLBmax
andGLBmin
variables, are used to determine when to open new trades. Imagine a rubber band stretched between theGLBmin
andGLBmax
price points. When the current price moves beyond these points, the script assumes the "rubber band" is being stretched too far and will eventually snap back, leading to a trade opportunity.
- Check for User Commands: Checks if the user has told it to:
-
Order Execution: If the script decides to open a trade, it sends a command to the trading platform to either buy or sell the specified currency pair.
-
Information Display: The script displays information on the chart, such as the current settings, the current profit/loss, and any messages or errors.
In simple terms, the script tries to identify overbought or oversold conditions and place trades hoping that the price will revert to the mean. It also has built-in risk management features (profit targets and stop losses) to limit potential losses.
Profitability Reports
//+------------------------------------------------------------------+
//| RUBBERBANDS_3.mq4 |
//| Version 1.0 |
//| Copyright © 2009, SummerSoft Labs |
//| http://www.summersoftlabs.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, SummerSoft Labs"
#property link "http://www.summersoftlabs.com/"
//////////////////////////////////////
extern double Lots = 0.02;
extern int maxcount = 10; // min=3
extern int pipstep = 100;
extern int backstep = 20;
//////////////////////////////////////
extern bool quiescenow = false;
extern bool donow = false;
extern bool stopnow = false;
extern bool closenow = false;
//////////////////////////////////////
extern bool use_sessionTP = true;
extern double sessionTP = 2000; // dollars per lot
extern bool use_sessionSL = true; // false;
extern double sessionSL = 4000; // dollars per lot
//////////////////////////////////////
extern bool useinvalues = false; // set to true on restart
extern double inmax = 0; // set former max on restart
extern double inmin = 0; // set former min on restart
//////////////////////////////////////
//global var's
double GLBmax;
double GLBmin;
bool GLBcloseall;
bool GLBclosebuyall;
bool GLBclosesellall;
bool GLBbuynow;
bool GLBsellnow;
string GLBdirection; // FORWARD or BACKWARD
string GLBbORs; // BUY or SELL (or NULL)
double GLBprofit;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double allprofit()
{
double myprofit=0;
int xtotal=OrdersTotal();
for(int cnt=0;cnt<xtotal;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
myprofit+=OrderProfit();
} //-if
} //-for
return(myprofit);
}
//////////////////////////////////////
double buyprofit()
{
double myprofit=0;
int xtotal=OrdersTotal();
for(int cnt=0;cnt<xtotal;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
myprofit+=OrderProfit();
} //-if
} //-for
return(myprofit);
}
//////////////////////////////////////
double sellprofit()
{
double myprofit=0;
int xtotal=OrdersTotal();
for(int cnt=0;cnt<xtotal;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
myprofit+=OrderProfit();
} //-if
} //-for
return(myprofit);
}
//////////////////////////////////////
int tradeno()
{
int cnt, total, xcnt;
total=OrdersTotal();
if (total==0)
{
return(0);
}
xcnt=0;
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol())
{
xcnt++;
}
} // for
return(xcnt);
}
//////////////////////////////////////
int buytotal()
{
int cnt, total, xcnt;
total=OrdersTotal();
if (total==0)
{
return(0);
}
xcnt=0;
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
xcnt++;
}
} // for
return(xcnt);
}
//////////////////////////////////////
int selltotal()
{
int cnt, total, xcnt;
total=OrdersTotal();
if (total==0)
{
return(0);
}
xcnt=0;
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
xcnt++;
}
} // for
return(xcnt);
}
//////////////////////////////////////
int close1by1()
{
int cnt, total, xcnt;
total=OrdersTotal();
if (total==0)
{
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
else /*OP_SELL*/
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
} // if ordertype
} // for
return(0);
}
//////////////////////////////////////
int closebuy1by1()
{
int cnt, total, xcnt;
total=OrdersTotal();
if (total==0)
{
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
} // if ordertype
} // for
return(0);
}
//////////////////////////////////////
int closesell1by1()
{
int cnt, total, xcnt;
total=OrdersTotal();
if (total==0)
{
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
} // if ordertype
} // for
return(0);
}
//////////////////////////////////////
void comment_stopped()
{
Comment(
"SummerSoft Labs","\n",
"Lots = "+DoubleToStr(Lots,2),"\n",
"maxcount = "+maxcount,"\n",
"pipstep = "+pipstep,"\n",
"backstep = "+backstep,"\n",
"GLBmax = "+DoubleToStr(GLBmax,4),"\n",
"GLBmin = "+DoubleToStr(GLBmin,4),"\n",
"use_sessionTP = "+use_sessionTP,"\n",
"sessionTP = "+DoubleToStr(sessionTP,0),"\n",
"use_sessionSL = "+use_sessionSL,"\n",
"sessionSL = "+DoubleToStr(sessionSL,0),"\n","\n",
"STOPPED BY USER"
);
}
////////////////////////////////////////////////////////////////////////////////////
void comment_quiesced()
{
Comment(
"SummerSoft Labs","\n",
"Lots = "+DoubleToStr(Lots,2),"\n",
"maxcount = "+maxcount,"\n",
"pipstep = "+pipstep,"\n",
"backstep = "+backstep,"\n",
"GLBmax = "+DoubleToStr(GLBmax,4),"\n",
"GLBmin = "+DoubleToStr(GLBmin,4),"\n",
"use_sessionTP = "+use_sessionTP,"\n",
"sessionTP = "+DoubleToStr(sessionTP,0),"\n",
"use_sessionSL = "+use_sessionSL,"\n",
"sessionSL = "+DoubleToStr(sessionSL,0),"\n","\n",
"QUIESCED BY USER"
);
}
//////////////////////////////////////
void comment_null()
{
Comment(
"SummerSoft Labs","\n",
"Lots = "+DoubleToStr(Lots,2),"\n",
"maxcount = "+maxcount,"\n",
"pipstep = "+pipstep,"\n",
"backstep = "+backstep,"\n",
"GLBmax = "+DoubleToStr(GLBmax,4),"\n",
"GLBmin = "+DoubleToStr(GLBmin,4),"\n",
"use_sessionTP = "+use_sessionTP,"\n",
"sessionTP = "+DoubleToStr(sessionTP,0),"\n",
"use_sessionSL = "+use_sessionSL,"\n",
"sessionSL = "+DoubleToStr(sessionSL,0),"\n","\n",
"RUNNING"
);
}
//////////////////////////////////////
void comment_pl(string bs, double pl, int total)
{
Comment(
"SummerSoft Labs","\n",
"Lots = "+DoubleToStr(Lots,2),"\n",
"maxcount = "+maxcount,"\n",
"pipstep = "+pipstep,"\n",
"backstep = "+backstep,"\n",
"GLBmax = "+DoubleToStr(GLBmax,4),"\n",
"GLBmin = "+DoubleToStr(GLBmin,4),"\n",
"total = "+total,"\n",
"use_sessionTP = "+use_sessionTP,"\n",
"sessionTP = "+DoubleToStr(sessionTP,0),"\n",
"use_sessionSL = "+use_sessionSL,"\n",
"sessionSL = "+DoubleToStr(sessionSL,0),"\n","\n",
"GLBdirection = "+GLBdirection,"\n",
"GLBbORs = "+GLBbORs,"\n",
"GLBprofit = "+DoubleToStr(GLBprofit,2),"\n",
bs,"\n",
"Profit/Loss = "+DoubleToStr(pl,2)
);
}
///////////////////////////////////////////////////////////////
int init()
{
if (useinvalues==true)
{
GLBmax=inmax;
GLBmin=inmin;
}
else
{
GLBmax=Ask;
GLBmin=Ask;
}
GLBcloseall=closenow;
GLBbuynow=donow;
GLBsellnow=donow;
GLBdirection="Forward";
GLBbORs="";
GLBprofit=0;
return(0);
}
/////////////////////////////////////////////////////////////////////////
int start()
{
int cnt, ticket, total;
bool closenow, buynow, sellnow;
// STOP NOW?
if (stopnow==true)
{
comment_stopped();
return(0);
}
comment_null();
total=tradeno();
if(total==0 && quiescenow==true)
{
comment_quiesced();
return(0);
}
// CLOSE BUY NOW?
// if true, close buy orders one by one
if (GLBclosebuyall==true && buytotal()>0)
{
closebuy1by1();
return(0);
}
if (GLBclosebuyall==true && buytotal()==0)
{
GLBclosebuyall=false;
}
// CLOSE SELL NOW?
// if true, close buy orders one by one
if (GLBclosesellall==true && selltotal()>0)
{
closesell1by1();
return(0);
}
if (GLBclosesellall==true && selltotal()==0)
{
GLBclosesellall=false;
}
// CLOSE ALL NOW to end session?
// if true, close one by one
if (GLBcloseall==true && total>0)
{
close1by1();
return(0);
}
if (GLBcloseall==true && total==0)
{
GLBcloseall=false;
GLBmax=Ask;
GLBmin=Ask;
GLBbuynow=false;
GLBsellnow=false;
GLBdirection="Forward";
GLBbORs="";
GLBprofit=0;
}
// check free margin
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// BUY NOW?
if (GLBbuynow==true)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"RUBBERBANDS_3",10000,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else
{
Print("Error opening BUY order : ",GetLastError());
return(-1);
}
GLBbuynow=false;
return(0);
}
// SELL NOW?
if (GLBsellnow==true)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"RUBBERBANDS_3",20000,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else
{
Print("Error opening SELL order : ",GetLastError());
return(-1);
}
GLBsellnow=false;
return(0);
}
// new order from 0 orders
if (total==0 && Seconds()==0)
{
GLBbuynow=true;
GLBsellnow=true;
return(0);
}
///////////////////////////////////////////////////////////////////////////////////////
// counter if you will
if (total>0)
{
// what's the profit made
double myprofit=allprofit();
comment_pl("myprofit",myprofit,total);
// close all trades for profit made?
if (use_sessionTP==true
&& GLBprofit+myprofit>=sessionTP*Lots)
{
GLBcloseall=true;
}
// close all trades for loss cut?
if (use_sessionSL==true
&& GLBdirection=="Backward"
&& myprofit<=(-1)*sessionSL*Lots)
{
GLBcloseall=true;
}
if (total>=maxcount)
{
return(0);
}
if (GLBdirection=="Backward")
{
return(0);
}
// do we buy or sell now?
// BUY
if ((GLBbORs=="" || GLBbORs=="BUY") && Ask>=GLBmax+pipstep*Point)
{
GLBmax=Ask;
GLBbuynow =true;
GLBbORs="BUY";
return(0);
}
if (GLBbORs=="BUY" && Ask<=GLBmax-backstep*Point)
{
GLBclosebuyall=true;
GLBprofit=GLBprofit+buyprofit();
GLBsellnow=true;
GLBdirection="Backward";
return(0);
}
// SELL
if ((GLBbORs=="" || GLBbORs=="SELL") && Ask<=GLBmin-pipstep*Point)
{
GLBmin=Ask;
GLBsellnow=true;
GLBbORs="SELL";
return(0);
}
if (GLBbORs=="SELL" && Ask>=GLBmin+backstep*Point)
{
GLBclosesellall=true;
GLBprofit=GLBprofit+sellprofit();
GLBbuynow=true;
GLBdirection="Backward";
return(0);
}
} // if total
return(0);
} //-start
///////////////////////////////////////////////////////////////////////////////////////
int deinit()
{
return(0);
}
///////////////////////////////////////////////////////////////////////////////////////
// the end.
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---