Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to export a variable from a while statement

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

    How to export a variable from a while statement

    hey there,

    I'm not sure how to do this (and I assume anything is possible in programming), but I want to export a variable by potentially running the while statement again if needed, and have it stop at CurrentBar to export a variable - I THINK that would be the way to do it. If at the CURRENTBAR that I'm looking at Close[0] is below or above "latestlow" I want something to happen. However, outside of while loop, or the indicator "latestlow" is always 0.

    Here's the method with while loops ---- SKIP below for more defined question please ---

    public int METHOD(int period, Brush color)
    {
    #region LowPivot
    int y = 0;
    int Lvls = 0;

    //Four Matching Lows
    if(Low[period]==Low[period+1] && Low[period]==Low[period+2] && Low[period]==Low[period+3])
    {
    y = 1;
    while(y<=period)
    {
    if(y!=period ? Low[period+3]<Low[period+3+y] : Low[period+3]<Low[period+3+y])
    Lvls++;
    if(y!=period ? Low[period]<Low[period-y] : Low[period]<Low[period-y])
    Lvls++;
    y++;
    }
    }
    //Three Matching Lows
    else if(Low[period]==Low[period+1] && Low[period]==Low[period+2])
    {
    y=1;
    while (y<=period)
    {
    if (y!=period ? Low[period+2]<Low[period+2+y] : Low[period+2]<Low[period+2+y])
    Lvls++;
    if (y!=period ? Low[period]<Low[period-y] : Low[period]<Low[period-y])
    Lvls++;
    y++;
    }
    }
    //Two Matching Lows
    else if (Low[period]==Low[period+1])
    {
    y=1;
    while(y<=period)
    {
    if (y!=period ? Low[period+1]<Low[period+1+y] : Low[period+1]<Low[period+1+y])
    Lvls++;
    if (y!=period ? Low[period]<Low[period-y] : Low[period]<Low[period-y])
    Lvls++;
    y++;
    }
    }
    //Regular Pivot
    else
    {
    y=1;
    while (y<=period)
    {
    if (y!=period ? Low[period]<Low[period+y] : Low[period]<Low[period+y])
    Lvls++;
    if (y!=period ? Low[period]<Low[period-y] : Low[period]<Low[period-y])
    Lvls++;
    y++;
    }
    }

    //Auxiliary Checks
    if (Lvls<period*2)
    {
    Lvls = 0;
    //Four Lows - First and Last Matching - Middle 2 are lower
    if (Low[period] <= Low[period+1] && Low[period]<=Low[period+2] && Low[period]==Low[period+3])
    {
    y = 1;
    while (y <= period)
    {
    if (y != period ? Low[period + 3] < Low[period + 3 + y] : Low[period + 3] < Low[period + 3 + y])
    Lvls++;
    if (y != period ? Low[period] < Low[period - y] : Low[period] < Low[period - y])
    Lvls++;
    y++;
    }
    }
    }
    if(Lvls < period * 2)
    {
    Lvls = 0;
    //Three Lows - Middle is lower than two outside
    if (Low[period] <= Low[period + 1] && Low[period]==Low[period + 2])
    {
    y = 1;
    while (y <= period)
    {
    if (y != period ? Low[period + 2]<Low[period + 2 + y] : Low[period + 2] < Low[period + 2 + y])
    Lvls++;
    if (y != period ? Low[period] < Low[period-y] : Low[period] <Low[period - y])
    Lvls++;
    y++;
    }
    }
    }
    if (Lvls >= period * 2)
    {
    Lower[period] = Low[period]; // draw the block

    if (lowdrawbar != CurrentBar)
    {
    if (showText)
    {
    Draw.Text(this, "Low"+lowcount, true, Low[period].ToString(), period, Low[period], -20, color,
    textFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
    }
    if (showRays)
    {
    Draw.Ray (this, "lowray"+lowcount, false, CurrentBar - lastlowbar, lastlow, period, Low[period],
    color, DashStyleHelper.Dash, lineWidth);
    }
    lowcount++;
    if (lowcount == history + 1)
    {
    lowcount = 1;
    }
    lowdrawbar = CurrentBar;
    }
    lastlow = Low[period]; // Save the low value for next ray drawn
    lastlowbar = CurrentBar - period; // Save the low value location for next ray drawn


    }

    ----- And we're back! ---

    The variable I need is "lastlow" at a specific point. Say, if Close[0] crosses below or above lastlow, I want something executed. The method in this indicator returns y - used for something else within the indicator, and that's just the number "3".

    If I print from within the method, or outside the method, prior to "return y;" from within the indicator, I get all the values at every single bar; but it will establish and keep the same latestlow for let's say 10+ bars. Since there are while methods, how can I access this at the RIGHT point, interjecting and picking up the latestlow, during the bar times that I'm looking it.
    "Print (latestlow + CurrentBar); "



    If i do -- ThisIndicator.latestlow I will always get a 0, since the while loop has not run, or if it has run, I will get the latest value only at the end of it.

    Any thoughts are very much appreciated.

    #2
    Hello lmatiukas.

    Thanks for the post.

    I am not sure what you are trying to do here. This method seems incomplete and I do not have the full context of the script to test. Please add print statements within this method to make sure the logic is moving the data in the way you desire.

    I would be able to better assist if you posted your script here or send it into platformsupport[at]ninjatrader[dot]com and copy/paste this forum link in the body of the email. The script can be found under Documents/NinjaTrader 8/bin/Custom/Indicators or Strategies folder.

    Something that might help initially is simplifying the method and make sure that it works for one condition set, then add the rest of the conditions/while loops prior to that.
    To break while inside of a while loop, you can use the 'break' keyword to exit the loop. You can also use 'continue;' to skip a loop iteration.

    I look forward to your reply.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello lmatiukas,

      I wanted to make sure you saw my original post.
      Chris L.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by aeadon, Today, 02:27 AM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by op1031, Today, 01:52 AM
      0 responses
      1 view
      0 likes
      Last Post op1031
      by op1031
       
      Started by Aramunno, Yesterday, 10:11 PM
      2 responses
      16 views
      0 likes
      Last Post Aramunno  
      Started by sdauteuil, Yesterday, 09:38 PM
      0 responses
      5 views
      0 likes
      Last Post sdauteuil  
      Started by fscabrera03, Yesterday, 09:23 PM
      0 responses
      11 views
      0 likes
      Last Post fscabrera03  
      Working...
      X