This script is an automated trading program designed to work with the MetaTrader platform. It attempts to automatically place buy and sell orders based on the Bollinguer Bands indicator on a 15 minute interval. Here's a breakdown of what it does:
-
Bollinger Band Calculation: The script uses the Bollinger Bands indicator, which consists of three lines: an upper band, a middle line (simple moving average), and a lower band. These bands are calculated based on the past 20 periods and how spread out the price is from the average(deviation of 2).
-
Price Monitoring: The script constantly monitors the current price of a currency pair.
-
Closing Existing Positions: The script checks whether to close existing buy or sell positions. If the current price goes above the middle line, it will close all buy orders. If the current price goes below the middle line, it will close all sell orders. This is a basic strategy to exit trades when the price moves toward the average.
-
**New Candle Check:**The script checks if a new 15-minute candle has formed. This is determined by checking the volume of transactions.
-
Managing Pending Orders: If there are existing pending orders (Buy Limit or Sell Limit) when a new candle starts, the script cancels all of them.
-
Placing Orders: When a new candle is detected, the script places new pending orders:
- Buy Limit Order: It places a "Buy Limit" order, which is an order to buy if the price drops to the lower Bollinger Band.
- Sell Limit Order: It places a "Sell Limit" order, which is an order to sell if the price rises to the upper Bollinger Band.
-
Error Handling: If the script encounters problems placing or closing orders, it prints an error message.
In simple terms, the script watches the price movements and attempts to "buy low" near the lower Bollinger Band and "sell high" near the upper Bollinger Band, and will close current open buy/sell positions when price hits the middle Bollinger band. It repeats this process with each new 15-minute candle.
//+------------------------------------------------------------------+
//| bb-automated.mq4 |
//| Copyright 2017, Mohammad Soubra |
//| https://www.mql5.com/en/users/soubra2003/seller |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Mohammad Soubra"
#property link "https://www.mql5.com/en/users/soubra2003/seller"
#property version "1.00"
#property strict
#include <stdlib.mqh>
input int BB_period = 20;
input double BB_dev = 2;
double BB_UPPER, BB_SMA, BB_LOWER;
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
HideTestIndicators(true);
//This function sets a flag hiding indicators called by the Expert Advisor
BB_UPPER = iBands(NULL ,PERIOD_M15 ,BB_period ,BB_dev ,0 ,PRICE_CLOSE ,MODE_UPPER ,0);
BB_SMA = iBands(NULL ,PERIOD_M15 ,BB_period ,BB_dev ,0 ,PRICE_CLOSE ,MODE_SMA ,0);
BB_LOWER = iBands(NULL ,PERIOD_M15 ,BB_period ,BB_dev ,0 ,PRICE_CLOSE ,MODE_LOWER ,0);
//Close executed Buy/Sell when touch the center MA
if(Close[0]>=BB_SMA)
CloseAllBuy();
else if(Close[0]<=BB_SMA)
CloseAllSell();
if(iVolume(NULL,PERIOD_M15,0)==1)
//New candle
{
if(OrdersTotal()>0) CloseAllPending();
//---
BuyLimitExecute();
SellLimitExecute();
}
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
Comment("");
}
//+------------------------------------------------------------------+
//| Expert BuyLimitExecute function
//+------------------------------------------------------------------+
void BuyLimitExecute()
{
int buylimit= OrderSend(OrderSymbol(), //Pair
/*DOWN*/ OP_BUYLIMIT, //Command Type
0.01, //Lot Size
BB_LOWER, //Needed Price
3, //Max. Slippage
0, //Stop Loss
0, //Take Profit
"BB Automated", //Comment
1221, //Magic No.
0, //Expiration (Only Pending Orders)
clrNONE); //Arrow Color
if(buylimit>0)
Print("Buy-Limit order successfully placed.");
else
Print("Error in placing buy-limit order: ", ErrorDescription(GetLastError()));
}
//+------------------------------------------------------------------+
//| Expert SellLimitExecute function
//+------------------------------------------------------------------+
void SellLimitExecute()
{
int selllimit = OrderSend(OrderSymbol(), //Pair
/*UP*/ OP_SELLLIMIT, //Command Type
0.01, //Lot Size
BB_UPPER, //Needed Price
3, //Max. Slippage
0, //Stop Loss
0, //Take Profit
"BB Automated", //Comment
1221, //Magic No.
0, //Expiration (Only Pending Orders)
clrNONE); //Arrow Color
if(selllimit>0)
Print("Sell-Limit order successfully placed.");
else
Print("Error in placing sell-limit order: ", ErrorDescription(GetLastError()));
}
//+------------------------------------------------------------------+
//| CLOSE ALL OPENED BUY
//+------------------------------------------------------------------+
void CloseAllBuy()
{
int total = OrdersTotal();
for(int i=total-1; i>=0; i--)
{
int ticket = OrderSelect(i,SELECT_BY_POS);
int type = OrderType();
bool result = false;
if( OrderMagicNumber() == 1221 )
{
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),6,clrNONE);
}
if(!result)
{
Alert("Order ",OrderTicket()," failed to close. Error: ",ErrorDescription(GetLastError()));
Sleep(750);
}
}
}
}
//+------------------------------------------------------------------+
//| CLOSE ALL OPENED SELL
//+------------------------------------------------------------------+
void CloseAllSell()
{
int total = OrdersTotal();
for(int i=total-1; i>=0; i--)
{
int ticket = OrderSelect(i,SELECT_BY_POS);
int type = OrderType();
bool result = false;
if( OrderMagicNumber() == 1221 )
{
switch(type)
{
//Close opened short positions
case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),6,clrNONE);
}
if(!result)
{
Alert("Order ",OrderTicket()," failed to close. Error: ",ErrorDescription(GetLastError()));
Sleep(750);
}
}
}
}
//+------------------------------------------------------------------+
//| CLOSE ALL PENDING POSITIONS
//+------------------------------------------------------------------+
void CloseAllPending()
{
int total = OrdersTotal();
for(int i=total-1; i>=0; i--)
{
int ticket = OrderSelect(i,SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
case OP_BUYLIMIT : result = OrderDelete(OrderTicket(),clrNONE); break;
case OP_SELLLIMIT : result = OrderDelete(OrderTicket(),clrNONE);
}
if(result==false)
{
Comment("Order ",OrderTicket()," failed to close. Error: ",ErrorDescription(GetLastError()));
Sleep(750);
}
}
}
//+------------------------------------------------------------------+
Comments