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

execute only a single trade each day session

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

    execute only a single trade each day session

    Hi all,

    I have a strategy condition which triggers several buys because the condition is true. I'd like to run only the very first trade of the day / session and do not execute further trades. How can I include this limitation?

    I tried following without success:

    I added a user variable bool FirstTradeOpened = false
    In my condition I added the if FirstTradeOpened = false then ...
    In the same set where I run the action "EnterLong" I added the action "Set UserVariable FirstTradepened = true"

    but this resulted that no trade is done at all.
    Last edited by patricia70; 12-15-2020, 11:35 AM.

    #2
    Hello patricia70, thanks for writing in.

    It could be you are not resetting the variable back to false after the next session. You can check for Misc> First Bar Of Session> Equals> True, and set FirstTradepened back to false if this is true.

    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris and thanks for assisting me. Unfortunately I did not understand what you mean. I will try to explain my current setup more detailled.

      User Variables:
      I have added a user variable of type bool. It is called FirstTradeOpened and set to default 'false'

      Conditions and Actions:

      [SET 1]

      If All of the following conditions are met:
      ...some of my checks...
      FirstTradeOpened = false

      Do the following:
      EnterLong(DefaultQuantity, "");
      FirstTradeOpened = true;

      Where should I add the check Bars.IsFirstBarOfSession and the action as you suggested? Could you explain a little more detailled what and why exactly I'd need this Bars.IsFirstBarOfSession and how "session" is defined at all, please?

      thank you so much.
      Patricia

      EDIT: Just realized that the very first trade happens at the most left edge of the chart. So it seems that my condition as shown on the initial post worked. However this is not what I wanted to achieve. I want to have one single trade each day. :-)
      Last edited by patricia70; 12-15-2020, 12:14 PM.

      Comment


        #4
        Hello patricia70, thanks for your reply.

        The FirstTradeOpened variable needs to be reset back to false in order to make a trade once every day. The first bar of the session would be the best place to reset the bool variable. This should be in the first set of the strategy:


        if(Bars.IsFirstBarOfSession == true)
        {
        FirstTradeOpened = false;
        }

        You will find this under the Misc folder of the condition builder.

        The session is defined by the Session Template setting in the Data Series menu of the chart you are running the strategy. The default session tempalte for any instrument can be found by searching the instrument in Tools>Instruments.

        If you are just beginning with the builder we have a full set of examples here that demonstrates building various conditions based on time/indicator/price data:

        https://ninjatrader.com/support/help...on_builder.htm

        Please let me know if I can assist any further.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          The session is defined by the Session Template setting in the Data Series menu of the chart you are running the strategy. The default session tempalte for any instrument can be found by searching the instrument in Tools>Instruments.
          Let's say we have 90 days history loaded in a 15M chart. In my understanding FirstBarOfSession would be the very first bar at the left edge of the chart with bar index = 0 shown in the data box. So, why do we care about the first bar of this chart which has the index 0?

          EDIT: Solved and replying myself After using testwise the code block
          Code:
          if(Bars.IsFirstBarOfSession)
            Print(string.Format("Bar number {0} was the first bar processed of the session at {1}.", CurrentBar, Time[0]));
          I understand now that this is the first bar of the trading session.

          Originally posted by NinjaTrader_ChrisL View Post
          The FirstTradeOpened variable needs to be reset back to false in order to make a trade once every day.
          Please, can you explain what is wrong with the condition I showed in my previous post?

          EDIT: used a debug print to see the content of the bool variable FirstTradeOpened. It was always set to FALSE. That's why this won't work because no trade will get triggered.

          Originally posted by NinjaTrader_ChrisL View Post
          if(Bars.IsFirstBarOfSession == true)
          {
          FirstTradeOpened = false;
          }
          As explained before I am using various conditions in my SET1. I use a time window in which I allow trades happening. When adding your suggested code block no trade will happen at all in my understanding. Nevertheless, I added this condition in SET1 to check out what will happen. My condition SET1 looks like that (TradeTimeFrom and TradeTimeUntil are user inputs)

          If all of the following conditions are met:
          Times[Default.input[0].TimeOfDay >= TradeTimeFrom.TimeOfDay
          Times[Default.input[0].TimeOfDay < TradeTimeUntil.TimeOfDay
          Position.MarketPosition = MarketPosition.Flat
          Default.input[0] > High[1]
          Bars.IsFirstBarOfSession = true

          Do the following
          EnterLong(DefaultQuantity, "");
          FirstTradeOpened = false;

          As expected nothing happens, no trade at all. I think we're mixing something up here Sorry for my bad english and explaining so bad. Let's start over with another try. To be more precise, my goal is simply to have one single trade within the specified time window. I am using 10:30 to 11:30 as time window and I want only the very first trade happen. No other trade should occur in this time frame. The next time window will be at 17:15 to 18:00 and again there should only occur the first trade.

          How do I reach this goal? sorry for asking again, but I'm still confused because it does not work as expected. Thanks for your patience and trying to assist.
          Patricia.
          Last edited by patricia70; 12-15-2020, 02:25 PM.

          Comment


            #6
            Hello patricia70,

            This is Jim responding on behalf of Chris.

            The main concept we need to understand with using a bool is that it is a true/false variable that we control.

            We can create a condition that checks if the bool is true, and in the actions of that condition, we can set the bool to false. This condition will become true once and then will no longer become true until that bool gets set back to true.

            Demo - https://drive.google.com/file/d/18zH...w?usp=drivesdk

            We look forward to assisting.
            JimNinjaTrader Customer Service

            Comment


              #7
              Hi Jim

              much appreciate yours and Chris assistance. I SOLVED IT !!!
              I had just to use it in a separate condition SET and not mix it up with my existing one.

              thanks both to you and great video, thumbs up! Now I will need to find out how to adjust my strategy so I can use two separate time windows. Have a nice day.
              Patricia.
              Last edited by patricia70; 12-15-2020, 02:42 PM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by fitspressoburnfat, Today, 04:25 AM
              0 responses
              2 views
              0 likes
              Last Post fitspressoburnfat  
              Started by Skifree, Today, 03:41 AM
              1 response
              4 views
              0 likes
              Last Post Skifree
              by Skifree
               
              Started by usazencort, Today, 01:16 AM
              0 responses
              1 view
              0 likes
              Last Post usazencort  
              Started by kaywai, 09-01-2023, 08:44 PM
              5 responses
              604 views
              0 likes
              Last Post NinjaTrader_Jason  
              Started by xiinteractive, 04-09-2024, 08:08 AM
              6 responses
              23 views
              0 likes
              Last Post xiinteractive  
              Working...
              X