//+------------------------------------------------------------------+
//| TimeAssistent.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
//--- input parameters
extern int Magic=123;
extern string Время_срабатывания_и_ отк лючения= "Значения по умолчанию";
extern string OrderReplaceTime="00:00:00";
extern string OrderRemoveTime="00:00:30";
extern string ___Параметры_Канала___="Значения по умолчанию";
extern double Lots=0.1;
extern int ChannelWidth=200;
extern int Trailing=200;
extern bool Trailing0=true;
extern int SlippControl=30;
extern bool SecondOrderDel=true;
extern string ___Параметры_Сетки___="Значения по умолчанию";
extern int GridCount=5;
extern int GridStep=150;
extern double GridLotStep=0.01;
//extern int GridStep;
datetime ort,xrt;
double ch,tr0,tr;
double c;
int iB,iS,iBS,iSS;
int TicketBuy,TicketSell;
int lotdec;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
int i,j,ho,mi,se;
OBJCREATE();
i=StringFind(OrderReplaceTime,":",0);
ho=StrToInteger(StringSubstr(OrderReplaceTime,0,i));
j=StringFind(OrderReplaceTime,":",i+1);
mi=StrToInteger(StringSubstr(OrderReplaceTime,i+1,j-i-1));
se=StrToInteger(StringSubstr(OrderReplaceTime,j+1));
ort=iTime(Symbol(),PERIOD_D1,0)+ho*60*60+mi*60+se;//+StrToTime(OrderReplaceTime);
if (OrderRemoveTime=="") xrt=0;
else {
i=StringFind(OrderRemoveTime,":",0);
ho=StrToInteger(StringSubstr(OrderRemoveTime,0,i));
j=StringFind(OrderRemoveTime,":",i+1);
mi=StrToInteger(StringSubstr(OrderRemoveTime,i+1,j-i-1));
se=StrToInteger(StringSubstr(OrderRemoveTime,j+1));
xrt=iTime(Symbol(),PERIOD_D1,0)+ho*60*60+mi*60+se;//+StrToTime(OrderRemoveTime);
}
ch=ChannelWidth/2*Point;
tr=Trailing*Point;
if (tr==0) tr=ch;
lotdec = -NormalizeDouble(MathLog(MarketInfo(Symbol(),MODE_MINLOT))/MathLog(10),0);
if (Trailing0) tr0=(GridStep-30)*Point; else tr0=tr;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
OBJDEL();
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
datetime t;
string s;
int b;
double hl,ptr,ptp;
t=TimeCurrent();
s="OrderReplaceTime ="+TimeToStr(ort,TIME_SECONDS)+" "+(ort-TimeCurrent())+"\n";
if (xrt>0) s=s+"OrderRemoveTime ="+TimeToStr(xrt,TIME_SECONDS)+" "+(xrt-TimeCurrent())+"\n";
// Comment(s);
c=MarketInfo(Symbol(),MODE_BID);
ORDERSCOUNT();
// Graf1();
if (t<ort && t<xrt) Graf1();
if (t>=ort && t<xrt && TicketBuy==0 && TicketSell==0) {
ORDERSEND();
}
if (t>=ort && t<xrt && iB==0 && iS>0 && SecondOrderDel) ORDERDELETE(OP_BUYSTOP);
// if (t>=ort && t<xrt && (iBS>0 || iSS>0)) Graf1();
if (t>=xrt) Graf1();
if (t>=ort && t<xrt && iB>0 && iS==0 && SecondOrderDel) ORDERDELETE(OP_SELLSTOP);
if (t>=xrt && iBS>0 && iB==0) ORDERDELETE(OP_BUYSTOP);
if (t>=xrt && iSS>0 && iS==0) ORDERDELETE(OP_SELLSTOP);
if (iB>0) TralingBuy();
if (iS>0) TralingSell();
//----
return(0);
}
//+------------------------------------------------------------------+
void Graf1()
{
datetime t=TimeCurrent(),tx;
if (t<ort) {
tx=MathMax(0,ort-TimeCurrent());
ObjectSet("TA_BUY",OBJPROP_PRICE1,c+ch);
ObjectSet("TA_SELL",OBJPROP_PRICE1,c-ch);
ObjectSet("TA_R",OBJPROP_PRICE1,c+ch);
ObjectSet("TA_R",OBJPROP_PRICE2,c-ch);
ObjectSet("TA_R",OBJPROP_TIME1,ort);
ObjectSet("TA_R",OBJPROP_TIME2,ort);
ObjectSet("TA_R",OBJPROP_RAY,false);
ObjectSet("TA_LAB",OBJPROP_XDISTANCE,0);
ObjectSet("TA_LAB",OBJPROP_YDISTANCE,20);
ObjectSetText("TA_LAB",TimeToStr(ort,TIME_SECONDS)+" "+tx,15,"Arial",Yellow);
}
else if (t<xrt) {
tx=MathMax(0,xrt-TimeCurrent());
ObjectSet("TA_BUY",OBJPROP_PRICE1,0);
ObjectSet("TA_SELL",OBJPROP_PRICE1,0);
// ObjectSet("TA_R",OBJPROP_PRICE1,c+ch);
// ObjectSet("TA_R",OBJPROP_PRICE2,c-ch);
ObjectSet("TA_R",OBJPROP_TIME1,xrt);
ObjectSet("TA_R",OBJPROP_TIME2,xrt);
ObjectSetText("TA_LAB",TimeToStr(xrt,TIME_SECONDS)+" "+tx,15,"Arial",Red);
}
else {
ObjectSetText("TA_LAB","",15);
ObjectSet("TA_R",OBJPROP_TIME1,0);
ObjectSet("TA_R",OBJPROP_TIME2,0);
}
}
//+------------------------------------------------------------------+
void ORDERSEND()
{
int i;
double pr,lot;
for (i=0;i<=GridCount-1;i++){
pr=c+ch+i*GridStep*Point;
lot=NormalizeDouble(Lots+i*GridLotStep,lotdec);
TicketBuy=OrderSend(Symbol(),OP_BUYSTOP,lot,pr,SlippControl,0,0,"",Magic,0,Blue);
}
for (i=0;i<=GridCount-1;i++){
pr=c-ch-i*GridStep*Point;
lot=NormalizeDouble(Lots+i*GridLotStep,lotdec);
TicketBuy=OrderSend(Symbol(),OP_SELLSTOP,lot,pr,SlippControl,0,0,"",Magic,0,Red);
}
}
//+------------------------------------------------------------------+
void ORDERSCOUNT()
{
int total=OrdersTotal();
int i;
TicketBuy=0; TicketSell=0;
for (i = total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
if (OrderType() == OP_BUY) {iB++; TicketBuy=OrderTicket();}
if (OrderType() == OP_SELL) {iS++;TicketSell=OrderTicket();}
if (OrderType() == OP_BUYSTOP) {iBS++;TicketBuy=OrderTicket();}
if (OrderType() == OP_SELLSTOP) {iSS++;TicketSell=OrderTicket();}
}
}
void OBJCREATE()
{
if (ObjectFind("TA_BUY")<0) ObjectCreate("TA_BUY",OBJ_HLINE,0,0,0,0,0);
if (ObjectFind("TA_SELL")<0) ObjectCreate("TA_SELL",OBJ_HLINE,0,0,0,0,0);
if (ObjectFind("TA_R")<0) ObjectCreate("TA_R",OBJ_TREND,0,ort,c+ch,ort,c-ch);
if (ObjectFind("TA_LAB")<0) ObjectCreate("TA_LAB",OBJ_LABEL,0,0,0);
ObjectSetText("TA_LAB","",15);
}
void OBJDEL()
{
int t=ObjectsTotal();
for (int i=t-1;i>=0;i--){
string s=ObjectName(i);
if (StringSubstr(s,0,2)=="TA") ObjectDelete(s);
}
}
void ORDERDELETE(int ot)
{
int total=OrdersTotal();
for (int i=total-1;i>=0;i--){
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic || OrderType()!=ot) continue;
int ticket = OrderTicket();
OrderDelete(ticket);
}
}
void TralingBuy ()
{
datetime t;
string s;
int b;
double hl,ptr,ptp;
double c=MarketInfo(Symbol(),MODE_BID);
int total=OrdersTotal();
for (int i=total-1;i>=0;i--){
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic || OrderType()!=OP_BUY) continue;
int ticket = OrderTicket();
t=OrderOpenTime();
b=iBarShift(Symbol(),1,t);
b=iHighest(Symbol(),1,MODE_HIGH,b+1,0);
hl=iHigh(Symbol(),1,b);
ptr=hl-tr; ptp=hl-tr0;
if (c<=ptr || c<=ptp) OrderClose(ticket,Lots,c,1000);
}
}
void TralingSell ()
{
datetime t;
string s;
int b;
double hl,ptr,ptp;
double c=MarketInfo(Symbol(),MODE_ASK);
int total=OrdersTotal();
for (int i=total-1;i>=0;i--){
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic || OrderType()!=OP_SELL) continue;
int ticket = OrderTicket();
t=OrderOpenTime();
b=iBarShift(Symbol(),1,t);
b=iLowest(Symbol(),1,MODE_LOW,b+1,0);
hl=iLow(Symbol(),1,b);
ptr=hl+tr; ptp=hl+tr0;
if (c>=ptr || c>=ptp) OrderClose(ticket,Lots,c,1000);
}
}