Robert Payne offering to help with ThinkScript

Status
Not open for further replies.

RobertPayne

Member
This is Robert Payne of Fun with ThinkScript. I would like to contribute to this forum by offering to answer questions for those of you who are coding your own indicators. If you are stuck on something, perhaps I can help you figure it out.

Mod edit

Due to the inactivity of @RobertPayne, I have decided to lock this thread. Thank you, Robert, for your contribution. All unanswered queries will be moved to their own dedicated thread. If anyone else has any other questions, please create a new post in the Questions forum. Our community members will chime in to help whenever possible. :)
 
@RobertPayne TOS coders in Nebraska told me that creating a % Above 200 SMA of S&P 500 cannot currently be done. Using the S&P 100, Do you happen to know if there is a workaround for creating a chart like this? Thank you. We can do it w Hi's/Low's per below but not % Above xxDayMA. I could list all 100 stocks and create a ratio, I suppose, but not sure. https://tos.mx/VhAvab
 
@markos You would have to assign the closing price of each and every one of the 500 stocks in your script, then run calculations on each. Similar to the way you have done the dozen or so stocks in your example above. With that many stocks, you would probably run into the "script too long" error and would most certainly bring TOS to its knees. It would be far too slow to be of any use---and that's assuming it ever finished the calculations.
 
You would have to assign the closing price of each and every one of the 500 stocks in your script, then run calculations on each. Similar to the way you have done the dozen or so stocks in your example above. With that many stocks, you would probably run into the "script too long" error and would most certainly bring TOS to its knees. It would be far too slow to be of any use---and that's assuming it ever finished the calculations.
lol - you will have the TOS Gods hunting for you ;)- the services will throttle the request and timeout your call. one idea, is that if you can do enough o the logic in a watchlist, then export it or use the DDE feature, and complete the calculation in excel, and leverage the TOS watchlist/data as the real time data that is feed into a custom spreadsheet/tool you built with the formula. DDE will require windows or vm, won't work on Mac version of Excel.

Best!
 
That's awesome to see you around, I really admire your work. I'm working on something that's going to aim to be an MTF Div w/ Master Key. Going to try to knock my head against a wall and see if I make it work but if I get too caught up I may come back and take up your offer.

ZCSL0Q2.png
 

Attachments

  • ZCSL0Q2.png
    ZCSL0Q2.png
    197.9 KB · Views: 161
@RobertPayne - I don't have a question about a script per se. I was wondering if it's possible to have a Candle Plot MTF indicator? Here's a link that shows what I'm referring to - https://www.tradingview.com/chart/EURUSD/CI3qgX6c-Candle-Plot-MTF-Introduction/

Yes, it is possible. I wrote one at the beginning of the year just to see if I could. The top chart is a 5 min chart with 15 minute overlay. The bottom chart is the same 5 min chart with a 60 min overlay.

If the community wants to have this indicator, I recommend starting a new thread here in the tutorial section. Then, as members work on it, and contribute their code and ideas to that thread, I will be willing to answer questions to assist in developing it yourselves.

I feel it could be beneficial if it were developed in the open. Several of the concepts used in this indicator could be applied in other indicators to further develop scripting abilities.

vFzgeZz.png
 
Last edited:
I cannot figure out why this does not produce angle in degrees. not like trigonometry was ever my strong point but it seems straightforward yet I dont get right result:
Code:
def height = MiddleLR - lowest(middleLR,length);
def Angle = ATan(height/length)*180/Double.Pi;
addlabel(yes,angle +"h:"+height, if angle >anglemin then color.green else if angle<-anglemin then color.red else color.gray);
When this is complete would you please post the finished script ? Many Thanks.
 
@RobertPayne Hi Robert, Please could you take a look at my setting and let me know what I'm doing wrong and why it doesn't work. I'm trying to setup a scanner where MACD crosses DI-. The blue line is DI-

These are my setting

LY8x1Ss.png


Thanks a bunch!
 

Attachments

  • LY8x1Ss.png
    LY8x1Ss.png
    17.7 KB · Views: 137
I want to get options volume for all strikes. I know how to get it for one strike . Is there a way to get cumulative volume without iterating through every single strike? Dont even think iteration possible (since there is no proper cycle in thinkscript)
 
@RobertPayne am new here and I am not a coder by any means...I have a converted Double Stochastics Indicator from Ninja Trader platform to TOS...The issue is that its the Slow Stochastics and I am wondering IF there is a way to add on or convert the Double Stochastics from Ninja Trader to a FULL Stochastics that is found in TOS? Not much would need to be done other than adding the "slowing period" and the "break out signals" and possibly change the color of the line from Cyan to Green...If someone can help me out I would really appreciate it...I've back tested this indicator and it seems very promising so this might be a really nice asset to the community.

The code can be found here: https://futures.io/thinkorswim/34312-double-stochs-tos-thinkorswim-trading-platform.html
 
Last edited:
Hi @RobertPayne,

Thanks for your help with coding in this thread. It's much appreciated.

In another thread I wrote about my struggle getting a code up and running based on TD Sequential. I was wondering if you could help. Below is the code.

Code:
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def bar2 = if (beforeStart[2] == 1 and beforeStart[1] == 0) or (isRollover[1] and beforeStart[1] == 0) then 1 else 0;
def bar3 = if (beforeStart[3] == 1 and beforeStart[2] == 0) or (isRollover[2] and beforeStart[2] == 0) then 1 else 0;
def bar4 = if (beforeStart[4] == 1 and beforeStart[3] == 0) or (isRollover[3] and beforeStart[3] == 0) then 1 else 0;

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open[1] else
                 if (close>brk[1] and close>close[1]) or (close<brk[1] and close<close[1]) then open[4] else brk[1];
def hi =  if bar1 then if close > open then close else open else if bar2 or bar3 or bar4 then hi[1] else
                 if open[1] <= brk[1] and close > brk[1] then open else hi[1];
def lo =  if bar1 then if close > open then open else open else if bar2 or bar3 or bar4 then lo[1] else
                 if open[1] >= brk[1] and close < brk[1] then close else lo[1];
def dir = if bar1 or bar2 or bar3 then 0 else if bar4 then (if close < open[4] then -1 else if close > open[4] then 1 else 0) else
                 if close>brk then 1 else if close<brk then -1 else dir[1];


plot dir_plot = round(dir,0);
dir_plot.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
dir_plot.SetDefaultColor(Color.white);

Basically, my goal is to mark bars with 1 or -1 depending on if we are in an uptrend or a downtrend. An uptrend occurs when we make higher highs without crossing back below the open of four bars before the latest high; we switch to a downtrend once this occurs, and stay in a downtrend as we make lower lows, until we cross back above the open four bars before the latest low; and then the process repeats.

The code above doesn't do this. In order to do this, the following line:

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open[1] else
if (close>brk[1] and close>close[1]) or (close<brk[1] and close<close[1]) then open[4] else brk[1];

would need to read:

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open[1] else
if (close>brk[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[4] else brk[1];

But of course, I can't refer to 'hi' and 'lo' before defining them. Any suggestions on how to get around this? I feel like this isn't that hard an issue but I just can't see the solution.
 
Last edited:
But of course, I can't refer to 'hi' and 'lo' before defining them. Any suggestions on how to get around this? I feel like this isn't that hard an issue but I just can't see the solution.

You can define the variables first.

Code:
def brk;
def hi;
def lo;
def dir;

brk = if bar1 then open else if bar2 or bar3 or bar4 then open[1] else
if (close>brk[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[4] else brk[1];
hi =  if bar1 then if close > open then close else open else if bar2 or bar3 or bar4 then hi[1] else
                 if open[1] <= brk[1] and close > brk[1] then open else hi[1];
lo =  if bar1 then if close > open then open else open else if bar2 or bar3 or bar4 then lo[1] else
                 if open[1] >= brk[1] and close < brk[1] then close else lo[1];
dir = if bar1 or bar2 or bar3 then 0 else if bar4 then (if close < open[4] then -1 else if close > open[4] then 1 else 0) else
                 if close>brk then 1 else if close<brk then -1 else dir[1];
 
Hi @RobertPayne , I have been using your code on peaks and valleys from funwiththinkscript with a few modifications from me and I wanted to write a column that will change the background color when the close of the current bar enters within 10 cents of the previous peak or valley. I know how to change the background color but what I'm having trouble with that. The code I've been putting together hasn't been working. Would you be able to take a look and tell me what I'm doing wrong? Thanks in advance
Code:
# +--------------------------------------------------+
# |   Example showing how to hide a plot when the    |
# | most recent close is not within a certain range. |
# |                   robert payne                   |
# |              [rrpayne.blogspot.com]              |
# +--------------------------------------------------+

# | define a peak and plot it
input magnitude = 5;

def peak = high > Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakValue = if peak then high else peakValue[1];
plot peakLine = peakValue;
     peakLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     peakLine.SetDefaultColor(Color.green);

# | get the bar numbers for the most recent close
# | and the most recent peak
def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
def peakBar = if peak then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastPeakValue = GetValue(high, BarNumber() - HighestAll(peakBar));
def prevPeakValue = GetValue(peakValue[1], BarNumber() - HighestAll(peakBar));

# | find the value of the most recent close
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);

# | define what is considered to be "in range" of the previous peak
input percent = 0.5;

def inRange = mostRecentClose > (prevPeakValue * (1 - percent / 100)) and mostRecentClose < (prevPeakValue * (1 + percent / 100));

# | extend the most recent peak
plot lastPeakExtension = if BarNumber() >= HighestAll(peakBar) then lastPeakValue else Double.NaN;
     lastPeakExtension.SetDefaultColor(Color.light_green);
# | extend the previous peak only if the most recent close value is "in range"
plot prevPeakExtension = if BarNumber() >= HighestAll(peakBar) - 1 then prevPeakValue else Double.NaN;
     prevPeakExtension.SetDefaultColor(Color.Dark_Orange);
     prevPeakExtension.SetHiding(!inRange);

# | define a valley and plot it


def valley = low < lowest(low[1], magnitude) and low <= lowest(low[-magnitude], magnitude);
def valleyValue = if valley then low else valleyValue[1];
plot valleyLine = valleyValue;
     valleyLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     valleyLine.SetDefaultColor(Color.red);

# | get the bar numbers for the most recent close
# | and the most recent valley

def valleyBar = if valley then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastvalleyValue = GetValue(low, BarNumber() - lowestAll(valleyBar));
def prevvalleyValue = GetValue(valleyValue[1], BarNumber() - lowestAll(valleyBar));




# | define what is considered to be "in range" of the previous peak


def inRange2 = mostRecentClose < (prevValleyValue * (1 - percent / 100)) and mostRecentClose > (prevValleyValue * (1 + percent / 100));

# | extend the most recent peak
plot lastValleyExtension =  if BarNumber() <= lowestAll(valleyBar) then lastvalleyValue else Double.NaN;
     lastValleyExtension.SetDefaultColor(Color.plum);
# | extend the previous peak only if the most recent close value is "in range"
plot prevValleyExtension = if BarNumber() >= lowestAll(ValleyBar) - 1 then prevValleyValue else Double.NaN;
     prevValleyExtension.SetDefaultColor(Color.Dark_Orange);
     prevValleyExtension.SetHiding(!inRange2);

plot breakout = if close crosses above peakline then 1 else 0;
breakout.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
     breakout.SetDefaultColor(Color.Green);
     breakout.SetLineWeight(2);
     breakout.HideBubble();
plot breakdown = if close crosses below valleyline then 1 else 0;
     breakdown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
     breakdown.SetDefaultColor(Color.red);
     breakdown.SetLineWeight(2);
     breakdown.HideBubble();


AddCloud(valleyline, valleyline + 0.11, CreateColor(255, 204, 204));
AddCloud(peakline, peakline - 0.11, CreateColor(204, 255, 204));


Here is the code I'm trying for the column but it is not working
Code:
# | define a peak and plot it
input magnitude = 15;

def peak = high > Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakValue = if peak then high else peakValue[1];
def peakLine = peakValue;
    
# | define a valley and plot it

def valley = low < lowest(low[1], magnitude) and low <= lowest(low[-magnitude], magnitude);
def valleyValue = if valley then low else valleyValue[1];
def valleyLine = valleyValue;


def ShortReversalRange = close  crosses above peakline -.10 and  (peakline - .10) > (valleyline + 0.10);

def LongReversalRange = close crosses below valleyline +.10 and  (peakline - .10) > (valleyline + 0.10);


plot scan = if shortreversalrange or longreversalrange then 1 else 0;


 AssignBackgroundColor(if LongReversalRange then Color.DARK_GREEN else if ShortReversalRange then Color.DARK_RED else color.current);
 
To Robert Payne or anyone who can help:

I need help writing script that can look back on 60 or less 5 min. bars and put an arrow on the chart where the third time a bar low has stopped at the same price (bouncing off support) in an uptrend or look back on 60 or less 5 min. bars and put an arrow on the chart where the third time a bar high has stopped at the same price (bouncing off resistance() in a downtrend. Also there has to be at least one bar gap or more between the bars reaching the same price low or high. If you can help me with this it might be easier to look at my computer screen where I can show you many examples of what I am trying to do. Also, I am new to this website. I can write the script for the uptrend or downtrend. I just need help with the look back 60 or less bars.
 
Last edited by a moderator:
@Coy Ponish Here is a line of high and low of past 60 bars. Maybe someone else will tackle the rest.

Code:
# Line At Price
plot priceLine = highestAll(if isNaN(close[-60])
                            then close
                            else Double.NaN);
priceline.SetStyle(Curve.Long_Dash);
priceLine.SetLineWeight(2);
priceLine.SetDefaultColor(CreateColor(255,215,0));
priceline.HideTitle();

plot pricelinel = lowestall(if isNaN(close[-60])
                            then close
                            else Double.NaN);
pricelinel.SetStyle(Curve.Long_Dash);
priceLine.SetLineWeight(2);
priceLine.SetDefaultColor(CreateColor(255,215,0));
priceline.HideTitle();
 
Status
Not open for further replies.

New Indicator: Buy the Dip

Check out our Buy the Dip indicator and see how it can help you find profitable swing trading ideas. Scanner, watchlist columns, and add-ons are included.

Download the indicator

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
486 Online
Create Post

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