can anyody point me where is my code wrong? since it doesn't want to entry order by itself

 

hi,

can anybody help me with my code? my code are just simple, i want to get the open price at 12.00, and then wait until the price move above or below the open price around 20pips. After that entry buy or sell depend on the situation, if goes up, then sell, if goes down, then buy. and the code only works once a day.


My code is

int total=OrdersTotal();

double hargaskrg=Close[0];
int letakcandle=iBarShift(NULL, PERIOD_H1, StrToTime("12:00"), false);
double hargaopen=Open[letakcandle];
int ticket;

if (total == 0)
{
if (hargaskrg == hargaopen + 20*Point)
{
OrderSend(NULL,OP_SELL,0.1,hargaskrg,3,hargaopen-60*Point,hargaopen+100*Point,"My order #2",16384,0,Green);
return(0);
}
else if (hargaskrg == hargaopen - 20*Point)
{
OrderSend(NULL,OP_BUY,0.1,hargaskrg,3,hargaopen+60*Point,hargaopen-100*Point,"My order #2",16384,0,Green);
return(0);
}
}


after i try to attach to my chart, i still didn't want to make an entry. i don't know where is my error

 
For Starters, don't use hargaskrg == hargaopen. Instead use hargaskrg >= hargaopen and hargaskrg <= hargaopen respectively. Also, don't copy and paste code in here. Use the SRC button above or Attach the .mql4 file.
 

Please use this to post code . . . it makes it easier to read.

You don't seem to be adjusting your TP, SL or Slippage depending on your Broker being 4 or 5 digit, why not ?

You don't seem to be checking if your order was placed correctly and you don't seem to be looking at any errors generated if it wasn't, why not ?

You can't place a Market order (OP_BUY, OP_SELL) at a price above/below Ask and Bid

 
ubzen:
For Starters, don't use hargaskrg == hargaopen. Instead use hargaskrg >= hargaopen and hargaskrg <= hargaopen respectively. Also, don't copy and paste code in here. Use the SRC button above or Attach the .mql4 file.

hi ubzen, thanks for your time. and sorry to have confused you. ok, next time i'll use SRC button or just attach my mql file

regarding ur advice, let's say in example to be easier

in EURUSD, the open price is 1.4300, i want to wait until 1.4320, then entry the sell order at 1.4320. if i use hargaskrg >= hargaopen, will it entry again at 1.4321 ? because 1.4321 is also fulfilled the condition of hargaskrg >= hargaopen.

 
RaptorUK:

Please use this to post code . . . it makes it easier to read.

You don't seem to be adjusting your TP, SL or Slippage depending on your Broker being 4 or 5 digit, why not ?

You don't seem to be checking if your order was placed correctly and you don't seem to be looking at any errors generated if it wasn't, why not ?

You can't place a Market order (OP_BUY, OP_SELL) at a price above/below Ask and Bid


hi raptor, thanks also for your time looking at my problem. sorry if i ask a lot, but slippage is same as spread ?

if yes, my broker is using spread 3poins.

for the error report, i try to add "Print" code. but still the same, last time i try also, didn't generate any report

will you take a look at my code? i've attached it.

the logic is easy.

same as what i've type above,

when the market open at 12.00, i get the open price, then wait until it goes up or down 20pips,

case 1, if goes down 20 pips, i open buy position. with TP 60pip from open price and SL 100pip from open price.

if it continues down again another 20pip, meaning down 40pips from open price, i open another buy position with same TP and SL from first position.

if it continue moving down again another 20pip, meaning already down 60pips from open price, i open 3rd position of buy with same TP and SL

and vice versa for the sell position.

either TP or SL is hit, all condition will be removed at once. and the EA will run again the next day.

Files:
pagi2.mq4  4 kb
 

in EURUSD, the open price is 1.4300, i want to wait until 1.4320, then entry the sell order at 1.4320. if i use hargaskrg >= hargaopen, will it entry again at 1.4321 ? because 1.4321 is also fulfilled the condition of hargaskrg >= hargaopen.

Yeah, but with your == it'll open again if price went down from 1.4321 to 1.4320. I don't have the ability to read minds and neither does the computer it just do as you say. If you want a limiter then add a limiter. Example: if OrdersTotal()==0 then OrderSend(). <--- Very bad example but allot for coding use this. Your simple code have too many Issues to list out. So one thing at a time.

The reason why you use > and < is because Doubles are rearly ever == in mql4 environment. What you think is 1.4320 usually is something like 1.4320000123 and your EA will almost never place that trade Live. It'll have even less chance within the back-tester which runs on 1-Minute data.

 
lucif:

1. hi raptor, thanks also for your time looking at my problem. sorry if i ask a lot, but slippage is same as spread ?

if yes, my broker is using spread 3poins.

2. for the error report, i try to add "Print" code. but still the same, last time i try also, didn't generate any report


1. slippage is not the same as spread . . .

From here: https://book.mql4.com/trading/ordersend

"slippage is the maximum allowed deviation of the requested order open price from the market price for market orders (points). This parameter is not processed for placing of pending orders."

2. OK, well done for trying to add some code to print the error. . . . take a look at your code, where does your variable ticket get set to the ticket number of the order ?

Are you using a 4 digit Broker ? are values for GBPUSD quoted with 4 decimals or 5 ? Without adjusting your values then your code may currently work for a 4 digit Broker but nor correctly for a 5 digit Broker . .

 
ubzen:

in EURUSD, the open price is 1.4300, i want to wait until 1.4320, then entry the sell order at 1.4320. if i use hargaskrg >= hargaopen, will it entry again at 1.4321 ? because 1.4321 is also fulfilled the condition of hargaskrg >= hargaopen.

Yeah, but with your == it'll open again if price went down from 1.4321 to 1.4320. I don't have the ability to read minds and neither does the computer it just do as you say. If you want a limiter then add a limiter. Example: if OrdersTotal()==0 then OrderSend(). <--- Very bad example but allot for coding use this. Your simple code have too many Issues to list out. So one thing at a time.


Example: if OrdersTotal()==0 then OrderSend(). <--- Very bad example but allot for coding use this.

thanks ubzen, but for the example you've say, i still don't really get it, may i know why it can't be used? since my logic is i check first wether there is an open position or not using OrdersTotal(), if 0, then i'll open the position.

anyway, this is just my logic and I would be happy to share it with you once i can implemented it
If this is a pain in the, well you know, i certainly do not want to make you put a lot of time into it... But if it something that is not too difficult...
Let me know what you think, thanks ubzen..

 

Also . . .

int tipe=OrderType();

You can only get OrderType for an existing order and only after you have used orderSelect . . see: https://docs.mql4.com/trading/OrderSelect and https://docs.mql4.com/trading/OrderType

 
RaptorUK:

1. slippage is not the same as spread . . .

From here: https://book.mql4.com/trading/ordersend

"slippage is the maximum allowed deviation of the requested order open price from the market price for market orders (points). This parameter is not processed for placing of pending orders."

2. OK, well done for trying to add some code to print the error. . . . take a look at your code, where does your variable ticket get set to the ticket number of the order ?

Are you using a 4 digit Broker ? are values for GBPUSD quoted with 4 decimals or 5 ? Without adjusting your values then your code may currently work for a 4 digit Broker but nor correctly for a 5 digit Broker . .


hi raptor, thanks for the explanation. so slippage is some kind like the maximum allowed price tolerance ?

my ticket variable get set to the ticket number when the ordersend executed, right? correct me if i'm wrong..

seems like i'm using 4 digit broker, because there are 4 decimals quoted for GBPUSD.

 

Example: if OrdersTotal()==0 then OrderSend(). <--- Very bad example but allot for coding use this.
thanks ubzen, but for the example you've say, i still don't really get it, may i know why it can't be used? since my logic is i check first wether there is an open position or not using OrdersTotal(), if 0, then i'll open the position.

The reason it's a bad coding example is because there might be other EA's or Manual orders placed by you. The OrdersTotal() could be >0 even when this EA didn't place any order yet. So this EA will miss an opportunity to open an order because it thinks it already created an order. To solve this issue, you'll usually do a OrderSelect() Loop and Filter by Magic#.

You can send me a private message with your idea. I'll let you know if it's a basic idea or not. Unfortunately, I have a few projects on the top of my priority list right now. Therefore, I wouldn't be able to provide tutor level support nor code it out for you at this time.

Reason: