One trade per bar

 
Hi there friends, I've coded another algo <Deleted> but very poor in demo account. I think the problem besides on making several trades over the same M15 bar, this is why I need some help to find the condition to only trade once per candle. Any help would be welcome.
 
David Diez: Hi there friends, I've coded another algo  <Deleted>  but very poor in demo account. I think the problem besides on making several trades over the same M15 bar, this is why I need some help to find the condition to only trade once per candle. Any help would be welcome.

You have posted many topics before, so you already know that you have to post your code if you want help with it.

Anyway, how to detect a new bar has already been asked and answer so many times, so learn to use the search function. Here is a post from another thread with a similar question about a new bar detection. This code code works on both MQL5 and MQL4.

Forum on trading, automated trading systems and testing trading strategies

Knowing when Candles shift

Fernando Carreiro, 2021.04.30 08:27

What do you mean by "candles shift"? Do you mean when the current bar closes and a new one opens? Is that it?

If that is the case, then there are several examples here in the forum for detecting a new bar. Basically you just monitor the time of the last or current bar and detect when it changes. You can do the same for the other time-frames as well!

// Check for New Bar
static datetime dtBarCurrent   = WRONG_VALUE;
       datetime dtBarPrevious  = dtBarCurrent;
                dtBarCurrent   = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE );

       bool     boolNewBarFlag = ( dtBarCurrent != dtBarPrevious );

if( boolNewBarFlag ) { Print( "... do something ..." ); }

 
Fernando Carreiro:

You have posted many topics before, so you already know that you have to post your code if you want help with it.

Anyway, how to detect a new bar has already been asked and answer so many times, so learn to use the search function. Here is a post from another thread with a similar question about a new bar detection. This code code works on both MQL5 and MQL4.


Does not work.

 
David Diez: Does not work.

It does work. I use it all the time and many others have used it too. You are the one that does not know how to use it.

I have already told you and you already know what to do; so post your code if you want help!

 

But since you were argumentative (twice 2019.09.21) and (five times 2019.05.13) I won't be helping. Two (2) years later, you are still arguing 2021.06.10) with people trying to help you.

 
William Roeder:

But since you were argumentative (twice 2019.09.21) and (five times 2019.05.13) I won't be helping. Two (2) years later, you are still arguing 2021.06.10) with people trying to help you.

Would be arguing if I say that I'm not arguing?

 
Fernando Carreiro:

It does work. I use it all the time and many others have used it too. You are the one that does not know how to use it.

I have already told you and you already know what to do; so post your code if you want help!

Ok, this is the code:

      // Check for New Bar
      static datetime dtBarCurrent=WRONG_VALUE;
             datetime dtBarPrevious=dtBarCurrent;
                      dtBarCurrent=(datetime)SeriesInfoInteger(SName,WTimeframe,SERIES_LASTBAR_DATE);
             bool     NewBarFlag=(dtBarCurrent!=dtBarPrevious);
      
      // Start
      if(NewBarFlag==true){
         if(LotSize>=minLot&&Spread<=MaxSpread){
            if(Ask<=iHigh(SName,WTimeframe,OperationMode+1)&&BuyOrders<1&&
            iHigh(SName,WTimeframe,OperationMode)>iHigh(SName,WTimeframe,OperationMode+1)+TS){
               double SL=0;if(SL_Divider>0){SL=Ask-ATR[1]/SL_Divider;}
               double TP=0;if(TP_Divider>0){TP=Ask+ATR[1]/TP_Divider;}
               if(!trade.PositionOpen(SName,ORDER_TYPE_BUY,LotSize,Ask,SL,TP,CustomComment)){
                  Print("PositionOpen error ",trade.ResultRetcode());
                  return;
                  }
               }
            else if(Bid>=iLow(SName,WTimeframe,OperationMode+1)&&SellOrders<1&&
            iLow(SName,WTimeframe,OperationMode)<iLow(SName,WTimeframe,OperationMode+1)-TS){
               double SL=0;if(SL_Divider>0){SL=Bid+ATR[1]/SL_Divider;}
               double TP=0;if(TP_Divider>0){TP=Bid-ATR[1]/TP_Divider;}
               if(!trade.PositionOpen(SName,ORDER_TYPE_SELL,LotSize,Bid,SL,TP,CustomComment)){
                  Print("PositionOpen error ",trade.ResultRetcode());
                  return;
                  }
               }
            }
         }

OHLC backtest on XAUUSD (5years) without those lines:

<Image deleted>

With those lines just doesn't trade  🤷‍♂️

 
David Diez: Ok, this is the code: OHLC backtest on XAUUSD (5years) without those lines: With those lines just doesn't trade  🤷‍♂️

Please provide a code sample that actually compiles and can be tested.

As it stands now, all we can do is see if the code syntax and grammar is acceptable, and maybe identify some minor logic inconsistency. But on a whole, we are not able to evaluate the final logic of code as it functions in the context of the rest of your code.

Since you don't know how to debug and test your own code, then you have to at least provide something that we will be able compile, test, and debug for you.

Alternatively, add more "Prints" to various areas in question to see where the code is failing to execute.

 
Fernando Carreiro:

Please provide a code sample that actually compiles and can be tested.

As it stands now, all we can do is see if the code syntax and grammar is acceptable, and maybe identify some minor logic inconsistency. But on a whole, we are not able to evaluate the final logic of code as it functions in the context of the rest of your code.

Since you don't know how to debug and test your own code, then you have to at least provide something that we will be able compile, test, and debug for you.

Alternatively, add more "Prints" to various areas in question to see where the code is failing to execute.

Could I send it via PM? I don't want to share the whole code here in the topic.

But basicly that's the ordersend function, the rest of the code works in every advisor I made.
 
David Diez: Could I send it via PM? I don't want to share the whole code here in the topic. But basicly that's the ordersend function, the rest of the code works in every advisor I made.

Sorry! For private requests, unless the project interests me, I charge a fee. This case does not interest me, so if you want free advice and help (from me and/or others), you will have to use the public forum.

 
Fernando Carreiro:

Sorry! For private requests, unless the project interests me, I charge a fee. This case does not interest me, so if you want free advice and help (from me and/or others), you will have to use the public forum.

It's another HiLo-Breakout, you've seen the entry condition. Rest of lines are money management, orders count and so on.