
//+------------------------------------------------------------------+
//| DSergExp.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
//--- Inputs
extern double Lots = 0.1; // лот
extern int StopLoss = 0; // лось
extern int TakeProfit = 0; // язь
extern int BULevel = 0; // уровень БУ
extern int BUPoint = 30; // пункты БУ
extern int TrailingStop = 0; // трал
extern int Expiration = 14; // истечениe ордера в часах
extern int StopMarket = 0; // 0-Stop 1-Market
extern int Slip = 30; // реквот
extern int Shift = 1; // на каком баре сигнал индикатора
extern int Delta = 50; // расстояние от цены
extern int Magic = 123; // магик
extern string IndName = "DSerg";
extern int Nlin = 25; // Мин. длина канала линейной регрессии
extern int r0 = 150; // Макс. высота канала в пунктах
extern double t0 = 2.618; // Цель при пробое отн. ширины канала
extern bool useClose = true; // Использовать для расчёта Close,\n если false - High/Low
extern int Nbars = 5000; // Количество баров для расчёта
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0;
color clr=Green;
double sl=0,tp=0;
double strelka=iCustom(NULL,0,IndName,"",Nlin,"",r0,"",t0,"",useClose,"",Nbars,7,Shift);
double krestik=iCustom(NULL,0,IndName,"",Nlin,"",r0,"",t0,"",useClose,"",Nbars,2,Shift);
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits); else sl=NormalizeDouble(krestik,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits); else tp=NormalizeDouble(strelka,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits); else sl=NormalizeDouble(krestik,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits); else tp=NormalizeDouble(strelka,Digits);
}
r=OrderSend(NULL,type,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,TimeCurrent()+Expiration*3600,clr);
Print("Price: ",price," SL: ",sl," TP: ",tp);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountOrders()
{
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()>=0) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OpenOrd()
{
double up=iCustom(NULL,0,IndName,"",Nlin,"",r0,"",t0,"",useClose,"",Nbars,5,Shift);
double dn=iCustom(NULL,0,IndName,"",Nlin,"",r0,"",t0,"",useClose,"",Nbars,6,Shift);
if(up>0)
{
if(StopMarket==0) PutOrder(4,Ask+Delta*Point);
if(StopMarket==1) PutOrder(0,Ask);
}
if(dn>0)
{
if(StopMarket==0) PutOrder(5,Bid-Delta*Point);
if(StopMarket==1) PutOrder(1,Bid);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Trailing()
{
bool mod;
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(Bid-OrderOpenPrice()>TrailingStop*Point)
{
if(OrderStopLoss()<Bid-TrailingStop*Point)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
if(OrderType()==OP_SELL)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if((OrderStopLoss()>(Ask+TrailingStop*Point)) || (OrderStopLoss()==0))
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
return;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()>=(Ask+(BULevel+BUPoint)*Point) && (OrderOpenPrice()<OrderStopLoss() || OrderStopLoss()==0))
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-BUPoint*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double up=iCustom(NULL,0,IndName,"",Nlin,"",r0,"",t0,"",useClose,"",Nbars,5,Shift);
double dn=iCustom(NULL,0,IndName,"",Nlin,"",r0,"",t0,"",useClose,"",Nbars,6,Shift);
double strelka=iCustom(NULL,0,IndName,"",Nlin,"",r0,"",t0,"",useClose,"",Nbars,7,Shift);
double krestik=iCustom(NULL,0,IndName,"",Nlin,"",r0,"",t0,"",useClose,"",Nbars,2,Shift);
if(CountOrders()<1 && t!=Time[0])
{
OpenOrd();
t=Time[0];
}
if(BULevel>0) BU();
if(TrailingStop>0) Trailing();
Comment("\n up: ",up,
"\n dn: ",dn,
"\n strelka: ",strelka,
"\n krestik: ",krestik);
}
//+------------------------------------------------------------------+
AM2