//+------------------------------------------------------------------+
//| Sluggish.mq4 |
//| Copyright 2014, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, AM2"
#property link "http://www.forexsystems.biz"
#property description "Simple expert advisor"
#define MAGIC 20141020
//--- Inputs
extern int StopLoss = 1000; // Стоплосс ордера
extern int TakeProfit = 1000; // Тейкпрофит ордера
extern int TrailingStop = 1000; // Трал ордера
extern int Slip = 3; // Проскальзывание цены
extern int ClosePos = 0; // 0-закрытие по ТП, 1-по сигналу индикатора
extern int Risk = 0; // Риск на сделку в % от депо
extern double Lots = 0.1; // Лот
//----
extern int BBPer = 20; // Период индикатора
extern double BBDev = 1; // Отклонение индикатора
//----
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
int res;
double BBBuy=iCustom(Symbol(),0,"3Bollinger_Bands_Stop_v2",BBPer,BBDev,2,0);
double BBSell=iCustom(Symbol(),0,"3Bollinger_Bands_Stop_v2",BBPer,BBDev,3,0);
//--- sell conditions
if(BBSell>0)
{
res=OrderSend(Symbol(),OP_SELL,fLots(),Bid,Slip,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGIC,0,Red);
}
//--- buy conditions
if(BBBuy>0)
{
res=OrderSend(Symbol(),OP_BUY,fLots(),Ask,Slip,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGIC,0,Blue);
}
Comment("\nBuy ",BBBuy, "\nSell ",BBSell);
//---
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double BBBuy=iCustom(Symbol(),0,"3Bollinger_Bands_Stop_v2",BBPer,BBDev,2,0);
double BBSell=iCustom(Symbol(),0,"3Bollinger_Bands_Stop_v2",BBPer,BBDev,3,0);
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(BBSell>0)
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,Slip,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(BBBuy>0)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,Slip,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
//---
}
//+------------------------------------------------------------------+
double fLots()
{
double lot=Lots;
double lot_min=MarketInfo( Symbol(),MODE_MINLOT);
double lot_max=MarketInfo( Symbol(),MODE_MAXLOT);
if (Risk>0)
{
lot=fND(AccountBalance()*Risk/100000,2);
}
if (lot<lot_min) lot=lot_min;
if (lot>lot_max) lot=lot_max;
return(lot);
}
//+------------------------------------------------------------------+
double fND(double d,int n=-1)
{
if(n<0) return(NormalizeDouble(d, Digits));
return(NormalizeDouble(d, n));
}
//--------------------------------------------------------------------
void Trailing()
{
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol() || OrderMagicNumber()==MAGIC)
if(OrderType()==OP_BUY)
{
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>TrailingStop*Point)
{
if(OrderStopLoss()<Bid-TrailingStop*Point)
{
bool mod=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Blue);
}
}
}
}
if(OrderType()==OP_SELL)
{
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if((OrderStopLoss()>(Ask+TrailingStop*Point)) || (OrderStopLoss()==0))
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Red);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbol
if(OrdersTotal()<1) CheckForOpen();
if(ClosePos>0) CheckForClose();
if(TrailingStop!=0) Trailing();
//---
}
//+------------------------------------------------------------------+
AM2