Total Risk Including Stop_Losses

 

Hello fellow mql programmers.

I'm trying to:

1) Calculate the total risk of all open positions function to be used with EA's I create in the future.

2) Calculate the total number of Un-Hedged Lots I currently have running.

3) Calculate the total number of Un-Hedged Points I currently have running.

4) Get a Total Risk in Dollars which I can later subtract from Equity.

*I'm not just interested in if I have enough free-margin to cover a New position. I want to make pretty sure I don't get a margin call should all open positions and the New position starts losing money. This is getting tricky for me because I don't want to view Hedge position as double the value. However, the hedged positions are Not seen as 0 position either. Therefore I cannot just say Buy_Lots - Sell_Lots. If anyone have a similar code of calculating this please provide. If you want the challenge of fixing my Newbie code, its provided below. If you think this approach DOES NOT have merit please explain. Thanks All. 



//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
//+----------Total Risk Including Stop_Losses
double TRISL(){
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
double TRISL; double Buy_Lots; double Sell_Lots;
double Buy_Points; double Sell_Points; double Pos_Lots; double Pos_Points; 
double Tick_Value=(MarketInfo(Symbol(),MODE_TICKVALUE));
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true){
if(OrderType()==OP_BUY){Buy_Lots+=OrderLots(); Buy_Points+=(Price_Sell-OrderStopLoss());}
if(OrderType()==OP_SELL){Sell_Lots+=OrderLots(); Sell_Points+=(Price_Buy-OrderStopLoss());}}}
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
Pos_Points=(Buy_Points-Sell_Points); Pos_Lots=MathAbs((Buy_Lots-Sell_Lots));
if(Pos_Points>0){Pos_Lots=Buy_Lots;} if(Pos_Points<0){Pos_Lots=Sell_Lots;}
TRISL=(Pos_Lots*Tick_Value*(Pos_Points*10000))+(AccountMargin());
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
return(TRISL);}
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
Reason: