How to limit the trade per bar?

hello everybody…
i have problem in making trade per bar…
i want every bar only can trade 1 times…
but can not find it…
this is my code

extern double TakeProfit = 50;
extern double StopLose = 40;
extern double Lots = 0.1;
datetime timebar = 0;

int start()

{
int total,ticket;
double HighEnvelope = iEnvelopes(NULL,0,4,MODE_LWMA,0,PRICE_CLOSE,0.30,M ODE_LOWER,1);

if (Low[1]<HighEnvelope )
{
if(timebar == Time[0]) return(0);
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLosePoint,Ask+TakeProfitPoint,0,Green) ;

timebar = Time[0];

}
return(0);

}

what is the wrong?
thx…

If you want maximum one trade per bar, can I suggest having a “stoptime”?
In the fapturbo longterm settings, you can set the stoptime, which is the amount of time the robot rests after the first trade before it makes another trade. And it’s in minutes. So if you want your robot to trade on an M15 chart, you might want to put 15 (15 minutes) for the stoptime. I hope this helps.

thx for replying…
but i don’t want to using stop time, because i want it 's automatic based of timeframe that we choose… example if we choose 1 hour, so the ea automatic open 1 trade / 1 hour, if choose 15 minutes, so 1 trade per 15 minutes…

i see many forum using code like :
tetime Timestamp = 0;

if(Timestamp != iTime(NULL,0,0))
{
Timestamp = iTime(NULL,0,0);

  ..your code here..

}

or like

if (barsPrev != Bars) {
// trade
barsPrev = Bars;
} else {
// no trade
}

or like

bool opened_on_current_bar = false;

for (int i = OrdersTotal() - 1; i >=0; i-- ) {
if ( ! OrderSelect( i, SELECT_BY_POS ) )
continue;
if ( OrderOpenTime() >= Time[ 0 ] ) {
opened_on_current_bar = true;
break;
}
}
// Here the variable opened_on_current_bar is set appropriately

but all of that can not work… still display many position per bar…
i don’t know where is wrong…

can u add somecode to my code?
thx…

But the stoptime would work, you just have to change it in the settings every time to switch to another time frame. 1 hour would just be stoptime = 60.

Sorry, I actually can’t. I’m a total noober at coding. Really sorry. I wish I took time into learning.

Hi there,

New_Bar function is a pretty standard function used to detect a new bar on the charts.

Also you should normalise your doubles and I have also included code to make provision for 2/3/4/5 digit brokers/instruments, which should become standard in all your coding efforts.

extern double TakeProfit = 50;
extern double StopLose = 40;
extern double Lots = 0.1;
double
   Pip;
//+------------------------------------------------------------------+
int init()
{
   Pip = Point;
   if(Digits==3||Digits==5)Pip*=10;
   TakeProfit *= Pip;
   StopLose   *= Pip;
}
//+------------------------------------------------------------------+
int start()
{
int 
   total,
   ticket;
double 
   HighEnvelope = iEnvelopes(NULL,0,4,MODE_LWMA,0,PRICE_CLOSE,0.30,MODE_LOWER,1);
   if(New_Bar()){
      if(Low[1]<HighEnvelope){
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,NormalizeDouble(Ask-StopLose,Digits),NormalizeDouble(Ask+TakeProfit,Digits),0,Green) ;
      }
   }
   return(0);
}
//+------------------------------------------------------------------+
bool New_Bar()
{
   static datetime New_Time=0;
   if(New_Time!=Time[0]){
      New_Time=Time[0];
      return(true);
   }
   return(false);
}

ok, thx very much kenny…
it 's work…
your are great.
thx…:slight_smile: