• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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, 05:20 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by MARTIN MARTIN, Yesterday, 01:01 PM
      3 responses
      39 views
      0 likes
      Last Post MARTIN MARTIN  
      Started by NickyD, Today, 11:42 AM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by cupir2, Today, 11:25 AM
      3 responses
      14 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by 7robert, Today, 11:15 AM
      2 responses
      7 views
      0 likes
      Last Post 7robert
      by 7robert
       
      Started by meandthetrio, Today, 10:52 AM
      0 responses
      11 views
      0 likes
      Last Post meandthetrio  
      Working...
      X