In pine script code, how can I draw line between 2 time ranges (9:20PM to 3:20PM) between 2 values - line

I am writing some pine script code to draw line between 2 time ranges (starting line from 9:20PM to end line at 3:20PM) also between 2 values (ex: 38000 to 38200). How can I code it? we can achieve using line tool in tradingview, but I want it in pine script.
I tried the below code, but it is not working as expected. The line should be like trendline not horizontal line.
indicator(title="Line", shorttitle="Line", overlay=true)
starttime = (hour(time) == 09 and minute(time) == 25)
endtime = (hour(time) == 15 and minute(time) == 25)
s_closeup200 := 38000
s_closedown200 := 38200
line_x1 = ta.valuewhen(time==starttime,time, 0)
line_x2 = ta.valuewhen((hour(time) == 09 and minute(time) == 25), s_closeup200,0)
line_x3 = ta.valuewhen(time==endtime, time, 0)
line_x4 = ta.valuewhen((hour(time) == 15 and minute(time) == 25), s_closedown200, 0)
line.new(x1 = line_x1, y1 = line_x2, x2 = line_x3, y2 = line_x4, xloc = xloc.bar_time)

Related

Is there a way to auto-align parts of code within a line of code?

Let's say I have to write code = 0, on 100 lines of code, and the code varies in character length, but you want all the = 0 to be at the same character length.
So to go from this:
one = 1,
onetwo = 12,
onetwothree = 123
To this
one = 1,
onetwo = 12,
onetwothree = 123

Pinescript conditional line end/delete

I’m trying to create lines that auto plot at certain intervals using the line.new() function. So for example every new monthly open there will be lines plotted 5% and 10% above and beneath price. They’re then extended to the right indefinitely.
I then want to have the lines end once high/low has breached the line. I can’t seem to figure how to do this using the line.delete() function, although I doubt this is the correct path to take anyway due to the fact this deletes the entire line rather than just end it at breach point.
Due to the fact lines are extended indefinitely/until price has breached, there may be instances in which lines are never touched and are only removed once the 500 max line limit is reached. So I haven’t figured a way to use array references for lines to find a solution - and the 40 plot limit for pine plots isn’t really a sufficient amount of lines.
If this isn’t possible then just deleting the entire line upon breach is a backup option but I haven’t figure how to do this either!
Any help is much appreciated, thanks in advance!
You can use additional arrays to track the price values and their crossed state more easily. Each element of the arrays corresponds to the values associated with the same line. We add, remove or modify them based on whether a particular line's price value has been crossed or the line limit has been exceeded.
//#version=5
indicator("Monthly Interval Lines", overlay = true, max_lines_count = 500)
var float[] interval_prices = array.new_float()
var line[] interval_lines = array.new_line()
var bool[] intervals_crossed = array.new_bool()
new_month = timeframe.change("M")
if new_month
array.unshift(interval_lines, line.new(x1 = bar_index, y1 = open * 1.05, x2 = bar_index + 1, y2 = open * 1.05, extend = extend.right, color = color.green))
array.unshift(interval_prices, open * 1.05)
array.unshift(intervals_crossed, false)
array.unshift(interval_lines, line.new(x1 = bar_index, y1 = open * 1.10, x2 = bar_index + 1, y2 = open * 1.10, extend = extend.right, color = color.green))
array.unshift(interval_prices, open * 1.10)
array.unshift(intervals_crossed, false)
array.unshift(interval_lines, line.new(x1 = bar_index, y1 = open * 0.95, x2 = bar_index + 1, y2 = open * 0.95, extend = extend.right, color = color.red))
array.unshift(interval_prices, open * 0.95)
array.unshift(intervals_crossed, false)
array.unshift(interval_lines, line.new(x1 = bar_index, y1 = open * 0.90, x2 = bar_index + 1, y2 = open * 0.90, extend = extend.right, color = color.red))
array.unshift(interval_prices, open * 0.90)
array.unshift(intervals_crossed, false)
size = array.size(intervals_crossed)
if size > 0
if size > 500
for i = size - 1 to 500
line.delete(array.pop(interval_lines))
array.pop(intervals_crossed)
size := array.size(intervals_crossed)
for i = 0 to size - 1
price_val = array.get(interval_prices, i)
already_crossed = array.get(intervals_crossed, i)
crossed_price_val = low < price_val and high > price_val
gapped_price_val = (close[1] < price_val and open > price_val) or (close[1] > price_val and open < price_val)
if not already_crossed and (crossed_price_val or gapped_price_val)
array.set(intervals_crossed, i, true)
interval_line = array.get(interval_lines, i)
line.set_extend(interval_line, extend.none)
line.set_x2(interval_line, bar_index)

Pine script setting timestamp for last quarter not working

I wrote a simple strategy in pine scrip, which is based on crossover/crossunder for two different SMAs.
It is important for me to test my strategy in some time frames, especially in the last quarter. It doesn't work. I got only a few results. I should get results for whole period (colored on green), when SMAs crossed.
Unwanted result
My script is working very well, when I don't use time range or when I set dateCond on true value.
Below I present source code:
//#version=4
strategy("Moving Average Cross 1", initial_capital=1000, overlay=true)
start = timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
end = timestamp(syminfo.timezone, 2021, 4, 1, 0, 0)
fastSMA = sma(close, 9)
slowSMA = sma(close, 50)
long = crossover(fastSMA, slowSMA)
short = crossunder(fastSMA, slowSMA)
orderSize = floor(strategy.equity / close)
plot(fastSMA, title="20", color=#00ffaa, linewidth=3)
plot(slowSMA, title="50", color=#FFC1CC, linewidth=2)
dateCond = time > start
// dateCond = true
bgcolor(dateCond ? #00ffaa : na)
if dateCond
strategy.entry("long", strategy.long, qty=orderSize, when = long)
strategy.entry("short", strategy.short, qty=orderSize, when = short)
strategy.close("long", when = short)
strategy.close("short", when = long)
I tried with different time ranges (when I set start date on the 1 January 2020 it works very well). I additionally colored a background to check condition, but coloring it also work good. I haven't more ideas why for the last quarter it isn't working properly. I will appreciate any help.
I tested script mainly for pair ETH/USDT (Binance)
Here we use larger capital and close positions before entering a new one. This way the whole position is closed before a new one is opened:
//#version=4
strategy("Moving Average Cross 1", initial_capital=100000, overlay=true)
start = timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
end = timestamp(syminfo.timezone, 2021, 4, 1, 0, 0)
fastSMA = sma(close, 9)
slowSMA = sma(close, 50)
long = crossover(fastSMA, slowSMA)
short = crossunder(fastSMA, slowSMA)
orderSize = floor(strategy.equity / close)
plot(fastSMA, title="20", color=#00ffaa, linewidth=3)
plot(slowSMA, title="50", color=#FFC1CC, linewidth=2)
dateCond = time > start
// dateCond = true
bgcolor(dateCond ? #00ffaa : na)
strategy.close("long", when = short)
strategy.close("short", when = long)
if dateCond
strategy.entry("long", strategy.long, qty=orderSize, when = long)
strategy.entry("short", strategy.short, qty=orderSize, when = short)

Deleting new lines once the price crosses it after the line was created

I have been creating new lines in pinescript that extend and want to delete them when the future price hits or crosses the line price. Any help will be appreciated.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//#version=4
study("My RS", overlay=true)
float d = 1.0
t = time("60")
start = na(t[1]) or t > t[1]
ticker = syminfo.ticker
src=input(title="Source", type=input.source, defval=open)
float d_r = na
float d_s = na
if (start)
d_r := src + d
d_s := src - d
line lr = na
line ls = na
// drawing r/s lines every hour
if (start)
lr := line.new(x1 = bar_index, y1 = d_r, x2 = bar_index - 1, y2 = d_r, extend = extend.left, color = color.red, width = 2, style = line.style_dashed)
ls := line.new(x1 = bar_index, y1 = d_s, x2 = bar_index - 1, y2 = d_s, extend = extend.left, color = color.lime, width = 2, style = line.style_dashed)
// want to delete lines when the future price crosses the line, which is not working for me
for i = 0 to 100
if not na(lr[i]) and close < high[i]
line.delete(lr[i])
if not na(ls[i]) and close < low[i]
line.delete(ls[i])
you need to get the coordinate of the lines, so use inside a loop if line.get_y1(id[i]) < close if it true, line.delete(id[i]).
Is this what're you looking for?

Finding a point on a diagonal line when i have the start point and end point of the Line

Hi am looking for some help
I have a Diagonal line drawn on a picture box on my forum and i need to know if the user has clicked the line
I have the Start point and End Point of the Line and the mouse x,y location
So i basically need to find out if the x,y of the mouse is on the line.
can anyone help?
Thanks
Example: Line Start point (A) is (0, 0), END point (B) is (10, 5).
Slope of line is therefore:
m(slope) = (y2 - y1) / (x2 - x1)
= (5 - 0) / (10 - 0)
= 5 / 10
= 0.5
To check if your point(x,y) (C) is on the line it must have the same slope from A->C and C->B. so do the same calculation again. Say point is (4, 2)
m(AC) = (2 - 0) / (4 - 0)
= 2 / 4
= 0.5
m(CB) = (5 - 2) / (10 - 4)
= 3 / 6
= 0.5
Therefore this point would be on line AB.
If point was (20, 10)
m(AC) = (10 - 0) / (20 - 0)
= 10 / 20
= 0.5
However:
m(CB) = (5 - 10) / (10 - 20)
= -5 / -10
= -0.5
Similarly if point was (2, 2)
m(AC) = (2 - 0) / (2 - 0)
= 2 / 2
= 1
m(CB) = (5 - 2) / (10 - 2)
= 3 / 8
= 0.375
So for a point to be on a line m(AB) == m(AC) == m(CB)
You may have a bit of work arounds to perform as you may not be able to get decimal values, and your line may be more than one pixel in width, but these basic principles should see you through.
Given two points, (2,4) and (-1,-2) determine the slope intercept form of the line.
1. Determine the slope
y1-y2 4-(-2) 6
----- = ------= --- = 2 = M
x1-x2 2-(-1) 3
2. To slope intercept form using one of the original points and slope from above.
(y - y1) = m(x - x1)
(y - 4) = 2(x - 2)
y - 4 = 2x - 4
y = 2x + 0 (0 is y intercept)
y = 2x (y = 2x + 0) is in slope intercept form
3. To determine if a point lies on the line, plug and chug with the new point.
new point (1,2) does y = 2x? 2 = 2(1) = true so (1,2) is on the line.
new point (2,2) does y = 2x? 2 = 2(2) = false so (2,2) is not on the line.
In your original problem you said line, but I think you might mean line segment. If you mean the latter you will also need to verify that the new x and y are within the bounds of the given segment.
The code will look something like this
Dim pta As Point = New Point(2, 4)
Dim ptb As Point = New Point(-1, -2)
Dim M As Double
If pta.X - ptb.X <> 0 Then
M = (pta.Y - ptb.Y) / (pta.X - ptb.X)
End If
'(y - pta.y) = M(x - pta.x)
'y - pta.y = Mx - m(pta.x)
'y = Mx - M(pta.x) + pta.y
Dim yIntercept As Double = (-M * pta.X) + pta.Y
Dim ptN1 As Point = New Point(1, 2)
Dim ptN2 As Point = New Point(2, 2)
If ptN1.Y = (M * (ptN1.X)) + yIntercept Then
Stop
Else
Stop
End If
If ptN2.Y = (M * (ptN2.X)) + yIntercept Then
Stop
Else
Stop
End If