OrderClose not working - page 3

 

Thanks thrdel, I will look into your coding. You are really so kind. Thanks again

 
thrdel:


If you mean that it isn't the best way to close an order I agree but if the guy doesn't want to use OrderSelect (or doesn't know how) and doesn't leave any open orders in the market it works.

We try to help each other here and your answer did not helped a bit, you didn't show him how to use Order Select function and his EA isn't working any better now due to your comment.

Next time you want to help, do it the right way, put the right code in there so others may actually learn something .

 
Thanks everyone who post to my question. My intention is to enter trade when bar2 meet certain conditions. I am testing on GBPJPY and open one trade at a time as I am very new to MQL4. With only one trade i am able to use ticket in Orderclose(). I am still not familiar with OrderSelect. So the trade will close if bar3 meet certain condition. I would like to ask any question, is there a way to store the execution of the program inside a file for debugging purpose? As I found that when the program execute, I dont know which part of the execution went wrong.
 
chuale:
Thanks everyone who post to my question. My intention is to enter trade when bar2 meet certain conditions. I am testing on GBPJPY and open one trade at a time as I am very new to MQL4. With only one trade i am able to use ticket in Orderclose(). I am still not familiar with OrderSelect. So the trade will close if bar3 meet certain condition. I would like to ask any question, is there a way to store the execution of the program inside a file for debugging purpose? As I found that when the program execute, I dont know which part of the execution went wrong.

You're welcome Chuale, I learned much from others on this forum too so no problem. You don't really need to save the execution of the program into a file, there are a few ways to watch your variables values and any errors that may come up. I use mostly Comment() function and Print () function. I can send examples to you if you need. As I mentioned before GBPJPY didn't give me positive results but USDJPY did. I only run a back test for a month (Jan21/2014 - Feb25/2015) and with my settings the return was a bit over 400 USD. I will test other pairs as soon as I have a bit of time and let you know. Also if you need help with how to use Print() or Comment() to see values on screen in real time, let me know. Previously attached picture was your EA performance graph over a month period. Doesn't look to bad so far.
 

Dear thedel,

Thank you

Yes, i am using Print(). I need to know how to use OrderSelect() if there are more open trades. For a single trade, if I use SELECT_BY_POS, what shall be the value for index (OrderSelect (int index, in select, int pool=MODE_TRADES)? Is it 1?

or what value?

 
chuale:
Thanks everyone who post to my question. My intention is to enter trade when bar2 meet certain conditions. I am testing on GBPJPY and open one trade at a time as I am very new to MQL4. With only one trade i am able to use ticket in Orderclose(). I am still not familiar with OrderSelect. So the trade will close if bar3 meet certain condition. I would like to ask any question, is there a way to store the execution of the program inside a file for debugging purpose? As I found that when the program execute, I dont know which part of the execution went wrong.


Here is an example of how to use Order Select function :

In this example, the CountTrades function checks all open and pending orders, discards the ones that don't have a matching symbol and magic number and counts only the ones that we need.

In this example it will select from most recent one to the first one.

If you need more help with it let me know.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
/*
On every tick program checks what the value of myTrades is
by calling (executing) the CountTrades() function and asign the value
returned to myTrades..
*/
int myTrades = CountTrades();
/*
Comment() function will display the words in quotes and the value of 
myTrades variable.
*/
Comment("You have "+myTrades+" open trades.");
   
  }
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;// we start counting from zero
int trade;  // define a variable that will hold the total number of trades that are open
// Get the number of trades from OrdersTotal() and asign that value to trade variable.
for(trade=OrdersTotal()-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES); // Select orders by position (OP_BUY and OP_SELL) - orders can be selected by position or by ticket
                                                 // select from Open or pending orders = MODE_TRADES or select from History = MODE_HISTORY
   if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber)// if the order has a different symbol or magic number, skip to next order
   continue;
   if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)// if symbol is right and magic number is right, that is my order ,then
   if(OrderType()==OP_SELL                                         // if it's an open sell do this.....
   || OrderType()==OP_BUY)                                         // if it's an open buy do this......      
   count++;                                                        // in this example increase the counter since I'm counting all my orders
   }
return(count);                                                     // When finished counting, return the result
}
 
thrdel:


If you mean that it isn't the best way to close an order I agree but if the guy doesn't want to use OrderSelect (or doesn't know how) and doesn't leave any open orders in the market it works.

We try to help each other here and your answer did not helped a bit, you didn't show him how to use Order Select function and his EA isn't working any better now due to your comment.

Next time you want to help, do it the right way, put the right code in there so others may actually learn something .


if you try to help then explain it correctly

you said

thrdel:

First you count orders with OrdersTotal().

total=OrdersTotal();


bad example...... you gave

if the guy doesn't want use OrderSelect() then he has to stop programming EA's

big nonsens what you tell here, https://www.mql5.com/en/forum/149958/page2#915413

i commented your example code and told you what you had to do

there are plenty examples i have given how to do ...

google for it if you want find them

 
thrdel:


There are a couple of things you did wrong. I assume you want this EA to work on JPY pairs on M1 time frame. Do you want to place orders on new bar only and close orders on new bar only or do you want entry on new bar and exit if conditions are met? Here is what I think :

First you count orders with OrdersTotal().

Then calculate your variables

If there are orders in the market check if they have to be closed

If no orders in the market, check if entry conditions are met.

Let me know if you have any questions.

 
when compiled the following:it said Time function not define. Please advise.
if(Time[0]==previousTime) return(0);         //EA will not go past this point unless it is a new bar
   previousTime=Time[0];                        // If it was a new bar , it's old now but continue to the end
 
chuale:
when compiled the following:it said Time function not define. Please advise.


ok i got it fixed as I typed wrongly the parenthesis for Time, it is suppose to be [] instead of (). Thanks
Reason: