Announcement

Collapse
No announcement yet.

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 Kaiviti57, Today, 04:13 AM
      0 responses
      1 view
      0 likes
      Last Post Kaiviti57  
      Started by Cross161, Yesterday, 10:33 PM
      0 responses
      9 views
      0 likes
      Last Post Cross161  
      Started by neilzep, Yesterday, 08:55 PM
      0 responses
      8 views
      0 likes
      Last Post neilzep
      by neilzep
       
      Started by javier.filgueira, Yesterday, 03:29 PM
      3 responses
      11 views
      0 likes
      Last Post javier.filgueira  
      Started by Da vinci, Yesterday, 03:15 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Working...
      X