Okay, here's a breakdown of what this MetaTrader script does, explained in a way that doesn't require any programming knowledge:
Overall Goal:
The script attempts to automatically place buy or sell orders on a specific currency pair, based on a calculated range of price movement, and then automatically close those orders under certain conditions. It's designed to work on a repeating schedule.
Here's the process step-by-step:
-
Setup and Configuration: The script starts by reading a set of instructions you provide, like:
MAGICMA
: A unique identification number for orders placed by this script. This helps the script manage its own trades and not interfere with others.lots
: How big each trade should be (the volume/size of the trade).StopLoss
: How far the price can move against the trade before the script automatically closes it to prevent large losses.TakeProfit
: How far the price needs to move in favor of the trade before the script automatically closes it and takes the profit.checkhour
andcheckminute
: The specific time of day (hour and minute) when the script should perform its main calculations and potentially open new trades.days2check
: The number of previous days the script will analyze to estimate the typical price range.checkmode
: Chooses between two methods to calculate average range.profitK
,lossK
,offsetK
: Adjustment numbers that affect the take profit, stop loss, and entry point calculation.
-
Time Check: The script constantly checks the current time. When the current hour and minute match the configured
checkhour
andcheckminute
, it proceeds to the next steps. -
Range Calculation:
- Historical Data: The script looks back at the price movements of the currency pair over the past few days (defined by
days2check
). - Average Range: It calculates an average price range from the historical data. There are two ways to do this based on
checkmode
. It either looks at the difference between the highest and lowest price each day, or it measures the difference in the closing price from one day to the next. It then uses that information to find the typical price movement for that time of day.
- Historical Data: The script looks back at the price movements of the currency pair over the past few days (defined by
-
Price Level Determination:
- Offset: The average range is divided by
offsetK
to create an offset value. - Buy Price: It calculates a "buy price" which is higher than the highest price reached during the specified time yesterday, adding the offset. The expectation is that if the price goes above this level, it may be a good time to buy.
- Sell Price: It calculates a "sell price" which is lower than the lowest price reached during the specified time yesterday, subtracting the offset. The expectation is that if the price goes below this level, it may be a good time to sell.
- Offset: The average range is divided by
-
Order Management (Closing Existing Trades):
- Check for existing orders: Before placing new orders, it checks if the script has any open trades. If it does, it closes them if the
closemode
is set to 2 and the current day is different from the day the trade was originally opened. It also closes orders if the current orders have reached their profit or loss target.
- Check for existing orders: Before placing new orders, it checks if the script has any open trades. If it does, it closes them if the
-
Order Placement:
- Price Check: It checks the current price against the calculated "buy price" and "sell price."
- Buy Order: If the current price is greater than or equal to the "buy price," the script places a buy order. The script uses a adjusted portion of the average range (modified by profitK and lossK) to set the take profit and stop loss levels for this order.
- Sell Order: If the current price is less than or equal to the "sell price," the script places a sell order, again with take profit and stop loss levels.
- Trade Limit: It also checks if it has already placed the maximum number of trades allowed, it will not place more trades.
-
Loop: The script continuously repeats steps 2-6, constantly monitoring the time and price to make decisions.
In simpler terms:
Imagine this script as a robot trader that wakes up at a specific time each day. It looks back at the previous days to get an idea of how much the price usually moves. Based on that, it sets two "tripwires" ? one above the current price and one below. If the price hits either of those tripwires, the robot automatically places a trade in that direction. The robot also has safety settings (stop loss) to prevent big losses and target settings (take profit) to automatically close the trade when it makes a certain amount of money. It also has a maximum number of trades it is willing to place at any one time.
Important Notes:
- This script is based on price range and breakout strategy which may be risky.
- The values for things like
StopLoss
,TakeProfit
, andlots
are crucial and need to be carefully chosen based on your risk tolerance and understanding of the market. - This explanation is a simplified overview and doesn't cover every single detail of the script.
//+------------------------------------------------------------------+
//| 9-0-7-1-2-2-2-1 ttt.mq4 |
//| 9-0-2-1-2-1-2.5 Dan |
//| 9-0-9-2-1-1-1-1 |
//| 9-0-6-1-2-2-2-1(m15) http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Dan"
#property link "http://www.metaquotes.net"
//---- input parameters
extern int MAGICMA = 20050610;
extern double lots=0.1;
extern int StopLoss=200;
extern int TakeProfit=200;
extern int checkhour=8;
extern int checkminute=0;
extern int days2check=7;
extern int checkmode=1;
extern double profitK=2;
extern double lossK=2;
extern double offsetK=2;
extern int closemode=1;
//+------------------------------------------------------------------+
int latestopenhour=23;
int tradesallowed=1;
double sellprice;
double buyprice;
int profit;
int loss;
int offset;
int avrange;
double daj[30];
int i;
int dd;
int bb;
int d;
int pp;
double hh;
double ll;
int p;
double totalrange;
//============================================================
void start() {
if (!IsTradeAllowed()) return;
if (Bars==bb) return;
bb=Bars;
if ( (Hour()==checkhour) && (Minute()==checkminute) ) {
dd=Day();
pp=((checkhour*60)/Period()+checkminute/Period());
hh=High[Highest(NULL,0,MODE_HIGH,pp,1)];
ll=Low[Lowest(NULL,0,MODE_LOW,pp,1)];
p=((24*60)/Period());
totalrange=0;
if(checkmode==1) {
for(i=1;i<=days2check;i++) {
daj[i]=(High[Highest(NULL,0,MODE_HIGH,p,p*i+1)]-Low[Lowest(NULL,0,MODE_LOW,p,p*i+1)]);
totalrange=totalrange+daj[i];
avrange=MathRound((totalrange/i)/Point);
}
}
if(checkmode==2) {
for( i=1;i<=days2check;i++) {
daj[i]=MathAbs(Close[p*i+pp]-Close[p*(i-1)+pp]);
totalrange=totalrange+daj[i];
avrange=MathRound((totalrange/i)/Point);
}
}
offset=MathRound((avrange)/offsetK);
sellprice=ll-offset*Point;
buyprice=hh+offset*Point;
if (CalculateCurrentOrders()>0) {
for(i=0;i<OrdersTotal();i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
if (OrderType()==OP_BUY) {
OrderClose(OrderTicket(),lots,Bid,3,White);
break;
}
if (OrderType()==OP_SELL) {
OrderClose(OrderTicket(),lots,Ask,3,White);
break;
}
}
}
}
if (closemode==2 && Day()!=dd && (CalculateCurrentOrders()>0)) {
for(i=0;i<OrdersTotal();i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
if (OrderType()==OP_BUY) {
OrderClose(OrderTicket(),lots,Bid,3,White);
break;
}
if (OrderType()==OP_SELL) {
OrderClose(OrderTicket(),lots,Ask,3,White);
break;
}
}
}
for(i=0;i<OrdersTotal();i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
if ((OrderType()==OP_BUY) && (((Close[1]-OrderOpenPrice())>=profit*Point) || ((OrderOpenPrice()-Close[1])>=loss*Point))) {
OrderClose(OrderTicket(),lots,Bid,3,White);
break;
}
if ((OrderType()==OP_SELL) && (((Close[1]-OrderOpenPrice())>=loss*Point) || ((OrderOpenPrice()-Close[1])>=profit*Point))) {
OrderClose(OrderTicket(),lots,Ask,3,White);
break;
}
}
int lastopenhour=23;
if ((Hour()<=lastopenhour) && (Day()==dd) && (Day()!=d) && (CalculateCurrentOrders()<tradesallowed)) {
if (Close[1]>=buyprice) {
profit=MathRound((avrange)/profitK);
loss=MathRound((avrange)/lossK);
OrderSend(Symbol(),OP_BUY,lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"ttt",MAGICMA,0,Blue);
dd=Day();
if (tradesallowed==1) {d=Day();}
// return(d);
}
if (Close[1]<=sellprice) {
profit=MathRound((avrange)/profitK);
loss=MathRound((avrange)/lossK);
OrderSend(Symbol(),OP_SELL,lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"ttt",MAGICMA,0,Red);
dd=Day();
if (tradesallowed==1) {d=Day();}
}
return;
}
}
int CalculateCurrentOrders() {
int ord=0;
string symbol=Symbol();
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
ord++;
}
}
return(ord);
}
Comments