Offering to help with scripting

Status
Not open for further replies.
markos

markos

Well-known member
VIP
@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
 
I

imnobody

Member
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.

You are him, from fun w/ thinkscript??
 
RobertPayne

RobertPayne

Member
@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

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.
 
D

diazlaz

Well-known member
2019 Donor
VIP
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!
 
I

imnobody

Member
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.

 
RobertPayne

RobertPayne

Member
@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.

 
Last edited:
R

RDMercer

Member
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.
 
A

Art

Member
@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-
Something like



The blue line is DI-

These are my setting



Thanks a bunch!
 
S

skynetgen

Well-known member
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)
 
H

HighBredCloud

Well-known member
VIP
@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:
F

febreezus

New member
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:
RobertPayne

RobertPayne

Member
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];
 
B

Brazilianpillar

New member
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);
 
Status
Not open for further replies.

Similar threads

Top