// Стартовый час.mq4
// © 2003-2006 Mandor ®
// E-mail:
mandorr@gmail.com// Используется только период H1
extern int StartHour1 = 14;
extern int StartHour2 = 14;
extern int StartHour3 = 14;
extern int StartHour4 = 15;
extern int MinStop = 11;
extern double Lots = 0.1;
// Стартовая функция
void start()
{
if(Bars<100 || IsTradeAllowed()==false) return;
if(Period()!=PERIOD_H1) return;
// переменные
int i=0;
int res=0;
int nMagic=0;
int nTicket=0;
int nSlippage=2;
int nHour=0;
int nValue=0;
int nOpenPrice=0;
int nStopLoss=0;
int nTakeProfit=0;
int nAsk=MathRound(Ask/Point);
int nBid=MathRound(Bid/Point);
int nSpread=nAsk-nBid;
// уровни для отложенных ордеров
int nHighStop=MathRound(High[1]/Point)+nSpread;
int nLowStop =MathRound(Low [1]/Point);
nHighStop++;
nLowStop --;
// количество ордеров
int nTotalBuy=0,nTotalBuyStop=0,nTotalSell=0,nTotalSellStop=0;
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
{
if(OrderType()==OP_BUY) nTotalBuy++;
if(OrderType()==OP_BUYSTOP) nTotalBuyStop++;
if(OrderType()==OP_SELL) nTotalSell++;
if(OrderType()==OP_SELLSTOP) nTotalSellStop++;
}
}
// установка Stop Loss ордеров Buy
if(nTotalBuy>0)
{
nStopLoss=nLowStop;
if(nLowStop>nBid-MinStop) nStopLoss=nBid-MinStop;
nTicket=0; nTakeProfit=0; nOpenPrice=0;
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
if(OrderType()!=OP_BUY) continue;
nValue=MathRound(OrderStopLoss()/Point);
if(nValue>0) continue;
nTicket=OrderTicket();
nOpenPrice=MathRound(OrderOpenPrice()/Point);
break;
}
if(nTicket>0)
{
OrderModify(nTicket,nOpenPrice*Point,nStopLoss*Point,nTakeProfit*Point,0,Blue);
return;
}
}
// установка Stop Loss ордеров Sell
if(nTotalSell>0)
{
nStopLoss=nHighStop;
if(nHighStop<nAsk+MinStop) nStopLoss=nAsk+MinStop;
nTicket=0; nTakeProfit=0; nOpenPrice=0;
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
if(OrderType()!=OP_SELL) continue;
nValue=MathRound(OrderStopLoss()/Point);
if(nValue>0) continue;
nTicket=OrderTicket();
nOpenPrice=MathRound(OrderOpenPrice()/Point);
break;
}
if(nTicket>0)
{
OrderModify(nTicket,nOpenPrice*Point,nStopLoss*Point,nTakeProfit*Point,0,Red);
return;
}
}
// проверка стартового часа
nHour=TimeHour(Time[1]);
if(nHour!=StartHour1 && nHour!=StartHour2 && nHour!=StartHour3 && nHour!=StartHour4) return;
// корректировка Stop Loss ордеров Buy
if(nTotalBuy>0)
{
nStopLoss=nLowStop;
if(nLowStop>nBid-MinStop) nStopLoss=nBid-MinStop;
nTicket=0; nTakeProfit=0; nOpenPrice=0;
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
if(OrderType()!=OP_BUY) continue;
nValue=MathRound(OrderStopLoss()/Point);
if(nValue>=nStopLoss) continue;
nTicket=OrderTicket();
nOpenPrice=MathRound(OrderOpenPrice()/Point);
break;
}
if(nTicket>0)
{
OrderModify(nTicket,nOpenPrice*Point,nStopLoss*Point,nTakeProfit*Point,0,Blue);
return;
}
}
// корректировка Stop Loss ордеров Sell
if(nTotalSell>0)
{
nStopLoss=nHighStop;
if(nHighStop<nAsk+MinStop) nStopLoss=nAsk+MinStop;
nTicket=0; nTakeProfit=0; nOpenPrice=0;
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
if(OrderType()!=OP_SELL) continue;
nValue=MathRound(OrderStopLoss()/Point);
if(nValue<=nStopLoss && nValue>0) continue;
nTicket=OrderTicket();
nOpenPrice=MathRound(OrderOpenPrice()/Point);
break;
}
if(nTicket>0)
{
OrderModify(nTicket,nOpenPrice*Point,nStopLoss*Point,nTakeProfit*Point,0,Red);
return;
}
}
// корректировка цены ордеров Buy Stop
if(nTotalBuyStop>0)
{
nOpenPrice=nHighStop;
if(nHighStop<nAsk+MinStop) nOpenPrice=nAsk+MinStop;
nTicket=0; nStopLoss=0; nTakeProfit=0;
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
if(OrderType()!=OP_BUYSTOP) continue;
nValue=MathRound(OrderOpenPrice()/Point);
if(nValue<=nOpenPrice) continue;
nTicket=OrderTicket();
break;
}
if(nTicket>0)
{
OrderModify(nTicket,nOpenPrice*Point,nStopLoss*Point,nTakeProfit*Point,0,Blue);
return;
}
}
// корректировка цены ордеров Sell Stop
if(nTotalSellStop>0)
{
nOpenPrice=nLowStop;
if(nLowStop>nBid-MinStop) nOpenPrice=nBid-MinStop;
nTicket=0; nStopLoss=0; nTakeProfit=0;
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()!=Symbol()) continue;
if(OrderType()!=OP_SELLSTOP) continue;
nValue=MathRound(OrderOpenPrice()/Point);
if(nValue>=nOpenPrice) continue;
nTicket=OrderTicket();
break;
}
if(nTicket>0)
{
OrderModify(nTicket,nOpenPrice*Point,nStopLoss*Point,nTakeProfit*Point,0,Red);
return;
}
}
// установка ордера Buy Stop
if(nTotalBuy==0 && nTotalBuyStop==0)
{
nOpenPrice=nHighStop;
if(nHighStop<nAsk+MinStop) nOpenPrice=nAsk+MinStop;
nStopLoss=0; nTakeProfit=0;
res=OrderSend(Symbol(),OP_BUYSTOP,Lots,nOpenPrice*Point,nSlippage,nStopLoss*Point,nTakeProfit*Point,"",nMagic,0,Blue);
return;
}
// установка ордера Sell Stop
if(nTotalSell==0 && nTotalSellStop==0)
{
nOpenPrice=nLowStop;
if(nLowStop>nBid-MinStop) nOpenPrice=nBid-MinStop;
nStopLoss=0; nTakeProfit=0;
res=OrderSend(Symbol(),OP_SELLSTOP,Lots,nOpenPrice*Point,nSlippage,nStopLoss*Point,nTakeProfit*Point,"",nMagic,0,Red);
return;
}
// конец функции
}