Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Last active April 3, 2018 13:01
Show Gist options
  • Save currencysecrets/3058934 to your computer and use it in GitHub Desktop.
Save currencysecrets/3058934 to your computer and use it in GitHub Desktop.
This function helps to calculate the lot size needed for any position where the entry price, initial stop loss price and amount being risked is all known. The function returns the amount according to the number of decimals allowed by the broker - generally speaking, if mini lots are allowed to be traded this means you can have 2 decimal places.
/*
* Calculates the position size of the trade based upon entry price, stop loss & amount risked.
* @param entryPrice double Entry price of the order
* @param iniStop double Initial stop loss of the order
* @param riskAmt double Risk per trade (in dollars of account's currency)
*
* @return amount in mini-lots
*/
double getLots( double entryPrice, double iniStop, double riskAmt ) {
string sym = Symbol();
double stopDist = MathAbs( entryPrice - iniStop ) + ( MarketInfo( sym, MODE_SPREAD ) * MarketInfo( sym, MODE_POINT ) );
double lots = ( riskAmt * MarketInfo( sym, MODE_TICKSIZE ) ) / ( stopDist * MarketInfo( sym, MODE_TICKVALUE ) );
return( N( lots, 2 ) ); // this will round up/down!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment