Here's a breakdown of what this trading script does, explained in a way that avoids technical jargon:
Overall Goal:
This script aims to automatically buy and sell currencies (or other assets) based on signals from a "ZigAndZag" indicator. It's designed to open a single trade at a time, and then close that trade when it receives another signal.
Here's the step-by-step logic:
-
Setup:
- The script starts by taking in some settings that you, the user, can adjust. These include:
- Lots: How much of the currency to buy or sell in each trade.
- ZZbar: Specifies which past bar, relative to the current one, the "ZigAndZag" indicator uses to generate buy/sell signals.
- Closebar: Specifies which past bar, relative to the current one, the "ZigAndZag" indicator uses to generate close signals.
- Maxord: (Currently set to 1) This limits the script to having only one open trade at any given time.
- Sl (Stop Loss): The number of pips (a standard unit of measure in currency trading) away from the opening price that the trade will automatically close to limit losses if the price moves against the trade.
- Tp (Take Profit): The number of pips away from the opening price that the trade will automatically close to secure profits if the price moves in favor of the trade.
- Magic: A unique identifying number assigned to the trades opened by this script. This helps the script manage its own trades and not interfere with others.
- The script starts by taking in some settings that you, the user, can adjust. These include:
-
Waiting for a New Bar:
- The script constantly checks if a new price bar has formed. It only acts when a new bar appears. This prevents the script from repeatedly executing the same actions on the same price data.
-
Checking for Signals:
- The script uses a custom indicator called "ZigAndZag" to generate trading signals. It looks at specific bars based on the
ZZbar
andClosebar
settings to identify buy, sell, and close signals.- It checks three signals from the ZigAndZag indicator
- If the buy signal is active it sets buy = true
- If the sell signal is active it sets sell = true
- if the close signal is active it sets close = true
- The script uses a custom indicator called "ZigAndZag" to generate trading signals. It looks at specific bars based on the
-
Opening a Trade:
- If a "buy" signal is active and there are no trades currently open (OrdersTotal()<Maxord), the script opens a "buy" trade.
- If a "sell" signal is active and there are no trades currently open, the script opens a "sell" trade.
- When opening a trade, the script also sets:
- Stop Loss (Sl): Automatically closes the trade to prevent a large loss.
- Take Profit (Tp): Automatically closes the trade to secure a profit.
- The amount to trade (Lots).
- The script also checks to make sure the market allows trading, and waits if it doesn't.
-
Closing a Trade:
- If a "close" signal is active and there is an open trade, the script closes all trades opened by this script (identified by the "magic" number). It closes all trades to prevent potential loses from an unwanted trade
- The script also checks to make sure the market allows trading, and waits if it doesn't.
In simple terms:
The script waits for a specific indicator ("ZigAndZag") to tell it to either buy or sell. When it gets a signal, it opens a trade. When the indicator tells it to close, it closes the trade. It does this automatically, aiming to profit from price movements identified by the "ZigAndZag" indicator. The stop loss and take profit settings are like safety nets, automatically closing the trade to limit losses or secure gains.
//+------------------------------------------------------------------+
//| ZigAndZag_trader.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//---- input parameters
extern double Lots=0.1;//òîðãóåìûé ëîò
extern int ZZbar=3;//áàðîâ íàçàä ñ êîòîðîãî áåðåì ñèãíàë ê òîðãîâëå
extern int Closebar=3;//áàðîâ íàçàä ñ êîòîðîãî áåðåì ñèãíàë ê çàêðûòèþ
extern int Maxord=1;//êîëè÷åñòâî òîðãóåìûõ îðäåðîâ(ïîêà îñòàâèòü 1)âîìîæíî áóäåò ìóëüòèîðäåðíàÿ ñèñòåìà
extern int Sl=0;//ñòîïëîñ ïîäáèðàåòñÿ ïðè îïòèìèçàöèè
extern int Tp=0;//òåéê ïîäáèðàåòñÿ ïðè îïòèìèçàöèè
extern int magic=78977;//ìàãèê
//-----------------------
static int prevtime = 0 ;
bool buy,sell,close,UseSound=false;
//+------------------------------------------------------------------+
int start()
{
// Æäåì, êîãäà ñôîðìèðóåòñÿ íîâûé áàð
if (Time[0] == prevtime) return(0);
prevtime = Time[0];
buy=false;sell=false;
//-----------
if(iCustom(NULL,0,"ZigAndZag",4,Closebar)!=0){close=true;}
if(iCustom(NULL,0,"ZigAndZag",5,ZZbar)!=0){buy=true;}
if(iCustom(NULL,0,"ZigAndZag",6,ZZbar)!=0){sell=true;}
//Comment(close+"\n"+buy+"\n"+sell+"\n"+OrdersTotal());
//------------
if(buy&&OrdersTotal()<Maxord){open(false,Sl,Tp,Lots);buy=false; }
if(sell&&OrdersTotal()<Maxord){open(true,Sl,Tp,Lots);sell=false; }
//-------------------------------
if(close&&OrdersTotal()>0){
for(int i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
del(OrderTicket());
close=false;
}}}
//----
return(0);
}
//+------------------------------------------------------------------+
//--------Ôóíêöèÿ îòêðûòèÿ îðäåðîâ-------------------------------------+
int open(bool tip,int Sl,int Tp,double lots)
{//tip = false => OP_BUYSTOP ; tip = true => OP_SELLSTOP;
GetLastError();
int err;
double lastprise,prise,sl,tp; // ñàìàÿ ñâåæàÿ öåíà
int ticket;
int slip =(MarketInfo(Symbol(),MODE_SPREAD))*Point;//ìàêñ îòêëîíåíèå = ñïðåäó
//------
while (!IsTradeAllowed()){ Sleep(5000);}// åñëè ðûíîê çàíÿò òî ïîäîæäåì 5 ñåê
if (tip == false)
{
prise = NormalizeDouble(MarketInfo(Symbol(),MODE_ASK),Digits);
if(Sl!=0){sl = NormalizeDouble((MarketInfo(Symbol(),MODE_BID)-(Sl*Point)),Digits);}else{sl=0;}
if(Tp!=0){tp = NormalizeDouble((MarketInfo(Symbol(),MODE_ASK)+(Tp*Point)),Digits);}else{tp=0;}
for(int i=0;i<5;i++)
{
RefreshRates();// îáíîâèì öåíó
ticket = OrderSend(Symbol(), OP_BUY,lots ,prise, slip,sl,tp,NULL,magic,0, Blue);
if (ticket < 0)
{
if(UseSound){PlaySound("timeout.wav");}
Print("Öåíà ñëèøêîì áëèçêî!",prise," ",sl," ",tp," Íå ìîãó ïîñòàâèòü îðäåð BUY!");
}
else
{
break;
}
}
}
if(tip==true)
{
prise = NormalizeDouble(MarketInfo(Symbol(),MODE_BID),Digits);
if(Sl!=0){sl = NormalizeDouble((MarketInfo(Symbol(),MODE_ASK)+(Sl*Point)),Digits);}else{sl=0;}
if(Tp!=0){tp = NormalizeDouble((MarketInfo(Symbol(),MODE_BID)-(Tp*Point)),Digits);}else{tp=0;}
for( i=0;i<5;i++)
{
RefreshRates();// îáíîâèì öåíó
ticket = OrderSend(Symbol(), OP_SELL, lots ,prise, slip,sl,tp,NULL,magic,0, Red);
if (ticket < 0)
{
if(UseSound){PlaySound("timeout.wav");}
Print("Öåíà ñëèøêîì áëèçêî!",prise," ",sl," ",tp," Íå ìîãó ïîñòàâèòü îðäåð SELL!");
}
else
{
break;
}
}
}
return(ticket);
}
//-------------------------------------------------------------------+
int del(int ticket)
{
int err;
GetLastError();//îáíóëÿåì îøèêó
OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
string symbol = OrderSymbol();
if(OrderType()==OP_BUY)
{
RefreshRates();
double prise = MarketInfo(symbol,MODE_BID);
OrderClose(ticket,OrderLots(),prise,3,Green);
err = GetLastError();
}
if(OrderType()==OP_SELL)
{
RefreshRates();
prise = MarketInfo(symbol,MODE_ASK);
OrderClose(ticket,OrderLots(),prise,3,Green);
err = GetLastError();
}
if (err == 0&&UseSound){PlaySound("expert.wav");} if (err != 0) {PlaySound("timeout.wav");Print(err);}
while (!IsTradeAllowed()){ Sleep(5000);}// åñëè ðûíîê çàíÿò òî ïîäîæäåì 5 ñåê
return(err);
}
Comments