HlopMaster_2.6
Download scriptThis script is an automated trading system designed to manage trades by balancing entry logic, risk control, and active trade monitoring. Instead of just executing simple buy or sell commands, it uses a structured approach to manage how many positions are open and how they react to price movements.
Core Trading Logic
The system primarily operates using Pending Orders (specifically "Stop" orders). Rather than entering the market immediately at current prices, it places orders at specific distances (defined by a "pips" setting) above or below the current price.
- Dual Entry: The script attempts to place both a Buy Stop and a Sell Stop simultaneously.
- Dynamic Trailing: Once an order is triggered and becomes an active position, the system monitors it closely. If the price moves in your favor by a certain amount, it automatically adjusts the stop-loss level to "trail" the price, helping to lock in potential profits while still allowing the trade room to breathe.
Risk Management and Safety
A significant portion of this script is dedicated to "safety nets" to prevent large account drawdowns:
- Profit and Loss Limits: It monitors the overall profit/loss of your trading session. If the total loss exceeds a pre-defined currency amount, or if the total profit hits a specific target, the script will automatically clear all active orders and close all positions to secure the result.
- Margin Protection: The system checks your account's "Margin Level." If your available margin drops below a specific percentage (meaning you are running low on funds to maintain open trades), it will initiate a cleanup by deleting pending orders and closing existing positions.
- Volume Limits: It keeps track of the total volume (Lots) currently being traded. If the combined size of all open trades exceeds a critical limit, the script will take corrective action to close out positions and protect the balance.
Execution Flow
- Monitoring: The script constantly checks if there are any active positions. If none exist, it places new pending orders.
- Scaling: It features logic to manage position sizes. It can detect the size of your largest current trade and determine whether to add more volume or maintain the current scale.
- Order Cleanup: The script is "tidy"—it regularly scans for and deletes expired or unnecessary pending orders that are no longer relevant to the current market state.
- Auto-Closing: It includes a dedicated manager that monitors the distance of open positions from their entry points. If a position reaches a specific profit target (based on the maximum lot size), it can trigger a full closure of all trades.
Summary for the User
In plain terms, this is a defensive and systematic trading tool. It doesn't just "guess" where to enter; it sets "traps" at specific distances, manages the risk of those traps constantly by moving stop-losses, and acts as an automated "circuit breaker" that shuts everything down if the account faces too much risk or hits a desired profit goal.
#property copyright "FORTRADER.RU, Þðèé, ftyuriy@gmail.com, modified by Evgeniy Trofimov"
#property link "http://FORTRADER.RU, Price"
//+------------------------------------------------------------------+
//Ïàðàìåòðû äëÿ EURUSD M30 (íà÷àëüíûé äåïîçèò 500000 ðóá) ñ÷¸ò - Classic
extern int pips=250;
extern int profitpips=250;
extern double Lots=0.04; // - ôèêñèðîâàííûé ëîò
extern double RISK=0.02; // - Money Managment (ðèñê ïðè Lots=0.00)
extern double MinMargin=1000.00; // - ìèíèìàëüíûé óðîâåíü ìàðæè â ïðîöåíòàõ
extern double MaximumLoss=5000; // - ìàêñèìàëüíûé óáûòîê â âàëþòå äåïîçèòà
extern double MaximumProfit=200; // - ìàêñèìàëüíàÿ ïðèáûëü
extern double LimitLot=0.64; // - êðèòè÷åñêèé îáú¸ì îòêðûòûõ ñäåëîê
extern double LimitCountOrders= 0;// - êðèòè÷åñêîå êîëè÷åñòâî îòêðûòûõ ñäåëîê
extern int LimitDeltaTick=0; // - ìîäåëèðîâàíèå êîíòðîëüíûõ òî÷åê
//+------------------------------------------------------------------+
static datetime LastTime;
static double LastAsk;
static int Slippage=30;
static double round;
//+------------------------------------------------------------------+
int init() {
round = MathAbs(MathLog(MarketInfo(Symbol(), MODE_LOTSTEP))/MathLog(10.0))+0.5; //êîýôôèöèåíò îêðóãëåíèÿ ëîòà
return(0);
}
//+------------------------------------------------------------------+
int SL=0;
int TP=0;
int err;
int start() {
if(MathAbs(Ask-LastAsk)>LimitDeltaTick) {
LastAsk=Ask;
CheckGameOver();
}
if(LastTime==Time[0]) return(0);
LastTime=Time[0];
if(!OrdersExist()) {
err=OpenNewOrders();
}else{
TrailStopOrders();
}
if(CountPos(0)==0 && CountPos(1)==0 && ChStopPosLimitSumm()<2){
_DeleteOrder();
}
if(ChStopPosLimitSumm()<1){
if(ProcentMargin()<MinMargin || TotalLots()+GetMaxLot()*2>LimitLot){
_DeleteOrder();
CloseAllPozitions();
} else {
DopOrder(GetMaxLot()*2);
}
}
err=CloseManager();
return(0);
}//start()
//+------------------------------------------------------------------+
double op,sl,tp;
int OpenNewOrders(){
int err;
op=Ask-pips*Point;if(SL>0){sl=Ask;}else{sl=0;}if(TP>0){tp=op-TP*Point;}else{tp=0;}
err=OrderSend(Symbol(),OP_SELLSTOP,GetLots(),NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"",0,0,Red);
if(err<0){
Print("OrderSend()- Îøèáêà OP_SELLSTOP. op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());return(-1);
LastTime=Time[1];
}
op=Bid+pips*Point;if(SL>0){sl=Bid;}else{sl=0;}if(TP>0){tp=op+TP*Point;}else{tp=0;}
err=OrderSend(Symbol(),OP_BUYSTOP,GetLots(),NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"",0,0,Red);
if(err<0){
Print("OrderSend()- Îøèáêà OP_BUYSTOP. op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());return(-1);
LastTime=Time[1];
}
return(err);
}//OpenNewOrders()
//+------------------------------------------------------------------+
int TrailStopOrders() {
int i;bool err;
for(i=1; i<=OrdersTotal(); i++) {
if(OrderSelect(i-1,SELECT_BY_POS)==true) {
if( OrderType()==OP_BUYSTOP && OrderSymbol()==Symbol() && (OrderOpenPrice()-Bid)>pips*Point) {
err=OrderModify(OrderTicket(),Bid+pips*Point,OrderStopLoss(),OrderTakeProfit(),0,Green);
if(err==false){
LastTime=Time[1];
return(-1);
}
}//if(OrderType()==OP_BUYSTOP
}//if(OrderSelect(i-1,SEL
if(OrderSelect(i-1,SELECT_BY_POS)==true) {
if(OrderType()==OP_SELLSTOP && OrderSymbol()==Symbol() && (Ask-OrderOpenPrice())>pips*Point) {
err=OrderModify(OrderTicket(),Ask-pips*Point,OrderStopLoss(),OrderTakeProfit(),0,Green);
if(err==false){
LastTime=Time[1];
return(-1);
}
}//if(OrderType()==OP_SELLSTOP
}//if(OrderSelect(i-1,
}// for( i=1; i<=O
return(0);
}//TrailStopOrders()
//+------------------------------------------------------------------+
int CountPos(int type) {
int i,b,s;
for( i=1; i<=OrdersTotal(); i++) {
if(OrderSelect(i-1,SELECT_BY_POS)==true) {
if(OrderType()==OP_BUY && OrderSymbol()==Symbol()){b=b+1;}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol()){s=s+1;}
}
}
if(type==0){return(s);}
if(type==1){return(b);}
}//CountPos()
//+------------------------------------------------------------------+
int ChStopPosLimitSumm() {
int i,z;
for( i=1; i<=OrdersTotal(); i++) {
if(OrderSelect(i-1,SELECT_BY_POS)==true) {
if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP ){z=z+1;}
}
}
return(z);
}//ChStopPosLimitSumm()
//+------------------------------------------------------------------+
int _DeleteOrder() {
//Óäàëåíèå âñåõ îòëîæåííûõ îðäåðîâ
for(int i=1; i<=OrdersTotal(); i++) {
if(OrderSelect(i-1,SELECT_BY_POS)==true) {
if(OrderType()>3 && OrderSymbol()==Symbol()) {
OrderDelete(OrderTicket());
}//if
}//if
}//Next i
return(0);
}//_DeleteOrder()
//+------------------------------------------------------------------+
int CloseManager() {
//Çàêðûòèå âñåõ ïîçèöèé è îòëîæåííûõ îðäåðîâ,
//åñëè â ñäåëêå ñ ìàêñèìàëüíûì ëîòîì âåëè÷èíà
//ïðîôèòíûõ ïóíêòîâ äîñòèãëà óêàçàííîé ãðàíèöû
int err;
double maxlot=GetMaxLot();
for(int i=1; i<=OrdersTotal(); i++) {
if(OrderSelect(i-1,SELECT_BY_POS)) {
if(OrderSymbol()==Symbol()) {
if(OrderLots()==maxlot) {
if(OrderType()==OP_SELL && ((OrderOpenPrice()-Ask)>profitpips*Point)) {
_DeleteOrder();
CloseAllPozitions();
}//if
if(OrderType()==OP_BUY && ((Bid-OrderOpenPrice())>profitpips*Point)) {
_DeleteOrder();
CloseAllPozitions();
}//if
}//if(OrderLots()==maxlot)
}//if(OrderSymbol()==Symbol())
}//if
}//Next i
return(err);
}//CloseManager()
//+------------------------------------------------------------------+
double GetMaxLot() {
//Ôóíêöèÿ âîçâðàùàåò ðàçìåð ìàêñèìàëüíîãî ëîòà,
//íàéäåííîãî ñðåäè îòêðûòûõ ïîçèöèé
int i; double maxlot;
for(i=1; i<=OrdersTotal(); i++) {
if(OrderSelect(i-1,SELECT_BY_POS)) {
if(OrderType()<2 && OrderSymbol()==Symbol()) {
if(maxlot<OrderLots()) {
maxlot=OrderLots();
}
}
}
}
return(maxlot);
}//GetMaxLot()
//+------------------------------------------------------------------+
int GetLastTypePos() {
//Ôóíêöèÿ âîçâðàùàåò òèï ïîñëåäíåé îòêðûòîé ïîçèöèè
int i; datetime dt; int type;
for( i=1; i<=OrdersTotal(); i++) {
if(OrderSelect(i-1,SELECT_BY_POS)==true) {
if(OrderType()<2 && OrderSymbol()==Symbol()) {
if(dt<OrderOpenTime()){
dt=OrderOpenTime();
type=OrderType();
}
}
}//
}//Next i
return(type);
}//GetLastTypePos()
//+------------------------------------------------------------------+
int DopOrder(double Lots) {
//Ôóíêöèÿ âûñòàâëÿåò îòëîæåííûé îðäåð, íà ïðîäàæó,
//åñëè ïîñëåäíÿÿ îòêðûòàÿ ñäåëêà - ïîêóïàåò
//è íàîáîðîò
int type=GetLastTypePos();
if(type==OP_SELL){
op=Bid+pips*Point;
if(SL>0){sl=Bid;} else {sl=0;}
if(TP>0){tp=op+TP*Point;} else {tp=0;}
err=OrderSend(Symbol(),OP_BUYSTOP,Lots,NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"",0,0,Red);
if(err<0){
LastTime=Time[1];
Print("OrderSend()- Îøèáêà OP_BUYSTOP. op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());
return(-1);
}
} else if(type==OP_BUY) {
op=Ask-pips*Point;
if(SL>0){sl=Ask;} else {sl=0;}
if(TP>0){tp=op-TP*Point;} else {tp=0;}
err=OrderSend(Symbol(),OP_SELLSTOP,Lots,NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"",0,0,Red);
if(err<0){
LastTime=Time[1];
Print("OrderSend()- Îøèáêà OP_SELLSTOP. op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());
return(-1);
}
}
return(0);
}//DopOrder()
//+------------------------------------------------------------------+
int CloseAllPozitions() {
double PriceClose;
int total = OrdersTotal();
for (int i = total-1; i >= 0; i--) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol()) {
if(OrderType()==OP_BUY) {
PriceClose=Bid;
} else if(OrderType()==OP_SELL) {
PriceClose=Ask;
}
if(!OrderClose(OrderTicket(),OrderLots(),PriceClose,Slippage)) {
Print("Ñòàâêà ",OrderTicket()," íå çàêðûâàåòñÿ ïî ïðè÷èíå îøèáêè ¹ ",GetLastError());
LastTime=Time[1];
}
} // Åñëè ñâîé
} // Next i
return(0);
}//CloseAllPozitions()
//+------------------------------------------------------------------+
bool OrdersExist() {
//Ôóíêöèÿ âîçâðàùàåò true, åñëè êîëè÷åñòâî îðäåðîâ íà òåêóùåì èíñòðóìåíòå > 0
int total = OrdersTotal();
for (int i = 0; i < total; i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) {
return(true);
}
}
return(false);
}//OrdersExist()
//+------------------------------------------------------------------+
int WorkOrdersCount() {
//Ôóíêöèÿ âîçâðàùàåò êîëè÷åñòâî îòêðûòûõ ïîçèöèé íà òåêóùåì èíñòðóìåíòå
int j=0;
int total = OrdersTotal();
for (int i = 0; i < total; i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol()) {
if(OrderType()<2) j++;
}
}
return(j);
}//WorkOrdersCount()
//+------------------------------------------------------------------+
double TotalLots(){
//Ôóíêöèÿ âîçâðàùàåò îòêðûòûé îáúåì âñåõ ñäåëîê ïî òåêóùåìó èíñòðóìåíòó
double j;
int total = OrdersTotal();
for (int i = 0; i < total; i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) {
if(OrderType()<2) {
j=j+OrderLots();
}
}
}
return(j);
}//TotalLots()
//+------------------------------------------------------------------+
double ProfitTotal() {
//Ôóíêöèÿ âîçâðàùàåò ïðèáûëü ïî òåêóùåìó èíñòðóìåíòó
double j=0.0;
int total = OrdersTotal();
for (int i = 0; i < total; i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol()) {
if(OrderType()<2) j=j+OrderProfit();
}
}
return(j);
}//ProfitTotal()
//+------------------------------------------------------------------+
int CheckGameOver() {
//Ôóíêöèÿ "Game Over" èëè "Ñëàâà áîãó" çàêðûâàåò âñå ñäåëêè, åñëè...
//...èìååòñÿ ïðèáûëü
if(WorkOrdersCount()>LimitCountOrders) {
if(ProfitTotal()>MaximumProfit) {
_DeleteOrder();
CloseAllPozitions();
}
}
//...îáùèé óáûòîê ïî âñåì ñäåëêàì âûðîñ âûøå íàçíà÷åííîé íîðìû ìàêñèìàëüíîãî óáûòêà
if(ProfitTotal()<-MaximumLoss) {
_DeleteOrder();
CloseAllPozitions();
}
return(0);
}//CheckGameOver()
//+------------------------------------------------------------------+
double GetLots() {
int limitPipsLoss=10000; // - êîëè÷åñòâî ïóíêòîâ ìàêñèìàëüíîé ïðîñàäêè
double lot, result;
double MINLOTS, MAXLOTS, LOTVAL;
MINLOTS = MarketInfo(Symbol(), MODE_MINLOT);
MAXLOTS = AccountFreeMargin()*0.50/MarketInfo(Symbol(),MODE_MARGINREQUIRED); // 50% îò ìàðæè
if(MAXLOTS>MarketInfo(Symbol(), MODE_MAXLOT)) MAXLOTS = MarketInfo(Symbol(), MODE_MAXLOT);
LOTVAL = MarketInfo(Symbol(), MODE_TICKVALUE);
if(Lots == 0)
lot = NormalizeDouble(AccountFreeMargin() * RISK / (limitPipsLoss * LOTVAL), round);
else {
lot = Lots;
return(lot);
}
if(lot < MINLOTS) {
lot=MINLOTS;
Print("Íåäîñòàòî÷íî ñðåäñòâ íà ñ÷¸òå.");
} else if(lot > MAXLOTS) {
lot = MAXLOTS;
}
return(lot);
}//GetLots()
//+------------------------------------------------------------------+
double ProcentMargin() {
double P = AccountEquity()*100/MarketInfo(Symbol(), MODE_MARGINREQUIRED);
//Print("Óðîâåíü ìàðæè: ", P, " %");
return(P);
}//ProcentMargin()
//+------------------------------------------------------------------+
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments