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

Restrict to trading only 1 time per day per direction.

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

    Restrict to trading only 1 time per day per direction.

    As I run a backtest to see the results of my strategy it shows multiple buys and sells during one day as per my criteria. Can I restrict it to only trade once. Thus, after one exit it would no longer take any more long entries. Yet it would still take short entries. And vice versa.

    #2
    You can do this but you have to self program. Create a bool variable...

    bool hasTraded = false;

    within OnBarUpdate()

    Code:
    if (FirstBarOfSession)
        hasTraded = false;
     
    if (tradeCondition && !hasTraded)
    {
        hasTraded = true;
        EnterLong();
    }
    RayNinjaTrader Customer Service

    Comment


      #3
      Works but only for the long trades.

      I coded as you suggested and it seems to work correctly (restricts to one trade per day)
      This is what I coded:
      bool HasTradedLong = false;

      If (Bars.FirstBarOfSession)
      HasTradedLong = false;

      If (tradecondition && !HasTradedLong)
      {
      HasTradedLong - true;
      EnterLong();
      }

      The above works properly.

      However if I add another variable and use it for my short entries it seems not to work. Still works for my long entries but doesn't restrict my short entries to one per day.

      This is what I have:

      bool HasTradedLong = false;
      bool HasTradedShort = false;

      If (Bars.FirstBarOfSession)
      HasTradedLong = false;
      HasTradedShort = false;

      If (tradecondition && !HasTradedLong)
      {
      HasTradedLong = true;
      EnterLong();
      }
      If (tradecondition && !HasTradedShort)
      HasTradedShort = true;
      EnterShort();

      This generates multiple short entries per day.
      Also, why do I use Bars.FirstBarOfSession - wouldn't it always return False, as the bar I am going long or short is never the first bar of the session. Yet I am puzzled as to why it works properly for the long entries.

      What am I doing wrong?

      Thanks

      Comment


        #4
        Try one of these guys

        http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm


        Originally posted by strategy1 View Post
        I coded as you suggested and it seems to work correctly (restricts to one trade per day)
        This is what I coded:
        bool HasTradedLong = false;

        If (Bars.FirstBarOfSession)
        HasTradedLong = false;

        If (tradecondition && !HasTradedLong)
        {
        HasTradedLong - true;
        EnterLong();
        }

        The above works properly.

        However if I add another variable and use it for my short entries it seems not to work. Still works for my long entries but doesn't restrict my short entries to one per day.

        This is what I have:

        bool HasTradedLong = false;
        bool HasTradedShort = false;

        If (Bars.FirstBarOfSession)
        HasTradedLong = false;
        HasTradedShort = false;

        If (tradecondition && !HasTradedLong)
        {
        HasTradedLong = true;
        EnterLong();
        }
        If (tradecondition && !HasTradedShort)
        HasTradedShort = true;
        EnterShort();

        This generates multiple short entries per day.
        Also, why do I use Bars.FirstBarOfSession - wouldn't it always return False, as the bar I am going long or short is never the first bar of the session. Yet I am puzzled as to why it works properly for the long entries.

        What am I doing wrong?

        Thanks
        Last edited by twtrader; 07-22-2008, 09:50 PM. Reason: Do not feel like writing this guys code for hem

        Comment


          #5
          Isn't it exacatly what I posted.

          I posted exactly that and it doesn't work. Please see my post.

          Thanks

          Comment


            #6
            If (Bars.FirstBarOfSession)
            HasTradedLong = false;
            HasTradedShort = false;

            You need brackets.

            Code:
            if (Bars.FirstBarOfSession)
            {
                     HasTradedLong = false;
                     HasTradedShort = false;
            }
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Same here:
              if (tradecondition && !HasTradedShort)
              HasTradedShort = true;
              EnterShort();
              Code:
              if (tradecondition && !HasTradedShort)
              {
                        HasTradedShort = true;
                        EnterShort();
              }

              Comment


                #8
                Now it works. But why did it work for long trades before.

                Thanks a lot guys. Just added the brackets and now it works. But why did it work for the long trades without brackets before? Seems inconsistent to me.

                Comment


                  #9
                  Check out your code below: it had brackets for the long trades. Your code was inconsistent.

                  Comment


                    #10
                    strategy1,

                    You have to consider how an IF statement works. If you don't have brackets, only the line following the IF statement will get executed. If you do have brackets, anything within the brackets will be conditioned by the logic within the IF.

                    This is true for other conditional logic keywords, such as for(), while(), foreach(), etc.

                    SO.
                    If (Bars.FirstBarOfSession)
                    HasTradedLong = false;
                    HasTradedShort = false;

                    means that only HasTradedLong = false will get executed.
                    HasTradedShort will be executed every bar, hence you will never know you traded short and will always trade short.

                    if you do this.
                    If (Bars.FirstBarOfSession)
                    {
                    HasTradedLong = false;
                    HasTradedShort = false;
                    }

                    then, the HasTradedLong / HasTradedShort will get executed on the first bar of the session, resetting them. This is what you want.


                    Google conditional statements C# and you'll find a wealth of infomation.

                    hope this helps

                    Anthony
                    mrlogik
                    NinjaTrader Ecosystem Vendor - Purelogik Trading

                    Comment


                      #11
                      That's what it was.

                      mrlogik,
                      This explains why it worked for the long trades without brackets.

                      Thanks a lot.

                      Comment


                        #12
                        Sorry to dig up this old thread...

                        If I wanted the strategy to trade three times and have the second and third instances trade only after the first had traded, what would the code look like?

                        hastradedlong1
                        hastradedlong2
                        hastradedlong3


                        Part two of the question is what is a way to restrict the second Instance from trading until at least 10 minutes after the first?

                        Comment


                          #13
                          mainstream,

                          Assuming you were flat before trading the first time and you are running it on a 1min chart this is what you could try something like this.

                          Untested code below. Should give you a general framework idea.
                          Code:
                          if (some trade logic for trade #1)
                          {
                               EnterLong("tradeOne");
                               tradedOne = true;
                          }
                          
                          if (Position.MarketPosition == MarketPosition.Long)
                          {
                               if (tradedOne == true && tradedTwo != true && BarsSinceEntry("tradeOne") >= 10)
                               {
                                    EnterLong("tradeTwo");
                                    tradedTwo = true;
                               }
                          
                               if (tradedTwo == true && BarsSinceEntry("tradeTwo") >= 10)
                               {
                                    EnterLong("tradeThree");
                                    tradedThree = true;
                               }
                          }
                          
                          if (some exit conditions)
                          {
                               // close your trades.
                          }
                          
                          if (Position.MarketPosition == MarketPosition.Flat)
                          {
                               if (tradedOne == true)
                                    tradedOne = false;
                               if (tradedTwo == true)
                                    tradedTwo = false;
                               if (tradedThree == true)
                                    tradedThree = false;
                          }
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by mainstream View Post
                            Sorry to dig up this old thread...

                            If I wanted the strategy to trade three times and have the second and third instances trade only after the first had traded, what would the code look like?

                            hastradedlong1
                            hastradedlong2
                            hastradedlong3


                            Part two of the question is what is a way to restrict the second Instance from trading until at least 10 minutes after the first?
                            It probably makes more sense to use a counter that you test before each trade and increment on each trade. Then when you reach the designated number of trades, you just never enter the trade entry code block.

                            Code:
                            if (intNumTradesLong < intDailyLimitLong)
                            {
                            // trade entry logic here
                            EnterLong();
                            intNumTradesLong++;
                            }
                            Last edited by koganam; 02-18-2011, 03:55 PM.

                            Comment


                              #15
                              Thanks much. This will get me started. Have a good holiday weekend.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Christopher_R, Today, 12:29 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Christopher_R  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              166 responses
                              2,235 views
                              0 likes
                              Last Post sidlercom80  
                              Started by thread, Yesterday, 11:58 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post thread
                              by thread
                               
                              Started by jclose, Yesterday, 09:37 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,415 views
                              0 likes
                              Last Post Traderontheroad  
                              Working...
                              X