Help! - Is it possible to add Trailing Stop on OrderSend? I need trailing Stop on every new order placed.

 

Beginner here.

I have different conditions to open 5 orders at different time. However trailing stop will only run after the 5th order is opened.

How to code the trailing stop to function like OrderSend()'s take profit? Which is up and running once any new order is opened?

 
johnnybegoode:

Beginner here.

I have different conditions to open 5 orders at different time. However trailing stop will only run after the 5th order is opened.

How to code the trailing stop to function like OrderSend()'s take profit? Which is up and running once any new order is opened?

There is no such thing as a trailing stop in MQ4. EA's repeatedly modify EACH order (the SL) as the price moves.

Search the forum you'll find many examples of code

 
WHRoeder:

There is no such thing as a trailing stop in MQ4. EA's repeatedly modify EACH order (the SL) as the price moves.

Search the forum you'll find many examples of code

Thank you for your reply

I know, I have the regular trailing stop code that would not work for me.

example: If I have conditions that place 5 orders at different time, it will only run after the 5th order have been place, and that will cause me to miss out in opportunitiries of the previous already open orders.

Are there other ways to code trailing stop?


Regular trailing stop code:


for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(isCrossed == 2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet) ;
// close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);

 
johnnybegoode:

Thank you for your reply

I know, I have the regular trailing stop code that would not work for me.

example: If I have conditions that place 5 orders at different time, it will only run after the 5th order have been place, and that will cause me to miss out in opportunitiries of the previous already open orders.

Are there other ways to code trailing stop?


Regular trailing stop code:


for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(isCrossed == 2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet) ;
// close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);

The answer is it depends upon you and how you integrate into your own code. There is absolutely nothing to stop you checking the market and modifying all orders as each additional order is added.

Sounds like you need to:

a) Spend a little time and understand your own EA and how it works

b) Avoid thinking of code snippets as discrete functions - instead, look at them, understand the techniques embedded in them and then

c) INTERGRATE rather than APPEND



This is a common fault among non-programmers on the forum. It would be great if everyone's code was modular and you could drag and drop all additional functions you can find willy nilly. But hey, the perfect Services Oriented Architecture doesn't exist. And if it did, it wouldn't be written in MQL :-)


CB

 
johnnybegoode:

Thank you for your reply

I know, I have the regular trailing stop code that would not work for me.

example: If I have conditions that place 5 orders at different time, it will only run after the 5th order have been place, and that will cause me to miss out in opportunitiries of the previous already open orders.

Are there other ways to code trailing stop?


Regular trailing stop code:


for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
...

if(isCrossed == 2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet) ;
// close position
return(0); // exit
}

What CB said.

In addition, you don't want the returns, you want to process ALL orders.

In addition, you don't want to count up while closing orders. close cnt=0, cnt=1 becomes cnt=0 and you miss some.

for(cnt=TotalOrders()-1; cnt >= 0; cnt--) {...}

 
Anyone knows if any change in MT5 - is the trailing-stop still client-side only?
Reason: