Sum of all Open Stop Losses (in USD), expressed as a % of Total Account value, where the base account is in USD

 

I have developed the below code to try and sum all open stop losses, across multiple currencies in the portfolio and then express as a % of the Total Account Value (in USD base currency).  I don't get an output that makes sense - can anyone help improve the code to give an accurate and stable result?

void SumOfAllStopLosses()
 {
Buys=0; Sells=0; ShortStopLossSum = 0; LongStopLossSum = 0;
int maxIterations = OrdersTotal(); // set a maximum number of iterations
int iterations = 0; // initialize the iteration counter
for(int i=maxIterations-1;i>=0;i--)

while (iterations <= maxIterations)
// Loop through all open orders
{
   if (OrderSelect(iterations, SELECT_BY_POS, MODE_TRADES)) 
    {
        if(OrderStopLoss()>0.00 &&(OrderType()==OP_SELL))
        // Calculate the stop loss value for the current order
{
ShortStopLossValue = (((((MathAbs(OrderStopLoss() - MarketInfo(Symbol(), MODE_BID))) / MarketInfo(Symbol(), MODE_POINT)) * (((MarketInfo(Symbol(), MODE_TICKVALUE)) / (MarketInfo(Symbol(), MODE_TICKSIZE)))))/ (MarketInfo(Symbol(), MODE_LOTSIZE)))* OrderLots());ShortStopLossValue++;//
    }
      
   if(OrderStopLoss()>0.00 &&(OrderType()==OP_BUY))
        // Calculate the stop loss value for the current order
            
     {
     
    
LongStopLossValue = (((((MathAbs(MarketInfo(Symbol(), MODE_BID))-(OrderStopLoss())) / MarketInfo(Symbol(), MODE_POINT)) * (((MarketInfo(Symbol(), MODE_TICKVALUE)) / (MarketInfo(Symbol(), MODE_TICKSIZE)))))/ (MarketInfo(Symbol(), MODE_LOTSIZE)))* OrderLots());LongStopLossValue++;//


           LongStopLossSum += LongStopLossValue;
          
           ShortStopLossSum += ShortStopLossValue;
    }   
    }
        iterations++; // increment the iteration counter
        if (iterations >= OrdersTotal()) {
            break; // exit the loop when the maximum number of iterations has been reached
            return(ShortStopLossSum);
            return(LongStopLossSum);
        }
    } 

// Calculate the stop loss percentage
double stopLossPercentage = MathAbs((ShortStopLossSum + LongStopLossSum) / (AccountBalance ())); 

// Print the result to the Experts tab
Print("Total Stop Losses as % of Account Balance: ", stopLossPercentage, "%");
Comment("Total Long Lots: " + DoubleToStr(BuyLots, 2),"      ", "Total Short Lots: " + DoubleToStr(SellLots, 2), "      ", "Total Stop Losses as % of Account Balance: " + DoubleToStr(stopLossPercentage, 2),"%" );

}

 

MQL4 and MetaTrader 4, has it's own section on the forum. I have moved your topic to the correct section. Please don't create another topic.

 

AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

Replace OOP with OrderClosePrice and sum

 

Forum on trading, automated trading systems and testing trading strategies

How to calculate take profit from currency

Fernando Carreiro, 2022.09.05 17:00

These are all the same equation written in different ways ...

[Volume]      = [Money Value] * [Tick Size] / ( [Tick Value] * [Stop Size] )
[Stop Size]   = [Money Value] * [Tick Size] / ( [Tick Value] * [Volume]    )
[Money Value] = [Volume]      * [Stop Size] * [Tick Value]   / [Tick Size]

[Volume] in lots
[Stop Size] in quote price change
[Money Value] in account currency
 
Fernando Carreiro #:

Thank you for your feedback.  I think that I have understood and now have applied to my code.  I will test it when the markets open again and let you know.

 
William Roeder #:

AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

Replace OOP with OrderClosePrice and sum

Thank you for your feedback. 

Reason: