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 Mindset, 04-21-2026, 06:46 AM
          0 responses
          88 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by M4ndoo, 04-20-2026, 05:21 PM
          0 responses
          134 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by M4ndoo, 04-19-2026, 05:54 PM
          0 responses
          68 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by cmoran13, 04-16-2026, 01:02 PM
          0 responses
          119 views
          0 likes
          Last Post cmoran13  
          Started by PaulMohn, 04-10-2026, 11:11 AM
          0 responses
          67 views
          0 likes
          Last Post PaulMohn  
          Working...
          X