Help me please about code mql4

 

I want to open Order in every candle,not every Tick

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

void OnTick()

  {

   if(Close[1]>Open[1])      // Buy

      int  ticket = OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-StopLoss*Point,Bid+TakeProfit*Point," open ",1234,0,Green);

      

   if(Close[1]<Open[1])    //Sell

   ticket   = OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,Ask+StopLoss*Point,Bid-TakeProfit*Point," open ",1234,0,Red);

}
 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Driss Tamda: I want to open Order in every candle,not every Tick
    So where is your new bar code?
    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. int  ticket = OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-StopLoss*Point,Bid+TakeProfit*Point," open ",1234,0,Green);
    Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  4. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

 
datetime oldTime, newTime;//global variables
//--------------
int OnInit(){  
    oldTime = Time[0];//time of pg launch
    return(INIT_SUCCEEDED);
}
//------------
void OnTick(){
    newTime = Time[0];
    if (newTime != oldTime){  
        oldTime = newTime;
        //...your pg
    }
}

Only if there is a new candle your pg is launched

 
MQL4/5
datetime oldTime = 0, newTime[];//global variables
//------------
void OnTick(){
     if( CopyTime(_Symbol, PERIOD_CURRENT, 0, 1, newTime) < 1 ) return;
     if (newTime[0] != oldTime){  
          oldTime = newTime[0];
          //...your pg
     }
}
Reason: