Here is simplified code for using P/L based on + or - of Order Margin for each individual order.
MT4 does not have a predefined code for Order Margin for each order so I made this. It tested ok for me. Have not used in live trading as of original posting date, since other parameters in my ea triggered exits first. But I'm archiving it here so others can see it and customize it and also so I can easily refer to it later as well. I've seen questions about how to calculate order margin and thought I'd leave this out here for anyone interested to test or develop further.
Note: I personally won't be updating or be involved in replying or responding to posts regarding this code or this topic.
I trimmed and edited the code to make it a bit more readable, so there could be typos or errors. Use with caution and ample due diligence.
MT4 does not have a predefined code for Order Margin for each order so I made this. It tested ok for me. Have not used in live trading as of original posting date, since other parameters in my ea triggered exits first. But I'm archiving it here so others can see it and customize it and also so I can easily refer to it later as well. I've seen questions about how to calculate order margin and thought I'd leave this out here for anyone interested to test or develop further.
Note: I personally won't be updating or be involved in replying or responding to posts regarding this code or this topic.
I trimmed and edited the code to make it a bit more readable, so there could be typos or errors. Use with caution and ample due diligence.
Inserted Code
// MARGIN P/L if ((UseOMProfit == true) || (UseOMLoss == true)) { int OMTotal = OrdersTotal(); for(int OMi=OMTotal-1;OMi>=0;OMi--) { if (OrderSelect(OMi, SELECT_BY_POS,MODE_TRADES)) { OrderMargin = NormalizeDouble(OrderLots()*MarketInfo(SymbolToManage,MODE_MARGINREQUIRED),2); if (UseOMProfit == true) { if ((Count2055L > 0) && (OrderType() == OP_BUY) && (OrderProfit() > ((OrderMargin)*OMPAmt)) && (OrderMagicNumber() == MagicNumberB)) { CloseBuys(); Print("EA OMP Close Buy Request"); } if ((Count2055S > 0) && (OrderType() == OP_SELL) && (OrderProfit() > ((OrderMargin)*OMPAmt)) && (OrderMagicNumber() == MagicNumberS)) { CloseSells(); Print("EA OMP Close Sell Request"); } } if (UseOMLoss == true) { if ((Count2055L > 0) && (OrderType() == OP_BUY) && (OrderProfit() < 0) && ((OrderProfit()*-1) > ((OrderMargin)*OMLAmt)) && (OrderMagicNumber() == MagicNumberB)) { CloseBuys(); Print("EA OML Close Buy Request"); } if ((Count2055S > 0) && (OrderType() == OP_SELL) && (OrderProfit() < 0) && ((OrderProfit()*-1) > ((OrderMargin)*OMLAmt)) && (OrderMagicNumber() == MagicNumberS)) { CloseSells(); Print("EA OML Close Sell Request"); } } } } }