How to set buy auto by code mql4? please show a example.

 

I start learning code MQl4. please help me . How to use OrderSend for BUY, set it... thanks a lot

 
You have an example in the documentation for OrderSend() -> https://docs.mql4.com/trading/OrderSend.
 
int start()
{
//----
if (OrdersTotal() == 0){
OrderSend(Symbol(), OP_BUY, 1,Ask, 3,Ask + stop_loss * Point, Bid - take_profit * Point, IDENT, MAGIC, 0, Blue);
}
//----
return(0);

}

Simple that is all.

why code experts not run?

 

Because u did not declare the variables: stop_loss, take_profit, IDENT, MAGIC. When u press compile, pay attention to the errors u get; in this case it would list each variable as "variable not defined".

 

Thanks Gordon !

here my variables.

extern color Do = Red;

#define MAGIC 1234
#define IDENT "Akira"

extern double lots = 1;
extern double stop_loss = 80;

extern double take_profit = 600;

please help me!!!!!!!!!!!!!

 

Your stoploss and takeprofit are reversed... Stoploss should be lower than opening price and takeprofit higher (for buy). You should always add a print to GetLastError() after each trading function so as to have an indication of what went wrong if it did. In this case it outputs error 130 which is 'invalid stops' (and indeed they are...).

You can see an example in the documentation for GetLastError() -> https://docs.mql4.com/check/GetLastError.


And one more thing - next time:

 

error at 4055.

here my EA file

Files:
test2.mq4  2 kb
 
Error 4055 is indicator related and this expert doesn't use them, so this error must come from somewhere else. U still have the same problem with stoploss and takeproft (error 130). Next time please first refer to the error codes (https://docs.mql4.com/constants/errors) and do a search for the error before posting a message.
 

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. What are Function return values ? How do I use them ? - MQL4 forum
  3. Not adjusting 4/5 digit brokers (including slippage) Not adjusting for ECN brokers.
  4. TP/SL wrong direction.
  5. Not filtering OrdersTotal using magic number makes it incompatible (on multiple charts) with every EA (including itself) and manual trading.