Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Question About Coding A Counter

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

    Question About Coding A Counter

    Hello staff,

    I would like to use an Integer counter to limit the amount of times a specific trade can be triggered intra-bar.
    If my integer in Variables is "private int C1 = 0;" Am I correct with increasing the counter in the following method?

    if (BS1 != null && BS1 == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled)
    {
    C1 = C1 +1;
    }
    }


    I am using (IsFirstTickOfBar) to reset the counter for each bar.

    I'm looking forward to your reply,

    GLFX005

    #2
    Hello GLFX005,

    If this is to limit the trades you likely want to move the addition to where you submit the entry rather than waiting for the fill. Its possible you may receive a few ticks between submission and the fill.

    Otherwise that seems correct, you can also use C1++; to increment the variable.

    I look forward to being of further assistance.

    Comment


      #3
      Hello Jesse, thank you for your reply. I have an additional question.

      My strategy is set to Calculate.OnPriceChange. I have the following piece of code.

      if (AL1 != null && AL1 == execution.Order)
      {
      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
      {
      C1++;
      TP1921 = ExitLongMIT(0, true, Position.Quantity, Close[1] + 1 * TickSize, "TP1921", "AL1");
      SL1921 = ExitLongStopMarket(0, true, Position.Quantity, Indicator[0], "SL1921", "AL1");
      }
      }


      My question is:

      If AL1 has 10 pending contracts but half of it is filled once and the other half is filled a few seconds later, will "C1++;" increase C1 only once, or will it happen twice?

      Note that I am using "Position.Quantity" to automatically update the TP/SL to correspond the amount of orders that were filled on the entry of AL1.

      I'm looking forward to your reply,

      GLFX005

      Comment


        #4
        Hello GLFX005,

        That would be twice because of the partial fill in this case. If the intention is to increase this by 1 only for the total quantity increment it when you submit the order would be best. You only submit the order one time so the increment would be 1 for the whole amount.

        I look forward to being of further assistance.

        Comment


          #5
          Hello Jesse,

          The problem is that I sometimes cancel the order so if I would increase the counter when the order is submitted, but not actually filled, the order won't have traded yet the counter was increased and that's not what I'm intending to count. I'm trying to think of a workaround but I can't really come up with a solution. At first I thought I could perhaps double the counter so that it adds up with the Filled and PartFilled adding two counts, but then I figured that that I can't rely on an order being PartFilled all the time, sometimes it might Fill completely at once. Another solution I thought of is to measure a different order state, but I can't see anything that makes sense in this scenario.

          Comment


            #6
            Hello GLFX005 ,

            If that is something that happens you could watch for that in the OnOrderUpdate override to reduce the counter if its cancelled. You could also look at checking if the filled quantity matches the submitted quantity if you see partial fills.

            If the intention is to prevent more orders and we are working in the OnEachTick processing the best case scenario would be to increment this when you call the entry logic so it can't possibly call the entry again until you say so. What happens after that point regarding either maintaining the count or reducing it could be controlled by what happened after the order was submitted such as cancelled or filled.



            I look forward to being of further assistance.

            Comment


              #7
              Hello Jesse,

              What I did instead is to add a uniquely named SL specifically for the order that I only want to be able to trade a certain amount of times. If that trade makes a profit, it's fine, but it's the consecutive losses of the same type of order that I want to avoid. Therefor I added a unique order called "AL1SL" and added the counter to increase when "AL1SL" is Filled. This order will always return Filled because it's a Market order and doesn't require PartFilled logic.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              23 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              120 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              63 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
              45 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X