NailShort
Download scriptThis automated trading script is designed to manage "Sell" positions on a specific market symbol, utilizing a logic focused on identifying price peaks and maintaining a specific volume of open trades based on profit thresholds.
Core Operational Logic
Initial Entry Strategy The system begins by monitoring the market history over a predefined number of recent price bars (defaulting to 10). If there are currently no active "Sell" positions, the script automatically initiates one. It calculates the entry point by identifying the highest price reached within that recent window and then adds a specific distance (Delta) above that peak to determine the Take Profit level.
Dynamic Position Management Once an initial trade is active, the script continuously monitors the performance of the most recently opened "Sell" order. Instead of closing trades immediately based on fixed profit or loss targets alone, it looks at the floating profit of the latest position:
- Profit Monitoring: The script checks if the current profit of the latest Sell order has dropped below a specific threshold (OpenT).
- Layering Strategy: If the profit falls below this threshold, the system interprets this as a signal to "layer" or add more positions. It will open an additional Sell order.
- Target Calculation: Similar to the initial entry, each new position opened during this phase is assigned a Take Profit level based on the highest price found in the recent bar history plus the Delta offset.
Technical Behavior Summary
- Automatic Execution: The script manages all trade openings independently, provided the trading platform allows automated actions and sufficient historical data is available.
- Identification: It uses a unique "Magic Number" to ensure it only manages its own trades and does not interfere with other manual or automated trades on the same account.
- Trade Type: This specific logic is exclusively programmed for Sell (short) positions.
In essence, the script functions as a trend-following tool that seeks to enter the market at significant price points and continues to scale into positions if the current price action moves against the initial trade, aiming to capture a reversal back toward a calculated profit zone.
//+------------------------------------------------------------------+
//| Gerat.mq4 |
//| Copyright © 2009, Nail_Saby |
//| http://gerat.ucoz.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, http://gerat.ucoz.ru/"
#property link "gerat.ru@mailru"
#define MAGICNUM 20084379
extern double OpenT = -50;
extern double Lot = 0.1;
extern int N_MaxBar = 10;
extern double TakeProfit = 400;
extern double Delta = 1000;
//ïîäñ÷åò òåêóùåãî êîëè÷åñòâà îòêðûòûõ ïîçèöèé íà ïðîäàæó
int CalcNumShorts()
{
int nShorts = 0;
for (int i = 0; i < OrdersTotal(); i++) {
int res = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (res == 0) {
Print ("Îøèáêà ïðè âûáîðå îðäåðà: ", GetLastError());
return;
}
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICNUM
&& OrderType() == OP_SELL) nShorts++;
}
return (nShorts);
}
//ïîïûòêà îòêðûòü íîâûé îðäåð
void CheckForOpen()
{
bool found = false;
//ïîèñê ïîñëåäíåãî îòêðûòîãî îðäåðà íà ïðîäàæó
for (int i = OrdersTotal() - 1; i >= 0; i--) {
int res = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (res == 0) {
Print ("Îøèáêà ïðè âûáîðå îðäåðà: ", GetLastError());
return;
}
if (OrderMagicNumber() == MAGICNUM &&
OrderSymbol() == Symbol() && OrderType() == OP_SELL) {
found = true; //...íàøëè ïîñëåäíèé îòêðûòûé îðäåð íà ïðîäàæó
break;
}
}
if (!found) {
Print ("Îøèáêà: íå ñìîãëè íàéòè îæèäàåìîãî îòêðûòîãî îðäåðà íà ïðîäàæó: ", GetLastError());
return;
}
//åñëè óáûòîê ïðåâûøàåò çíà÷åíèå ïåðåìåííîé OpenT, îòêðûâàåì îðäåð íà ïðîäàæó
if (OrderProfit() <= OpenT) {
//Print (OrderProfit());
double Max = Close[ArrayMaximum(Close, N_MaxBar, 1)];
Print (Bid, " ", Max + Delta * Point);
res = OrderSend (Symbol(), OP_SELL, Lot, Bid, 3, Max + Delta * Point, Bid - TakeProfit * Point, "", MAGICNUM, 0, Red);
if (res == 0) {
Print ("Îøèáêà îòêðûòèÿ îðäåðà íà ïðîäàæó: ",GetLastError());
return;
}
}
}
void start()
{
//ïðîâåðêà íåîáõîäèìûõ óñëîâèé äëÿ òîðãîâëè
if (Bars < N_MaxBar || IsTradeAllowed() == false) return;
//åñëè íåò îòêðûòûõ ïîçèöèé, îòêðûâààåì ïîçèöèþ íà ïðîäàæó
if (CalcNumShorts() == 0) {
double Max = Close[ArrayMaximum(Close, N_MaxBar, 1)];
Print (Bid, " ", Max + Delta * Point);
int res = OrderSend (Symbol(), OP_SELL, Lot, Bid, 3, Max + Delta * Point, Bid - TakeProfit * Point, "", MAGICNUM, 0, Red);
if (res == 0) {
Print ("Îøèáêà îòêðûòèÿ îðäåðà íà ïðîäàæó: ",GetLastError());
return;
}
}
else CheckForOpen();
return;
}
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments