This script is designed to automatically trade on the Forex market, based on a set of rules and calculations. Here's a breakdown of what it does, without getting into technical jargon:
1. Initial Setup and User Preferences:
- The script starts by defining some settings that the user can adjust. These settings include:
- MAGICMA: A unique identification number for the script, helping it manage its own trades separately from others.
- lots: The size of each trade (e.g., 0.1 represents a standard lot size).
- StopLoss: How much the price needs to move against the trade before it's automatically closed to limit losses.
- TakeProfit: How much the price needs to move in favor of the trade before it's automatically closed to secure profits.
- checkhour and checkminute: The specific time of day (hour and minute) when the script will perform its analysis and potentially place new trades.
- days2check: How many past days the script should consider when calculating the average price range.
- checkmode: A setting that determines how the average price range is calculated using past days, offering two different methods.
- profitK, lossK, offsetK: These are factors used to adjust the TakeProfit, StopLoss, and price levels at which the script considers placing trades, based on the calculated average price range.
- closemode: A setting that, when enabled, automatically closes trades at the end of the day.
2. Core Logic and Timing:
- The script primarily focuses on trading during a specific time each day, defined by
checkhour
andcheckminute
. - It waits for the specified time, and when that time arrives, it starts calculating the average price movement.
- It uses either
checkmode==1
which calculate the hightest and lowest for the period orcheckmode==2
which calculates the difference between closing prices in that period. - It looks at the price data from the past
days2check
days to determine the typical price range for the currency pair.
3. Calculating Trade Entry Points:
- The script calculates two key price levels:
sellprice
andbuyprice
. - These levels are calculated by taking into account the highest and lowest prices of the current day up to the set time (
checkhour
andcheckminute
), and then adding or subtracting an offset, determined by the average price range andoffsetK
. - This offset creates a buffer zone around the high and low, aiming to avoid entering trades prematurely.
4. Trade Management:
- The script checks if there are any existing trades open with its specific
MAGICMA
number. - If it finds any trades, it will close any existing open orders if the
Hour()
matches the setcheckhour
. - If
closemode
is enabled, it closes all its existing trades at the end of the day before the next day's trading starts. - The script also monitors existing trades for profit and loss levels:
- It checks if the trade has reached either the
TakeProfit
orStopLoss
levels. - If either level is reached, it closes the trade to either secure profits or limit losses.
- It checks if the trade has reached either the
5. Placing New Trades:
- The script checks if the current time is within a certain window (
Hour()<=lastopenhour
) and that there are not already too many trades open. - It checks if the current price has reached the precalculated
buyprice
orsellprice
levels. - If the price reaches
buyprice
, the script will place a "buy" order (betting the price will go up). - If the price reaches
sellprice
, the script will place a "sell" order (betting the price will go down). - When placing these orders, it uses the user-defined
StopLoss
andTakeProfit
levels.
In simple terms: The script waits for a specific time each day, calculates where it thinks the price might move based on past price action, and then places buy or sell orders. It manages those trades by closing them when they reach a certain profit or loss level, or automatically at the end of the day. It acts based on predetermined rules set by the user.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 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