Download MetaTrader 5

Need an EA/SCRIPT to close an order at custom TP and SL levels

To add comments, please log in or register
Download documentation as one file. It is easy!
Proximus
760
Proximus 2013.12.18 22:14 

Does anyone know any EA or SCRIPT which will calculate the TP and SL levels by itself and closes the orders when either the SL calculated or TP calculated would be hit.Before you ask, this would be to get around STOPLEVEL and to allow me to trade little position sizes.

To understand it better it would look like this:

  ................................ 
 if (OrderSymbol() == Symbol() && OrderMagicNumber()==magic) 
    { 
if(OrderType()==OP_BUY && *TAKEPROFIT IS HIT* ) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Yellow); 
if(OrderType()==OP_SELL && *STOPLOSS IS HIT* ) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Yellow); 
if(OrderType()==OP_BUY && *STOPLOSS IS HIT* ) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Yellow); 
if(OrderType()==OP_SELL && *TAKEPROFIT IS HIT* ) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Yellow); 

.........................
}

Please if anyone know anything similar that can do this please help or give me a link to an existing EA or Script that can do this, thanks!

Ubzen
5394
Ubzen 2013.12.19 02:57  

Try Searching for StopLoss Managers.

Here.

Here.

Here.

Proximus
760
Proximus 2013.12.19 08:48  
ubzen:

Try Searching for StopLoss Managers.

Here.

Here.

Here.


Thanks but those are not good, since they use the classical Ordermodify() to put stoploss.I dont want that, in fact thats the point why i want automatic SL and TP.


What i want is to use the OrderClose(), and simulate the SL and TP with it, to let me manage small positions which could not be done by the Ordermodify() "classical SL and TP" because of the stoplevel.

On the GBPUSD it doesnt let me to put a SL and TP smaller than 8 pips, that is why i need to simulate the SL and TP in other ways.

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.19 09:02  
Proximus:

Thanks but those are not good, since they use the classical Ordermodify() to put stoploss.I dont want that, in fact thats the point why i want automatic SL and TP.


What i want is to use the OrderClose(), and simulate the SL and TP with it, to let me manage small positions which could not be done by the Ordermodify() "classical SL and TP" because of the stoplevel.

On the GBPUSD it doesnt let me to put a SL and TP smaller than 8 pips, that is why i need to simulate the SL and TP in other ways.

You prefer to risk your account by running with no SL so if you lose connection or your OrderClose() fails you have open ended risk ?
Tjipke de Vries
6315
Tjipke de Vries 2013.12.19 09:08  
Proximus:

Thanks but those are not good, since they use the classical Ordermodify() to put stoploss.I dont want that, in fact thats the point why i want automatic SL and TP.


What i want is to use the OrderClose(), and simulate the SL and TP with it, to let me manage small positions which could not be done by the Ordermodify() "classical SL and TP" because of the stoplevel.

On the GBPUSD it doesnt let me to put a SL and TP smaller than 8 pips, that is why i need to simulate the SL and TP in other ways.


then make SL and TP xx pips more if price is less then xx pips from your stoplevel you can do an orderclose on that trade
Proximus
760
Proximus 2013.12.19 09:27  
RaptorUK:
You prefer to risk your account by running with no SL so if you lose connection or your OrderClose() fails you have open ended risk ?


I will put normal SL and TP too, but those will be right at the stoplevel, so if the GBPUSD has a stoplevel of 8 pips the normal SL and TP will be ~10 pips,just for safety in case my internet goes out or anything bad happens.

BUT

Since my trading strategy is a quick scalping method, i must have positions ~ 4-5 pips which i cant do on the GBPUSD with normal TP and SL levels, so for that i want the OrderClose() to simulate the a 4 pip TP and a 5 pip SL for example.

So the OrderClose should close all buy & sell trades which are 4 pips away from the open price in profit, OR 5 pips away in losses from the open price, thats all i want!

I hope i explained it clear, please help me :)

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.19 09:30  
Proximus:

I hope i explained it clear, please help me :)

Not sure what you need help with, just OrderClose() according to your strategy . . .
Ubzen
5394
Ubzen 2013.12.19 09:38  
Proximus: I will put normal SL and TP too, but those will be right at the stoplevel, so if the GBPUSD has a stoplevel of 8 pips the normal SL and TP will be ~10 pips,just for safety in case my internet goes out or anything bad happens.

BUT

Since my trading strategy is a quick scalping method, i must have positions ~ 4-5 pips which i cant do on the GBPUSD with normal TP and SL levels, so for that i want the OrderClose() to simulate the a 4 pip TP and a 5 pip SL for example.

So the OrderClose should close all buy & trades which are 4 pips away from the start price in profit, OR 5 pips away in losses from the start price, thats all i want!

I hope i explained it clear, please help me :)

This was understood. This is very simple stuff. You check the OrderOpenPrice()-OrderClosePrice(). If the difference is >StopLossInPips then OrderClose(). If you're having problems coding the above, please try the Jobs service. I guarantee that someone there can do-it for you.

I've seen one of those order_manager OrderClose() the order as alternative should the StopLevel be >than the desired StopLoss. Anyways, if you Search for something and cannot find it .... then your best hope is coding it .... or paying someone else to code it for you.
Proximus
760
Proximus 2013.12.19 10:22  

Ok i see, i coded a little bit, here is the orderclose function then, i just thought there exists some pre-built out there but i guess i was mistaken.

I coded my own custom Orderclose, but there is a problem, it doesnt close orders as it should, can some1 help me correct it please:

extern double CLOSELOSSPIPS=10;
extern double CLOSEPROFITPIPS=10;

///////////////////////////////////////

if(OrdersTotal()> 0){
  for (int cnt = OrdersTotal()-1 ; cnt >= 0; cnt--) 
  { 
if(!OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES))
Print("Order SELECT failed, error # ", GetLastError() );
    
    if (OrderSymbol() == Symbol() && OrderMagicNumber()==magic) 
    { 
//////////////SL
if(OrderProfit()<0)
        if( MathAbs(OrderOpenPrice()-OrderClosePrice()) >CLOSELOSSPIPS*Point )  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Yellow); 
     
/////////////TP
if(OrderProfit()>0)
        if( MathAbs(OrderOpenPrice()-OrderClosePrice()) >CLOSEPROFITPIPS*Point)  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Yellow); 

      
    } 
  } }

The purpose is to run it on GBP/USD and close the orders at 10 pipette distance (5 digit broker so its 1 pip), and since i know that the MT4 strategy tester doesnt account for slippage, the spread was set to 1 pipette just for testing, yet the backtest showed very bizarre results.

Bars in test 4468265
Ticks modelled 14715298
Modelling quality 25.00%
Mismatched charts errors 0
Initial deposit 100000000.00
Total net profit -490.19
Gross profit 1072.76
Gross loss -1562.95
Profit factor 0.69
Expected payoff -0.03
Absolute drawdown 490.27
Maximal drawdown 490.95 (0.00%)
Relative drawdown 0.00% (490.95)
Total trades 17989
Short positions (won %) 9039 (35.25%)
Long positions (won %) 8950 (35.33%)
Profit trades (% of total) 6348 (35.29%)
Loss trades (% of total) 11641 (64.71%)
Largest
profit trade 1.61
loss trade -1.40
Average
profit trade 0.17
loss trade -0.13
Maximum
consecutive wins (profit in money) 10 (1.45)
consecutive losses (loss in money) 37 (-5.41)
Maximal
consecutive profit (count of wins) 1.78 (3)
consecutive loss (count of losses) -5.41 (37)
Average
consecutive wins 2
consecutive losses 3


As you can see the average profit was 0.17, and average loss 0.13, but how the heck can that happen if i have 10 pipette SL & TP, so the average win and loss should be 0.10 both +- 0.01 because of spread and since slippage is not simulated....,so i guess i made an error in the code,so please help me fix it :)

Ubzen
5394
Ubzen 2013.12.19 10:31  
Proximus: Ok i see, i coded a little bit, here is the orderclose function then, i just thought there exists some pre-built out there but i guess i was mistaken.

I coded my own custom Orderclose, but there is a problem, it doesnt close orders as it should, can some1 help me correct it please:

The purpose is to run it on GBP/USD and close the orders at 10 pipette distance (5 digit broker so its 1 pip), and since i know that the MT4 strategy tester doesnt account for slippage, the spread was set to 1 pipette just for testing, yet the backtest showed very bizarre results.

As you can see the average profit was 0.17, and average loss 0.13, but how the heck can that happen if i have 10 pipette SL & TP, so the average win and loss should be 0.10 both +- 0.01 because of spread and since slippage is not simulated....,so i guess i made an error in the code,so please help me fix it :)

You didn't make any mistakes. Price doesn't always move 1_Pipette_at_a_Time. Not in real-live, neither in the strategy-tester. I would have been surprised if you received exactly 0.10 for both after allot of trades ... real or tested.

If you need to test tick_sensitive strategies, mt4_strategy tester isn't for you. (neither is mt5 tester). You'll be better off live testing it ... imo.

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.19 10:40  
Proximus:

Ok i see, i coded a little bit, here is the orderclose function then, i just thought there exists some pre-built out there but i guess i was mistaken.

I coded my own custom Orderclose, but there is a problem, it doesnt close orders as it should, can some1 help me correct it please:

You don't need this . . .

if(OrdersTotal()> 0){

. . . what will your for loop do if Ordertotal() == 0 ?

If your OrderSelect() fails . . .

if(!OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES))
Print("Order SELECT failed, error # ", GetLastError() );   //  you print this line . . . 
    
    if (OrderSymbol() == Symbol() && OrderMagicNumber()==magic)   //  . . . and then carry on regardless as if everything was OK

. . . what will OrderSymbol() return when you have a failed OrderSelect() ?

12
To add comments, please log in or register