Plot horizontal lines based on latest Mark price

jebersole

New member
Ok, so I've been messing with this script for about 2 days now and finally decided I needed to ask for some help. I can normally do enough research to find the parts I need and then piece them together. It helps me build my skills in the technology for a nice time investment. However, I'm missing something very very simple that I can't seem to figure out and I would love someone to shed some light on my problem (stupidity) - lack of thinkscript dynamics.

Background: I am a day trader and I closely watch the price of a ticker relative to whole dollars. For example, if a stock has a current price of $423.44, then I will be closely watching its movement between the $423.00 and $424.00 range which is can often be the support and resistance levels. My chart shows 20 days of data in 1m intervals (bars) for the ticker.

Goal: I wanted to write a new script that would plot a horizontal line at the whole value above and below the current mark price. Actually for transparency, i want to plot 3 lines above and 3 lines below, but i can easily do that after doing the first two. So if the last mark price was $423.44, then I would want to draw a horizontal line at both $423.00 and $424.00. But I want to draw (plot) the line only for the current day even though I'm showing 20 days of data. I dont care where the lines would have been relative to other days. I only care about today's lines. So, if the price moves from $423.44 up through $424.00 and lands say at $424.15, then i would like to plot the lines now at $424.00 and $425.00. I want these lines to move with the CURRENT price as it moves up and down.

Along with reading some of the documentation and my research, I found a few golden nuggets to help me achieve some of my goals.

Get the Current Day so I could use If statements to only plot based on the current day
def isCurrentDay = if GetLastDay() == GetDay() then 1 else 0;

Plot the support and resistance lines only if it is the current day
Plot firstResistance = if iscurrentDay then Ceil(423.44) else Double.NaN;
firstResistance.setPaintingStrategy(paintingStrategy.HoRIZONTAL);
firstResistance.SetDefaultColor(Color.Red);
Plot firstSupport = if iscurrentDay then Floor(423.44) else Double.NaN;
firstSupport.setPaintingStrategy(paintingStrategy.HoRIZONTAL);
firstSupport.SetDefaultColor(Color.Green);

Ok, great, now I have my lines that only show up for the current day and they are right where i want them, but unfortunately I have a hard coded value of 423.44 in my examples. The next thing I needed was to find out the current price so my lines would redraw based on that current price. And like WTF, i can't for the life of me get the current price of the current Mark value of the current bar to fill in that hard coded value. I tried using this kind of stuff to get the values...
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def LastKnownPrice = If isLastBar Then Ceil(close[0]) else Double.NaN;

Sure, it gets the value of the most recent bar, but since the charting gets painted from the left to the right, i dont know the latest bar value before i need it and obviously if i used the few lines above it will only draw the line if its the last bar. I need to know the current mark/close price back when the chart paints the first bar of that day. I also tried to use CompoundValue, but i dont think i was using it correctly. Does anyone have some advice on how to achieve my task? How do i get the current close/mark price regardless of Aggregation period, etc. Thanks in advance
 
Solution
54645.png
23423.png


Ruby:
def LastDay = GetDay()==GetLastDay();

def ZeroLevel = if (IsNaN(close) && !IsNaN(close[1])) or IsNaN(close) then ZeroLevel[1] else
                if LastDay then fold c=0 to 1500 while !IsNaN(GetValue(close,-c)) do GetValue(close,-c) else double.nan;

def TA = if (IsNaN(close) && !IsNaN(close[1])) or IsNaN(close) then TA[1] else if !LastDay then 0 else
         if (ZeroLevel - RoundUp(ZeroLevel,0) == 0) then ZeroLevel+1 else RoundUp(ZeroLevel,0);
    
def TB = if (IsNaN(close) && !IsNaN(close[1])) or IsNaN(close) then TB[1] else if !LastDay then 0 else
         if (ZeroLevel - RoundDown(ZeroLevel,0) == 0) then ZeroLevel-1 else RoundDown(ZeroLevel,0);

def dZL = if IsNaN(close) then dZL[1] else if...
54645.png
23423.png


Ruby:
def LastDay = GetDay()==GetLastDay();

def ZeroLevel = if (IsNaN(close) && !IsNaN(close[1])) or IsNaN(close) then ZeroLevel[1] else
                if LastDay then fold c=0 to 1500 while !IsNaN(GetValue(close,-c)) do GetValue(close,-c) else double.nan;

def TA = if (IsNaN(close) && !IsNaN(close[1])) or IsNaN(close) then TA[1] else if !LastDay then 0 else
         if (ZeroLevel - RoundUp(ZeroLevel,0) == 0) then ZeroLevel+1 else RoundUp(ZeroLevel,0);
    
def TB = if (IsNaN(close) && !IsNaN(close[1])) or IsNaN(close) then TB[1] else if !LastDay then 0 else
         if (ZeroLevel - RoundDown(ZeroLevel,0) == 0) then ZeroLevel-1 else RoundDown(ZeroLevel,0);

def dZL = if IsNaN(close) then dZL[1] else if (ZeroLevel - RoundUp(ZeroLevel,0) == 0) && IsNaN(close[-2000] && LastDay) then
          ZeroLevel else ZeroLevel;

plot ZL =dZL;
ZL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ZL.SetDefaultColor(Color.CYAN);
ZL.SetLineWeight(1);

plot PAC1 = if !TA then Double.NaN else TA;
PAC1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC1.SetDefaultColor(Color.LIME);
PAC1.SetLineWeight(1);

plot PAC2 = PAC1+1;
PAC2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC2.SetDefaultColor(Color.LIME);
PAC2.SetLineWeight(1);

plot PAC3 = PAC2+1;
PAC3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PAC3.SetDefaultColor(Color.LIME);
PAC3.SetLineWeight(1);

plot PBC1 = if !TB then Double.NaN else TB;
PBC1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC1.SetDefaultColor(Color.YELLOW);
PBC1.SetLineWeight(1);

plot PBC2 = PBC1-1;
PBC2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC2.SetDefaultColor(Color.YELLOW);
PBC2.SetLineWeight(1);

plot PBC3 = PBC2-1;
PBC3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PBC3.SetDefaultColor(Color.YELLOW);
PBC3.SetLineWeight(1);
 
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
468 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