Created
September 13, 2013 13:17
-
-
Save currencysecrets/6550586 to your computer and use it in GitHub Desktop.
This simple script determines whether a new trade is viable according to the current level of portfolio risk. It does NOT take into account any strong positive or negative correlations that exist with the existing trades and only uses the AccountBalance() and NOT any draw down amount in equity.
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
extern int maxPortfolioRisk = 30; // maximum amount at risk (0 = nothing; 100 = all) | |
// this function returns the profit/loss if the stop is hit | |
double profitAtStop( double open, double stop, double lots, bool isLong, string sym = "" ) { | |
if ( sym == "" ) { sym = Symbol(); } | |
double result = lots * MarketInfo( sym, MODE_TICKVALUE ) / MarketInfo( sym, MODE_TICKSIZE ); | |
if ( isLong ) { | |
return( N( ( stop - open ) * result, 2 ) ); | |
} else { | |
return( N( ( open - stop ) * result, 2 ) ); | |
} | |
} | |
// 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 = 0; i < tot; i++ ) { | |
if ( OrderSelect( i, SELECT_BY_POS, MODE_TRADES) ) { | |
if ( OrderType() == OP_BUY ) { | |
result += profitAtStop( OrderOpenPrice(), OrderStopLoss(), OrderLots(), true, OrderSymbol() ); | |
} | |
if ( OrderType() == OP_SELL ) { | |
result += profitAtStop( OrderOpenPrice(), OrderStopLoss(), OrderLots(), false, OrderSymbol() ); | |
} | |
} | |
} | |
return( result ); | |
} | |
// placed somewhere within your system, once entry gates have established that there | |
// is going to be a trade | |
double newTradeRisk = profitAtStop( openPrice, iniStopLossPrice, qty, isLong, symbol ); // data needs to be entered here relevant to the trade | |
double stopPL = getStopPL() - newTradeRisk; // where newTradeRisk is the amount to be risked with current trade | |
if ( stopPL / AccountBalance() > -( maxPortfolioRisk / 100 ) ) { | |
// ...process trade... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment