Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to get look back of bars and an exit strategy worked out.

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

    #16
    Note I only have one DataSeries. Just using a 2min Chart:

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 2);

    }

    Comment


      #17
      So I did a test and built a basic setup in Strategy Builder(SB) to test this and by default SB puts the BarsExit thing like this: BarsSinceExitExecution(0,"Twenty", 4)

      So I did that and it will trigger the first Entry and then I have a "SetProfitTarget("Twenty", CalculationMode.Ticks, 60);" in the State.Configure section.

      and I have another statement within my OnBarUpdate() section as such:

      if((waitFourBars == true) && (Position.MarketPosition == MarketPosition.Short) && ninZaTStop1.Signal_Trend[0] == 1)
      {
      Print("I should be closing");
      ExitShort("Twenty");
      waitFourBars = false;
      }


      So I do two types of checks. One is based off of a profit target and another is based off of a setup. So if the profit target doesn't get hit before the IF statement that does its checks does it will exit before the profit target. What I'm noticing is it will run one time hit the SetProfitTarget and just stop.

      If will never get into this:

      if( waitFourBars == true && (BarsSinceExitExecution(0, "Twenty", 4) > 4))
      {
      Print("Is more than four bars: " );
      }

      This IF statement is within the OnBarUpdate() function. I'm not sure if the SetProfitTarget is not triggering an ExitSignal or if there is another function somewhere that needs to tell the strategy that an exit occurred as I am assuming that is what the BarsSinceExitExecution() is. A listener for stuff like that.

      Comment


        #18
        Hello vegas3416,

        Thanks for your reply.

        That you have used AddDataSeries() means that you have added an additional data series to the script. For clarity, when you apply a script to a chart, the script will automatically use the charts bars as the primary data series.

        If you are adding the 2 minute bars and then applying to a 2 minute chart then you will get 2x OnBarUpdate()s and the BarsSinceExitExecution will see that you are using multiple series so it will expect you to use the correct method signature.

        Paul H.NinjaTrader Customer Service

        Comment


          #19
          Hello vegas3416,

          It looks like we are getting out of sequence with the replies.

          My post #19 was in reply to your post #16 and #17.

          To avoid confusion, for post #18 to answer that set of questions, please post screenshots of your strategy builder sets.


          Paul H.NinjaTrader Customer Service

          Comment


            #20
            Maybe this will help. I've removed a lot of my other functions and just broke it down to a very basic setup. When doing it this way I am now getting this error:

            Strategy 'ShortTrendInitial': Error on calling 'OnStateChange' method: Index was outside the bounds of the array.

            Attached Files

            Comment


              #21
              Hello vegas3416,

              Thanks for your reply.

              In the code you posted, I would suggest adding print statements in the State.DataLoaded to see which one of the statements is in error.

              Paul H.NinjaTrader Customer Service

              Comment


                #22
                So I added this print statement to State.DataLoaded and got rid of unused stuff and nothing back:

                }
                else if (State == State.DataLoaded)
                {

                ninZaStepMAPro1 = ninZaStepMAPro(Closes[1], ninZaStepMAPro_BaseMode.PriceAction, true, ninZa_MAType.EMA, 14, 1.4, ninZaStepMAPro_StepSizeMode.Auto, 10, 14);

                ninZaTStop1 = ninZaTStop(Close, 3.2, ninZaTStop_Unit.ninZaATR, 100, 2, 1);



                SetProfitTarget("Twenty", CalculationMode.Ticks, 60);


                Print("StepMA: " + ninZaStepMAPro1);
                Print("TSTOP: " + ninZaTStop1);
                Last edited by vegas3416; 02-26-2021, 11:22 AM.

                Comment


                  #23
                  Hello vegas3416,

                  Thanks for your reply.

                  Debugging can be tedious. If you do not get a print then move the print to the top of DataLoaded and then perhaps a print in between each line and see what prints out.

                  You can just use simple prints like:

                  Print ("A");
                  ninZaStepMAPro1 = ninZaStepMAPro(Closes[1], ninZaStepMAPro_BaseMode.PriceAction, true, ninZa_MAType.EMA, 14, 1.4,
                  Print ("B");
                  ninZaStepMAPro_StepSizeMode.Auto, 10, 14);
                  Print("C");
                  ninZaTStop1 = ninZaTStop(Close, 3.2, ninZaTStop_Unit.ninZaATR, 100, 2, 1);


                  If you then get an output of A and nothing then it would be the line after A, etc. etc,


                  it looks like you removed this line: MACrossBuilder1 = MACrossBuilder(Closes[1], CDMAtype.EMA, NinjaTrader....
                  Which would have been in error because you no longer have the added data series so Closes[1] would cause that type error.

                  Also, if you still have this: if (CurrentBars[0] < 1 || CurrentBars[1] < 1) you will eventually get an error when you get to OnBarUpdatre because again you do not have the added data series so do not need || CurrentBars[1] < 1
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #24
                    OK, so I figured out the issue. Because I removed that dataSeries from prior the indicator was reading off that dataSeries but fixed that. So now I'm reading correctly but the `BarsSinceExitExecution is not working as expected.


                    So I have a section where it checks some particular values and then it enters a short. THATS working. It enters a short and then sets a variable to TRUE.

                    Then I do two checks.

                    if((waitFourBars == true) && (Position.MarketPosition == MarketPosition.Short) && ninZaTStop1.Signal_Trend[0] == 1)
                    {
                    Print("I should be closing: " + ninZaTStop1.Signal_Trend[0] );
                    BackBrush = Brushes.Cyan;
                    ExitShort("Twenty");\
                    }

                    else if( waitFourBars == true && (BarsSinceExitExecution() >= 3))
                    {
                    Print("Is more than four bars, my market position: " + Position.MarketPosition );

                    waitFourBars = false;
                    }



                    So what its doing here is if the variable `waitFourBars` is true (which gets set when it enters the SHORT and Position is IN a SHORT and a indicator is showing LONG now I want you to exit. The other statement is checking variable is TRUE but its looking at the exit execution because there is also a SetProfitTarget("Twenty", CalculationMode.Ticks, 60);


                    So if the profit target hits before the top IF statement is true then do the ELSE IF statement. Which says Ok the variable is true and you exited because the target profit was hit. Once its been 3 or more bars since the exit change the variable back to FALSE. This is partially working.

                    First go around. Target profit hits before IF statement. Its been 3 or more bars and now the variable gets changes to false. It enters Again and this time the target profit hits before the signal triggered the first IF statement but its still in a Short state. Therefore it seems to be paying attention to the first entry signal and not this second one so it thinks its already been 3 bars since that exit and enters a short immediately again vs waiting 3 bars or more.

                    My guess here is that the `Signal name` of `Twenty` is getting saved in the SetProfitTarget vs being reset.

                    Because my NinjaScript Output is showing this:

                    I am in a twenty
                    Is more than four bars, my market position: Flat
                    I am in a twenty
                    Is more than four bars, my market position: Short
                    I am in a twenty
                    Is more than four bars, my market position: Short
                    I am in a twenty
                    Is more than four bars, my market position: Flat
                    I am in a twenty
                    Is more than four bars, my market position: Flat
                    I am in a twenty
                    Is more than four bars, my market position: Short

                    Is there a way to reset the signal name?

                    I basically have roughly 5 different setups. This particular setup I'm working on only takes 60tick profit. It enters a short using the signal name "Twenty' every time it enters that particular short setup. I thought maybe it was b/c of that signal name and tried removing it and putting the SetProfitTarget without a signal name in different places within the code but that didn't work either.

                    How can I set a profit target for particular setups and get this to work as it should? Been skimming the docs but still not quite finding the right function for this. Attached picture for how its working as well.
                    Attached Files

                    Comment


                      #25
                      Hello vegas3416,

                      Thanks for your reply.

                      I am having a difficult time following what you are trying to do.

                      What I would suggest is that you isolate your code that is not working as you expect and do additional print statements outside of the conditions so that on a bar-by-bar basis you can see the values that the conditions are using.

                      As far as "BarsSinceExitExecution is not working as expected" again I would suggest isolating your code to just that part so that you can test it without the distractions of your other code. You may even want to go so far as to create a new strategy just to test this aspect to ensure you have the correct functionality.

                      Paul H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by hayleeabb, Today, 04:21 PM
                      1 response
                      5 views
                      0 likes
                      Last Post NinjaTrader_ShawnB  
                      Started by mg.watts, Today, 03:17 PM
                      1 response
                      15 views
                      0 likes
                      Last Post NinjaTrader_ChrisL  
                      Started by JTizz, Today, 01:20 PM
                      1 response
                      8 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by omermirza, Today, 12:39 PM
                      2 responses
                      13 views
                      0 likes
                      Last Post omermirza  
                      Started by mballagan, Today, 01:02 PM
                      1 response
                      10 views
                      0 likes
                      Last Post NinjaTrader_Kate  
                      Working...
                      X