Вопрос по коду: Как определить начало очередного бара? - страница 2

 
void CheckForOpen()
{
double Atr;
int res;
string sHeaderLetter;
string sBodyLetter;
//---- начинаем торговлю только с первым тиком нового бара
//if(Volume[0]>1) return; ???????????????????????????????
//---- определяем Moving Average
Atr = iATR(NULL,0,pATR,0);
//Alert(Atr);
//---- Условие продажи
if ((Open[0]>=Support[1]&&Close[0]<Support[1])||(Open[0]<Support[1]&&Close[1]>=Support[1]))
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);//Ask+Atr - SL Ask+SL,Ask-TP
if (SndMl == True && res != -1)
{
sHeaderLetter = "Operation SELL by " + Symbol()+"";
sBodyLetter = "Order Sell by "+ Symbol() + " at " + DoubleToStr(Bid,4)+ ", and set stop/loss at " + DoubleToStr(Ask+Atr,4)+"";
//sndMessage(sHeaderLetter, sBodyLetter);
}
return;
}
//---- Условие покупки
if ((Open[0]<=Resist[1]&&Close[0]>Resist[1])||(Open[0]>Resist[1]&&Close[1]<=Resist[1]))
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);//Bid-Atr - SL Bid-SL,Bid+TP
if ( SndMl == True && res != -1)
{
sHeaderLetter = "Operation BUY at " + Symbol()+"";
sBodyLetter = "Order Buy at "+ Symbol() + " for " + DoubleToStr(Ask,4)+ ", and set stop/loss at " + DoubleToStr(Bid-Atr,4)+"";
//sndMessage(sHeaderLetter, sBodyLetter);
}
return;
}
}

void CheckForClose()
{
string sHeaderLetter;
string sBodyLetter;
bool CloseOrd;
//----
//if(Volume[0]>1) return; ???????????????????????????????
//----
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//----
if(OrderType()==OP_BUY)
{
if((Open[0]>=Support[1]&&Close[0]<Support[1])||(Open[0]<Support[1]&&Close[1]>=Support[1])) CloseOrd=OrderClose(OrderTicket(),OrderLots(),Bid,3,Lime);
if ( SndMl == True && CloseOrd == True)
{
sHeaderLetter = "Operation CLOSE BUY at" + Symbol()+"";
sBodyLetter = "Close order Buy at "+ Symbol() + " for " + DoubleToStr(Bid,4)+ ", and finish this Trade";
//sndMessage(sHeaderLetter, sBodyLetter);
}
break;
}
if(OrderType()==OP_SELL)
{
if((Open[0]<=Resist[1]&&Close[0]>Resist[1])||(Open[0]>Resist[1]&&Close[1]<=Resist[1])) OrderClose(OrderTicket(),OrderLots(),Ask,3,Lime);
if ( SndMl == True && CloseOrd == True)
{
sHeaderLetter = "Operation CLOSE SELL at" + Symbol()+"";
sBodyLetter = "Close order Sell at "+ Symbol() + " for " + DoubleToStr(Ask,4)+ ", and finish this Trade";
//sndMessage(sHeaderLetter, sBodyLetter);
}
break;
}
}
//----

}


//+------------------------------------------------------------------+

int start()

{

//----

if(Bars<100 || IsTradeAllowed()==false) return;

//---- calculate open orders by current symbol

CalculateIndicator();

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();

else CheckForClose();

//----

return(0);

}



 

Самое простое и при этом самое надёжное это смотреть за изменением переменной Bars.

int BBars=0;
int start()
  {
   if(BBars!=Bars)
     {BBars=Bars;
      // код....
     }
   return(0);
  }

код проверен как в тестере так и при работе в интернете и без него, самая надёжная конструкция.

 

Все правильно. У Вас на первом тике бара распознается, есть ли открытые ордера или нет:

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();

else CheckForClose();

Если есть, идет закрытие, если нет - то открытие, и потом ждем наступления следующего бара.

Т.е. я хочу сказать, что этот алгоритм уже забит в советник. Если Вы хотите поменять алгоритм, обращайтесь к специалистам.

 

int START_TIME=0;//в init

bool fn_NEW_BAR()
{
bool NB;
if(START_TIME!=Time[0])
{
START_TIME=Time[0];
NB=1;
}
else
NB=0;
return(NB);

}

Так будет точней!

 
sempfid:
void CheckForOpen()
{
double Atr;
int res;
string sHeaderLetter;
string sBodyLetter;
//---- начинаем торговлю только с первым тиком нового бара
//if(Volume[0]>1) return; ???????????????????????????????
//---- определяем Moving Average
Atr = iATR(NULL,0,pATR,0);
//Alert(Atr);
//---- Условие продажи
if ((Open[0]>=Support[1]&&Close[0]<Support[1])||(Open[0]<Support[1]&&Close[1]>=Support[1]))
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);//Ask+Atr - SL Ask+SL,Ask-TP
if (SndMl == True && res != -1)
{
sHeaderLetter = "Operation SELL by " + Symbol()+"";
sBodyLetter = "Order Sell by "+ Symbol() + " at " + DoubleToStr(Bid,4)+ ", and set stop/loss at " + DoubleToStr(Ask+Atr,4)+"";
//sndMessage(sHeaderLetter, sBodyLetter);
}
return;
}
//---- Условие покупки
if ((Open[0]<=Resist[1]&&Close[0]>Resist[1])||(Open[0]>Resist[1]&&Close[1]<=Resist[1]))
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);//Bid-Atr - SL Bid-SL,Bid+TP
if ( SndMl == True && res != -1)
{
sHeaderLetter = "Operation BUY at " + Symbol()+"";
sBodyLetter = "Order Buy at "+ Symbol() + " for " + DoubleToStr(Ask,4)+ ", and set stop/loss at " + DoubleToStr(Bid-Atr,4)+"";
//sndMessage(sHeaderLetter, sBodyLetter);
}
return;
}
}

void CheckForClose()
{
string sHeaderLetter;
string sBodyLetter;
bool CloseOrd;
//----
//if(Volume[0]>1) return; ???????????????????????????????
//----
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//----
if(OrderType()==OP_BUY)
{
if((Open[0]>=Support[1]&&Close[0]<Support[1])||(Open[0]<Support[1]&&Close[1]>=Support[1])) CloseOrd=OrderClose(OrderTicket(),OrderLots(),Bid,3,Lime);
if ( SndMl == True && CloseOrd == True)
{
sHeaderLetter = "Operation CLOSE BUY at" + Symbol()+"";
sBodyLetter = "Close order Buy at "+ Symbol() + " for " + DoubleToStr(Bid,4)+ ", and finish this Trade";
//sndMessage(sHeaderLetter, sBodyLetter);
}
break;
}
if(OrderType()==OP_SELL)
{
if((Open[0]<=Resist[1]&&Close[0]>Resist[1])||(Open[0]>Resist[1]&&Close[1]<=Resist[1])) OrderClose(OrderTicket(),OrderLots(),Ask,3,Lime);
if ( SndMl == True && CloseOrd == True)
{
sHeaderLetter = "Operation CLOSE SELL at" + Symbol()+"";
sBodyLetter = "Close order Sell at "+ Symbol() + " for " + DoubleToStr(Ask,4)+ ", and finish this Trade";
//sndMessage(sHeaderLetter, sBodyLetter);
}
break;
}
}
//----

}


//+------------------------------------------------------------------+

int start()

{

//----

if(Bars<100 || IsTradeAllowed()==false) return;

//---- calculate open orders by current symbol

CalculateIndicator();

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();

else CheckForClose();

//----

return(0);

}




У Вас и открытие и закрытие по Open. Это ж стандартный советник Moving