This script is designed to automatically trade on a currency market within a specific timeframe, based on price movements relative to a calculated channel. Here's a breakdown of its logic:
-
Initialization: When the script starts, it gathers basic market information like the spread (difference between buying and selling price), the minimum distance to place a stop-loss order (stop level), and the freeze level (minimum price change required to execute an order). It also takes input parameters from the user like the start and end time of trading, the channel period and the stop loss.
-
Time-Based Trading: The script is designed to only trade during a specific window of time each day. It determines this window using the "start" and "end" parameters. It checks if the current time falls within this window, and a "work" variable is set according to that which allow the trading. The script will not trade outside of this window. It also handles special case that increase the time trading frame to next day after Friday to account for weekends.
-
Channel Calculation: The script continuously monitors the price and identifies the highest high and the lowest low price over a specified number of past periods (defined by the "period" parameter). This range defines a channel where the price moves in.
-
Order Execution:
- If the current buying price (Bid) goes above the highest high of the channel, the script closes all existing buy orders and places a new sell order.
- Conversely, if the Bid goes below the lowest low of the channel, the script closes all existing sell orders and places a new buy order.
-
Stop-Loss Management: A stop-loss level is dynamically calculated as a percentage (defined by the "SLpp" parameter) of the channel's width. This value is used when opening an order, and has to be at least the minimum stop level allowed by the broker. This helps to limit potential losses.
-
Order Closing: The script has a function to close all open orders of a specific type. It loops through all existing orders and, based on whether the current action is a "sell" or "buy" signal, it closes the opposite type of order.
In essence, this script tries to capitalize on price breakouts from a defined channel during a specified time window. It identifies potential overbought or oversold conditions and automatically executes trades in the opposite direction, with a stop-loss to manage risk.
Profitability Reports
//+------------------------------------------------------------------+
//| MC.mq4 |
//| space_cowboy |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "space_cowboy"
#property link "http://www.metaquotes.net"
//---- input parameters
extern int start=21;//÷àñ íà÷àëà ðàáîòû
extern int end=9;//÷àñ îêîí÷àíèÿ ðàáîòû (ïëþñ ê íà÷àëüíîìó)
extern int period=36; //ïåðèîä äëÿ ïîñòîðîåíèÿ êàíàëà â áàðàõ
extern double SLpp=300; //ñòîï â ïðîöåíòàõ îò ðàçìàõà êàíàëà
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int spread, stoplevel, freeze;
int handle;
int init()
{
//----
stoplevel=MarketInfo(Symbol(), MODE_STOPLEVEL)+1;
spread=MarketInfo(Symbol(), MODE_SPREAD);
freeze=MarketInfo(Symbol(), MODE_FREEZELEVEL)+1;
SLpp/=100;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int tmp, tc, st, et, dtmp;
int objs=0;
double high, low, raz;
bool op=false, work=false;
int start()
{
//----
tc=TimeCurrent();
//***********************
int d1bars=iBars(NULL,PERIOD_D1);
if(tc>=et)
{
if(dtmp!=d1bars)
{
int dow=0;
if(start+end > 24) {dow=DayOfWeek();}
int dt=iTime(NULL,PERIOD_D1,0);
st=dt+start*3600;
et=dt+(start+end)*3600;
if(dow==5) et+=172800;
}
dtmp=d1bars;
}
if(tc>=st && tc<=et) work=true;
else work=false;
//***********************
if(tmp!=Bars)
{
high=High[iHighest(NULL,0,MODE_HIGH,period,1)];
low=Low[iLowest(NULL,0,MODE_LOW,period,1)];
raz=((high-low)/Point)*SLpp;
if(raz<stoplevel) raz=stoplevel;
}
tmp=Bars;
if(Bid>=high)
{
close_all(true);
if(work && OrdersTotal()==0) sell();
}
if(Bid<=low)
{
close_all(false);
if(work && OrdersTotal()==0) buy();
}
//----
return(0);
}
//+------------------------------------------------------------------+
int sell()
{
int t=-1;
t=OrderSend(Symbol(),OP_SELL,1,Bid,0,NormalizeDouble(Ask+raz*Point, Digits),0,"order_sell",29072007,0,0x0000FF);
return(t);
}
int buy()
{
int t=-1;
t=OrderSend(Symbol(),OP_BUY,1,Ask,0,NormalizeDouble(Bid-raz*Point, Digits),0,"order_buy",19072007,0,0xFF0000);
return(t);
}
void close_all(bool fl)
{
int tot=OrdersTotal();
for(int i=0;i<tot;i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderType()==0 && fl) OrderClose(OrderTicket(),OrderLots(),Bid,0);
if(OrderType()==1 && !fl) OrderClose(OrderTicket(),OrderLots(),Ask,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
---