NexusFi: Find Your Edge


Home Menu

 





Using Fold with an adjustable end point


Discussion in ThinkOrSwim

Updated
    1. trending_up 2,670 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 1 posts
    2. attach_file 1 attachments




 
Search this Thread

Using Fold with an adjustable end point

  #1 (permalink)
MarkTheAssKicker
Portland, OR
 
Posts: 1 since Nov 2017
Thanks Given: 0
Thanks Received: 0

I'm using a fold statement and want to have the "to" portion of the loop set to a dynamic number, as opposed to a static one. Like this, where BarsAgoForHigh is calculated previously using a different Fold statement

Def GetLowest = fold j = 0 to BarsAgoForHigh

In other words, BarsAgoForHigh is recalculated every bar, and the fold statement needs to loop that many times



If I set it to a static number, like this, it loops the desired 5 times:

Def GetLowest = fold j = 0 to 5


Here's more specifically what I'm doing. I'm looking for a large upward price swing, followed by price stabilization.

In this example, the stabilization period is 7 days, and the time-frame for the price swing up is 21. So price went up in days 1 through 21, then stabilized in week 4 (days 22-28).

This is the logic:

Starting with 7 days ago, Look back at the prior 21 days to see if there is a jump in price

def HighestHigh = Highest(High[7], 21); # Find the highest high from 7 bars ago back to 28 days ago
def LowestLow = Lowest(Low[7], 21); # Find the highest high from 7 bars ago back to 28 days ago


If there is a price jump, I use a fold loop to calculate how many bars back the highest high was (This variable is called BarsAgoForHigh)

def BarsAgoForHigh = fold i = 0 to 7 with t = 0
do
if high == HighestHigh then
(
t+i
)
else
(
t
);

Then, I use another Fold loop to evaluate what the lowest low is since that high point, which occurred BarsAgoForHigh bars ago. This is where the problem is

def GetLowest = fold j = 0 to BarsAgoForHigh with u = 10000
do
if getvalue(low, j+1) < getvalue(low, j) && getvalue(low, j+1) < u then
(
u*0+ getvalue(low, j+1)

)
else
(
u
);

When BarsAgoForHigh is static (for example, 5), it plots fine
When BarsAgoForHigh is dynamic (as outlined above) it doesn't plot anything (not even other simple plot commands in the study)


The function Lowest(low, BarsAgoForHigh) would be great, however it also does not allow a dynamic length (that's why I turned to Fold loops)

Thank you in advance. I'm just learning this language and find its simplicity can be difficult to work with


side note, is there a way to have an if statement that has multiple then commands?

Edited for clarity

Attached Thumbnails
Click image for larger version

Name:	Capture.JPG
Views:	2579
Size:	88.4 KB
ID:	303194  
Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Increase in trading performance by 75%
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
ZombieSqueeze
Platforms and Indicators
Exit Strategy
NinjaTrader
How to apply profiles
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
28 thanks
Tao te Trade: way of the WLD
24 thanks
Diary of a simple price action trader
21 thanks
My NQ Trading Journal
14 thanks
HumbleTraders next chapter
9 thanks
  #2 (permalink)
 kjhosken 
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
Posts: 96 since Sep 2016
Thanks Given: 7
Thanks Received: 35

I'm not great with folds, but I know TOS doesn't really like dynamic variables. Perhaps try asking the thinkscript lounge as well (it's available within the platform under support/chat -> chat rooms), Mobius is a great coder and hangs out there many mornings.

Follow me on Twitter Reply With Quote




Last Updated on July 30, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts