//+------------------------------------------------------------------+
//| Fibo5.mq4 |
//| Copyright 2019, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Lots = 0.1; // лот
extern double KLot = 2; // умножение лота
extern double MaxLot = 5; // максимальный лот
extern double Level1 = 0.236; // уровень 1
extern double Level2 = 0.382; // уровень 2
extern double Level3 = 0.5; // уровень 3
extern double Level4 = 0.618; // уровень 4
extern ENUM_TIMEFRAMES TF1 = PERIOD_W1; // тф 1
extern ENUM_TIMEFRAMES TF2 = PERIOD_MN1; // тф 2
extern int StopLoss = 500; // лось
extern int TakeProfit = 280; // язь
extern int TrailingStop = 0; // трал
extern int Slip = 30; // реквот
extern int Count = 20; // баров для расчета
extern int Magic = 123; // магик
//+------------------------------------------------------------------+
//| 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;
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;
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
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 || type==-1) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lot()
{
double lot=Lots;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderProfit()>0) break;
if(OrderProfit()<0)
{
lot=OrderLots()*KLot;
break;
}
}
}
}
if(lot>MaxLot)lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutFibo(string name,datetime time1,double price1,datetime time2,double price2)
{
ObjectDelete(0,name);
ObjectCreate(0,name,OBJ_FIBO,0,time1,price1,time2,price2);
//--- установим цвет
ObjectSetInteger(0,name,OBJPROP_COLOR,Red);
//--- установим стиль линии
ObjectSetInteger(0,name,OBJPROP_STYLE,0);
//--- установим толщину линии
ObjectSetInteger(0,name,OBJPROP_WIDTH,1);
//--- успешное выполнение
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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;
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double up1 = iHigh(NULL,PERIOD_W1,1);
double dn1 = iLow(NULL,PERIOD_W1,1);
double delta1=up1-dn1;
double l01=dn1;
double l1001=up1;
double l2361=dn1+delta1*Level1;
double l3821=dn1+delta1*Level2;
double l5001=dn1+delta1*Level3;
double l6181=dn1+delta1*Level4;
PutFibo("Fibo1",Time[0],up1,Time[Count],dn1);
double up2=iHigh(NULL,PERIOD_MN1,1);
double dn2=iLow(NULL,PERIOD_MN1,1);
double delta2=up2-dn2;
double l02=dn2;
double l1002=up2;
double l2362=dn2+delta2*Level1;
double l3822=dn2+delta2*Level2;
double l5002=dn2+delta2*Level3;
double l6182=dn2+delta2*Level4;
PutFibo("Fibo2",Time[0],up2,Time[Count],dn2);
if(TrailingStop>0) Trailing();
if(CountOrders()<1)
{
if(Bid>l5001 && Low[1]<l5001 && Close[1]>l5001) PutOrder(0,Ask);
if(Bid>l5001 && Low[1]<l6181 && Close[1]>l6181) PutOrder(0,Ask);
if(Bid<l5001 && High[1]>l3821 && Close[1]<l3821) PutOrder(1,Bid);
if(Bid<l5001 && High[1]>l2361 && Close[1]<l2361) PutOrder(1,Bid);
}
Comment("\n 0.0 ",l01,
"\n 100.0 ",l1001,
"\n 23.6 ",l2361,
"\n 38.2 ",l3821,
"\n 50.0 ",l5001,
"\n 61.8 ",l6181);
}
//+------------------------------------------------------------------+
igrun