А вот при срабатывании второй третью уже не хочет ставить
Покажите участок графика где это происходит? Мне это нужно для отладки.
А вот при срабатывании второй третью уже не хочет ставить
//+------------------------------------------------------------------+
//| Grider.mq4 |
//| Copyright 2017, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Lot = 0.1; // объем первого ордера
extern double MaxLot = 10; // максимальный лот
extern double KLot = 2; // умножение лота ордеров сетки
extern double KStep = 2; // увеличение шага
extern double Loss = 4500; // лось в рублях
extern double Profit = 50; // язь в рублях
extern int StopLoss = 0; // стоплосс
extern int TakeProfit = 0; // тейкпрофит
extern int BULevel = 0; // уровень БУ
extern int BUPoint = 30; // пункты БУ
extern int Step = 100; // шаг ордеров
extern int Delta = 100; // отступ от цены
extern int Count = 5; // количество ордеров
extern int Slip = 3; // проскальзывание
extern int Timer = 5; // таймер в секундах
extern int Magic = 123; // магик
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
CloseAll();
DelOrder();
Sleep(Timer*1000);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price,double lot)
{
int r=0;
color clr=Green;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits);
}
r=OrderSend(NULL,type,lot,NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CountOrders(int type=-1)
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==type || (OrderType()>=0 && type==-1)) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Лот |
//+------------------------------------------------------------------+
double Lots(int type)
{
double lots=Lot;
double MaximumLot=MarketInfo(NULL,MODE_MAXLOT);
double MinimumLot=MarketInfo(NULL,MODE_MINLOT);
lots=NormalizeDouble(lots*MathPow(KLot,CountOrders(type)),2);
if(lots>MaximumLot) lots=MaximumLot;
if(lots<MinimumLot) lots=MinimumLot;
return(lots);
}
//+------------------------------------------------------------------+
//| Шаг для гридера |
//+------------------------------------------------------------------+
double Steps(int ot)
{
double step=Step;
step=NormalizeDouble(Step*MathPow(KStep,CountOrders(ot)),2);
return(step);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void BU()
{
bool m;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY)
{
if(OrderOpenPrice()<=(Bid-(BULevel+BUPoint)*Point) && OrderOpenPrice()>OrderStopLoss())
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+BUPoint*Point,OrderTakeProfit(),0,Yellow);
//ставим селлстоп по цене OrderOpenPrice()+BUPoint
PutOrder(5,NormalizeDouble(OrderOpenPrice()+BUPoint*Point,Digits),OrderLots());//sellstop
return;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()>=(Ask+(BULevel+BUPoint)*Point) && (OrderOpenPrice()<OrderStopLoss() || OrderStopLoss()==0))
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-BUPoint*Point,Digits),OrderTakeProfit(),0,Yellow);
//ставим байстоп по цене OrderOpenPrice()-BUPoint
PutOrder(4,NormalizeDouble(OrderOpenPrice()-BUPoint*Point,Digits),OrderLots());//buystop
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера |
//+------------------------------------------------------------------+
double AllProfit(int ot=-1)
{
double pr=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
pr+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
return(pr);
}
//+------------------------------------------------------------------+
//| Закрытие позиции по типу ордера |
//+------------------------------------------------------------------+
void CloseAll(int ot=-1)
{
bool cl;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==0 && (ot==0 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
}
if(OrderType()==1 && (ot==1 || ot==-1))
{
RefreshRates();
cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Удаление отложенных ордеров |
//+------------------------------------------------------------------+
void DelOrder()
{
bool del;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()>1) del=OrderDelete(OrderTicket());
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(BULevel>0) BU();
if(CountOrders(-1)<1)
{
for(int i=0;i<Count;i++)
{
PutOrder(4,Bid+Delta*Point+Steps(4)*Point*i,Lots(4));//buystop
PutOrder(5,Bid-Delta*Point-Steps(5)*Point*i,Lots(5));//sellstop
}
}
if((Profit>0 && AllProfit()>Profit) || (Loss>0 && AllProfit()<-Loss))
{
CloseAll();
DelOrder();
Sleep(Timer*1000);
}
Comment("\n All Profit: ",AllProfit());
}
//+------------------------------------------------------------------+
Важное условие: чтобы при обновлении хая или лоу сделки переставлялись.
void PutOrder(int type,double price,double lot)
{
int r=0;
color clr=Green;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits);
}
r=OrderSend(NULL,type,lot,NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
PutOrder(0,Ask,Lot1);
PutOrder(1,Bid,Lot1);
PutOrder(0,Ask,Lot2);
PutOrder(1,Bid,Lot2);
PutOrder(0,Ask,Lot3);
PutOrder(1,Bid,Lot3);
AM2