Last active
October 28, 2019 17:01
-
-
Save currencysecrets/8973477 to your computer and use it in GitHub Desktop.
This function calculates the risk per trade
This file contains hidden or 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
extern double EXT_RISK_MINAMT = 50; // minimal amount of money to risk per trade | |
extern int EXT_RISK_MULT = 15; // maximum number of consecutive losses needed to wipeout account | |
// calculates the amount of money to risk per trade | |
double getRiskAmount() { | |
return( MathMax( EXT_RISK_MINAMT, ( AccountBalance() + getStopPL() ) / EXT_RISK_MULT ) ); | |
} | |
// this function returns the profit/loss if the stop is hit | |
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( N( ( stop - open ) * result, 2 ) ); | |
} else if ( type == OP_SELL ) { | |
return( N( ( open - stop ) * result, 2 ) ); | |
} | |
return ( 0 ); | |
} | |
// this function will calculate the total profit/loss if all current trailing stops were hit | |
double getStopPL() { | |
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 ); | |
} | |
// simply call the getRiskAmount() function in your script to get the dollar amount of portfolio at risk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't compile because function N is not defined.