Hello All !
I have been studying on coding my EA for 2 months. But I cant even code a simple EA for Buy Market Order. Eventhough it seems i did everything correctly, it does not work and apparently i am missing sth. I really appreciate your help and time. Thanks in advance for your sparing time and help.
Here is the code!
Seems you did everything correctly? The compiler says otherwise... solve the compilation errors first.
Hello All !
I have been studying on coding my EA for 2 months. But I cant even code a simple EA for Buy Market Order. Eventhough it seems i did everything correctly, it does not work and apparently i am missing sth. I really appreciate your help and time. Thanks in advance for your sparing time and help.
Here is the code!
Hi emsahii.
If there's a struggle, its a good idea to get back to basics. Just write a simple EA without all the bells and whistles. And make sure you understand the debugging tools; the errors log in the editor, and the EA log and journal tabs on the MT4 terminal....
As Seng says, there are compilation errors, and even if you fixed them, you might not be learning for next time.....
If you ran only this code, that will at least create a buy order for you, with a SL and TP - if the Ask price is above the MA. It won't place an order until trading opens of course.
#property strict void OnInit() { OnTick(); // code runs on the weekend when there are no ticks! } void OnTick() { if(OrdersTotal()>1)return; // just one order if(Ask>iMA(_Symbol,0,15,0,MODE_EMA,PRICE_CLOSE,0)){ double lots=.01; double orderprice=Ask; double sl=Bid-15*Point; double tp=Ask+50*Point; int ticket=OrderSend(_Symbol,OP_BUY,lots,Ask,3,sl,tp,"Comment",0,0,clrNONE); } }
Then, you can look at expanding the code, adding criteria, magic numbers etc, running the code every x minutes etc.
double orderprice=Ask; double sl=Bid-15*Point; double tp=Ask+50*Point;You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
- Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
- Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To
trigger at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 - 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.)
You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
- Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
- Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To
trigger at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 - 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.)
Hi emsahii.
If there's a struggle, its a good idea to get back to basics. Just write a simple EA without all the bells and whistles. And make sure you understand the debugging tools; the errors log in the editor, and the EA log and journal tabs on the MT4 terminal....
As Seng says, there are compilation errors, and even if you fixed them, you might not be learning for next time.....
If you ran only this code, that will at least create a buy order for you, with a SL and TP - if the Ask price is above the MA. It won't place an order until trading opens of course.
Then, you can look at expanding the code, adding criteria, magic numbers etc, running the code every x minutes etc.
Seems you did everything correctly? The compiler says otherwise... solve the compilation errors first.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello All !
I have been studying on coding my EA for 2 months. But I cant even code a simple EA for Buy Market Order. Eventhough it seems i did everything correctly, it does not work and apparently i am missing sth. I really appreciate your help and time. Thanks in advance for your sparing time and help.
Here is the code!