I've written code to perform dynamic position sizing based on the % of the total account balance I want to risk. However I'm struggling with the last step to calculate the number of UnitsToBuy when I have the amount I wish to risk in dollars and the number of points(ticks) that risk in dollars is equivalent to. Can someone please suggest what that calculation should be?
I'm focusing on GBPUSD but ideally the sizing calculation should work with other currencies too, particularly where the point value is different (ie. GBPJPY).
Is there a way to find the tick size of the current instrument?
double Risk = 3.0; //Risk in % of Account double backTestCash = 50000; protected override void OnBarUpdate() { if (Historical) { // BACKTEST CASH = backTestCash + Performance.AllTrades.TradesPerformance.Currency.CumProfit; } else { // LIVE CASH = GetAccountValue(AccountItem.CashValue); // returns 0 during backtest } TradeRiskInPoints = ( (High[0]-Close[0])+(atrfactor*ATR(14)[0]) )*1000; // 1000 is for GBPUSD points, but I'd have to use a different value for GBPJPY RiskInCash = CASH*(Risk/100); UnitsToBuy = (int)Math.Floor( ???? ); // BUY SHORT --------------------------------- if ( GoShort==true && Position.MarketPosition == MarketPosition.Flat ) { entryOrder = EnterShort(UnitsToBuy,"TR_Short"); ShortStopLossPrice = High[0]+(atrfactor*ATR(14)[0]); } // SHORT STOP LOSS ----------------------- if ( (High[0] >= ShortStopLossPrice) && (Position.MarketPosition == MarketPosition.Short) ) { ExitShort("StopLoss","TR_Short"); } }
Comment