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

Floating point troubles

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

    Floating point troubles

    Hello
    I am trying to use the floating point arithmetic to place my stop limit orders and cancel them if the condition for triggering becomes false, but it doesn't seem to work.
    I expected the logic below to return a ceiling of 2, in case the StopOffsetTicks / CancelOffsetTicks was 2 ((2 + 1) / 2 = 1.5, hence a ceiling of 2), but it returns a ceiling of 1.
    Am I doing something wrong here?

    offsetLimitStop = (StopOffsetTicks + 1) / 2;
    cancelLongShort = (CancelOffsetTicks + 1) / 2;
    .
    .
    .
    AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, currentAsk + Math.Ceiling(offsetLimitStop) * TickSize, currentAsk + Math.Ceiling(offsetLimitStop) * TickSize,
    TimeInForce.Day, longOrderId, "3C", longAtmId, (atmCallbackErrorCode, atmCallBackId) =>...
    .
    .
    .
    else if ((longAtmId.Length != 0 && isLongAtmStrategyCreated && CONDITION FALSE && currentAsk <= triggerPriceLong - Math.Ceiling(cancelLongShort) * TickSize) // Cancel if condition becomes false and price pulls back by CancelOffsetTicks * TickSize

    #2
    Hello itrader46, thanks for your post.

    I can't tell what is wrong from the posted snippet. I would recommend Printing out these values you are operating on to explain the output your getting. the ceiling of 1 + and decimal will always return 2. The Print function will be a valuable tool for debugging. See here for documentation.

    If offsetLimitStop and cancelLongShort are integers already, then the ceiling method will do nothing to them. The decimal truncation will happen preemptively if this calculation produces a decimal value: (StopOffsetTicks + 1) / 2, (CancelOffsetTicks + 1) / 2.

    After using Print statements the problem should become clear. I would also be happy to test a reduced script that reproduces the same issue. See here for instructions on exporting.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by itrader46 View Post

      offsetLimitStop = (StopOffsetTicks + 1) / 2;
      cancelLongShort = (CancelOffsetTicks + 1) / 2;
      .
      One example. ( I assume that both left-hand vars are ints)
      Code:
      offsetLimitStop = (int)Math.Ceiling((StopOffsetTicks + 1) / 2.0);
      By dividing the int by a double, not another int, you make the result a double. Apply Math.Ceiling to the result, and cast it back to an int.
      Last edited by koganam; 10-22-2019, 06:20 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by wzgy0920, 04-20-2024, 06:09 PM
      2 responses
      26 views
      0 likes
      Last Post wzgy0920  
      Started by wzgy0920, 02-22-2024, 01:11 AM
      5 responses
      32 views
      0 likes
      Last Post wzgy0920  
      Started by wzgy0920, Yesterday, 09:53 PM
      2 responses
      49 views
      0 likes
      Last Post wzgy0920  
      Started by Kensonprib, 04-28-2021, 10:11 AM
      5 responses
      192 views
      0 likes
      Last Post Hasadafa  
      Started by GussJ, 03-04-2020, 03:11 PM
      11 responses
      3,234 views
      0 likes
      Last Post xiinteractive  
      Working...
      X