extern double Risk = 5;
extern int lock_min_step = 30; //ограничение на минимальный шаг, чтобы не получилось каждый пипс новый лок
extern int lock_part = 2; //насколько частей будем дробить убыток который находится в рынке, если 2 то значит на 1/2
string lock_signalsymbol = "EURUSD";
static double lock_eqviti,lock_last, lock_lot;
static bool lock_flag,flagADX,old_uptrend;
2. Функцию start() переименовываем в start_main() и добавляем над ней:
int start(){
double AccountRisk = (AccountBalance()-AccountEquity())/AccountBalance()*100;
if (AccountRisk >Risk) lock_flag=true;
if(OrdersTotal()<1){lock_eqviti=AccountBalance();lock_last = Close[0];}
if(lock_flag) start_lock(); else start_main();
return(0);
}
3. В функцию init() добавляем
int init(){
init_lock();
return(0);
}
4. Ниже добавляем:
int init_lock(){
lock_eqviti=AccountBalance();
lock_lot = 0;
lock_last = Close[0];
flagADX=true;
old_uptrend=false;
signal();
lock_flag = false;
return(0);
}
void start_lock(){
int sig,step;
if(OrdersTotal()<1){init_lock();return(0);}
notakeprofit();
if (AccountEquity()>=lock_eqviti){CloseAllFirstProfit();init_lock();return(0);}
else{
sig=signal();
step =MathAbs(sig);
if (step<lock_min_step) return(0);
if (Ask>lock_last+(step*Point) && sig>0 ){
if (lock_lot==0)lock_lot=NormalizeLot(GetAmountLotFromOpenPos("", OP_SELL,-1)/lock_part);
RefreshRates();
OrderSend(Symbol(),OP_BUY,lock_lot,Ask,5,0,0,"lock",0,Blue);
lock_last = Ask;
}
if (Bid<lock_last-(step*Point) && sig<0){
if (lock_lot==0) lock_lot=NormalizeLot(GetAmountLotFromOpenPos("", OP_BUY,-1)/lock_part);
RefreshRates();
OrderSend(Symbol(),OP_SELL,lock_lot,Bid,5,0,0,"lock",0,Blue);
lock_last = Bid;
}
}
return;
}
int signal() {
int i, j, result;
double smin, smax, SsMax, SsMin, SSP, price,KanalMin,KanalMax;
SSP = MathCeil(150 / iADX(lock_signalsymbol, PERIOD_M5, 14, PRICE_TYPICAL, MODE_MAIN, 1));
for (j = 120; j >= 0; j--) {
SsMax = iHigh(lock_signalsymbol, PERIOD_M5,j);SsMin = iLow(lock_signalsymbol, PERIOD_M5,j);
for (i = j; i <= j + SSP - 1; i++) {
price = iHigh(lock_signalsymbol, PERIOD_M5,i);
if (SsMax < price)SsMax = price;
price = iLow(lock_signalsymbol, PERIOD_M5,i);if (SsMin >= price)SsMin = price;}
smin = SsMin + (SsMax - SsMin)*0.30;smax = SsMax - (SsMax - SsMin)*0.30;
if (iClose(lock_signalsymbol, PERIOD_M5,j) < smin) {flagADX = false; KanalMin = smin;}
if (iClose(lock_signalsymbol, PERIOD_M5,j) > smax) {flagADX = true; KanalMax=smax;}
result = 0;
if (flagADX != old_uptrend && flagADX == true) result = +1;
if (flagADX != old_uptrend && flagADX == false) result = -1;
old_uptrend = flagADX;
}
if (Digits==5)return(MathAbs(NormalizeDouble((KanalMax-KanalMin)/Point/10,0))*result);
if (Digits==4)return(MathAbs(NormalizeDouble((KanalMax-KanalMin)/Point,0))*result);
}
void CloseAllFirstProfit() {
int i, k=OrdersTotal();
for (i=k-1; i>=0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) if (OrderType()==OP_BUY || OrderType()==OP_SELL) if (OrderProfit()+OrderSwap()>0) ClosePosBySelect();
k=OrdersTotal();
for (i=k-1; i>=0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) if (OrderType()==OP_BUY || OrderType()==OP_SELL) ClosePosBySelect();
}
void ClosePosBySelect() {
if (OrderType()==OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 100, CLR_NONE);
if (OrderType()==OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 100, CLR_NONE);
}
double GetAmountLotFromOpenPos(string sy="", int op=-1, int mn=-1) {
double l=0;
int i,k=OrdersTotal();
if (sy=="0") sy=Symbol();
for (i=0; i<k; i++) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) if (OrderSymbol()==sy || sy=="") if (OrderType()==OP_BUY || OrderType()==OP_SELL) if (op<0 || OrderType()==op) if (mn<0 || OrderMagicNumber()==mn) l+=OrderLots();
return(l);
}
bool notakeprofit(){
bool ok=true;
for (int i=OrdersTotal(); i >=0; i--) if ( OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) if (OrderSymbol() == Symbol() && OrderTakeProfit()>0){ OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(),NormalizeDouble(0,Digits), 0, CLR_NONE);ok=false;return(ok);}
return(ok);
}
double NormalizeLot(double lo, bool ro=True, string sy="") {
double l, k;
if (sy=="" || sy=="0") sy=Symbol();
double ls=MarketInfo(sy, MODE_LOTSTEP);
double ml=MarketInfo(sy, MODE_MINLOT);
double mx=MarketInfo(sy, MODE_MAXLOT);
if (ml==0) ml=0.1;
if (mx==0) mx=100;
if (ls>0) k=1/ls; else k=1/ml;
if (ro) l=MathCeil(lo*k)/k; else l=MathFloor(lo*k)/k;
if (l<ml) l=ml;
if (l>mx) l=mx;
return(l);
}