Frank_ud- add mody
Download scriptThis script is designed as an automated trading assistant for the MetaTrader 4 platform. It monitors your open positions in real-time and manages trades based on specific price movements, volume levels, and profit targets.
The logic can be broken down into three main functional areas:
1. Position Monitoring and Analysis
The script constantly scans your active account to identify all "Buy" and "Sell" orders for a specific currency pair. It organizes this data into internal lists, specifically tracking:
- Volume (Lots): It identifies which of your open positions has the largest size.
- Pricing: It notes the entry prices of your current trades.
- Profitability: It calculates the current profit of these positions relative to their volume.
2. Automatic Trade Management (Closing Logic)
The script acts as a safety net or a profit-taker by monitoring "Take Profit" thresholds:
- Threshold Checking: If the most significant position in either direction reaches a specific intensity level (defined by the
tpsetting), the script triggers a closing sequence. - Sequential Closing: Instead of just closing one trade, it attempts to close multiple positions sequentially. This is designed to clear out winning (or high-risk) positions rapidly to lock in results or reduce exposure.
3. Dynamic Entry Logic (Opening Trades)
The script looks for specific market conditions to open new positions automatically:
- Price Gaps: It checks if the current market price has moved significantly away from your existing trade levels (based on the
shsetting). If the price is "too far" or if no trades currently exist, it prepares to enter. - Margin Safety: Before opening any new trade, the script verifies that you have sufficient free margin (ensuring you don't exceed 95% of your account balance), providing a basic layer of capital protection.
- Smart Lot Sizing: When opening a new trade, the script calculates the volume based on your largest existing position. It essentially "scales" your position size relative to what you are already trading.
- Automatic Take Profit: Every new trade opened by the script automatically includes a predetermined profit target (defined by the
profitPsetting).
Summary of Behavior
In plain terms, this script works like an automated portfolio manager. It watches your largest trades; if they become highly profitable according to your settings, it helps close them out. Simultaneously, if the market moves into a zone where you have no protection or where a new opportunity exists, it will automatically open new positions with adjusted volumes while ensuring your account maintains a healthy margin buffer.
//+------------------------------------------------------------------+
//| ôðàíê_óä.mq4 |
//| Copyright © 2006, Ðàìèëü Ñàôèóëëîâè÷ Èðãèçîâ |
//| popcorn@aaanet.ru |
//+------------------------------------------------------------------+
#define m 20050611
//----
extern int tp = 5; // THIS IS THE INTENSITY
extern int sh = 5;
extern int profitP = 17;
//----
datetime lastt;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int kol_buy()
{
int kol_ob = 0;
//----
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderType() == OP_BUY)
kol_ob++;
}
return(kol_ob);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int kol_sell()
{
int kol_os = 0;
//----
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderType() == OP_SELL)
kol_os++;
}
return(kol_os);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int slip, i, ii, tic, total, kk, gle;
double lotsi = 0.00;
bool sob = false, sos = false, scb = false, scs = false;
int kb, kb_max = 0;
kb = kol_buy() + 1;
double M_ob[11][8];
ArrayResize(M_ob,kb);
int ks = 0, ks_max = 0;
ks = kol_sell() + 1;
double M_os[11][8];
ArrayResize(M_os,ks);
ArrayInitialize(M_ob, 0.00);
int kbi = 0;
//----
for(i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
break;
//----
if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
{
kbi++;
M_ob[kbi][0] = OrderTicket();
M_ob[kbi][1] = OrderOpenPrice();
M_ob[kbi][2] = OrderLots();
M_ob[kbi][3] = OrderType();
M_ob[kbi][4] = OrderMagicNumber();
M_ob[kbi][5] = OrderStopLoss();
M_ob[kbi][6] = OrderTakeProfit();
M_ob[kbi][7] = OrderProfit();
}
}
M_ob[0][0] = kb;
double max_lot_b = 0.00;
//----
for(i = 1; i < kb; i++)
if(M_ob[i][2] > max_lot_b)
{
max_lot_b = M_ob[i][2];
kb_max = i;
}
double buy_lev_min = M_ob[kb_max][1];
ArrayInitialize(M_os,0.00);
int ksi = 0;
//----
for(i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
//----
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
{
ksi++;
M_os[ksi][0] = OrderTicket();
M_os[ksi][1] = OrderOpenPrice();
M_os[ksi][2] = OrderLots();
M_os[ksi][3] = OrderType();
M_os[ksi][4] = OrderMagicNumber();
M_os[ksi][5] = OrderStopLoss();
M_os[ksi][6] = OrderTakeProfit();
M_os[ksi][7] = OrderProfit();
}
}
M_os[0][0] = ks;
double max_lot_s = 0.00;
//----
for(i = 1;i < ks; i++)
if(M_os[i][2] > max_lot_s)
{
max_lot_s = M_os[i][2];
ks_max = i;
}
double sell_lev_max = M_os[ks_max][1];
//----
if(Bars < 100 || IsTradeAllowed() == false)
return(0);
sob = (kol_buy() < 1 || buy_lev_min - sh*Point > Ask) &&
AccountFreeMargin() > AccountBalance()*0.05;
sos = (kol_sell() < 1 || sell_lev_max + sh*Point < Bid) &&
AccountFreeMargin() > AccountBalance()*0.05;
//----
if(M_ob[kb_max][2] > 0.00)
scb = M_ob[kb_max][7] / (M_ob[kb_max][2]*10) > tp;
//----
if(M_os[ks_max][2] > 0.00)
scs = M_os[ks_max][7] / (M_os[ks_max][2]*10) > tp;
kk = 0;
ii = 0;
//----
if(scb)
{
while(kol_buy() > 0 && kk < 3)
{
for(i = 1; i <= kb; i++)
{
ii = M_ob[i][0];
//----
if(!OrderClose(ii,M_ob[i][2],Bid,slip,White))
{
gle = GetLastError();
kk++;
Print("Îøèáêà ¹", gle, " ïðè close buy ", kk);
Sleep(6000);
RefreshRates();
}
}
kk++;
}
}
kk = 0;
ii = 0;
//----
if(scs)
{
while(kol_sell() > 0 && kk < 3)
{
for(i = 1; i <= ks; i++)
{
ii = M_os[i][0];
//----
if(!OrderClose(ii,M_os[i][2], Ask, slip, White))
{
gle = GetLastError();
kk++;
Print("Îøèáêà ¹", gle, " ïðè close sell ", kk);
Sleep(6000);
RefreshRates();
}
}
kk++;
}
}
kk = 0;
tic = -1;
//----
if(sob)
{
if(max_lot_b == 0.00)
lotsi = 0.01;
else
lotsi = 0.01+max_lot_b;
//----
while(tic == -1 && kk < 3)
{
tic = OrderSend(Symbol(), OP_BUY, lotsi, Ask, slip, 0, Ask + (profitP)*Point, // the basic script is bid - (tp+x)*Point
" ", m, 0, Yellow);
Print("tic_buy=", tic);
//----
if(tic==-1)
{
gle = GetLastError();
kk++;
Print("Îøèáêà ¹", gle, " ïðè buy ", kk);
Sleep(600);
RefreshRates();
}
}
lastt = CurTime();
return;
}
tic = -1;
kk = 0;
//----
if(sos)
{
if(max_lot_s == 0.00)
lotsi = 0.01;
else
lotsi = 0.01+max_lot_s;
//----
while(tic == -1 && kk < 3)
{
tic = OrderSend(Symbol(), OP_SELL, lotsi, Bid, slip, 0, Bid - (profitP)*Point, // the basic script is bid - (tp+x)*Point
" ", m, 0, Red);
Print("tic_sell=", tic);
//----
if(tic == -1)
{
gle = GetLastError();
kk++;
Print("Îøèáêà ¹", gle, " ïðè sell ", kk);
Sleep(600);
RefreshRates();
}
}
lastt = CurTime();
return;
}
}
//+------------------------------------------------------------------+
Comments