how change index in fold?

Meysam1984

New member
hi i want create something like Tenkan future so i use this code and worked correctly.

Code:
def futureTenkan = fold i = 0 to tenkanPeriod with result = Double.NaN do
     if i == 1 then
        (Highest(high, tenkanPeriod - 8) + Lowest(low, tenkanPeriod - 8)) / 2
    else if i == 2 then
        (Highest(high, tenkanPeriod - 7) + Lowest(low, tenkanPeriod - 7)) / 2
    else if i == 3 then
        (Highest(high, tenkanPeriod - 6) + Lowest(low, tenkanPeriod - 6)) / 2
    else if i == 4 then
        (Highest(high, tenkanPeriod - 5) + Lowest(low, tenkanPeriod - 5)) / 2
    else if i == 5 then
        (Highest(high, tenkanPeriod - 4) + Lowest(low, tenkanPeriod - 4)) / 2
    else if i == 6 then
        (Highest(high, tenkanPeriod - 3) + Lowest(low, tenkanPeriod - 3)) / 2
    else if i == 7 then
        (Highest(high, tenkanPeriod - 2) + Lowest(low, tenkanPeriod - 2)) / 2
    else
        (Highest(high, tenkanPeriod - 1) + Lowest(low, tenkanPeriod - 1)) / 2;


def shiftedFutureTenkan = futureTenkan[tenkanPeriod];
plot TenkanFuture = if IsNaN(close[0]) then shiftedFutureTenkan else Double.NaN;



TenkanFuture.SetDefaultColor(Color.CYAN);
TenkanFuture.SetPaintingStrategy(PaintingStrategy.LINE);



but in this form If i change Tenkan period to more the 8 i can not calculate it. I use the above code because this code is not working and I get error on i variable that not the const value
Code:
def futureTenkan = fold i = 0 to tenkanPeriod with result = Double.NaN do
    if i == 0 then
        Double.NaN
    else
        (Highest(high, tenkanPeriod - i) + Lowest(low, tenkanPeriod - i)) / 2;
also in first code block the plot draw with last loop, but i want each candle calculate separately and differently.
for more information i want create this for mql to thinkscript.
the mql code is like:
Code:
TenkanFutureBuffer[9]=EMPTY_VALUE;
         TenkanFutureBuffer[8]=TenkanBuffer[0];
         for(int q=7 ; q>=0; q--)
           {
            highest = high[ArrayMaximum(high,0,q+1)];
            lowest = low[ArrayMinimum(low,0,q+1)];
            T = (highest + lowest)/2;
            TenkanFutureBuffer[q]=T;
           }
how can create first code block in second block. so my code is reduce and work with any period?
 
Last edited:
Solution
hi i want create something like Tenkan future so i use this code and worked correctly.

Code:
def futureTenkan = fold i = 0 to tenkanPeriod with result = Double.NaN do
     if i == 1 then
        (Highest(high, tenkanPeriod - 8) + Lowest(low, tenkanPeriod - 8)) / 2
    else if i == 2 then
        (Highest(high, tenkanPeriod - 7) + Lowest(low, tenkanPeriod - 7)) / 2
    else if i == 3 then
        (Highest(high, tenkanPeriod - 6) + Lowest(low, tenkanPeriod - 6)) / 2
    else if i == 4 then
        (Highest(high, tenkanPeriod - 5) + Lowest(low, tenkanPeriod - 5)) / 2
    else if i == 5 then
        (Highest(high, tenkanPeriod - 4) + Lowest(low, tenkanPeriod - 4)) / 2
    else if i == 6 then
        (Highest(high, tenkanPeriod - 3) +...
hi i want create something like Tenkan future so i use this code and worked correctly.

Code:
def futureTenkan = fold i = 0 to tenkanPeriod with result = Double.NaN do
     if i == 1 then
        (Highest(high, tenkanPeriod - 8) + Lowest(low, tenkanPeriod - 8)) / 2
    else if i == 2 then
        (Highest(high, tenkanPeriod - 7) + Lowest(low, tenkanPeriod - 7)) / 2
    else if i == 3 then
        (Highest(high, tenkanPeriod - 6) + Lowest(low, tenkanPeriod - 6)) / 2
    else if i == 4 then
        (Highest(high, tenkanPeriod - 5) + Lowest(low, tenkanPeriod - 5)) / 2
    else if i == 5 then
        (Highest(high, tenkanPeriod - 4) + Lowest(low, tenkanPeriod - 4)) / 2
    else if i == 6 then
        (Highest(high, tenkanPeriod - 3) + Lowest(low, tenkanPeriod - 3)) / 2
    else if i == 7 then
        (Highest(high, tenkanPeriod - 2) + Lowest(low, tenkanPeriod - 2)) / 2
    else
        (Highest(high, tenkanPeriod - 1) + Lowest(low, tenkanPeriod - 1)) / 2;


def shiftedFutureTenkan = futureTenkan[tenkanPeriod];
plot TenkanFuture = if IsNaN(close[0]) then shiftedFutureTenkan else Double.NaN;



TenkanFuture.SetDefaultColor(Color.CYAN);
TenkanFuture.SetPaintingStrategy(PaintingStrategy.LINE);



but in this form If i change Tenkan period to more the 8 i can not calculate it. I use the above code because this code is not working and I get error on i variable that not the const value
Code:
def futureTenkan = fold i = 0 to tenkanPeriod with result = Double.NaN do
    if i == 0 then
        Double.NaN
    else
        (Highest(high, tenkanPeriod - i) + Lowest(low, tenkanPeriod - i)) / 2;
also in first code block the plot draw with last loop, but i want each candle calculate separately and differently.
for more information i want create this for mql to thinkscript.
the mql code is like:
Code:
TenkanFutureBuffer[9]=EMPTY_VALUE;
         TenkanFutureBuffer[8]=TenkanBuffer[0];
         for(int q=7 ; q>=0; q--)
           {
            highest = high[ArrayMaximum(high,0,q+1)];
            lowest = low[ArrayMinimum(low,0,q+1)];
            T = (highest + lowest)/2;
            TenkanFutureBuffer[q]=T;
           }
how can create first code block in second block. so my code is reduce and work with any period?

you may think your code 'works correctly' , but it is not.
you are not using the variable 'result' , so values from each loop are replaced with the next one. nothing is combined.
it is looping and calculating different values on every loop, but it isn't saving them and using previous values.
futureTenkan always equals the last else statement.

(Highest(high, tenkanPeriod - 1) + Lowest(low, tenkanPeriod - 1)) / 2;

so you could replace all of that fold formula with this,

def futureTenkan = (Highest(high, tenkanPeriod - 1) + Lowest(low, tenkanPeriod - 1)) / 2;

you don't have a code line to set a value for , tenkanPeriod , so i'm guessing you posted partial code.


what are you really trying to do?

-------------------------

here is a modified test code to show this,
a bubble has parts of the fold statement,
futureTenkan , first 'else' , and last 'else'
tenkanPeriod , always equals the last part of the fold formula

Code:
input tenkanPeriod = 9;

def futureTenkan = fold i = 0 to tenkanPeriod
 with result = Double.NaN
 do
     if i == 1 then
        (Highest(high, tenkanPeriod - 8) + Lowest(low, tenkanPeriod - 8)) / 2
    else if i == 2 then
        (Highest(high, tenkanPeriod - 7) + Lowest(low, tenkanPeriod - 7)) / 2
    else if i == 3 then
        (Highest(high, tenkanPeriod - 6) + Lowest(low, tenkanPeriod - 6)) / 2
    else if i == 4 then
        (Highest(high, tenkanPeriod - 5) + Lowest(low, tenkanPeriod - 5)) / 2
    else if i == 5 then
        (Highest(high, tenkanPeriod - 4) + Lowest(low, tenkanPeriod - 4)) / 2
    else if i == 6 then
        (Highest(high, tenkanPeriod - 3) + Lowest(low, tenkanPeriod - 3)) / 2
    else if i == 7 then
        (Highest(high, tenkanPeriod - 2) + Lowest(low, tenkanPeriod - 2)) / 2
    else
        (Highest(high, tenkanPeriod - 1) + Lowest(low, tenkanPeriod - 1)) / 2;

plot z1 = futureTenkan;

addchartbubble(1, low,
"F " + futureTenkan + "\n" +
"8 " + ((Highest(high, tenkanPeriod - 8) + Lowest(low, tenkanPeriod - 8)) / 2) + "\n" +
"1 " + ((Highest(high, tenkanPeriod - 1) + Lowest(low, tenkanPeriod - 1)) / 2) + "\n" 
,color.yellow, no);
#
 

Attachments

  • 01a-img1.JPG
    01a-img1.JPG
    70.5 KB · Views: 30
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
400 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top