Download MetaTrader 5

Create a simple EA which restrict order if spread is more than defined limit.

To add comments, please log in or register
exptrades
64
exptrades  

Hi,

Need an EA having input as : 

"maximum spread allowed"   :  (any number like 10/20)

Now, suppose I try to place order and if spread at that time is less than or equal to defined number in input parameter, order should be placed. 

If spread is grater than number defined in input parameter, reject the order with message "Order rejected because of high spread"

I am newbie in mql4 coding, here is my code : 

==============================================


#property strict

#property show_inputs



extern int MaxSpread=0;

int OnStart()

{

//—


 //Max Spread in pipettes

int currentOrder =  OrderTicket();


if(MarketInfo(Symbol(), MODE_SPREAD)<MaxSpread){

Print("I can order");

return(0);

}

else{

OrderDelete(currentOrder);

return(1);

Print("Cannot order, spread is too high");

}

}

//+——————————————————————+


bool  OrderDelete(

   int        ticket,      // ticket

   color      arrow_color  // color

   );


=============================================================================


Error while attaching to the chart : 
<name of EA> is not an expert  and cannot be executed


also please let me know if code is correct or need updates.

Regards.


Sergey Golubev
Moderator
101553
Sergey Golubev  
whroeder1
17050
whroeder1  
exptrades: Need an EA having input as :  I am newbie in mql4 coding, here is my code : also please let me know if code is correct or need updates.
  1. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. int currentOrder =  OrderTicket();
    Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.
  3. You can not use any Trade Functions until you select an order.

  4. int OnStart(){
    Invalid signature, and is only used in scripts, not in EAs
    void OnStart none scripts Start event handler.
              Program Running - MQL4 programs - MQL4 Reference

  5. bool  OrderDelete(
       int        ticket,      // ticket
       color      arrow_color  // color
       );
    The function is predefined in MT4.
              OrderDelete - Trade Functions - MQL4 Reference

  6. OrderDelete(currentOrder);
    
    return(1);
    
    Print("Cannot order, spread is too high");
    You can only delete pending orders.
  7. Check your return codes for errors and report them.
              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
  8. Do you expect to ever see that message?

  9. Of course your "code needs updates." A) It doesn't compile. B) It doesn't create orders. C) All of the above.
Chris Mukengeshayi
863
Chris Mukengeshayi  
exptrades:

Hi,

Need an EA having input as : 

"maximum spread allowed"   :  (any number like 10/20)

Now, suppose I try to place order and if spread at that time is less than or equal to defined number in input parameter, order should be placed. 

If spread is grater than number defined in input parameter, reject the order with message "Order rejected because of high spread"

I am newbie in mql4 coding, here is my code : 

==============================================


#property strict

#property show_inputs



extern int MaxSpread=0;

int OnStart()

{

//—


 //Max Spread in pipettes

int currentOrder =  OrderTicket();


if(MarketInfo(Symbol(), MODE_SPREAD)<MaxSpread){

Print("I can order");

return(0);

}

else{

OrderDelete(currentOrder);

return(1);

Print("Cannot order, spread is too high");

}

}

//+——————————————————————+


bool  OrderDelete(

   int        ticket,      // ticket

   color      arrow_color  // color

   );


=============================================================================


Error while attaching to the chart : 
<name of EA> is not an expert  and cannot be executed


also please let me know if code is correct or need updates.

Regards.



Your code need to be re-written, this is like a full job you are asking, if you can't update your code

There is always the freelancer section.

The idea of EA you want will have fewer trade as you can expect, cause spread constantly change or increase when the best possible entry occur.

To add comments, please log in or register