Need help on CloseOrder at SAR cross

 

Hi great guys, I'm new to MQL4 and would need help on some basic things that I did not manage to search for in the forum. Here is my first tentative code for a script to close the current buy order if price crosses below SAR:

int start()
{
//----
if(iSAR(NULL,0,0.02,0.2,0)>Close[0])
{
OrderClose(1071658,1,Ask,5,Red);
}
//----
return(0);
}


My questions are:

1. When does the above script execute the CloseOrder? At the opening of the next bar (the current bar being the one that crosses SAR)?

2. Among OrderClose parameters, is the order_ide the number in the first column of View \ Terminal \ Trade tab?

3. Does the price paramater (that MetaEditor Help explains as "preferred closing price") always mean I have to put a Bid for closing a Buy order, and Ask for a Sell order? (If yes, why "preferred"?).

4. Is it possible to replace the Close[0] with Bid in the If condition? That is, can the script compare instantaneously the latest SAR value with the Bid value during the bar formation and execute the CloseOrder command right away instead of waiting for the bar to complete?

5. If the answer to 4) is Yes, what is the execution price for the CloseOrder command? The CloseOrder paramaters request the "closing" price, which seems like we have to wait for bar completion anyway.

6. If I have 3 open charts, with 3 orders for 3 pairs, order lot sizes are differenent, looking at the OrderClose parameters it seems I have to create a unique Script for each order which will function on a unique chart. So 3 scripts for 3 orders, am I right?

7. Program Run: When I have compiled the file (named SAR exit), I actually see it at the Terminal \ Navigator window, in Scipts folder. However, I do not see any "sign" when I drag and drop the "SAR exit" from the Navigator to the chart. What am I missing here? Also, the Terminal\ Navigator does not show whether a file is .mq4 or .ex4 (already compiled). Seems like a file not yet compiled (.mq4) also appeared in the Navigator window of the client terminal.

8. Is it possible to create trailing stops using SAR value (instead of executing a CloseOrder command)?

MetaEditor language seems like Chinese to me, I would very very much appreacite your help for my learning from scratch.

Cheers

 

1. When does the above script execute the CloseOrder? At the opening of the next bar (the current bar being the one that crosses SAR)?

If the condition is true, the next operation is to close the order

2. Among OrderClose parameters, is the order_ide the number in the first column of View \ Terminal \ Trade tab?

OrderClose() has a ticket number found via OrderTicket()

3. Does the price paramater (that MetaEditor Help explains as "preferred closing price") always mean I have to put a Bid for closing a Buy order, and Ask for a Sell order? (If yes, why "preferred"?).

You specify the current bid/ask price as your preferred price and a value for slippage, which can be 0 or more pips.

4. Is it possible to replace the Close[0] with Bid in the If condition? That is, can the script compare instantaneously the latest SAR value with the Bid value during the bar formation and execute the CloseOrder command right away instead of waiting for the bar to complete?

Close[0] is equivalent to Bid, and yes

5. If the answer to 4) is Yes, what is the execution price for the CloseOrder command? The CloseOrder paramaters request the "closing" price, which seems like we have to wait for bar completion anyway.

Close[0] is same as Bid. For example, assume no more price changes occur during the bar, then the current bid is equivalent to Close[0].

6. If I have 3 open charts, with 3 orders for 3 pairs, order lot sizes are differenent, looking at the OrderClose parameters it seems I have to create a unique Script for each order which will function on a unique chart. So 3 scripts for 3 orders, am I right?

yes, 3 commands, not necessarily 3 scripts

7. Program Run: When I have compiled the file (named SAR exit), I actually see it at the Terminal \ Navigator window, in Scipts folder. However, I do not see any "sign" when I drag and drop the "SAR exit" from the Navigator to the chart. What am I missing here? Also, the Terminal\ Navigator does not show whether a file is .mq4 or .ex4 (already compiled). Seems like a file not yet compiled (.mq4) also appeared in the Navigator window of the client terminal.

You probably did not see a file not already compiled.

Look in the terminal and you will see it attached and removed aftr it runs.

2008.01.03 00:51:10 Phy__EmptyScript EURUSD,M15: loaded successfully
2008.01.03 00:51:10 Phy__EmptyScript EURUSD,M15: uninit reason 0
2008.01.03 00:51:10 Phy__EmptyScript EURUSD,M15: removed

8. Is it possible to create trailing stops using SAR value (instead of executing a CloseOrder command)?

It is possible to create trailing stops using OrderModify() to change the StopLoss in an open order

 

Thanks phy for your advice, just a few more concerns,

I have revised the code as:

if(iSAR(NULL,0,0.02,0.2,0)>Close[0]

{

CloseOrder(OrderTicket(),OrderLots(),Bid,5,Red);

}

1) Is the code correct, and in so writing, can I use this one script to launch at several windows (for several open buy orders)?

2) The compiling returns no error. However, in the terminal Navigator\ Scripts, when I drag and drop the script (named (Th) Close Buy Order) from the Navigator to the chart window (GBPUSD,M1 for testing purpose), the following lines appeared

2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: removed
2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: uninit reason 0
2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: loaded successfully
2008.01.03 15:46:01 Compiling '(Th) Close Buy Order'

And actually nothing happened when the bar crosses the SAR. Moreover, I do not see anything on the chart to recognize that "something is attached". Is there that kind of sign?

3) I am a bit confused with your previous reply about replacing Close[0] with Bid. Just to clarify myself: say that I trade on H1 chart and launch this script. The latest bar opened above the SAR (i.e uptrend intact), but after 10 minutes it shoots below the SAR. Right now, does the script execute the CloseOrder command at the current Bid price? Or do we have to wait until the 1-hour bar completes its formation, and only now if SAR > Close will the CloseOrder be executed at the closing bid price (which is the same as the next bar's opening bid price)?

Many thanks again phy for your great help,

Cheers

 

Thanks phy for your advice, just a few more concerns,

I have revised the code as:

if(iSAR(NULL,0,0.02,0.2,0)>Close[0]

{

CloseOrder(OrderTicket(),OrderLots(),Bid,5,Red);

}

1) Is the code correct, and in so writing, can I use this one script to launch at several windows (for several open buy orders)?

No, it will do nothing

2) The compiling returns no error. However, in the terminal Navigator\ Scripts, when I drag and drop the script (named (Th) Close Buy Order) from the Navigator to the chart window (GBPUSD,M1 for testing purpose), the following lines appeared

2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: removed
2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: uninit reason 0
2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: loaded successfully
2008.01.03 15:46:01 Compiling '(Th) Close Buy Order'

And actually nothing happened when the bar crosses the SAR. Moreover, I do not see anything on the chart to recognize that "something is attached". Is there that kind of sign?

No.



3) I am a bit confused with your previous reply about replacing Close[0] with Bid. Just to clarify myself: say that I trade on H1 chart and launch this script. The latest bar opened above the SAR (i.e uptrend intact), but after 10 minutes it shoots below the SAR. Right now, does the script execute the CloseOrder command at the current Bid price? Or do we have to wait until the 1-hour bar completes its formation, and only now if SAR > Close will the CloseOrder be executed at the closing bid price (which is the same as the next bar's opening bid price)?

You are going to have to learn a bit more before you get something useful working.

Perhaps reading the documentation a bit more and looking at some other code samples will help, because you are clueless.

have a nice day.

 

Hi phy,

I have attempted the following code according to instructions in another thread:

int start()
{
double sar1 = iSAR(NULL,0,0.02,0.2,1);
double sar2 = iSAR(NULL,0,0.02,0.2,0);

//----
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)

{
//close buy order
if(sar1<Open[1] && sar2>Open[0])
{
CloseOrder(OrderTicket(),OrderLots(),Bid,3,Red);
}
//close sell order
if(sar1>Open[1] && sar2<Open[0])
{
CloseOrder(OrderTicket(),OrderLots(),Ask,3,Red);
}
}

else Print( "Error when order select ", GetLastError());
//----
return(0);
}

When compiling, it returns the "\end_of_program" - unbalanced left paranthesis. I can't see what parenthesis is left unbalanced. Could you please help?

Thank you very much for your kind support.

 

Here's one

if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)

 

Thanks phy, it's the one. I also corrected the CloseOrder typing error, which should be .

However, nothing proceeds as usual...

It keeps returning 3 lines (loaded successfully, uninit reason 0, removed). Also, it does not prompt "do you really want to attach... to chart...".

Please correct me if I'm wrong: the answer does not seem to be somewhere in the Documentation. the "uninit reason 0" seems like my problem has more to do with programming logic than with the coding itself. I will need to think over the logic, e.g I should use the while... operator or something... -:) ?

Let me clarify myself: my modest understanding of programming logic is that, one should be able to "state the logic" in real-life terms before coding it. Implicitly up to now, the script to solve my problem (close buy/sell orders at SAR cross after manually entering positions) has been stated as:

" If on the current chart, price crosses the SAR, then execute the OrderClose with these parameters...."

However, since the coding does not work, I wonder should the script be re-stated more precisely as:

"On the current chart, check the 2 last price bars and SAR dots. Compare them, if price crosses SAR then execute the OrderClose with these parameters. If not, wait for the next price bar/ SAR dot to develop and check again the crossing etc. .."

The "if not, then wait for the next bar..." sounds like one should play with a loop, a while operator, a break function... etc. Whew, if you are from China where people use chopsticks for eating rice, recall the first time you eat Italian spaghetti with the fork...

The Documentation seems to be more appropriate for C-conversant players than people without programming guts like me... -:( . I used to get along quite well with the Metastock Formula Language, but this MQL is true information technology -:) ...

I am not asking for a ready made solution to my needs, but any clues would very much be appreciated!

Cheers

 

If you are running a script:

You attach it to a chart

2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: loaded successfully

It runs on time through the code, and then it is done. Script (unless it loops internally) makes one pass through the code and that is it.
uninit reason 0 just says it is done.

2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: uninit reason 0

And then it is removed from the chart

2008.01.03 15:47:38 (Th) Close Buy Order GBPUSD,M1: removed

-------------------

Script: runs one time, this apparently is not what you want.

ExpertAdvisor: runs on every tick. System causes it to be run again and again.

Put your code into an EA.

------------------

 

Thanks phy, That's exactly the answer I need. I will need to start all over again.

 

Hello phy,

Could you please explain the OrderSelect() parameters? Due to English not being my mothertongue language, I find it hard to understand the terms in the documentation.

OrderSelect(index,SELECT_BY_POS,MODE_TRADES)

What is the "order trading pool"? Is it all the pending and open orders that you can see in the Terminal window\ Trade tab?

What is the order index? Is it simply its position in the line of orders, where 0 means the first order, 1 the second, 2 the third?

This is where I'm confused: Say I open 3 charts for 3 pairs, each of which has one open order (so 3 orders in total), if I run a script or an EA on a single chart with a single open order, what parameters should I use with the OrderSelect()?

OrderSelect(0,SELECT_BY_POS,MODE_TRADES)

Or looking at the line of open orders, see at what position the order I am going to use script/EA on is in the line, put that "position number" or "index" in the OrderSelect(), then turn to the chart containing that order, and execute script/EA on that chart?

If the latter is correct, it seems not very convenient since you have to edit each and every OrderSelect() to fit with a single order instead of taking advantage of the fact that just one (or no) order is open on any one chart.

Your clarification is greatly appreciated -:)

Cheers

 

Take a look at "MACD Sample" in the expert advisors that came with your MT4.

See how orderSelect is used.

Reason: