thinkscript While Loop like in Java or C

Discussion in 'Strategy Building' started by trader_knight, Sep 12, 2020.

  1. Hey guys I'm confused on how to implement a set of conditions that I want like an original while loop in Java or C because fold is acting like a for loop and I don't have a length for the fold <index>

    Code:
    def <result> = fold <index> = <start> to <end> [ with <variable> [ = <init> ] ] [ while <condition> ] do <expression>;
    
    because I want my condition to run until these conditions in the while are false. Appreciate any help received, Ty!
    
    while (x == 1 and y == 1 and z == 1)
    {
    
    
    };
     
    Last edited by a moderator: Sep 12, 2020
  2. ThinkScript is a script language, not a programming language, so you need to alter your mindset from typical programming, to what are the capabilities available, and how can you use that knowledge to accomplish your goal!

    Insufficient problem statement to make suggestion, but the notion of a "while" in thinkscript makes no sense! Think of replacing "while" with "if", then try to rethink in those terms!

    If you post more about what you are trying to do, you may get useful feedback. Also, note that during the trading days, the thinkscript chatroom has many TOS clients that are extremely proficient in thinkscript, and they may be able to provide helpful advice.
     
    TimtheEnchanter likes this.
  3. Step 1: I have two indicators that are custom made that each act like moving averages which crosses each other and they are both an AND statement. which prevents that even if the lines of the first indicator crosses each other it still waits until the 2nd indicator lines cross and then turns true.

    Step 2: After they are both true, While the lines have not yet cross again, I will be recalculate and overwriting the highs & lows breaks until they cross again and then the high & low pivots are set permanent.

    for example the lines can be going up and cross 3 times up & down continuously and still be consider a bearish trend so they act like moving averages just because they are lines but their meanings are different.
     
  4. Hm! Still waiting for you to post something resembling an objective or problem you are desiring to solve! Beware that if you are unable to state the problem, that may be a hint to the root issue!
     
  5. Peter8519

    Peter8519

    In the for-loop, the index has a fixed increment in each iteration.
    For example, in this loop, i will start from 1, 2, 3... 10.
    for (i=1, i<=10, i++) {
    do something....
    }
    Where as while loop, as you have indicated, x, y and z must all be 1 before the loop is executed.
    Once inside the while loop, anyone of x, y or z is not equal to 1, the loop will stop. But if the condition is such that x, y and z remain as 1, then it becomes an infinite loop.
     
  6. *Objective*
    Calculating and then plotting pivots points "Peaks & Valleys" when indicators lines cross.

    *Problem 1-2*
    1. I try the best of my efforts and could not code up a while loop in Thinkscript. I'm here in order to confirm whether or not a while loop without including the fold function exist in the language.

    2. I want to consider them as a house light switch instead of moving averages just it one crosses up we consider them Bullish and if down Bearish. I want to consider my lines While the lines have not yet cross again, I will be recalculate and overwriting the highs & lows breaks until they cross again and then the high & low pivots are set permanent.
     
    Last edited: Sep 13, 2020
  7. Thanks for replying Peter8519, but my question is not what is a "while loop" but I think a while loop does not exist in Thinkscript language. If someone knows how to use a while loop without using the function fold, so a while loop by itself in the Thinkscript language.

    It would be greatly appreciated!
     
  8. There is NO While concept in Thinkscript! If you have experience with ThinkScript, you will understand there is no need for a "while"!
    Each Bar will normally be processed once. The last bar (the bar that has not yet closed) will be processed for each change in the inferred symbol for the Chart (assuming you are using in chart).
    Perhaps you should begin with some sample code and then figure out what you want to do.
    Here is an intra-day pivot point example courtesy of Mobius. So if you are using intra-day, this may give you some ideas.


    # - - BEGIN SCRIPT
    # - - Study Name: AAAAA_PivotPoints


    input Aggregate = {W, default D, M};
    input ShowValuesAtTop = no;
    input ShowBubbles = yes;
    input ShowOnDaily = yes;
    input BubbleOffset = -3;

    def today = if getDay() == getLastDay() then 1 else double.NaN;
    def Agg = if Aggregate == Aggregate.D then AggregationPeriod.DAY else
    if Aggregate == Aggregate.W then AggregationPeriod.WEEK else
    if Aggregate == Aggregate.M then AggregationPeriod.MONTH else AggregationPeriod.MIN;

    def highestBarNumber = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);

    def isDaily = if !ShowOnDaily and GetAggregationPeriod() == AggregationPeriod.DAY then yes else no;

    def thisOpen = open(period = Agg)[1];
    def thisHigh = high(period = Agg)[1];
    def thisLow = low(period = Agg)[1];
    def thisClose = close(period = Agg)[1];

    def thisPP = if isDaily or isNaN(Today) then Double.NaN else (thisHigh + thisLow + thisClose) / 3;

    def thisR1 = if isDaily then Double.NaN else (2 * thisPP) - thisLow;
    def thisR2 = if isDaily then Double.NaN else thisPP + (thisHigh - thisLow);
    def thisR3 = if isDaily then Double.NaN else thisHigh + 2 * (thisPP - thisLow);
    def thisR1Mid = if isDaily then Double.NaN else (thisR1 + thisPP) / 2;
    def thisR2Mid = if isDaily then Double.NaN else (thisR1 + thisR2) / 2;

    def thisS1 = if isDaily then Double.NaN else (2 * thisPP) - thisHigh;
    def thisS2 = if isDaily then Double.NaN else thisPP - (thisHigh - thisLow);
    def thisS3 = if isDaily then Double.NaN else thisLow - 2 * (thisHigh - thisPP);
    def thisS1Mid = if isDaily then Double.NaN else (thisS1 + thisPP) / 2;
    def thisS2Mid = if isDaily then Double.NaN else (thisS1 + thisS2) / 2;

    plot R3 = thisR3;
    plot R2 = thisR2;
    plot R1 = thisR1;
    plot PP = thisPP;
    plot S1 = thisS1;
    plot S2 = thisS2;
    plot S3 = thisS3;

    AddLabel(!isDaily and ShowValuesAtTop, "R3: " + R3, Color.WHITE);
    AddLabel(!isDaily and ShowValuesAtTop, "R2: " + R2, Color.WHITE);
    AddLabel(!isDaily and ShowValuesAtTop, "R1: " + R1, Color.WHITE);
    AddLabel(!isDaily and ShowValuesAtTop, "PP: " + PP, Color.WHITE);
    AddLabel(!isDaily and ShowValuesAtTop, "S1: " + S1, Color.WHITE);
    AddLabel(!isDaily and ShowValuesAtTop, "S2: " + S2, Color.WHITE);
    AddLabel(!isDaily and ShowValuesAtTop, "S3: " + S3, Color.WHITE);


    R3.DefineColor("R3", Color.WHITE);
    R3.SetStyle(Curve.SHORT_DASH);
    R3.SetLineWeight(1);
    R3.HideBubble();
    R2.DefineColor("R2", Color.WHITE);
    R2.SetStyle(Curve.SHORT_DASH);
    R2.SetLineWeight(1);
    R2.HideBubble();
    R1.DefineColor("R1", Color.WHITE);
    R1.SetStyle(Curve.SHORT_DASH);
    R1.SetLineWeight(1);
    R1.HideBubble();
    PP.DefineColor("PP", Color.WHITE);
    PP.SetStyle(Curve.SHORT_DASH);
    PP.SetLineWeight(1);
    PP.HideBubble();
    S1.DefineColor("S1", Color.WHITE);
    S1.SetStyle(Curve.SHORT_DASH);
    S1.SetLineWeight(1);
    S1.HideBubble();
    S2.DefineColor("S2", Color.WHITE);
    S2.SetStyle(Curve.SHORT_DASH);
    S2.SetLineWeight(1);
    S2.HideBubble();
    S3.DefineColor("S3", Color.WHITE);
    S3.SetStyle(Curve.SHORT_DASH);
    S3.SetLineWeight(1);
    S3.HideBubble();


    def BubbleBar = if ShowBubbles and (BarNumber() == highestBarNumber - BubbleOffset) then yes else no;

    AddChartBubble(BubbleBar, R3, " R3", R3.TakeValueColor(), yes);
    AddChartBubble(BubbleBar, R2, " R2", R2.TakeValueColor(), yes);
    AddChartBubble(BubbleBar, R1, " R1", R1.TakeValueColor(), yes);
    AddChartBubble(BubbleBar, PP, " PP", PP.TakeValueColor(), yes);
    AddChartBubble(BubbleBar, S1, " S1", S1.TakeValueColor(), yes);
    AddChartBubble(BubbleBar, S2, " S2", S2.TakeValueColor(), yes);
    AddChartBubble(BubbleBar, S3, " S3", S3.TakeValueColor(), yes);


    # - - END SCRIPT
     
  9. Thanks for the clear yes or no answer for the while loop
    Thank you stepandfetchit & Mobius
     
  10. userque

    userque

    Do you still need help with this?
     
    #10     Sep 24, 2020