Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Attempted to divide by zero

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

    Attempted to divide by zero

    Hello.

    I'm running a strategy that I've run many times before without error.

    I have both SetTrailStop(() and SetProfitTarget() in the OnExecution() routine.

    I have trace=true on and I see messages saying, "Amended trail stop order" as the price moves.

    After some messages, the output says, "Attempted to divide by zero."

    I've never seen that message before.

    What could be causing that?

    #2
    Hello SilverSurfer1,

    The error can only be reproduced when calling SetTrailStop()? If this is commented out the behavior stops?

    Please provide the line of code calling SetTrailStop() so that we may verify the price supplied is valid.

    May I also confirm you are using 8.0.27.1? (Help -> About)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I'm running NT 7.0.1000.43

      I think I see kind of what's happening.

      _addDD = (int)Math.Abs(Performance.RealtimeTrades.TradesPer formance.Currency.DrawDown);
      _addDD = _addDD / (int)TickSize;

      I recently added the above two lines. Later I set the target price in SetProfitTarget() by adding the absolute value of the DrawDown to my initial profit of $200.

      When it runs those two statements, I immediately see an "Attempted Divide By Zero."

      There is something going on when I try to cast a "double" value to an integer and add it to another integer.

      Not 100% sure what is trying to divide by zero, but for now I'll take out that code.

      Thanks.


      Comment


        #4
        Hello SilverSurfer1,

        An integer is a whole number. If the TickSize (which is a double) was 0.25, and was cast to an int, it would have a value of 0 as the decimal would be dropped.

        Maybe you mean to divide first and then cast to an int?

        (int)(_addDD / TickSize)


        Thank you for providing the version. I overlooked where this was posted in the forums.

        Please note, NinjaTrader 7 is no longer in active development and we are no longer considering new feature requests or changes for NinjaTrader 7. Main development has shifted to our flagship platform NinjaTrader 8. This does mean updates from brokerages, data feeds, or operating systems that require an update in NinjaTrader will not be implemented in NinjaTrader 7 and may cause an interruption in service.

        While there is not a set ETA for the complete discontinuation of NinjaTrader 7, we highly recommend transitioning to NinjaTrader 8 as soon as possible before NinjaTrader 7 reaches its end of life to prevent interruption of service.

        Click below to download NinjaTrader 8.
        http://ninjatrader.com/GetStarted
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          if( Math.Abs(Performance.RealtimeTrades.TradesPerforma nce.Currency.DrawDown) > 0.00) {

          _addDD = (int)(Math.Abs(Performance.RealtimeTrades.TradesPe rformance.Currency.DrawDown) / TickSize);
          _profitTarget = _profitTarget + _addDD;

          }​

          This removed the "Attempted Divide by Zero"

          Thanks for the help.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          553 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          324 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          100 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          543 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          546 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X