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

stop trading for 3 bars after most recent trade, trouble understanding BarsInProgress

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

    stop trading for 3 bars after most recent trade, trouble understanding BarsInProgress

    Hello, I've been trying to implement a "trade cooldown" of sorts where my strategy will not enter another trade for 3 bars after the most recent trade. ideally this would be at least 3 bars from my entry signal which is why I started by using BarsSinceEntryExecution. I'm getting scenarios on a scalping strategy where there is fast chop and my strategy ends up taking trade after trade back to back, which is why I want to add this "cooldown" period of say 3 bars between trades. I added the following to my conditions for entry (the strategy is unlocked) and received the error You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntryExecution() method in the context of a multi-time frame and instrument strategy.

    && (BarsSinceEntryExecution(@"Long") > 3 || BarsSinceEntryExecution(@"Long") == -1)
    && (BarsSinceEntryExecution(@"Short") > 3 || BarsSinceEntryExecution(@"Short") == -1)

    This led me down the rabbithole of reading the manual section on Mutliple Timeframes & instruments, but I'm not sure I quite understand what I need to do to make this work. I thought I only have one dataseries loaded as I'll show below but I suppose loading the tick data also counts as a different index value than the primary? Otherwise I'm not sure why I would get this issue because I'm only accessing one time frame.

    I have my data set as follows ​

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }​
    The above is within protected override on state change and follows if state set defaults.
    Then in protected override OnBarUpdate I have the following:
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 2)
    return;​

    Then below that I have my entry conditions as normal (working fine as I have been testing them with playback for a while) followed by the new entry conditions to check when our most recent trade was and if we are 3 bars away for both our long and short positions.
    if ( //other entry condition stuff
    && (BarsSinceEntryExecution(@"Long") > 3 || BarsSinceEntryExecution(@"Long") == -1)
    && (BarsSinceEntryExecution(@"Short") > 3 || BarsSinceEntryExecution(@"Short") == -1)​)
    {
    Enter the trade
    }

    This is how I set it up and its giving the said error above, what am I missing? It may be worth noting I have the new entry conditions added both in my if statement conditions for entering my long setup and also within my if conditions for entering my short setup. The reason I doubled the barssinceentry for both Long and Short is because I want it to wait to take another trade if either a long or short was recently traded.

    Appreciate any help as I had some minimal prior coding experience but am new to coding strategies.

    #2
    Hi complex,

    You have 2 data series in your strategy, the Minute and the Tick.

    in that statement you need to specify on which series the execution took place... most likely "0" (the primary series) in your case... so try this:

    BarsSinceEntryExecution(0,"Long",0)

    instead of

    (BarsSinceEntryExecution(@"Long")

    but chances are, you also need to specify which series you're placing the order on... if you're not specifying that, it's probably 0, but if it's 1 then your statement would look like this:

    BarsSinceEntryExecution(1,"Long",0)

    Comment


      #3
      Oh thank you so much! I understand now that it reads the tick data as another series. I appreciate your speedy response, it is working now.

      Comment


        #4
        I have another question relevant to understanding the BarsSince syntax. I would like to try setting a stop Loss at the Low of the signal candle, which would be one candle prior to the candle we enter on. I believe the best way to go about this would be to use the BarsSinceEntry to say if bars since entry is 0, set an int value of the low one candle ago, then the stop will be repeatedly set to the integer variable when called in OnBarUpdate if marketposition == long etc. Is there a better way to do this? Or does that follow?

        Comment


          #5
          The syntax for getting the low of the previous candle with 2 data series would be as follows:

          For the primary (chart) series
          Lows[0][1]

          For the secondary series
          Lows[1][1]

          Where the second "[1]" refers to the candle [1] bars ago... the low of the most recently finished candle is:
          Lows[0][0]

          Comment


            #6
            hmmm okay, I created a private int signalCandleLow; then added a bool property UsePriorCandleLow default to false. Then I added the following:
            if (BarsSinceEntryExecution(0,@"Long",0) == 0)
            {
            signalCandleLow = Convert.ToInt32(Low[0][1]);
            }
            Which should set the signalCandleLow variable to the low of the most recent candle after execution? After that I have
            if (marketPosition == marketPosition.Long)
            {
            if (UsePriorCandleLow == true)
            {
            SetStopLoss(@"Long",CalculationMode.Ticks, signalCandleLow, false);
            }​
            }
            I am getting error "Can not apply indexing with to an expression type of double." which is referenced as an error on the line where signalCandleLow = Convert.ToInt32(Low[0][1]);
            I set signalCandleLow as a private int variable, why does it think its a double? I added the convert which I thought would make the Low return as a whole number.​

            (edit) just to clarify, the goal now is to set a stop loss at the candle low before entry, I cannot just set a stop loss with if (marketposition = long) {set stop loss low[0][1] because that will continually set it at low one candle ago while we are in a trade. If that were the case, then were the trade to be open for more than one candle, the stop would reupdate to the low of the next candle. so my thinking is I need to store the value of the signal candle low and then reference it later once it is set
            Last edited by somethingcomplex; 02-27-2024, 07:10 PM.

            Comment


              #7
              1. not sure why you created signalCandleLow as an int. It should probably be a double. Double = decimal... which will hold a price.

              2. you have a small typo:
              signalCandleLow = Convert.ToInt32(Low[0][1]);

              In that statement is should be "Lows" not "Low"
              "Lows" is a collection of values.
              Lows[0][1]

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by tradingnasdaqprueba, 04-09-2024, 09:52 AM
              6 responses
              26 views
              0 likes
              Last Post tradingnasdaqprueba  
              Started by PaulMohn, Today, 02:06 AM
              1 response
              3 views
              0 likes
              Last Post PaulMohn  
              Started by Mindset, Today, 01:27 AM
              0 responses
              5 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by EB Worx, 03-07-2023, 05:05 AM
              4 responses
              99 views
              1 like
              Last Post cls71
              by cls71
               
              Started by patricia70, 11-23-2020, 10:17 AM
              17 responses
              549 views
              1 like
              Last Post PaulMohn  
              Working...
              X