Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Position Size calculation help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Position Size calculation help

    Hi,
    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?


    Code:
    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");	
      }
    
    }

    #2
    Hello,

    Thank you for the post.

    I am unsure what the exact end result equation you want would be in regard to what goes where the question marks are, but to get the tick size, you could use the property TickSize.

    An example:

    Code:
    double myPrice = Close[0] - (2 * TickSize); // Close minus 2 ticks.
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse.

      For position size (in units) I think this might be it...

      Number of Units to Buy = (Account size * percent exposure)/# pips risk / pip value

      Where pip value is {10 = standard, 1 = mini, 0.1 = micro, if InteractiveBroker then 0.0001}

      Comment


        #4
        Hello,

        Thank you for the reply.

        Did you need assistance forming the provided equation into syntax? I was not sure as you said you think this is the equation.

        I could not suggest the equation its self but if this is correct to what your trading logic needs, I could assist with the syntax for what you have provided.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by realblubb, 04-28-2024, 09:28 AM
        2 responses
        32 views
        0 likes
        Last Post realblubb  
        Started by bmarovets, Today, 06:33 PM
        0 responses
        8 views
        0 likes
        Last Post bmarovets  
        Started by ETFVoyageur, Yesterday, 07:05 PM
        4 responses
        36 views
        0 likes
        Last Post ETFVoyageur  
        Started by JGriff5646, Yesterday, 10:02 PM
        2 responses
        23 views
        0 likes
        Last Post JGriff5646  
        Started by DawnTreader, Today, 05:58 PM
        0 responses
        5 views
        0 likes
        Last Post DawnTreader  
        Working...
        X