Announcement

Collapse
No announcement yet.

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.

    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.

        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 NullPointStrategies, Today, 05:17 AM
          0 responses
          30 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          124 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          64 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          41 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X