You're going to want to loop through all open trades, check if price is X pips away from open, if so, once-per-trade, modify stops, set to open price of the trade. That's your BE. For the trailing stop, it's a simple block and you have the offset start when price is x pips away from open. Although, I will tell you, this type of EA is VERY unlikely to be profitable. Think about spread, commissions, etc..