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

bool with loop help

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

    bool with loop help

    HI i am not sure if ia m doing it right but IsBullishFVGInLastBars prints false always even though there is a condition met. I want to enter long when condition is met in the last 10 bars. The condition works for sure, i checked it seperatly.

    Print("IsBullishFVGInLastBars(10)"+(IsBullishFVGIn L astBars(10) ));


    Code:
    protected override void OnBarUpdate()
    {
    Print("IsBullishFVGInLastBars(7)"+(IsBullishFVGInL astBars(7) ));
    ​
    if (IsBullishFVGInLastBars(10))
    EnterLong(1,PositionSize, "Long Entry");​
    }
    
    bool IsBullishFVGInLastBars(int lookbackBars)
    {
    
    for (int i = 1; i <= lookbackBars; i++)
    {
    
    if (IsBullishFVG(i))
    {
    return true;
    }
    
    }
    
    return false;
    }​
    
    private bool IsBullishFVG(int index)
    {
    return (Close[3] > Open[3] && Close[2] > Open[2] && Close[1] > Open[1]) &&
    Low[1] > High[3] && (Math.Abs(Low[1] - High[3]) >= minFvgSizeStrategy);
    
    }​​
    Last edited by tkaboris; 11-17-2023, 11:48 AM.

    #2
    Hello tkaboris,

    Thank you for your post.

    Creating/using bools, for loops, and if statements are all general C# concepts that are not specific to NinjaTrader. We do not provide C# programming education services or one-on-one educational support in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

    I suggest adding Print statements to your loop to see when different parts of it are being hit and why. Here is an example:
    Code:
    bool IsBullishFVGInLastBars(int lookbackBars)
    {
    
    for (int i = 1; i <= lookbackBars; i++)
    {
    
    if (IsBullishFVG(i))
    {
    Print("IsBullishFVG at index: " + i + ". returning true.);
    return true;
    }
    
    }
    
    Print("returning false");
    return false;
    }​​​
    You can then see when your bool is hitting the parts to return true and the parts to return false. If it is not behaving as expected, you may need to consider breaking out of the loop when it finds a true value as well as consider an if/else statement rather than just an if statement. We have a very basic page about loops in the help guide here:Otherwise, you will need to use external resources such as the following publicly available links:
    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team to follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one-on-one educational services.

    Thank you for using NinjaTrader.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      I added prints and in the loop it prints and evaluates as suppose to
      IsBullishFVG at index: 1. returning true.
      IsBullishFVGInLastBars(7):True​

      but on next bar it evaluates as false and it suppose to evaluate to true for next 7 bars..

      I suppose i should be changing to adding indexes but when i do this my strategy stops when i print Print("IsBullishFVGInLastBars(7):"+(IsBullishFVGIn LastBars(7) )); in onbarupdate.
      Error on calling 'OnBarUpdate' method on bar 13937: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

      Is the syntax right?

      return (Close[index - 3] > Open[index - 3] && Close[index - 2] > Open[index - 2] && Close[index - 1] > Open[index - 1]) &&
      Low[index - 1] > High[index - 3] && (Math.Abs(Low[index - 1] - High[index - 3]) >= minFvgSizeStrategy);​

      Comment


        #4
        Originally posted by tkaboris View Post
        I added prints and in the loop it prints and evaluates as suppose to
        IsBullishFVG at index: 1. returning true.
        IsBullishFVGInLastBars(7):True​

        but on next bar it evaluates as false and it suppose to evaluate to true for next 7 bars..

        I suppose i should be changing to adding indexes but when i do this my strategy stops when i print Print("IsBullishFVGInLastBars(7):"+(IsBullishFVGIn LastBars(7) )); in onbarupdate.
        Error on calling 'OnBarUpdate' method on bar 13937: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

        Is the syntax right?

        return (Close[index - 3] > Open[index - 3] && Close[index - 2] > Open[index - 2] && Close[index - 1] > Open[index - 1]) &&
        Low[index - 1] > High[index - 3] && (Math.Abs(Low[index - 1] - High[index - 3]) >= minFvgSizeStrategy);​
        There is a page about making sure you have enough bars in the data series you are accessing here:


        If you are looking back 7 bars, you will need at least those 7 bars to exist on the chart first and would need to add a check such as:
        if (CurrentBar < 7)
        return;

        Thank you for your time and patience.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          it must be something else
          i have if (CurrentBar < 40) return;
          my guess is with how indexes are referenced...?

          Comment


            #6
            Hello tkaboris,

            Thank you for your reply.

            Rather than guessing, you may use prints to narrow down which line of your script is causing the error and then further narrow down which value in that line is the culprit. The following links cover some helpful tips about using prints to debug as well as other debugging methods that should help you work your way through understanding and resolving unexpected behavior from your script:If you do not understand the output from your prints, please share what prints you have added to your script, what information shows up in the NinjaScript Output window when testing your script, as well as any other errors you are getting in the Log tab of the Control Center.

            Thank you for your time and patience.
            Emily C.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by pibrew, Today, 06:37 AM
            0 responses
            0 views
            0 likes
            Last Post pibrew
            by pibrew
             
            Started by rbeckmann05, Yesterday, 06:48 PM
            1 response
            12 views
            0 likes
            Last Post bltdavid  
            Started by llanqui, Today, 03:53 AM
            0 responses
            6 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Started by burtoninlondon, Today, 12:38 AM
            0 responses
            11 views
            0 likes
            Last Post burtoninlondon  
            Started by AaronKoRn, Yesterday, 09:49 PM
            0 responses
            15 views
            0 likes
            Last Post AaronKoRn  
            Working...
            X