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

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

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

  • NinjaTrader_PaulH
    replied
    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.

    Leave a comment:


  • vegas3416
    replied
    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

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    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

    Leave a comment:


  • vegas3416
    replied
    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.

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    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.

    Leave a comment:


  • vegas3416
    replied
    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

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    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.


    Leave a comment:


  • NinjaTrader_PaulH
    replied
    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.

    Leave a comment:


  • vegas3416
    replied
    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.

    Leave a comment:


  • vegas3416
    replied
    Note I only have one DataSeries. Just using a 2min Chart:

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

    }

    Leave a comment:


  • vegas3416
    replied
    I wish this were the case but this does not work. When I try to do what you put above I get this error:

    "Strategy 'ShortTrendInitial': Error on calling 'OnBarUpdate' method on bar 50: Strategy 'ShortTrendInitial/-1': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceExitExecution() method in the context of a multi-time frame and instrument strategy"

    Then if I try to put it in between a

    if(BarsInProgress == 0) {

    if( waitFourBars && BarsSinceExitExecution() == 4)
    {
    Print("Is more than four bars: " );
    }

    }

    I get the same error. If I try putting BarsSinceExitExecution(0,"Twenty", 4) > 4 or just doing it without the " > 4 " to see its value in a print statement I get " -1 "

    Leave a comment:


  • NinjaTrader_PaulH
    replied
    Hello vegas3416,

    Thanks for your reply.

    There are two ways to implement BarsSinceExitExecution(). If you are using a single data series (the Chart bars only) then you can use BarsSinceExitExecution() == 4.

    If your strategy has added data series then you must use it with the other method signature of: BarsSinceExitExecution(0,"",0) == 4. NOTE: Make sure you review the help guide to understand what the parameters 0, "", 0 are and set them according to your needs: https://ninjatrader.com/support/help...texecution.htm

    Leave a comment:


  • vegas3416
    replied
    Trying to get this to work correctly but not able to get that BarsSinceExitExecution to work properly.

    Essentially this is what I have:

    if(BarsInProgress == 0)
    {

    //Check for ExitShort
    if((waitFourBars) && (Position.MarketPosition == MarketPosition.Short) && ((Close[0] > High[1]) || ninZaTStop1.Signal_Trend[0] == 1))
    {
    Print("I should be closing");
    ExitShort("Twenty");
    waitFourBars = false;
    }



    if statement for a specific scenario1

    else if statement for a specific scenario2

    else if statement for a specific scenario3
    {
    EnterShort("scenario3");
    waitFourBars = true;

    }


    }


    Trying to figure out where I would put that BarsSinceExitExecution. Should I wrap the BarsInProgress with with I'm just not really sure where to add it.

    Goal is when I enter a short on a specific scenario it triggers the short and sets a boolean.

    Then with that boolean its looking to see if a specific scenario is met and if its not keep skipping over until it is. Once it is Exit the Short and then I want it to wait 4 bars before it start processing bars again to determine if it should start entering trades again.

    I've played around with it a few different ways but I'm just not quite understanding where I need to put that.

    Also because I keep getting this error:

    Strategy 'ShortTrendInitial': Error on calling 'OnBarUpdate' method on bar 1: Strategy 'ShortTrendInitial/-1': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceExitExecution() method in the context of a multi-time frame and instrument strategy.

    I'm not quite sure what that means in terms of this.

    Leave a comment:


  • vegas3416
    replied
    Think I found it. Sorry - if anything hopefully this helps others.

    For the Bars Since Exit: https://ninjatrader.com/support/help...texecution.htm

    // Only enter if at least 10 bars has passed since our last exit or if we have never traded yet
    if ((BarsSinceExitExecution() > 10 || BarsSinceE xitExecution() == -1) && CrossAbove(SMA(10), SMA(20), 1))
    EnterLong();
    Last edited by NinjaTrader_PaulH; 02-25-2021, 07:43 AM. Reason: Corrected the help guide link to the intended BarsSinceExitExecution()

    Leave a comment:


  • vegas3416
    replied
    Also is there a way to call something so that after a specific type of trade it doesn't enter a trade even if it meets the expectations until after 4 bars have completed?


    So trade enters short -> trade closes -> tag saved -> waits 4 bars while tag is a specific type. After 4 bars tag is cleared and strategy goes back to reevaluating the setups to determine if it should re-enter again.
    Last edited by vegas3416; 02-24-2021, 04:40 PM.

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by kujista, Today, 06:23 AM
4 responses
14 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by traderqz, Yesterday, 09:06 AM
2 responses
16 views
0 likes
Last Post traderqz  
Started by traderqz, Today, 12:06 AM
3 responses
6 views
0 likes
Last Post NinjaTrader_Gaby  
Started by RideMe, 04-07-2024, 04:54 PM
5 responses
28 views
0 likes
Last Post NinjaTrader_BrandonH  
Started by f.saeidi, Today, 08:13 AM
1 response
8 views
0 likes
Last Post NinjaTrader_ChelseaB  
Working...
X