Coding Help (fold, index and while)

V

venus360

New member
VIP
Can someone help with the thinkscript code to do the following? (I am a thinkscript newbie. I see the reference has fold, index, and while examples but don't know how to do program)

Let us assume following sequence of Up or Down points happened for the past 8 (or n) periods.
+1, +2, +3, -1, -2, -3, +4, +5
Bonus (If I can give more weightage to the most recent periods, it would be great)

I want to store in a variable upPoints adding up +1,+2,+3, +4,+5 = 15
I want to store in a variable downPoints adding up -1, -2, -3 = 6

I then want to plot (the ratio) upPoints / downPoint

Thanks a lot.
 
markos

markos

Well-known member
VIP
@venus360 welcome to useThinkScript. This post is hard to follow. Obviously if you've been trading futures for 10+ years, you know what you're doing.
Please post much more detail: Tick Chart, Candle Chart, Time Frame, Where & How will it be used?
Your wording, "store in a variable" needs clarification e.g. Up or Down periods, plotted in a label or lower, line chart or dots above the candle at (8 or n)... Help us help you.
 
E

eddielee394

Member
At first glance, you'll need to write a function that filters neg/pos values from the array & set each new array to it's own variable (upPoints, downPoints). Then recursively iterate over each in order to add the values together (similar to the reduce() method in javascript). If i have some time after market today i'll try to post some example code.
 
V

venus360

New member
VIP
Thank you @markos

I am sorry, if it is not clear. Here is more explanation.

I am looking to find Divergence of price and this ratio (up-down points) using the following method and back test for results.

I will use it with Daily charts but any period more than 15 mins should provide good results. Candle chart for price and plotting this ratio as a line (just like a moving average).

Let us assume following sequence of Up or Down points happened for the past 8 (or n) periods.
+1, +2, +3, -1, -2, -3, +4, +5

Bonus (If I can give more weightage to the most recent periods, it would be great)

What I wrote below is my crude form of pseudo code. Based on @eddielee394's response, he understood what I want. Only thing is I don't know how to code this piece. If I get some snippets of code, I will be able to put the whole thing together.

I want to store in a variable upPoints adding up +1,+2,+3, +4,+5 = 15
I want to store in a variable downPoints adding up -1, -2, -3 = 6

I then want to get (the ratio) upPoints / downPoint and then plot

I want to then plot this in the lower part of the chart (like an oscillator).

Hope this is better.
 
T

tomsk

Well-known member
VIP
@venus360 Per your request here is a study that plots the ratio of Up/Down points relative to the prior candle that utilizes the fold() construct. I have used 8 candles(periods) as per your description. Additionally I have verified the accuracy of the results by comparing it with manual computations of a stock like AMZN. The results were identical.

Code:
# UpPoints DownPoints Ratio
# tomsk
# 1.17.2020

# Plots the ratio of Up/Down points relative to the prior candle

declare lower;

def upPoints   = fold i = 0 to 8
                 with p
                 do p + GetValue(if close > close[1] then close - close[1] else 0, i);
def downPoints = fold j = 0 to 8
                 with r
                 do r + GetValue(if close < close[1] then close - close[1] else 0, j);
plot ratio = upPoints / AbsValue(downPoints);

AddLabel(1, "Up Points = " + upPoints, Color.Yellow);
AddLabel(1, "Down Points = " + downPoints, Color.Yellow);
# End UpPoints DownPoints Ratio
 
V

velocity12345

New member
Guys, I am a little new and need some help with fold and count.

I am trying to do the following:
I have a support level on each and every bar. I want to know what (day/bar) support was breached for a certain number of days held going forward and then place the number of days above the starting bar.

For example lets say on January 29th the SPX Support level was 3240 and lets say the "DaysHeld" equals 5. The study would then check to see if the next bar, 1-30-20, low is less than 3240, if not it goes to the next day, 1-31-20, if the low is less than 3240, (lets say it is) then the DaySupportBroken would equal 2. Then the days it took to break support would be placed on the 1-29-20 bar. ie. a number 2 would be placed above 1-29-20. If no low broke support from 1-29-20 going forward 5 days then then a zero would be placed above 1-29-20

This is what I have:

input DaysHeld = 5;
def PivotLevel = MovAvgExponential(low, DaysHeld ) - 96.62 ; #rigged value to break support
plot PivotLevelPlot = PivotLevel ;
PivotLevelPlot.SetDefaultColor(Color.CYAN);
PivotLevelPlot.SetLineWeight(2);
PivotLevelPlot.SetPaintingStrategy(PaintingStrategy.DASHES);

def DaySupportBroken = fold idx = 1 to DaysHeld with s = 0 do if Low < Less than the <startbar>support then tell me what day it broke support else 0;

plot CountDayBroken = DaySupportBroken ;
CountDayBroken SetPaintingStrategy(PaintingStrategy.Values_Above);


Any help would be greatly appreciated.

thanks,
 

Similar threads

Top