//+------------------------------------------------------------------+
//| OAO.mq4 |
//| Copyright 2016, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2"
#property link "http://www.forexsystems.biz"
#property version "1.00"
//--- Inputs
extern double Lots = 0.1; // лот
extern double KLot = 2; // умножение лота
extern double MaxLot = 5; // максимальный лот
extern int StopLoss = 50; // лось
extern int TakeProfit = 70; // язь
extern int Expiration = 10; // истечение ордера
extern int CloseOn = 0; // 1-закрытие,удаление ордеров на сигналах AO
extern bool BSDel = true; // удалим байстоп после пересечения
extern bool SSDel = true; // удалим селстоп после пересечения
extern bool One = false; // только один ордер
extern int Slip = 30; // реквот
extern int Magic = 123; // магик
bool b=true,s=true;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//---
Comment("");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0;
color clr;
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,TimeCurrent()+Expiration*3600,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() && 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());
}
}
}
}
//+------------------------------------------------------------------+
//| Закрытие позиции по типу ордера |
//+------------------------------------------------------------------+
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())
{
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);
}
}
}
}
}
//+------------------------------------------------------------------+
//| Удаление отложенных ордеров по типу |
//+------------------------------------------------------------------+
void DelOrderType(int type)
{
bool del;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
if(OrderType()==type) del=OrderDelete(OrderTicket());
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CrossBars()
{
int bar=0;
for(int i=0;i<100;i++)
{
double ao1=iAO(NULL,0,i);
double ao2=iAO(NULL,0,i+1);
if((ao1>0 && ao2<0) || (ao1<0 && ao2>0))
{
bar=i;
break;
}
}
return(bar);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lot()
{
double lot=Lots;
if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderProfit()<0)
{
lot=OrderLots()*KLot;
}
}
if(lot>MaxLot)lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
double ao1=iAO(NULL,0,1);
double ao2=iAO(NULL,0,2);
double ao3=iAO(NULL,0,3);
double low=Low[iLowest(NULL,0,MODE_LOW,CrossBars(),1)];
double high=High[iHighest(NULL,0,MODE_HIGH,CrossBars(),1)];
if(One)
{
if(CountTrades()<1)
{
if(ao1>0 && ao2>ao1 && ao2>ao3 && CountOrders(4)<1 && Bid<high && b)
{
PutOrder(4,high);
b=false;s=true;
}
if(ao1<0 && ao2<ao1 && ao2<ao3 && CountOrders(5)<1 && Bid>low && s)
{
PutOrder(5,low);
s=false;b=true;
}
}
}
if(!One)
{
if(ao1>0 && ao2>ao1 && ao2>ao3 && CountOrders(4)<1 && Bid<high && b)
{
PutOrder(4,high);
b=false;s=true;
}
if(ao1<0 && ao2<ao1 && ao2<ao3 && CountOrders(5)<1 && Bid>low && s)
{
PutOrder(5,low);
s=false;b=true;
}
}
if((ao1>0 && ao2<0 && SSDel) || (ao1<0 && ao2>0 && BSDel)) DelOrder();
if(CloseOn>0)
{
if(ao2>0 && ao1<0)
{
CloseAll(0);
DelOrderType(2);
DelOrderType(4);
}
if(ao2<0 && ao1>0)
{
CloseAll(1);
DelOrderType(3);
DelOrderType(5);
}
}
Comment(CrossBars());
}
//+------------------------------------------------------------------+
fxsev