//+------------------------------------------------------------------+
//| Petrov.mq4 |
//| Copyright 2017, AM2 |
//| http://www.forexsyatems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, AM2"
#property link "http://www.forexsyatems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Lots = 0.1; // лот
extern int StopLoss = 2000; // лось
extern int TakeProfit = 3000; // язь
extern int Step = 200; // расстояние для отложек
extern int Slip = 0; // реквот
extern int Shift = 1; // на каком баре сигнал
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,Lots,NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountTrades()
{
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()<2) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу |
//+------------------------------------------------------------------+
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() && OrderMagicNumber()==Magic)
{
if(OrderType()==type) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| Удаление отложенных ордеров |
//+------------------------------------------------------------------+
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());
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double LastOrderPrice()
{
double pr=0;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()<2) pr=OrderOpenPrice();
}
}
return(pr);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int LastOrderType()
{
int ot=8;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()<2) ot=OrderType();
}
}
return(ot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double LastOrderLot()
{
double lt=0;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()<2) lt=OrderLots();
}
}
return(lt);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Proverka(int type,double pr)
{
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(type==0 && OrderType()>1 && OrderOpenPrice()==NormalizeDouble(pr-Step*Point,_Digits)) return(true);
if(type==1 && OrderType()>1 && OrderOpenPrice()==NormalizeDouble(pr+Step*Point,_Digits)) return(true);
}
}
return(false);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(CountTrades()<1)
{
if(CountOrders(4)<1) PutOrder(4,Bid+Step*Point);
if(CountOrders(5)<1) PutOrder(5,Bid-Step*Point);
}
if(CountTrades()>0)
{
DelOrder();
}
if(CountTrades()>0)
{
if(CountOrders(4)<1 && LastOrderType()==0) PutOrder(5,LastOrderPrice()-Step*Point);
if(CountOrders(5)<1 && LastOrderType()==1) PutOrder(4,LastOrderPrice()+Step*Point);
}
Comment("\n Order Price: ",LastOrderPrice(),
"\n Order Type: ",LastOrderType(),
"\n Order Lot: ",LastOrderLot(),
"\n Proverka: ",Proverka(LastOrderType(),LastOrderPrice()));
}
//+------------------------------------------------------------------+
if(t!=time[0])
{
if(Arrows_Buffer_Up[2]>0) Alert("UP!!!");
if(Arrows_Buffer_Down[2]>0) Alert("DN!!!");
t=time[0];
}
Comment("\n UP: ",Arrows_Buffer_Up[2],
"\n DN: ",Arrows_Buffer_Down[2]);
#property indicator_level1 99.5
#property indicator_level2 0.5
int begin=iBarShift(NULL,0,StringToTime(StartTime));
int end=iBarShift(NULL,0,StringToTime(EndTime));
double up=High[iHighest(NULL,0,MODE_HIGH,begin-end,end)];
double dn=Low[iLowest(NULL,0,MODE_LOW,begin-end,end)];
AM2