JForex Example: Automatic position sizing

Proper position sizing is an integral part of risk management. It can be one of the easiest thing to do too. For example, I typically size my trading position based on the following factors:

  1. Amount of capital willing to put at risk.
  2. Stop price level.
  3. Volatility of the instrument.

The following JForex code calculates a lot size based on these three factors. It is part of my Dukascopy JForex July strategy (complete source code to the strategy is available via that link). [java firstline="232"] private double getLot(Instrument instrument) throws JFException { double riskAmt; double lotSize = 0; riskAmt = this.acctEquity * (this.riskPct / 100d); // CCYUSD only lotSize = riskAmt / (this.atr[instrument.ordinal()] * this.atrFactor); lotSize /= 1000000d; // in millions return roundLot(lotSize); } [/java] Referring to line 237, riskAmt is the amount of capital to put at risk for a trade (#1). Line 238 calculates lotSize, which is the position size that we want. The denominator in that division is the distance of the stop in pips. The strategy uses a multiple of ATR to set the stop loss. Nothing fancy here. Lines 240--241 are to set the lotSize value according to the JForex API specification. In which the lot amount is in millions and in steps of a thousand units, or 0.001 step size. As I've noted in the code, the caveat to this implementation is that the secondary currency of the instrument and your account currency needs to be in U.S. dollar. However, it's just a matter of conversion to extend this method for other currencies. Update: I expanded on this functionality in the JFUtil open source project.