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

Print just once

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

    Print just once

    Hello

    I have wrote an strategy that runs OnEachTick. It has a BreakEven condition that works perfect, but I would like just print in output window one line when stop moves to BE. Actually it does since position is opened.

    if (BarsInProgress == 0)
    && Condition 1
    && Condition 2

    ChangeOrder(stopLongOrder, stopLongOrder.Quantity, 0, Position.AveragePrice);
    Print(Time[0] + " || " + Name + " || " + Account + " || " + Instrument);



    How can I do it?

    Thanks in advanced

    #2
    Hello Impeesa,

    Thank you for the post.

    From the given code you would need to add curly braces so that your if condition can execute both commands:

    Code:
    if(....)
    {
        //all logic that should apply to this if condition
    }

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

    Comment


      #3
      Hello Jesse

      Curly braces were added and obviously just changes order once, but prints lines every tick until position is closed.

      if (BarsInProgress == 0)
      && Condition 1
      && Condition 2
      {
      ChangeOrder(stopLongOrder, stopLongOrder.Quantity, 0, Position.AveragePrice);
      Print(Time[0] + " || " + Name + " || " + Account + " || " + Instrument);
      }

      Any other option?

      Comment


        #4
        Hello Impeesa,

        If the condition remains true the print would keep happening, is that what you are seeing happen here?

        If thats the case you would likely need to add a bool to know when to print. For example:

        Code:
        private bool printOnce = false;
        later in OnBarUpdate:

        Code:
        if (BarsInProgress == 0)
        && Condition 1
        && Condition 2
        {
        ChangeOrder(stopLongOrder, stopLongOrder.Quantity, 0, Position.AveragePrice);
        if(!printOnce)
        {
             Print(Time[0] + " || " + Name + " || " + Account + " || " + Instrument);
             printOnce = true;
        }
        }

        You would otherwise need to make the condition happen less frequently to avoid printing on each tick. The variable would also need reset at some point when the print should be available again, possibly from OnPositionUpdate or just in a condition checking the position is flat.


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

        Comment


          #5
          Hello, Jesse

          Your example is perfect and it would be useful in other purposes. Of course, it works fine in my code.

          Thanks a lot

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Waxavi, Today, 02:00 AM
          0 responses
          2 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Started by elirion, Today, 01:36 AM
          0 responses
          3 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by gentlebenthebear, Today, 01:30 AM
          0 responses
          4 views
          0 likes
          Last Post gentlebenthebear  
          Started by samish18, Yesterday, 08:31 AM
          2 responses
          9 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by Mestor, 03-10-2023, 01:50 AM
          16 responses
          391 views
          0 likes
          Last Post z.franck  
          Working...
          X