Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Try to use Trade Counter to limit the orders per direction

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

    Try to use Trade Counter to limit the orders per direction

    Hello everybody

    I'm currently developing a trading strategy. I have no previous history in programming, so that's all pretty new to me.It actually worked all just the way it was supposed to (opening/closing trades), but now i tried to add the possibility (with a trade counter, one for short, one for long), to tell the strategy how many trades per direction (long/short) it's supposed to take, before not executing any more trades. But it doesn't take any trades at all now.

    I have it set to: CalculateOnBarClose = false;so once it gets the trade signal, it executes the trade. Once the target(s) gets filled it often opens a new one because the trade signal is still there. So sometimes I have 2 trades within one bar. That's OK, but now I'd like to be able to tell the program only to trade for example 3 times and then just to ignore the trade signal.
    Once the trade signal (the trade conditions) are not true anymore, it should reset the trade counter immediately, so in case an other signal comes, it will open a new position again (even if it's in the same direction as the trades were before.)
    I work with 3 targets (sometime all the same, sometimes different). So those 3 trades for the 3 targets should count as ONE trade taken, not 3.
    Below i have a copy of the ninjascript, the way i tried to write it:

    #2
    The script:

    #[Description("FirtsTry")]
    publicclass BMEXTTSv3SOXEasy : Strategy
    {
    ...
    ...
    ...
    privateint tradeCounterLong= 0;
    privateint tradeCounterShort= 0;
    privateint maxTradesLong = 3;
    privateint maxTradesShort = 3;
    privatebool EntryConditionLong;
    privatebool EntryConditionShort;



    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose = false;
    EntryHandling = EntryHandling.UniqueEntries;
    SetTrailStop(CalculationMode.Ticks, trailstop);
    }



    privatevoid GoLong()
    {
    tradeCounterLong++;

    SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
    SetProfitTarget("target2", CalculationMode.Price, Close[0] + (Target2*TickSize));
    SetProfitTarget("target3", CalculationMode.Price, Close[0] + (Target3*TickSize));

    EnterLong("target1");
    EnterLong("target2");
    EnterLong("target3");
    }

    privatevoid GoShort()
    {
    tradeCounterShort++;

    SetProfitTarget("target1", CalculationMode.Price, Close[0] - (Target1*TickSize));
    SetProfitTarget("target2", CalculationMode.Price, Close[0] - (Target2*TickSize));
    SetProfitTarget("target3", CalculationMode.Price, Close[0] - (Target3*TickSize));

    EnterShort("target1");
    EnterShort("target2");
    EnterShort("target3");
    }

    protectedoverridevoid OnBarUpdate()
    {
    EntryConditionLong =
    Position.MarketPosition == MarketPosition.Flat
    && ...
    && ...
    && ...
    .
    .
    . ;

    EntryConditionShort =
    Position.MarketPosition == MarketPosition.Flat
    && ...
    && ...
    && ...
    .
    .
    . ;
    //Reset counter for long
    if (EntryConditionLong == false)
    {
    tradeCounterLong = 0;
    }

    //...and for short
    if (EntryConditionShort == false)
    {
    tradeCounterShort = 0;
    }

    // entry long
    if (EntryConditionLong && (tradeCounterLong < maxTradesLong))
    {
    GoLong();
    }

    // entry short
    if (EntryConditionShort &&(tradeCounterShort < maxTradesShort))
    {
    GoShort();
    }
    }

    #region Properties
    ...
    ...
    ...
    ...
    #endregion
    }
    }

    Could somebody please tell me what i did wrong? I tried to apply what i read in other threads here (limit trades per day). But apparently its not working.
    Thank you for your help

    Comment


      #3
      Hello Tradingds,

      Welcome to the NinjaTrader forums. This concept is available in the following sample strategy:


      It looks like you are missing the FirstBarOfSession flag designed to reset this counter each day.
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Hi Ryan
        Thanks for the fast response. I've looked at this sample strategy before and tried to take some of it out. The way I understand FirstBarOfSession is, that that will be when you start the strategy in the morning, is that right?
        In my case, it should reset the counter not just every morning, but more important after every time the trade signal (the signal to enter a trade) isn't true anymore. So that could be several times per day.

        Comment


          #5
          FirstBarOfSession is true only for the first bar of the session, so if you need certain items reset every day is a good technique for this.

          What type of trade limiting are you trying to do with this? Limit trades per day? Per direction?
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            I'd like to limit trades per direction. For example if there's a downtrend, I get the signal to go short, and enter the first trade. Once the target is filled and the the signal is still good, I'll enter a second short trade and so on. Now I want to give the strategy a certain number of trades to take while the signal is still good. So let's say I'll allow 3 trades, then after the 3rd it should ignore the signal, even if it's still there.
            But as soon as the signal to go short is gone -> reset the count so I can go short again the next time I get the signal. So that way there are multiple times per day I need to reset the count
            And of course I want the same for long positions too.
            Dave

            Comment


              #7
              Trades per direction would be handled by properties EntriesPerDirection and EntryHandling, and the signal names of your strategy. These properties work closely together to determine if the strategy accepts further entry signals in a given direction. You should not need a custom counter in this case.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Yeah, I've looked at that before and tried it out as well (just now again). The way I understand it (and it's described in the help guide v7) that's only to use to determent how many positions at the SAME time can be opened if there is more than one signal to trade. But that's only good for the time a position is open, once it closes (like in my case a couple times) it's not doing the job. So that's not it either what I'm looking for.

                Comment


                  #9
                  Yes, that's it. Those properties limits the number of entry signals accepted in the same direction.

                  Unfortunately it's not clear your plans for this counter, so best suggestion here is to simplify your strategy a bit until it's at a point where you understand its behavior. You can do this by reducing the # of unique signals and conditions. Take advantage of Print statements and TraceOrders output to track all values and follow code flow. This article can help with these techniques:
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    Alright, thanks for your help. I'll follow that path. :-)

                    Comment


                      #11
                      Limit trades to 3

                      I have beein thinking along the same lines. This will only work if you are talking about consecutive trades. My idea is to have a variable tripped when the trade is triggered. eg shorttrade = 1

                      The next time a short trade is triggered it adds 1 to it so shorttrade would then = 2 and so on.

                      In condition statement add: shortrade < 4

                      shorttrade would be reset when a long trade is entered.


                      Code:
                       
                      if ( trade conditions here && shorttrade<4)
                      {
                      EnterShort(1, "Short 1a")
                      longtrade = 0;
                      if (shorttrade = 0)
                      {
                      (shorttrade =1;
                      }
                      if (shorttrade >0)
                      {
                      (shorttrade = shorttrade + 1;
                      }
                      }
                       
                      if ( trade conditions here && longtrade < 4)
                      {
                      EnterLong(1, "Long 1a")
                      shorttrade = 0
                       
                      if (longtrade = 0)
                      {
                      longtrade = 1;
                      }
                      if (longtrade >0)
                      {
                      (longtrade = longtrade + 1;
                      }
                      }
                       
                      }
                      Please note I have not tested the code. Just quickly writing my thoughts. Something like this should work if you are doing consecutive trades

                      SD

                      Comment


                        #12
                        Originally posted by Tradingds View Post
                        I'd like to limit trades per direction. For example if there's a downtrend, I get the signal to go short, and enter the first trade. Once the target is filled and the the signal is still good, I'll enter a second short trade and so on. Now I want to give the strategy a certain number of trades to take while the signal is still good. So let's say I'll allow 3 trades, then after the 3rd it should ignore the signal, even if it's still there.
                        But as soon as the signal to go short is gone -> reset the count so I can go short again the next time I get the signal. So that way there are multiple times per day I need to reset the count
                        And of course I want the same for long positions too.
                        Dave
                        You will need to set EntriesPerDirection to 3 in the Initialize() method.

                        ref: http://www.ninjatrader.com/support/h...rdirection.htm

                        Comment


                          #13
                          Limit 3 Trades Per Direction

                          Lots of mistakes in the code! See attached file for corrected working strategy.


                          Notes:
                          • Tested the short trades for proof of concept. When strategy is enabled "as is" there will be only 3 trades showing. See attached summary.
                          • To enable both sides remove commented slashes "//" in front of the code where it says: //Enable after test
                          • Remove ExitLong()
                          • Add your own code for the strategy.
                          Note: Run tests on both sides before risking any money! I am making the assumption both sides will work based on the test. Please report back if it does not. Thanks!
                          • Enjoy!
                          SD

                          Attached Files

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by CortexZenUSA, Today, 12:53 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post CortexZenUSA  
                          Started by CortexZenUSA, Today, 12:46 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post CortexZenUSA  
                          Started by usazencortex, Today, 12:43 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post usazencortex  
                          Started by sidlercom80, 10-28-2023, 08:49 AM
                          168 responses
                          2,266 views
                          0 likes
                          Last Post sidlercom80  
                          Started by Barry Milan, Yesterday, 10:35 PM
                          3 responses
                          13 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Working...
                          X