Created
February 19, 2014 19:57
-
-
Save currencysecrets/9100312 to your computer and use it in GitHub Desktop.
Be careful of using AccountEquity in your risk calculations, here's a better alternative.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double EXT_RISK_DIV = 10; | |
double EXT_RISK_MINAMT = 50; | |
double riskAmt = MathMax( getCashBalance() / EXT_RISK_DIV, EXT_RISK_MINAMT ); | |
// this function returns the actual cash balance of all positions if they were to close now | |
double getCashBalance() { | |
return( AccountBalance() + getOpenPL() ); | |
} | |
// this function will calculate the total profit/loss of all open positions if all stops are hit | |
double getOpenPL() { | |
int tot = OrdersTotal(); | |
double result = 0; | |
for( int i = tot; i > 0; i -= 1 ) { | |
if ( OrderSelect( i, SELECT_BY_POS, MODE_TRADES) ) { | |
result += profitAtStop( OrderSymbol(), OrderOpenPrice(), OrderStopLoss(), OrderLots(), OrderType() ); | |
} | |
} | |
return( result ); | |
} | |
// this function returns the profit/loss of a position | |
double profitAtStop( string sym, double open, double stop, double lots, int type ) { | |
double result = lots * MarketInfo( sym, MODE_TICKVALUE ) / MarketInfo( sym, MODE_TICKSIZE ); | |
if ( type == OP_BUY ) { | |
return( ( stop - open ) * result ); | |
} else if ( type == OP_SELL ) { | |
return( ( open - stop ) * result ); | |
} | |
return ( 0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment