
//+------------------------------------------------------------------+
//| Bablos.mq4 |
//| Copyright 2016, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
extern int StopLoss = 500; // лось
extern int TakeProfit = 10; // язь
extern int Delta = 10; // расстояние для отложек
extern int Spread = 10; // спред
extern int Vol = 40; // волатильность
extern int Slip = 0; // реквот
extern int StartHour = 0; // час начала торговли
extern int StartMin = 30; // минута начала торговли
extern int EndHour = 23; // час окончания торговли
extern int EndMin = 30; // минута окончания торговли
extern int StopTrade = 0; // 1-остановка после закрытия всех ордеров
extern int Magic1 = 111; // магик 1
extern int Magic2 = 222; // магик 2
extern double Lots = 0.1; // лот
bool trade=true;
int cycle=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool isTradeTimeInt(int hb=0,int mb=0,int he=0,int me=0)
{
datetime db, de; // Время начала и окончания работы
int hc; // Часы текущего времени торгового сервера
db=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+(string)hb+":"+(string)mb);
de=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+(string)he+":"+(string)me);
hc=TimeHour(TimeCurrent());
if(db>=de)
{
if(hc>=he) de+=24*60*60; else db-=24*60*60;
}
if(TimeCurrent()>=db && TimeCurrent()<=de) return(True);
else return(False);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price,int magic)
{
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,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CountOrders(int type)
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==type) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Подсчет позиций |
//+------------------------------------------------------------------+
int CountTrades(int ot=-1)
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==0 && (ot==0 || ot==-1)) count++;
if(OrderType()==1 && (ot==1 || ot==-1)) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ModifyOrders(int magic)
{
double all=0,tp=0,sl=0;
double 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()==OP_BUY || OrderType()==OP_SELL)
{
all+=OrderOpenPrice()*OrderLots();
count+=OrderLots();
}
}
}
}
if(count>0) all=NormalizeDouble(all/count,Digits);
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)
{
tp=NormalizeDouble(all+TakeProfit*Point,Digits);
sl=NormalizeDouble(all-StopLoss*Point,Digits);
if(OrderStopLoss()!=sl || OrderTakeProfit()!=tp)
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
if(OrderType()==OP_SELL)
{
tp=NormalizeDouble(all-TakeProfit*Point,Digits);
sl=NormalizeDouble(all+StopLoss*Point,Digits);
if(OrderStopLoss()!=sl || OrderTakeProfit()!=tp)
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Mode()
{
bool m;
double oop=0,sl=0,tp=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==2)
{
if((Bid-OrderOpenPrice()>Delta*Point)||(Bid-OrderOpenPrice()<Delta*Point))
{
oop=NormalizeDouble(Bid-Delta*Point,Digits);
if(StopLoss>0) sl=NormalizeDouble(oop-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(oop+TakeProfit*Point,Digits);
if(OrderOpenPrice()!=oop) m=OrderModify(OrderTicket(),oop,sl,tp,OrderExpiration(),Lime);
}
}
if(OrderType()==3)
{
if((OrderOpenPrice()-Bid>Delta*Point)||(OrderOpenPrice()-Bid<Delta*Point))
{
oop=NormalizeDouble(Bid+Delta*Point,Digits);
if(StopLoss>0) sl=NormalizeDouble(oop+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(oop-TakeProfit*Point,Digits);
if(OrderOpenPrice()!=oop) m=OrderModify(OrderTicket(),oop,sl,tp,OrderExpiration(),Lime);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double vol=MathAbs((Open[0]-Close[0])/Point);
double spread=MarketInfo(NULL,MODE_SPREAD);
if(cycle>0 && CountTrades()<1 && StopTrade>0) trade=false;
if(trade)
{
if(vol>Vol && isTradeTimeInt(StartHour,StartMin,EndHour,EndMin) && spread<=Spread)
{
if(CountOrders(3)<1) PutOrder(3,Bid+Delta*Point,Magic1);
if(CountOrders(2)<1) PutOrder(2,Bid-Delta*Point,Magic2);
}
if(CountOrders(0)>0) ModifyOrders(Magic2);
if(CountOrders(1)>0) ModifyOrders(Magic1);
Mode();
if(CountTrades()>0) cycle++;
}
Comment("\n Trades: ",CountTrades(),
"\n Vol: ",NormalizeDouble(vol,1),
"\n Spread: ",spread,
"\n Cycle: ",cycle,
"\n Trade: ",trade);
}
//+------------------------------------------------------------------+
Еще такой вопрос, как определяется волатильность в советнике?
double vol=MathAbs((Open[0]-Close[0])/Point);
//+------------------------------------------------------------------+
//| Bablos.mq4 |
//| Copyright 2016, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
extern int StopLoss = 500; // лось
extern int TakeProfit = 10; // язь
extern int Delta = 10; // расстояние для отложек
extern int Spread = 10; // спред
extern int Vol = 40; // волатильность
extern int Slip = 0; // реквот
extern int StartHour = 0; // час начала торговли
extern int StartMin = 30; // минута начала торговли
extern int EndHour = 23; // час окончания торговли
extern int EndMin = 30; // минута окончания торговли
extern int StopTrade = 0; // 1-остановка после закрытия всех ордеров
extern int Magic1 = 111; // магик 1
extern int Magic2 = 222; // магик 2
extern double Lots = 0.1; // лот
bool trade=true;
int cycle=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool isTradeTimeInt(int hb=0,int mb=0,int he=0,int me=0)
{
datetime db, de; // Время начала и окончания работы
int hc; // Часы текущего времени торгового сервера
db=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+(string)hb+":"+(string)mb);
de=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+(string)he+":"+(string)me);
hc=TimeHour(TimeCurrent());
if(db>=de)
{
if(hc>=he) de+=24*60*60; else db-=24*60*60;
}
if(TimeCurrent()>=db && TimeCurrent()<=de) return(True);
else return(False);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price,int magic)
{
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,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CountOrders(int type)
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==type) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Подсчет позиций |
//+------------------------------------------------------------------+
int CountTrades(int ot=-1)
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==0 && (ot==0 || ot==-1)) count++;
if(OrderType()==1 && (ot==1 || ot==-1)) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ModifyOrders(int magic)
{
double all=0,tp=0,sl=0;
double 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()==OP_BUY || OrderType()==OP_SELL)
{
all+=OrderOpenPrice()*OrderLots();
count+=OrderLots();
}
}
}
}
if(count>0) all=NormalizeDouble(all/count,Digits);
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)
{
tp=NormalizeDouble(all+TakeProfit*Point,Digits);
sl=NormalizeDouble(all-StopLoss*Point,Digits);
if(OrderStopLoss()!=sl || OrderTakeProfit()!=tp)
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
if(OrderType()==OP_SELL)
{
tp=NormalizeDouble(all-TakeProfit*Point,Digits);
sl=NormalizeDouble(all+StopLoss*Point,Digits);
if(OrderStopLoss()!=sl || OrderTakeProfit()!=tp)
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Mode()
{
bool m;
double oop=0,sl=0,tp=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==5)
{
if(Ask-Delta*Point>OrderOpenPrice())
{
oop=NormalizeDouble(Ask-Delta*Point,Digits);
if(StopLoss>0) sl=NormalizeDouble(oop+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(oop-TakeProfit*Point,Digits);
if(OrderOpenPrice()!=oop) m=OrderModify(OrderTicket(),oop,sl,tp,OrderExpiration(),Lime);
}
}
if(OrderType()==4)
{
if(OrderOpenPrice()-Ask>Delta*Point)
{
oop=NormalizeDouble(Ask+Delta*Point,Digits);
if(StopLoss>0) sl=NormalizeDouble(oop-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(oop+TakeProfit*Point,Digits);
if(OrderOpenPrice()!=oop) m=OrderModify(OrderTicket(),oop,sl,tp,OrderExpiration(),Lime);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double vol=MathAbs((Open[0]-Close[0])/Point);
double spread=MarketInfo(NULL,MODE_SPREAD);
if(cycle>0 && CountTrades()<1 && StopTrade>0) trade=false;
if(trade)
{
if(vol>Vol && isTradeTimeInt(StartHour,StartMin,EndHour,EndMin) && spread<=Spread)
{
if(CountOrders(4)<1) PutOrder(4,Bid+Delta*Point,Magic1);
if(CountOrders(5)<1) PutOrder(5,Bid-Delta*Point,Magic2);
}
if(CountOrders(0)>0) ModifyOrders(Magic2);
if(CountOrders(1)>0) ModifyOrders(Magic1);
Mode();
if(CountTrades()>0) cycle++;
}
Comment("\n Trades: ",CountTrades(),
"\n Vol: ",NormalizeDouble(vol,1),
"\n Spread: ",spread,
"\n Cycle: ",cycle,
"\n Trade: ",trade);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Bablos.mq4 |
//| Copyright 2016, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
extern int StopLoss = 500; // лось
extern int TakeProfit = 10; // язь
extern int Delta = 10; // расстояние для отложек
extern int Spread = 10; // спред
extern int Vol = 40; // волатильность
extern int Slip = 0; // реквот
extern int StartHour = 0; // час начала торговли
extern int StartMin = 30; // минута начала торговли
extern int EndHour = 23; // час окончания торговли
extern int EndMin = 30; // минута окончания торговли
extern int StopTrade = 0; // 1-остановка после закрытия всех ордеров
extern int Magic1 = 111; // магик 1
extern int Magic2 = 222; // магик 2
extern double Lots = 0.1; // лот
bool trade=true;
int cycle=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool isTradeTimeInt(int hb=0,int mb=0,int he=0,int me=0)
{
datetime db, de; // Время начала и окончания работы
int hc; // Часы текущего времени торгового сервера
db=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+(string)hb+":"+(string)mb);
de=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)+" "+(string)he+":"+(string)me);
hc=TimeHour(TimeCurrent());
if(db>=de)
{
if(hc>=he) de+=24*60*60; else db-=24*60*60;
}
if(TimeCurrent()>=db && TimeCurrent()<=de) return(True);
else return(False);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price,int magic)
{
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,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
int CountOrders(int type)
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==type) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Подсчет позиций |
//+------------------------------------------------------------------+
int CountTrades(int ot=-1)
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==0 && (ot==0 || ot==-1)) count++;
if(OrderType()==1 && (ot==1 || ot==-1)) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ModifyOrders(int magic)
{
double all=0,tp=0,sl=0;
double 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()==OP_BUY || OrderType()==OP_SELL)
{
all+=OrderOpenPrice()*OrderLots();
count+=OrderLots();
}
}
}
}
if(count>0) all=NormalizeDouble(all/count,Digits);
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)
{
tp=NormalizeDouble(all+TakeProfit*Point,Digits);
sl=NormalizeDouble(all-StopLoss*Point,Digits);
if(OrderStopLoss()!=sl || OrderTakeProfit()!=tp)
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
if(OrderType()==OP_SELL)
{
tp=NormalizeDouble(all-TakeProfit*Point,Digits);
sl=NormalizeDouble(all+StopLoss*Point,Digits);
if(OrderStopLoss()!=sl || OrderTakeProfit()!=tp)
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Mode()
{
bool m;
double oop=0,sl=0,tp=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==2)
{
if(Ask-Delta*Point>OrderOpenPrice())
{
oop=NormalizeDouble(Ask-Delta*Point,Digits);
if(StopLoss>0) sl=NormalizeDouble(oop-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(oop+TakeProfit*Point,Digits);
if(OrderOpenPrice()!=oop) m=OrderModify(OrderTicket(),oop,sl,tp,OrderExpiration(),Lime);
}
}
if(OrderType()==3)
{
if(OrderOpenPrice()-Ask>Delta*Point)
{
oop=NormalizeDouble(Ask+Delta*Point,Digits);
if(StopLoss>0) sl=NormalizeDouble(oop+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(oop-TakeProfit*Point,Digits);
if(OrderOpenPrice()!=oop) m=OrderModify(OrderTicket(),oop,sl,tp,OrderExpiration(),Lime);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double vol=MathAbs((Open[0]-Close[0])/Point);
double spread=MarketInfo(NULL,MODE_SPREAD);
if(cycle>0 && CountTrades()<1 && StopTrade>0) trade=false;
if(trade)
{
if(vol>Vol && isTradeTimeInt(StartHour,StartMin,EndHour,EndMin) && spread<=Spread)
{
if(CountOrders(3)<1) PutOrder(3,Bid+Delta*Point,Magic1);
if(CountOrders(2)<1) PutOrder(2,Bid-Delta*Point,Magic2);
}
if(CountOrders(0)>0) ModifyOrders(Magic2);
if(CountOrders(1)>0) ModifyOrders(Magic1);
Mode();
if(CountTrades()>0) cycle++;
}
Comment("\n Trades: ",CountTrades(),
"\n Vol: ",NormalizeDouble(vol,1),
"\n Spread: ",spread,
"\n Cycle: ",cycle,
"\n Trade: ",trade);
}
//+------------------------------------------------------------------+
AM2