Well, simply put the cursor on OrderSelect(..) and press f1 - now read what OrderSelect returns and what true might be as an integer!
extern int sl = 100;
extern int tp = 100;
extern int tf = 0;
extern int slippage = 5;
double DailyOpen = iOpen(NULL,PERIOD_D1,0);
double CurrentBar = iOpen(NULL,0,0);
You shouldn't assign values obtained from iOpen here; data might not be available yet.
{
if (CurrentBar > DailyOpen) return(true);
else return(EMPTY_VALUE);
}
A bool should return either true or false. Not EMPTY_VALUE, which is 2147483647, which will be interpreted as true.
The way you have written it, trendLong() always returns true. And so does trendShort().
{
if(trendLong() == true) ticket = OrderSelect(ticket,SELECT_BY_TICKET);
if(OrdersTotal() != 0)
{
ticket = OrderClose(ticket,lot,Bid,slippage);
}
else
{
(OrderCloseTime() < CurrentBar);
openBuy();
}
if (trendShort() == true)
ticket = OrderSelect(ticket,SELECT_BY_TICKET);
if (OrdersTotal() != 0)
{
ticket = OrderClose(ticket,lot,Bid,slippage);
}
else
{
(OrderCloseTime() < CurrentBar);
openSell();
}
}
This whole section doesn't really make any sense. There are expressions with no effect trying to use OrderCloseTime, you're assigning the value of OrderSelect (true / false) to ticket. Closing orders. I don't know what you're trying to do, but I'm sure it can be done more simply ;-)
extern int sl = 100;
extern int tp = 100;
extern int tf = 0;
extern int slippage = 5;
double DailyOpen = iOpen(NULL,PERIOD_D1,0);
double CurrentBar = iOpen(NULL,0,0);
You shouldn't assign values obtained from iOpen here; data might not be available yet.
{
if (CurrentBar > DailyOpen) return(true);
else return(EMPTY_VALUE);
}
A bool should return either true or false. Not EMPTY_VALUE, which is 2147483647, which will be interpreted as true.
The way you have written it, trendLong() always returns true. And so does trendShort().
{
if(trendLong() == true) ticket = OrderSelect(ticket,SELECT_BY_TICKET);
if(OrdersTotal() != 0)
{
ticket = OrderClose(ticket,lot,Bid,slippage);
}
else
{
(OrderCloseTime() < CurrentBar);
openBuy();
}
if (trendShort() == true)
ticket = OrderSelect(ticket,SELECT_BY_TICKET);
if (OrdersTotal() != 0)
{
ticket = OrderClose(ticket,lot,Bid,slippage);
}
else
{
(OrderCloseTime() < CurrentBar);
openSell();
}
}
This whole section doesn't really make any sense. There are expressions with no effect trying to use OrderCloseTime, you're assigning the value of OrderSelect (true / false) to ticket. Closing orders. I don't know what you're trying to do, but I'm sure it can be done more simply ;-)
Wow honest_knave!! THANK YOU! I was assigning iOpen values when they were not even there. Okay. So, I have simplified my attempt by removing extra functions and what not from code. Now, I see that EA does open both Buy and Sell trades. The next thing I am trying to do here is that if a condition becomes false then EA should close position and open a trade in favor of new condition. I am pasting my code here. Please review. Many many thanks for your advice and guidance.
-----------------------------------------------------------------
extern int sl = 100;
extern int tp = 100;
extern int slippage = 5;
int ticket;
void OnTick()
{
double DailyOpen = iOpen(NULL,PERIOD_D1,0);
double CurrentBar = iOpen(NULL,0,0);
if (CurrentBar > DailyOpen)
{
ticket = OrderSelect(ticket,SELECT_BY_TICKET); //trying to use this to check if any ticket is previously open
ticket = OrderClose(ticket,lot,Bid,slippage); //trying to close any previously open ticket
if (OrdersTotal() == 0)
ticket = OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-sl*Point,Ask+tp*Point,"EA Trade");
}
else
{
ticket = OrderSelect(ticket,SELECT_BY_TICKET);
ticket = OrderClose(ticket,lot,Bid,slippage);
if (OrdersTotal() == 0)
ticket = OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,Bid+sl*Point,Bid-tp*Point,"EA Trade");
}
}
I've edited your post to use SRC.
Thinking through your logic:
When this evaluates as true, you need to:
1. Close any short positions
2. Open a long position, if you haven't already.
You could have a variable to remember if you have already opened an order.
However, if the overhead isn't too much I always suggest checking the information rather than relying upon a saved variable.
This means your EA is more robust and can recover from restarts or in case something else closes out a position etc.
See if this makes sense to you.
{
bool buy_order_found = false; // presume there isn't a buy order
for(int i=OrdersTotal()-1; i>=0; i--) // loop through all the orders in the terminal
{
if(!OrderSelect(i,SELECT_BY_POS)) continue; // if unable to select the order, move on
if(OrderSymbol() != _Symbol) continue; // if a different symbol, move on
if(OrderMagicNumber() != magic_no) continue; // if a different magic number, move on
int order_type = OrderType();
if(order_type == OP_SELL) // if a sell order found, close it
{
if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage))
{
printf("OrderClose() failed. Error code: %i",GetLastError());
}
}
else if(order_type == OP_BUY) buy_order_found = true; // if a buy order found, change the flag
}
if(!buy_order_found) // if no buy orders were found, open one
{
RefreshRates();
if(OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-sl*Point,Ask+tp*Point,"EA Trade",magic_no)<0)
{
printf("OrderSend() failed. Error code: %i",GetLastError());
}
}
}
I've edited your post to use SRC.
Thinking through your logic:
When this evaluates as true, you need to:
1. Close any short positions
2. Open a long position, if you haven't already.
You could have a variable to remember if you have already opened an order.
However, if the overhead isn't too much I always suggest checking the information rather than relying upon a saved variable.
This means your EA is more robust and can recover from restarts or in case something else closes out a position etc.
See if this makes sense to you.
{
bool buy_order_found = false; // presume there isn't a buy order
for(int i=OrdersTotal()-1; i>=0; i--) // loop through all the orders in the terminal
{
if(!OrderSelect(i,SELECT_BY_POS)) continue; // if unable to select the order, move on
if(OrderSymbol() != _Symbol) continue; // if a different symbol, move on
if(OrderMagicNumber() != magic_no) continue; // if a different magic number, move on
int order_type = OrderType();
if(order_type == OP_SELL) // if a sell order found, close it
{
if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage))
{
printf("OrderClose() failed. Error code: %i",GetLastError());
}
}
else if(order_type == OP_BUY) buy_order_found = true; // if a buy order found, change the flag
}
if(!buy_order_found) // if no buy orders were found, open one
{
RefreshRates();
if(OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-sl*Point,Ask+tp*Point,"EA Trade",magic_no)<0)
{
printf("OrderSend() failed. Error code: %i",GetLastError());
}
}
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I am brand new to MQL4 and this is my very 1st attempt at writing an EA. I have created two functions for trendLong and trendShort. If current bar's open is higher than daily open then it should open a long position and vice versa for short positions.
1- What I see is that it only opens long positions.
2- It does not check if condition to sell is now true.
3- It does not close a position when opposite condition becomes true.
I hope someone of you can help guide me through the learning process.