This script is designed to automatically trade on the market based on signals generated by the "ZigZag" indicator. Here's how it works in simple terms:
-
Checks Trade Permissions: First, the script verifies if trading is allowed by the broker. If not, it pauses and checks again later.
-
Avoids Duplication: It also ensures that the script doesn't execute the same trading logic multiple times for the same price bar or candle, which could lead to unintended multiple trades.
-
Identifies ZigZag Points: The script then uses the "ZigZag" indicator to find significant highs and lows on the price chart. This indicator essentially highlights the turning points in the price movement. It looks back at recent price data to locate these points.
-
Determines Signal: Based on the last calculated ZigZag point the script determines the signal. If the last ZigZag point detected is a high, it interprets this as a potential sell signal. Conversely, if it detects a low, it interprets it as a potential buy signal.
-
Checks Existing Orders: The script then examines if there are any existing open orders (trades) associated with it (using a unique identifier). It compares the direction of the existing order with the current signal from the ZigZag indicator.
-
Closes Conflicting Orders: If an order exists and its direction (buy or sell) is opposite to the current signal, the script closes that existing order. For example, if the script has a buy order open, and the ZigZag indicator is now signaling a sell, the buy order will be closed.
-
Places New Orders: If the detected ZigZag point is within a certain period the script will place an order.If there are no open orders, or an existing order was just closed, the script places a new order based on the ZigZag signal. A buy order is placed if the ZigZag indicator signaled a low (potential upward movement), and a sell order is placed if the ZigZag indicator signaled a high (potential downward movement).
In summary, this script watches for turning points identified by the ZigZag indicator, closes conflicting trades, and opens new trades in the direction suggested by those turning points. The script's goal is to automatically capitalize on potential price swings based on the ZigZag pattern.
//+------------------------------------------------------------------+
//| ZigZagEvgeTrofi 1.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Trofimov Evgeniy"
#property link "http://www.fracpar.narod.ru/"
#define MAGICMA 20080919
#define Slippage 5
//---- indicator parameters
extern int ExtDepth=17; //10-21 !15 !17
extern int ExtDeviation=7; //!7
extern int ExtBackstep=5; //!5
extern double Lot=0.10;
//----
static int prevtime = 0;
int Urgency=2;
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int i, candle;
double ZigZag;
int CurrentCondition, Signal;
if (! IsTradeAllowed()) {
prevtime = Time[1];
MathSrand(TimeCurrent());
Sleep(30000 + MathRand());
return(0);
}
if (Time[0] == prevtime) return(0);
prevtime = Time[0];
while(candle<100)
{
ZigZag=iCustom(NULL,0,"ZigZag", ExtDepth,ExtDeviation,ExtBackstep, 0,candle);
if(ZigZag!=0) break;
candle++;
}
if(candle>99) return(0);
if(ZigZag==High[candle])
Signal=1;
else if(ZigZag==Low[candle])
Signal=2;
int total = OrdersTotal();
for (i = 0; i < total; i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICMA) {
CurrentCondition=OrderType()+1;
break;
}
}
if(Signal==CurrentCondition) return(0);
if(Signal!=CurrentCondition && CurrentCondition>0)
{
if(CurrentCondition==1)
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage);
else
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage);
Sleep(20000);
RefreshRates();
}
if(candle<=Urgency)
{
if(Signal==1)
OrderSend(Symbol(), OP_BUY, Lot, Ask, Slippage, 0, 0, "ZigZag", MAGICMA, 0, Blue);
else
OrderSend(Symbol(), OP_SELL, Lot, Bid, Slippage, 0, 0, "ZigZag", MAGICMA, 0, Red);
Sleep(20000);
prevtime = Time[1];
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments