How do I move Stop Loss to Breakeven with a certain ATR value?

Basically, I want my EA to manage open trades with OrderModify().

My EA is programmed to take trades when 2 Moving Averages are crossing.

Every time the 2 Moving Averages cross, it opens 2 trades.

Take Profit = 1.0 * ATR

Stop Loss = 1.5 * ATR

No Take Profit

Stop Loss = 1.5 * ATR

When price hits Take Profit on that one trade, I want my EA to simply move Stop Loss to Breakeven on the other trade.

But how do I program it to know what the Take Profit (1.0 * ATR) was at that specific moment it opened the trade?

All feedback is greatly appreciated!

Patrick Engelbreth:

Basically, I want my EA to manage open trades with OrderModify().

My EA is programmed to take trades when 2 Moving Averages are crossing.

Every time the 2 Moving Averages cross, it opens 2 trades.

Take Profit = 1.0 * ATR

Stop Loss = 1.5 * ATR

No Take Profit

Stop Loss = 1.5 * ATR

When price hits Take Profit on that one trade, I want my EA to simply move Stop Loss to Breakeven on the other trade.

But how do I program it to know what the Take Profit (1.0 * ATR) was at that specific moment it opened the trade?

All feedback is greatly appreciated!

maybe you can validate the number of opened positions for that symbol:

- if opened positions=0 you can evaluate the conditions to open the 2 trades.

- if 1 opened position then immediately set StopLoss to OpenPrice

FXreedom:

maybe you can validate the number of opened positions for that symbol:

- if opened positions=0 you can evaluate the conditions to open the 2 trades.

- if 1 opened position then immediately set StopLoss to OpenPrice

Thanks for the help!

I think I found out how to do it, but now I'm facing a new problem.

For some reason, the EA keeps modifying the orders incorrectly. Sometimes it works, sometimes it doesn't.

Here is the code and I assume that the problem is at the bottom:

```//+------------------------------------------------------------------+
//|                                                 OBVmod_MA_EA.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

input int EAFastMA=7;
input int EAFastMAShift=0;
input int EAFastMAMethod=0;
//input int EAFastMAAppliedTo=0;
input int EASlowMA=66;
input int EASlowMAShift=0;
input int EASlowMAMethod=0;
extern int MoveSLtoBEafter=100;
extern int PipsLockedIn=10;
//input int EASlowMAAppliedTo=0;
extern double LotSize = 0.01;
//extern int MagicNumber =0;
extern int ATR_PeriodValue =14;
double pips;

// Defining parameters for opening trades.
extern int    TakeProfitPercent = 100;
extern double  StopLossPercent = 150;

extern int        Slippage = 3;
int        MagicNumber1 = 1090608;

double myLots;
double myATR;
double StopLoss;
double TakeProfit;
double myPoint;

int ticket1 = -1;
int ticket1type = -1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
double TickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
if (TickSize == 0.00001 || TickSize == 0.001)
pips = TickSize*10;
else pips = TickSize;

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
// Making ATR and defining SL & TP
{
myATR = iATR(NULL, PERIOD_D1, ATR_PeriodValue, 1)/pips;
TakeProfit = myATR * TakeProfitPercent/100.0;
StopLoss = myATR * StopLossPercent/100.0;
}

//---Defining iCustom Indicators
{
double CurrentFast=iCustom(NULL,0,"Indicators\\Trendtwolinecross\\(i)OBVmod_indicator_MA",EAFastMA,EAFastMAShift,EAFastMAMethod,1,1);
double CurrentSlow=iCustom(NULL,0,"Indicators\\Trendtwolinecross\\(i)OBVmod_indicator_MA",EASlowMA,EASlowMAShift,EASlowMAMethod,1,1);
double PreviousFast=iCustom(NULL,0,"Indicators\\Trendtwolinecross\\(i)OBVmod_indicator_MA",EAFastMA,EAFastMAShift,EAFastMAMethod,1,2);
double PreviousSlow=iCustom(NULL,0,"Indicators\\Trendtwolinecross\\(i)OBVmod_indicator_MA",EASlowMA,EASlowMAShift,EASlowMAMethod,1,2);

int TicketSell;
int TicketSellBE;
{
if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow && OrdersTotal()>0)
{
//code to close Sell order(s)
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type   = OrderType();

bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;

//Close opened short positions
case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
}

}

if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow && OrdersTotal()>0)
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type   = OrderType();

bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;

//Close opened short positions
case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
}

}
//----code to open orders

if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow && OrdersTotal()==0) //open buy order if no open orders
{
//code to open buy order with TP 1 x ATR
// Open Buy order without TP
}
if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow && OrdersTotal()==0) //open sell order if no open orders
{
//code to open sell orderwith TP 1x ATR
TicketSell=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,1,0,Red);
// Open Sell order without TP
TicketSellBE=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),NULL,NULL,2,0,Red);

}
}
}
// Move SL for trades without tp to BE

{
//for(int b1=OrdersTotal()-1;b1>=0;b1--)
{
if(OrderMagicNumber()==1)
}

//for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderMagicNumber()==2)
if(OrderOpenPrice()>OrderStopLoss());
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsLockedIn*pips),OrderTakeProfit(),0,clrGreen);
}

}

//For Sell Orders
{
int SellOrderTP;
//for(int s1=OrdersTotal()-1;s1>=0;s1--)
{
if(OrderMagicNumber()==1)
if(OrderType()==OP_SELL)
SellOrderTP=OrderTakeProfit();
}

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