extern int StartHour=15; // Время открытия ордеров
extern int TimeZone=0; // Временная зона
extern int OrderExpirationDays=3; // Срок жизни ордера в днях, если он не закрылся по стопу или профиту
extern int TakeProfit=14; // Желаемая прибыль
extern int CurrencyFactor=37; // Валютный коэффициент
extern double Lots=0.01; // Величина лота
extern double RiskPercentLoss=30; // Процент максимального убытка от депозита на один ордер
extern bool RiskMM=true; // Мани Менеджмент
extern double RiskPercentMM=10; // Процент риска
int set_buy=0;
int set_sell=0;
int StopLoss, LotFactor;
double kLots;
void start()
{
if (Bars<100 || IsTradeAllowed()==false) return;
if (TimeZone<-23 || TimeZone>23) return;
TestOrderExpirationDays();
int hour=Hour()-TimeZone;
if (DayOfWeek()>=1 && DayOfWeek()<=5 && hour==StartHour)
{
if (set_buy==0 && TotalBuy()==0) { if (OpenBuy()>0) {set_buy=1; return;} }
if (set_sell==0 && TotalSell()==0) { if (OpenSell()>0) {set_sell=1; return;} }
}
else
{
set_buy=0;
set_sell=0;
}
}
void TestOrderExpirationDays()
{
if(RiskMM)CalculateMM();
int set_double_buy =0;
int set_double_sell=0;
int ticket, slippage;
double loss, profit;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if (OrderSymbol()!=Symbol()) continue;
if (OrderType()!=OP_BUY && OrderType()!=OP_SELL) continue;
[COLOR="Red"]if (CurTime()-OrderOpenTime()<=86400*OrderExpirationDays) continue;
{
if (OrderType()==OP_BUY )
{ if (OrderClose(OrderTicket(),OrderLots(),Bid,slippage)) set_double_buy=1; }
if (OrderType()==OP_SELL)
{ if (OrderClose(OrderTicket(),OrderLots(),Ask,slippage)) set_double_sell=1; }
}[/COLOR]
}
kLots=(Lots*RiskPercentMM)/3;
LotFactor=Lots*100;
StopLoss=(AccountBalance()*RiskPercentLoss)/(CurrencyFactor*LotFactor);
[COLOR="Blue"]if (set_double_buy>0)
{
slippage=2;
loss=0; if (StopLoss>0) loss=Ask-StopLoss*Point;
profit=0; if (TakeProfit>0) profit=Ask+TakeProfit*Point;
ticket=OrderSend(Symbol(),OP_BUY,kLots*Lots,Ask,slippage,loss,profit,"",0,0);
if (ticket>0) set_buy=0;
else Print("Open a order failed with error #",GetLastError());
}
if (set_double_sell>0)
{
slippage=2;
loss=0; if (StopLoss>0) loss=Bid+StopLoss*Point;
profit=0; if (TakeProfit>0) profit=Bid-TakeProfit*Point;
ticket=OrderSend(Symbol(),OP_SELL,kLots*Lots,Bid,slippage,loss,profit,"",0,0);
if (ticket>0) set_sell=0;
else Print("Open a order failed with error #",GetLastError());
}[/COLOR]
}
int OpenBuy()
{
int slippage=2;
double loss=0; if (StopLoss>0) loss=Ask-StopLoss*Point;
double profit=0; if (TakeProfit>0) profit=Ask+TakeProfit*Point;
int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slippage,loss,profit,"",0,0);
if (ticket==0) Print("Open a order failed with error #",GetLastError());
return (ticket);
}
int OpenSell()
{
int slippage=2;
double loss=0; if (StopLoss>0) loss=Bid+StopLoss*Point;
double profit=0; if (TakeProfit>0) profit=Bid-TakeProfit*Point;
int ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slippage,loss,profit,"",0,0);
if (ticket==0) Print("Open a order failed with error #",GetLastError());
return (ticket);
}
int TotalBuy()
{
int count=0;
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if (OrderSymbol()==Symbol() && OrderType()==OP_BUY) count++;
}
return (count);
}
int TotalSell()
{
int count=0;
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if (OrderSymbol()==Symbol() && OrderType()==OP_SELL) count++;
}
return (count);
}
//+------------------------------------------------------------------+
//| Money Management |
//+------------------------------------------------------------------+
void CalculateMM()
{
double MinLots=MarketInfo(Symbol(),MODE_MINLOT);
double MaxLots=MarketInfo(Symbol(),MODE_MAXLOT);
Lots=AccountFreeMargin()/100000*RiskPercentMM;
Lots=MathMin(MaxLots,MathMax(MinLots,Lots));
if(MinLots<0.1)Lots=NormalizeDouble(Lots,2);
else
{
if(MinLots<1)Lots=NormalizeDouble(Lots,1);
else Lots=NormalizeDouble(Lots,0);
}
if(Lots<MinLots)Lots=MinLots;
if(Lots>MaxLots)Lots=MaxLots;
return(0);
}
//+------------------------------------------------------------------+