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

Using Intrabar Tick Data But Entry Only Once

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

    Using Intrabar Tick Data But Entry Only Once

    Hi,

    I'm currently working on a strategy that is utilizing OnTickChange data using the linked method. I am using a Tick Chart.



    The strategy uses onTickChange to make intrabar entry, and does so fine with the Set1 and Set2, but if/when profit target is hit, it will submit another order after the profit target if the conditions are still true and this is happening on the same bar.

    1) What conditions or actions do I need to add to allow it to take only 1 entry per bar, but still use intrabar data to take entry?
    2) What if I wanted it to take up to 2 entries?
    3) If already in a position, and the price moves to cross an indicator, let's say an EMA, how can I close the entry and not take more entries on the same bar even if the entry conditions are still true?

    I want to limit the number of trades it can take on a single bar, either based on an exit condition being met intrabar, or limit how many entries it can make during a bar.

    Further, am I able to express that if an entry was taken near the low of the same bar it took entry within (x) number of ticks, it forces an exit on the next bar? I am already forcing an exit using BarsSinceEntryExecution and a number of bars to force exit as an input, but I'd like it to be more granular and exit before the bar closes if certain conditions are met.

    I should add that I am not skilled at code, and if a working solution can be made with the strategy builder that would be most helpful.

    Thanks kindly,


    #2
    Hi wisdomspoon, thanks for posting.

    1. Use a boolean and C# logic to control the trading:

    Code:
    private bool canTrade = true;
    protected override void OnBarUpdate()
    {
        if(<trade condition> && canTrade)
        {
             EnterLong();
             canTrade = false;
        }
    
        if(<exit condition> && Position.MarketPosition == MarketPosition.Long)
        {
           ExitLong();
           canTrade = true;
        }
    }
    2. Doing more than one entry will take more C# logic, like a counter that starts at 0 and you increment it each time you enter. The value of the counter would become part of your entry condition if(counter < 2) Also, set EntriesPerDirection to 2.

    3. See the "BuySellVolume" indicator for an example of keeping track of the active bar. e.g.

    Code:
    private bool canTrade = true;
    private int activeBar = -1;
    protected override void OnBarUpdate()
    {
        if (CurrentBar != activeBar) //new bar formed
        {
            activeBar = currentBar;
            if(Position.MarketPosition == MarketPosition.Flat)
                canTrade = true;
            else
                canTrade = false;
        }
    
        if(<trade condition> && canTrade)
        {
            EnterLong();
            canTrade = false;
        }
    
        if(<exit condition> && Position.MarketPosition == MarketPosition.Long)
        {
            ExitLong();
        }
    }
    To count the ticks in a bar, you will need to add to the above example and make a private int that holds the ticks since calling EnterLong() then if that number of ticks goes above a certain criteria then exit. Unfortunately, you will need to write the script by hand and not the strategy builder to make a counter work. The Builder can not do nested if statements either, so the best way to accomplish one trade per bar in the builder is to use the boolean like in the first example.

    Best regards,
    -ChrisL
    Last edited by NinjaTrader_ChrisL; 02-16-2022, 04:04 PM.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thanks for your response. This goes into great depth that I'll review when I get to adding code. For the first part, how would I express this in strategy builder to only take 1 trade per bar?

      Thanks,

      Comment


        #4
        Hi wisdomspoon, thanks for the follow up. I made an example in the strategy builder attached. To import the .zip go to Tools>Import>NinjaScript addon and select the downloaded file.

        Kind regards,
        -ChrisL
        Last edited by NinjaTrader_ChrisL; 02-16-2022, 05:27 PM. Reason: Updated example
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          There was an issue uploading the file, I re-attached here:
          Attached Files
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Hi Chris,

            Thanks so much for the 2nd upload, this looks much different. I will implement these across the strategy and let you know if it works.

            In the case that I'm using Set 1 and Set 2 for FirstTickOfBar and Working Bar, should these sets that you've provided go in their own sets after 1 and 2?
            In Set 2, there is a cross above condition - would my actual conditions that enter the trade replace this one?
            Also, this is a short position; I can swap the logic, but for Set 3, we would now want the High of 1 bar ago < Close of 0 bar right? 0 bar in this case is the bar I entered on?

            Comment


              #7
              Hi wisdomspoon, the script I posted yesterday is not working as expected. I will re-post it once I find the issue.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Hi Chris, yes that was my experience as well. Much appreciated if we can find a solution.

                Comment


                  #9
                  Hi wisdomspoon, I had to fix the entry condition to a simple Close[0]>Close[1] (just for the example). And I set ActiveBar to the CurrentBar to keep track of the current bar.

                  Kind regards,
                  -ChrisL
                  Attached Files
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chris,

                    This seems to work and only takes 1 trade per bar now - thank you.

                    It does seem to create a new issue though with this particular fix. If I add in a TakeProfit as an input with a default and min, and then add this into stops/targets, and tie it to the Long by setting "From Entry Signal" it no longer takes any trades at all. If I add a TakeProfit, does it need to be a variable? Or does the TakeProfit need to be included in the Conditions/Actions section?

                    Thanks,

                    Comment


                      #11
                      Just to help, this is how I'm setting up the TakeProfit.

                      Comment


                        #12
                        Hi, thanks for the follow up.

                        Turning on the Trace Orders will help to discover why a strategy is not making trades. The trace orders were ignoring the entry with the profit target enabled because the Entries Per Direction needs to be set to 2. So set that property to 2 in the Default properties and the profit target will work. I will ask if this is expected for this setup because logically it does not seem expected to need Entries Per Direction to 2 with this configuration.

                        Update: We need to set Order Fill Resolution to High, 1 Tick to get a more accurate fill estimation because the standard order fills only use the OHLC prices of the bar, so the exit order and the profit target order were filling at the same bar historically.

                        Kind regards,
                        -ChrisL
                        Last edited by NinjaTrader_ChrisL; 02-17-2022, 03:20 PM.
                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          Chris,

                          Everything seems to be working great. I managed to get it to work without adding a second order for direction. Thanks so much for all your help - NT support is top notch.

                          Comment


                            #14
                            Im happy to help, take care.
                            Chris L.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by futtrader, 04-21-2024, 01:50 AM
                            5 responses
                            56 views
                            0 likes
                            Last Post NinjaTrader_Eduardo  
                            Started by PeakTry, Today, 10:49 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post PeakTry
                            by PeakTry
                             
                            Started by llanqui, Today, 10:32 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post llanqui
                            by llanqui
                             
                            Started by StockTrader88, 03-06-2021, 08:58 AM
                            45 responses
                            3,992 views
                            3 likes
                            Last Post johntraderuser2  
                            Started by TAJTrades, Today, 09:46 AM
                            0 responses
                            8 views
                            0 likes
                            Last Post TAJTrades  
                            Working...
                            X