How to send a pending order after open a trade

fxcba
Trader
Posts: 46
Joined: Wed Aug 21, 2013 10:10 pm

How to send a pending order after open a trade

Post by fxcba »

Hello all, I've made an EA (a very basic one) but it opens trades as I want to, now I want the EA put a pending order if a trade is opened. I've been searching in forum with no success yet, if this has been discussed please guide me and if is not, any advise is wellcome.
Thank you
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

How to send a pending order after open a trade

Post by renexxxx »

Sending a pending order (BUY STOP, BUY LIMIT, SELL STOP or SELL LIMIT) is as easy as sending a market order. Of course you need to be aware that for a BUY STOP order the STOP price must be above the current market price, and for a BUY LIMIT order the LIMIT price must be below the current market price.

Say that you want to place a BUY STOP order after you have successfully placed a BUY (market) order, you do something like this:

Code: Select all

   int ticket = OrderSend( _Symbol, OP_BUY, lotSize, Ask, slippage, stopLoss, takeProfit, "MyComment", magicNumber, 0, clrGreen ); // This is the Market Order

   if ( ticket > 0 ) {
      ticket = OrderSend( _Symbol, OP_BUYSTOP, pendingLotSize, pendingPrice, slippage, pendingStopLoss, pendingTakeProfit, "PendingComment", magicNumber, 0, clrGreen ); // The is the pending BUY STOP order
   }
(give the OrderSend parameters appropriate values).

Of course, it is prudent to build in fault tolerant code, such that the OrderSend()'s are retried if they fail. Or you can make use of the OrderReliable library.

Hopefully, this is not too confusing.
fxcba
Trader
Posts: 46
Joined: Wed Aug 21, 2013 10:10 pm

How to send a pending order after open a trade

Post by fxcba »

hello renexxx, thank you for your kind answer, I'm afraid I wasn't clear on my request.
My EA opens on the market trades correctly as I wish, now I want to: after a trade is open to put a new pending order using the code you provide. I don't know how to make EA recognize the new open trade and subsenquentely launch the pending order. In fact is a recovery loss order so if trade goes wrong and SL is hit that order is activated but if TP is hit or trade is closed on profit that order must be cancelled, may be there is a code for that. Thank you again
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

How to send a pending order after open a trade

Post by renexxxx »

fxcba » Mon Sep 26, 2016 8:20 pm wrote:In fact is a recovery loss order so if trade goes wrong and SL is hit that order is activated but if TP is hit or trade is closed on profit that order must be cancelled, may be there is a code for that. Thank you again
I think the question is about how to detect if an existing order has hit TP or SL. Unfortunately, Empty4 doesn't send an event, can you listen for, and handle in a OnTakeProfit() or OnStopLoss() event handler. Instead, you need to 'poll' constantly, to see if the status of in existing order has changed. The attached code is an example of how you can do this. Hope this helps.
You do not have the required permissions to view the files attached to this post.
fxcba
Trader
Posts: 46
Joined: Wed Aug 21, 2013 10:10 pm

How to send a pending order after open a trade

Post by fxcba »

Thank you my friend I'll try it.
fxcba
Trader
Posts: 46
Joined: Wed Aug 21, 2013 10:10 pm

How to send a pending order after open a trade

Post by fxcba »

Hello, it's me asking for some help :oops: again

Code: Select all

 for(shift=limit; shift>=0; shift--)
     {
      rsibuf= iRSI(NULL,0,RSIbarsPeriod,PRICE_CLOSE,shift);
       b4ma=iMAOnArray(rsibuf,0,MAperiod,0,MODE_SMA,shift+1);
      nowma=iMAOnArray(rsibuf,0,MAperiod,0,MODE_SMA,shift); 
Why in the above code I get this error?
'rsibuf' - parameter conversion not allowed R_Cross.mq4 71 23
User avatar
tomele
Administrator
Posts: 1166
Joined: Tue May 17, 2016 3:40 pm
Location: Germany, Forest of Odes, Defending the Limes

How to send a pending order after open a trade

Post by tomele »

Hi.

rsibuf is no array.
Happy pippin, Thomas :-BD

It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so.
(Mark Twain)

Keep the coder going: Donate
fxcba
Trader
Posts: 46
Joined: Wed Aug 21, 2013 10:10 pm

How to send a pending order after open a trade

Post by fxcba »

Hello tomele, thank you for your answer, please can you give me a little more detail?, how do I need to declare rsi on iMAOnArray?
User avatar
tomele
Administrator
Posts: 1166
Joined: Tue May 17, 2016 3:40 pm
Location: Germany, Forest of Odes, Defending the Limes

How to send a pending order after open a trade

Post by tomele »

fxcba » 02 Nov 2016, 19:22 wrote:Hello tomele, thank you for your answer, please can you give me a little more detail?, how do I need to declare rsi on iMAOnArray?
Declare rsibuf as array by "double rsibuf[]"
Then fill it with "rsibuf[shift]=iRSI(NULL,0,RSIbarsPeriod,PRICE_CLOSE,shift);"
Now you can do "iMAOnArray(rsibuf...)"
Happy pippin, Thomas :-BD

It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so.
(Mark Twain)

Keep the coder going: Donate
fxcba
Trader
Posts: 46
Joined: Wed Aug 21, 2013 10:10 pm

How to send a pending order after open a trade

Post by fxcba »

Thank you very much tomele.
I can't get that stuff yet
Thanks again
Locked

Return to “Newbie Discussion”