nemox-0001_133
Download scriptThis script is an automated trading tool designed to execute trades based on specific price patterns and profit targets. Below is a detailed breakdown of its operational logic:
Core Operational Logic
The script monitors the market for specific "Fractal" price points—these are peaks and valleys in the price action that indicate potential reversal points. When these points are identified, the script follows a structured process of risk management, trade execution, and automated profit harvesting.
Key Features and Workflows
1. Profit Target & Auto-Closure
The system continuously calculates the total profit of all currently open positions.
- Global Take Profit: If the combined profit of all active trades exceeds a user-defined threshold (ProfitLevel), the script automatically triggers a "Close All" command.
- Execution Delay: After closing all positions, the script pauses for a short duration before allowing new trades to be opened, ensuring the market stabilizes.
2. Safety and Risk Management
- Margin Check: Before opening any new trade, the script verifies that the account has sufficient free margin. If funds are insufficient, it will issue an alert and halt trading activity.
- Stop Loss & Take Profit: Every trade is automatically equipped with a specific Stop Loss (to limit losses) and Take Profit (to secure gains) based on user-defined settings.
- Time Restrictions: The script includes a hardcoded date check; it is programmed not to run before July 2005.
3. Trade Execution Strategy
The script identifies two specific market conditions to initiate trades:
- Bearish Sentiment (Sell Signal): When a new high price peak (Upper Fractal) is detected, the system initiates a multi-step sell sequence. It first places a "Stop" order at a specific distance above the peak and then follows up with a primary Sell position.
- Bullish Sentiment (Buy Signal): When a new low price valley (Lower Fractal) is detected, the system initiates a multi-step buy sequence. It first places a "Stop" order at a specific distance below the valley and then follows up with a primary Buy position.
4. Technical Timing
To prevent the script from spamming orders or crashing during high volatility, it employs several timing mechanisms:
- Cooldown Periods: It enforces a small waiting window between individual order attempts.
- Sequence Delays: There is a specific 10-second delay between the placement of the "Stop" order and the primary trade to ensure the correct market conditions are met.
Summary for the User
In essence, this script acts as an automated "reversal" trader. It looks for price extremes (fractals), sets up a two-step entry process (a stop order followed by a market order), manages your risk automatically via fixed stops and profits, and has a built-in "emergency exit" that closes everything once your total profit reaches a desired level.
//+------------------------------------------------------------------+
//| ShadowTrader |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//| v1.2 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern double Lots = 0.1;
extern int TakeProfit = 500;
extern int StopLoss = 500;
extern int limit = 5; // Óðîâåíü óñòàíîâêè îòëîæåííîãî îðäåðà
extern int ProfitLevel = 30; // Îáùèé ïðîôèò äëÿ âñåõ ïîçèöèé ïðè êîòîðîì îíè çàêðûâàþòñÿ
static int magicNumber = 23456;
static datetime prev_min = D'1.1.1'; // Ïðåäûäóùèé ìèíèìóì
static datetime prev_max = D'1.1.1'; // Ïðåäûäóùèé ìàêñèìóì
bool AllowTrade,NewBar,OrdersClose,OpenSell,OpenBuy;
datetime LastTrade;
int ticket,n;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
AllowTrade=True;OrdersClose=false;OpenSell=false;OpenBuy=false;
LastTrade=CurTime();
ticket=0;n=0;
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if ((Year()<2005) || (Month()<7)) return (0);
double iFractals_up = 0; // Ìàêñèìóì
double iFractals_lo = 0; // Ìèíèìóì
double TP =0,SL=0,price=0,totalprofit=0;
int cnt=0,i=0,totalcountpos=0;
//Òèïû îòêðûòûõ îðäåðîâ
int ps=0,pb=0,ss=0,sb=0,ls=0,lb=0;
//+------------------------------------------------------------------------------------------------+
//| Ìîäóëü ïîäñ÷¸òà îòêðûòûõ îðäåðîâ
//+------------------------------------------------------------------------------------------------+
//Åñëè åñòü õîòü îäèí îòêðûòûé îðäåð, îïðåäåëÿåì òèï îòêðûòîãî îðäåðà
for (cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderType()==OP_SELL && OrderSymbol()==Symbol())
{ //åñòü îòêðûòàÿ êîðîòêàÿ ïîçèöèÿ
ps=ps+1;totalcountpos=totalcountpos+1;
totalprofit=totalprofit+OrderProfit();
}
if (OrderType()==OP_BUY && OrderSymbol()==Symbol())
{ //åñòü îòêðûòàÿ äëèííàÿ ïîçèöèÿ
pb=pb+1;totalcountpos=totalcountpos+1;
totalprofit=totalprofit+OrderProfit();
}
if (OrderType()==OP_SELLSTOP && OrderSymbol()==Symbol())
{ //åñòü îòëîæåííûé îðäåð SellStop
ss=ss+1;totalcountpos=totalcountpos+1;
}
if (OrderType()==OP_BUYSTOP && OrderSymbol()==Symbol())
{ //åñòü îòëîæåííûé îðäåð BuyStop
sb=sb+1;totalcountpos=totalcountpos+1;
}
}
//+------------------------------------------------------------------------------------------------+
//| Êîíåö ìîäóëÿ ïîäñ÷¸òà îòêðûòûõ îðäåðîâ
//+------------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------------------------------------+
//| Åñëè îáùèé ïðîôèò ïî âñåì îòêðûòûì ïðåâûøàåò íàø óðîâåíü, äàåì ïðèêàç íà çàêðûòèå âñåõ îðäåðîâ
//+------------------------------------------------------------------------------------------------+
if (totalprofit>ProfitLevel)
{
AllowTrade=false;
OrdersClose=true;
}
//+------------------------------------------------------------------------------------------------+
//| Ìîäóëü çàêðûòèÿ âñåõ îðäåðîâ
//+------------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------------------------------------+
//| Ìîäóëü çàêðûòèÿ âñåõ îðäåðîâ
//+------------------------------------------------------------------------------------------------+
if (OrdersClose)
{
AllowTrade=false;
bool result=false;
for (cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
if (((CurTime()-LastTrade)<10) || (LastTrade==0)) return (0);
result=false;
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
RefreshRates();
{
if ((OrderSymbol()==Symbol()) && (OrderType()==OP_BUY))
{
result=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),5,Yellow);
if (result){ LastTrade=CurTime();break; }
}
if ((OrderSymbol()==Symbol()) && (OrderType()==OP_SELL))
{
result=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),5,Blue );
if (result){ LastTrade=CurTime();break; }
}
if ((OrderSymbol()==Symbol()) && (OrderType()==OP_BUYSTOP) && (OrderMagicNumber()== magicNumber))
{
result=OrderDelete(OrderTicket());
if (result){ LastTrade=CurTime();break; }
}
if ((OrderSymbol()==Symbol()) && (OrderType()==OP_SELLSTOP) && (OrderMagicNumber()== magicNumber))
{
result=OrderDelete(OrderTicket());
if (result){ LastTrade=CurTime();break; }
}
}
}
if(result==false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
//+------------------------------------------------------------------------------------------------+
//| Êîíåö ìîäóëÿ çàêðûòèÿ âñåõ îðäåðîâ
//+------------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------------------------------------+
//| Åñëè îðäåðîâ áîëüøå íå îñòàëîñü, ÷åðåç 5 ñåêóíä íà÷èíàåì òîðãîâëþ çàíîâî
//+------------------------------------------------------------------------------------------------+
if (totalcountpos==0)
{
OrdersClose=false;
Sleep(5000);
AllowTrade=true;
}
//+------------------------------------------------------------------------------------------------+
//|
//+------------------------------------------------------------------------------------------------+
// Åñëè ñâîáîäíûé îñòàòîê ìåíüøå íåîáõîäèìîãî, òî âûõîäèì
if (AccountFreeMargin() < ((1000*Ask)*Lots)+100 )
{
Alert("Íåäîñòàòî÷íî äåíåã!");
return (0);
}
//+------------------------------------------------------------------------------------------------+
//| Ìîäóëÿ èíäèêàòîðà
//+------------------------------------------------------------------------------------------------+
for (i = 0; i < 300; i++)
{
iFractals_up = iFractals(NULL, 0, MODE_UPPER, i);
iFractals_lo = iFractals(NULL, 0, MODE_LOWER, i);
if ((iFractals_up != 0) || (iFractals_lo != 0)) break;
}
// Íîâûé ìàêñèìóì ?
if ((iFractals_up != 0) && (Time[i] != prev_max) && (i < 3))
{
OpenSell=true;prev_max = Time[i];
}
// Íîâûé ìèíèìóì ?
if ((iFractals_lo != 0) && (Time[i] != prev_min) && (i < 3))
{
OpenBuy=true;prev_min = Time[i];
}
//+------------------------------------------------------------------------------------------------+
//| Êîíåö ìîäóëÿ èíäèêàòîðà
//+------------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------------------------------------+
//| Îòêðûòèå îðäåðîâ
//+------------------------------------------------------------------------------------------------+
if (OpenSell && AllowTrade)
{
if ((CurTime()-LastTrade)<10) return (0);
ticket=0;
// Îòêðîåì îòëîæåííûé îðäåð.
while (ticket==0)
{
RefreshRates();
price = NormalizeDouble(High[i]+(limit*Point),Digits);
SL = price - StopLoss * Point;
TP = price + TakeProfit * Point;
ticket=OrderSend(Symbol(), OP_BUYSTOP, Lots, price, 3, SL, TP, "OP_BUYSTOP", magicNumber, 0, Pink);
Sleep(1000);
}
Sleep(10000);
ticket=0;
// Îòêðîåì îñíîâíîé îðäåð.
while (ticket==0)
{
RefreshRates();
SL = NormalizeDouble(Bid,Digits) + StopLoss * Point;
TP = NormalizeDouble(Bid,Digits) - TakeProfit * Point;
ticket=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),3,SL,TP,"OP_SELL",magicNumber,0,Blue);
Sleep(1000);
}
LastTrade=CurTime();
OpenSell=false;
}
if (OpenBuy && AllowTrade)
{
if ((CurTime()-LastTrade)<10) return (0);
ticket=0;
// Îòêðîåì îòëîæåííûé îðäåð.
while (ticket==0)
{
RefreshRates();
price = NormalizeDouble(Low[i]-(limit*Point),Digits);
SL = price + StopLoss * Point;
TP = price - TakeProfit * Point;
ticket=OrderSend(Symbol(), OP_SELLSTOP, Lots, price, 3, SL, TP, "OP_SELLSTOP", magicNumber, 0, SkyBlue);
Sleep(1000);
}
Sleep(10000);
ticket=0;
// Îòêðîåì îñíîâíîé îðäåð.
while (ticket==0)
{
RefreshRates();
SL = NormalizeDouble(Ask,Digits) - StopLoss * Point;
TP = NormalizeDouble(Ask,Digits) + TakeProfit * Point;
ticket=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),3,SL,TP,"OP_BUY",magicNumber,0,Yellow);
Sleep(1000);
}
LastTrade=CurTime();
OpenBuy=false;
}
//+------------------------------------------------------------------------------------------------+
//| Êîíåö îòêðûòèÿ îðäåðîâ
//+------------------------------------------------------------------------------------------------+
return(0);
}
//+------------------------------------------------------------------+
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments