MQL4 Learning - page 21

 

The main problems with the code, I think, were that it didn't take spread and stop level into account, and then that the stop-loss for the SELL was placed below the open price. The following is a rehash of the code resolving those problems.

void AdjustingPendingOrders()

{

Print ( "Adjusting the MT pending orders" );

double spread = MarketInfo( Symbol(), MODE_SPREAD );

double level = MarketInfo( Symbol(), MODE_STOPLEVEL );

double offset = MathMax( spread, level );

double price = Bid - offset * Point;

double SL;

for ( int N = OrdersTotal() - 1; N >= 0; N-- ) {

if ( ! OrderSelect( N, SELECT_BY_POS, MODE_TRADES ) )

continue;

if ( OrderType() != OP_BUYSTOP && OrderType() != OP_SELLSTOP )

continue;

//---- working with pending orders only

//---- modifying

if ( OrderType() == OP_BUYSTOP ) {

SL = price - StopLoss * Point;

} else {

SL = price + StopLoss * Point;

}

OrderModify( OrderTicket(), price, SL, OrderTakeProfit(), 0, CLR_NONE );

}

}
 
ralph.ronnquist:
The main problems with the code, I think, were that it didn't take spread and stop level into account, and then that the stop-loss for the SELL was placed below the open price. The following is a rehash of the code resolving those problems.
void AdjustingPendingOrders()

{

Print ( "Adjusting the MT pending orders" );

double spread = MarketInfo( Symbol(), MODE_SPREAD );

double level = MarketInfo( Symbol(), MODE_STOPLEVEL );

double offset = MathMax( spread, level );

double price = Bid - offset * Point;

double SL;

for ( int N = OrdersTotal() - 1; N >= 0; N-- ) {

if ( ! OrderSelect( N, SELECT_BY_POS, MODE_TRADES ) )

continue;

if ( OrderType() != OP_BUYSTOP && OrderType() != OP_SELLSTOP )

continue;

//---- working with pending orders only

//---- modifying

if ( OrderType() == OP_BUYSTOP ) {

SL = price - StopLoss * Point;

} else {

SL = price + StopLoss * Point;

}

OrderModify( OrderTicket(), price, SL, OrderTakeProfit(), 0, CLR_NONE );

}

}

Thank You, Ralph.ronquist, for Your helpful reply.

I ran my program and it not works, it does not move my pending orders.

I think that maybe the syntax of my pending orders does not correspond to this function.

Would You please check my pending orders here:

OrderSend(Symbol(),OP_BUYSTOP,0.1,Ask+10*Point,3,Ask-10*Point,Ask+20*Point," aaa ",0,0,Green);

OrderSend(Symbol(),OP_SELLSTOP,0.1,Bid-10*Point,3,Bid+10*Point,Bid-20*Point," aaa ",0,0,Green);

Thanks a lot.

 
Iva:

Would You please check my pending orders here:

OrderSend(Symbol(),OP_BUYSTOP,0.1,Ask+10*Point,3,Ask-10*Point,Ask+20*Point," aaa ",0,0,Green);

OrderSend(Symbol(),OP_SELLSTOP,0.1,Bid-10*Point,3,Bid+10*Point,Bid-20*Point," aaa ",0,0,Green);

The BUYSTOP order requests a BUY when price has fallen down to the order price, which thus must be below current price.

The SELLSTOP order requests a SELL when price has risen up to the order price, which thus must be above current price.

 

Code to close any Open Buy or Sell positions at Friday PM (Market Close)

I would like to include this with my ea, that not only stops the EA from Trading, but also closes all active buy or sell positions of that particular currency being traded before the market closes. I have a tendency to forget about the market close and end up having open positions after the market closes over the weekend - Risky!!!

Your expertise will be sincerely appreciated!!!

Dave <<<
 
 

Clarifications

#1: I will not post threads in the MQL4 section. Nobody goes there. I checked the date on people's questions, and the date or the right reply. 13 days, 7 days,4 days, sometimes even more.

#2: Codersguru, I think quit this forum. He never writes anymore.

Anyway,

this is the best section. Can some1 help me for the following?

If buystop triggered, then sellstop is deleted.

---Julia---

 

Here is proof of what I have to a question.

Why does it modify both my sellstop and buystop, and suddenly, only my buystop?

16:42:28 Expert Hawk GBPJPY,M1: loaded successfully

16:43:09 '574405': pending order buy stop 0.10 GBPJPY at 240.02 sl: 239.82 tp: 243.92

16:43:09 '574405': request was accepted by server

16:43:09 '574405': request in process

16:43:09 '574405': order was opened : #11688399 buy stop 0.10 GBPJPY at 240.02 sl: 239.82 tp: 243.92

16:43:10 '574405': pending order sell stop 0.10 GBPJPY at 239.74 sl: 239.94 tp: 235.84

16:43:10 '574405': request was accepted by server

16:43:10 '574405': request in process

16:43:10 '574405': order was opened : #11688400 sell stop 0.10 GBPJPY at 239.74 sl: 239.94 tp: 235.84

16:43:18 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 240.02 sl: 239.82 tp: 243.92 -> price: 240.01 sl: 239.81 tp: 243.91

16:43:18 '574405': request was accepted by server

16:43:18 '574405': request in process

16:43:18 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 240.01 sl: 239.81 tp: 243.91

16:43:18 '574405': modify pending order #11688400 sell stop 0.10 GBPJPY at 239.74 sl: 239.94 tp: 235.84 -> price: 239.73 sl: 239.93 tp: 235.83

16:43:19 '574405': request was accepted by server

16:43:19 '574405': request in process

16:43:19 '574405': pending order #11688400 sell stop 0.10 GBPJPY was modified -> price: 239.73 sl: 239.93 tp: 235.83

16:43:21 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 240.01 sl: 239.81 tp: 243.91 -> price: 240.00 sl: 239.80 tp: 243.90

16:43:21 '574405': request was accepted by server

16:43:21 '574405': request in process

16:43:21 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 240.00 sl: 239.80 tp: 243.90

16:43:21 '574405': modify pending order #11688400 sell stop 0.10 GBPJPY at 239.73 sl: 239.93 tp: 235.83 -> price: 239.72 sl: 239.92 tp: 235.82

16:43:22 '574405': request was accepted by server

16:43:22 '574405': request in process

16:43:22 '574405': pending order #11688400 sell stop 0.10 GBPJPY was modified -> price: 239.72 sl: 239.92 tp: 235.82

16:43:27 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 240.00 sl: 239.80 tp: 243.90 -> price: 240.01 sl: 239.81 tp: 243.91

16:43:27 '574405': request was accepted by server

16:43:27 '574405': request in process

16:43:27 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 240.01 sl: 239.81 tp: 243.91

16:43:27 '574405': modify pending order #11688400 sell stop 0.10 GBPJPY at 239.72 sl: 239.92 tp: 235.82 -> price: 239.73 sl: 239.93 tp: 235.83

16:43:28 '574405': request was accepted by server

16:43:28 '574405': request in process

16:43:28 '574405': pending order #11688400 sell stop 0.10 GBPJPY was modified -> price: 239.73 sl: 239.93 tp: 235.83

16:43:30 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 240.01 sl: 239.81 tp: 243.91 -> price: 239.99 sl: 239.79 tp: 243.89

16:43:31 '574405': request was accepted by server

16:43:31 '574405': request in process

16:43:31 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.99 sl: 239.79 tp: 243.89

16:43:32 '574405': modify pending order #11688400 sell stop 0.10 GBPJPY at 239.73 sl: 239.93 tp: 235.83 -> price: 239.71 sl: 239.91 tp: 235.81

16:43:32 '574405': request was accepted by server

16:43:32 '574405': request in process

16:43:32 '574405': pending order #11688400 sell stop 0.10 GBPJPY was modified -> price: 239.71 sl: 239.91 tp: 235.81

16:43:34 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.99 sl: 239.79 tp: 243.89 -> price: 239.97 sl: 239.77 tp: 243.87

16:43:34 '574405': request was accepted by server

16:43:34 '574405': request in process

16:43:34 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.97 sl: 239.77 tp: 243.87

16:43:35 '574405': modify pending order #11688400 sell stop 0.10 GBPJPY at 239.71 sl: 239.91 tp: 235.81 -> price: 239.69 sl: 239.89 tp: 235.79

16:43:35 '574405': request was accepted by server

16:43:35 '574405': request in process

16:43:35 '574405': pending order #11688400 sell stop 0.10 GBPJPY was modified -> price: 239.69 sl: 239.89 tp: 235.79

16:43:38 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.97 sl: 239.77 tp: 243.87 -> price: 239.99 sl: 239.79 tp: 243.89

16:43:39 '574405': request was accepted by server

16:43:39 '574405': request in process

16:43:39 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.99 sl: 239.79 tp: 243.89

16:43:39 '574405': modify pending order #11688400 sell stop 0.10 GBPJPY at 239.69 sl: 239.89 tp: 235.79 -> price: 239.71 sl: 239.91 tp: 235.81

16:43:39 '574405': request was accepted by server

16:43:39 '574405': request in process

16:43:39 '574405': pending order #11688400 sell stop 0.10 GBPJPY was modified -> price: 239.71 sl: 239.91 tp: 235.81

16:43:43 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.99 sl: 239.79 tp: 243.89 -> price: 239.98 sl: 239.78 tp: 243.88

16:43:43 '574405': request was accepted by server

16:43:43 '574405': request in process

16:43:43 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.98 sl: 239.78 tp: 243.88

16:43:43 '574405': modify pending order #11688400 sell stop 0.10 GBPJPY at 239.71 sl: 239.91 tp: 235.81 -> price: 239.70 sl: 239.90 tp: 235.80

16:43:44 '574405': modification of pending order #11688400 sell stop 0.10 GBPJPY at 239.71 sl: 239.91 tp: 235.81 -> price: 239.70 sl: 239.90 tp: 235.80 failed

16:43:47 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.98 sl: 239.78 tp: 243.88 -> price: 239.96 sl: 239.76 tp: 243.86

16:43:47 '574405': request was accepted by server

16:43:47 '574405': request in process

16:43:47 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.96 sl: 239.76 tp: 243.86

16:43:49 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.96 sl: 239.76 tp: 243.86 -> price: 239.97 sl: 239.77 tp: 243.87

16:43:49 '574405': request was accepted by server

16:43:49 '574405': request in process

16:43:49 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.97 sl: 239.77 tp: 243.87

16:44:00 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.97 sl: 239.77 tp: 243.87 -> price: 239.98 sl: 239.78 tp: 243.88

16:44:00 '574405': request was accepted by server

16:44:00 '574405': request in process

16:44:00 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.98 sl: 239.78 tp: 243.88

16:44:01 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.98 sl: 239.78 tp: 243.88 -> price: 239.97 sl: 239.77 tp: 243.87

16:44:02 '574405': request was accepted by server

16:44:02 '574405': request in process

16:44:02 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.97 sl: 239.77 tp: 243.87

16:44:03 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.97 sl: 239.77 tp: 243.87 -> price: 239.96 sl: 239.76 tp: 243.86

16:44:04 '574405': request was accepted by server

16:44:04 '574405': request in process

16:44:04 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.96 sl: 239.76 tp: 243.86

16:44:08 '574405': modify pending order #11688399 buy stop 0.10 GBPJPY at 239.96 sl: 239.76 tp: 243.86 -> price: 239.97 sl: 239.77 tp: 243.87

16:44:08 '574405': request was accepted by server

16:44:08 '574405': request in process

16:44:08 '574405': pending order #11688399 buy stop 0.10 GBPJPY was modified -> price: 239.97 sl: 239.77 tp: 243.87

16:44:11 Expert Hawk GBPJPY,M1: removed[/PHP]

[PHP]16:43:47

Check this time, it is when it stops modifying the sellstop.

 

Thank you very much.

 

To keep it simple, you have to count the orders which OrderType() == Op_BUY. and those which OrderType() == OP_BUYSTOP. If those counts are different from the previous, (OP_BUY -> +1 and OP_BUYSTOP -> -1) you can assume that a BuyStop has triggered.

If there is only one (or few) BuyStop, you can also keep in memory it's ticket from the OrderSend() and check the variation of it's OpenTime : when a pending is triggered, the OpenTime is modified according to the trigger time.

Then you just have to scan all the orders, and when you find a SellStop, delete it.

BTW, are you Dan ?

 

Hi Iva, are you Dan too ?

Reason: