Here's a breakdown of what this MetaTrader script does, explained in a way that someone without programming knowledge can understand:
Overall Purpose:
This script is designed to automatically place and manage pending trades on a currency pair at specific times of the day and then close any open positions at another specific time. It's essentially a simple automated trading strategy.
Key Components and How They Work:
-
Settings (External Variables):
- Lots: This determines the size of the trades the script will place. Think of it as how much of the currency you are buying or selling.
- ChasStart: This is the hour of the day when the script will start placing pending orders.
- ChasStop: This is the hour of the day when the script will close any open positions and delete any pending orders.
- Step: This value determines how far away from the current price the pending orders will be placed.
- TP: This sets the desired profit level for the trades.
-
Initialization (init() function):
- This part of the script runs once when the script is first loaded onto the chart. In this case, it doesn't actually do anything.
-
Deinitialization (deinit() function):
- This part of the script runs when the script is removed from the chart or the trading platform closes. Like the
init()
function, it also doesn't do anything in this script.
- This part of the script runs when the script is removed from the chart or the trading platform closes. Like the
-
Main Execution Loop (start() function): This is where the "brain" of the script resides. It runs repeatedly, checking conditions and taking actions.
-
Order Management (Early Section):
- It checks if there is one pending order with the script's unique identifier (
12321
). If it finds one, it deletes all other pending orders with that same identifier. This is likely a mechanism to ensure only one set of orders is active at a time.
- It checks if there is one pending order with the script's unique identifier (
-
Time Check (Time Logic):
- The script keeps track of the last time it ran. If the current time is the same as the last time it ran, it does nothing. This prevents the script from executing the same actions multiple times per minute.
- It also checks if trading is allowed. If not, it skips the trading logic.
-
Order Placement (Buy/Sell Stop Orders):
- If the current hour matches the
ChasStart
time and the minute is exactly zero, the script places two pending orders:- A Buy Stop order is placed slightly above the current market price (
Ask
). This means the order will only activate if the price rises to that level. TheStep
variable determines the distance above the current price where this order is placed. TheTP
variable then sets the target profit for that order. - A Sell Stop order is placed slightly below the current market price (
Bid
). This means the order will only activate if the price falls to that level. Again, theStep
variable determines the distance below the current price. AndTP
set the target profit for that order.
- A Buy Stop order is placed slightly above the current market price (
- These "stop" orders are designed to enter the market if the price starts moving strongly in either direction.
- If the current hour matches the
-
Position Closure (Closing Open Trades and Deleting Pending Orders):
- If the current hour matches the
ChasStop
time and the minute is zero, the script will:- Close any open trades that were placed by this script (identified by the unique number
12321
). - Delete any pending orders placed by this script.
- Close any open trades that were placed by this script (identified by the unique number
- If the current hour matches the
-
In essence, this script is a simple automated system that tries to catch potential price breakouts during a specific time window and then closes everything down at another specified time.
Profitability Reports
//+------------------------------------------------------------------+
//| 21hour.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern double Lots = 0.1;
extern int ChasStart = 10;
extern int ChasStop = 22;
extern int Step = 15;
extern int TP = 200;
int prevtime;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int OrderCountOtl=0;
int i=0;
int total = OrdersTotal();
for(i = 0; i <= total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == 12321)
{
if (OrderType()>1) OrderCountOtl++;
}
}
if (OrderCountOtl==1)
{
for(i = 0; i <= total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == 12321)
{
if (OrderType()>1) OrderDelete(OrderTicket());
}
}
}
if(Time[0] == prevtime)
return(0);
prevtime = Time[0];
if(!IsTradeAllowed())
{
prevtime = Time[1];
return(0);
}
if (TimeHour(TimeCurrent())==ChasStart && TimeMinute(TimeCurrent())==0)
{
OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Step*Point,3,0,Ask+(Step+TP)*Point,"",12321,0,Green);
OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Step*Point,3,0,Bid-(Step+TP)*Point,"",12321,0,Red);
}
if (TimeHour(TimeCurrent())==ChasStop && TimeMinute(TimeCurrent())==0)
{
i=0;
total = OrdersTotal();
for(i = 0; i <= total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == 12321)
{
if (OrderType()==OP_BUY)OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
if (OrderType()==OP_SELL)OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
if (OrderType()>1)OrderDelete(OrderTicket());
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
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
---