i want two horizontal lines on chart with marks day highest and lowest according to ATR.
Let’s say ATR (14) is 1$ and market opens at 10$.
So, one line at day highest 11$ (market open + ATR -> 10$ + 1$)
Other line at day lowest 9$ (market open – ATR -> 10$ - 1$)
If I use plot(l_atr') it works, but if I use hline I get an error: line 13: Cannot call 'hline' with argument 'price'='l_atr'. An argument of 'series float' type was used but a 'input float' is expected.
I’m not sure what it means with “input float”.
How can I fix this?
//#version=5
indicator("Arego ATR", overlay=true)
o = request.security("", "D", open, lookahead=barmerge.lookahead_on)
atr = request.security("", "D", ta.atr(14))
l = request.security("", "D", low, lookahead=barmerge.lookahead_on)
h = request.security("", "D", high, lookahead=barmerge.lookahead_on)
l_atr = o + (h - o) - atr
h_atr = o + (l - o) + atr
hline(l_atr)
//plot(h_atr)
//plot(l_atr)
Related
I have an indicator that plots percentage levels above current high/low/open/close (user selected) for the intraday levels. I would like to incorporate extended trading hours into the code. For example if the premarket high of day is higher than regular hours high of day, I'd like the indicator to calculate the percentage levels using the premarket high instead of the intraday. I'm not sure how to code this into the script but I assume it would be fairly simple (I'm just not a coder). Script below:
study(title="% Levels", overlay=true)
//Select Source to Plot Levels
calc = input(title="Calculation Source", defval="Open", options=["Open","High", "Low", "Close"])
showlast = input(title="Historical Bars to Display", defval=3, options= [1,2,3,4,5,10,15,20,30,50,100,200,300], group="Custom Line Plot Extension Settings || Base Settings for Stocks/ETF's are '1' & '0' Respectively || To Extend Lines: Ideally both values should be equal when adjusting || For Futures: 1 & 0 Recommended")
extendLines = input(title="Offset Starting Plot", defval=0, options=[0,1,3,5,10,15,20,30,50,100,200,300])
//Ticker Variables
o = security(syminfo.tickerid, "D", open)
h = security(syminfo.tickerid, "D", high)
l = security(syminfo.tickerid, "D", low)
c = security(syminfo.tickerid, "D", close)
calcm = if calc == "High"
h
else if calc == "Low"
l
else if calc == "Close"
c
else if calc == "Open"
o
//Calculations for % Levels
pct10= calcm*1.10
pctm10=calcm*0.90
pct12_5 = calcm*1.125
pctm12_5 = calcm*0.875
pct15= calcm*1.15
pctm15=calcm*0.85
//% Levels plotted based on Daily Open, High, Low, or Close
plot(pct10, title="10%", color=color.white, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pct12_5, title="12.5%", color=color.white, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pct15, title="15%", color=color.white, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pctm10, title="-10%", color=color.red, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pctm12_5, title="-12.5%", color=color.red, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pctm15, title="-15%", color=color.red, style=plot.style_line, show_last=showlast, offset=extendLines)
Not a coder so not sure what to try.
There are three built-in variables which you can use:
session.ismarket: Returns true if the current bar is a part of the regular trading hours (i.e. market hours), false otherwise
session.ispostmarket: Returns true if the current bar is a part of the post-market, false otherwise. On non-intraday charts always returns false.
session.ispremarket: Returns true if the current bar is a part of the pre-market, false otherwise. On non-intraday charts always returns false.
I'm analyzing how many users have used a particular hashtag and how they have contributed to the total number of tweets. My results are:
Data:
20.68% of tweets related to #HashtagX are created by 20 users. Now, these 20 users only represent 0.001% of the total of 14,432 users who have ever used the hashtag #HashtagX.
What happens if we take the top 100 users by number of tweets? 44% of tweets are created by the top 100 users.
If we extend to the top 500 users by number of users we see that 72% of tweets is created by the top 500.
I am wondering how to implement a slope graph because I think that is a good way to show the relationship between both variables, but it is not a default graph provides for any library.
One of the ways to show the relationship between both variables ("Users" vs "Tweets") is a Slope Chart.
Visualization obtained (solved graph for the question):
Slope Chart
1) Libraries
library(ggplot2)
library(scales)
library(ggrepel)
theme_set(theme_classic())
2) Data example
Country = c('20 accounts', '50 accounts', '100 accounts','200 accounts','300 accounts',
'500 accounts','1000 accounts','14.443 accounts')
January = c(0.14, 0.34, 0.69,1.38,2.07,3.46,6.92,100)
April = c(20.68, 33.61, 44.94, 57.49,64.11,72,80,100)
Tweets_N = c(26797, 43547, 58211, 74472,83052,93259,103898,129529)
a = data.frame(Country, January, April)
left_label <- paste(a$Country, paste0(a$January,"%"),sep=" | ")
right_label <- paste(paste0(round(a$April),"%"),paste0(Tweets_N," tweets"),sep=" | ")
a$color_class <- "green"
3) Plot
p <- ggplot(a) + geom_segment(aes(x=1, xend=2, y=January, yend=April, col=color_class), size=.25, show.legend=F) +
geom_vline(xintercept=1, linetype="dashed", size=.1) +
geom_vline(xintercept=2, linetype="dashed", size=.1) +
scale_color_manual(labels = c("Up", "Down"),
values = c("blue", "red")) +
labs(
x="", y = "Percentage") +
xlim(.5, 2.5) + ylim(0,(1.1*(max(a$January, a$April)))) # X and Y axis limits
# Add texts
p <- p + geom_text_repel(label=left_label, y=a$January, x=rep(1, NROW(a)), hjust=1.1, size=3.5,direction = "y")
p <- p + geom_text(label=right_label, y=a$April, x=rep(2, NROW(a)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="Accounts", x=1, y=1.1*(max(a$January, a$April)), hjust=1.2, size=4, check_overlap = TRUE) # title
p <- p + geom_text(label="Tweeets (% of Total)", x=2, y=1.1*(max(a$January, a$April)), hjust=-0.1, size=4, check_overlap = TRUE)
# title
# Minify theme
p + theme(panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(1,2,1,2), "cm"))
I am struggling to write a nprev function in KDB; xprev function returns the nth element but I need all the prev n elements relative to the current element.
q)t:([] i:1+til 26; s:.Q.a)
q)update xp:xprev[3;]s,p:prev s from t
Any help is greatly appreciated.
You can achieve the desired result by applying prev repeatedly and flipping the result
q)n:3
q)select flip 1_prev\[n;s] from t
s
-----
" "
"a "
"ba "
"cba"
"dcb"
"edc"
..
If n is much smaller than the rows count, this will be faster than some of the more straightforward solutions.
The xprev function basically looks like this :
xprev1:{y til[count y]-x} //readable xprev
We can tweak it to get all n elements
nprev:{y til[count y]-\:1+til x}
using nprev in the query
q)update np: nprev[3;s] , xp1:xprev1[3;s] , xp: xprev[3;s], p:prev[s] from t
i s np xp1 xp p
-------------------
1 a " "
2 b "a " a
3 c "ba " b
4 d "cba" a a c
5 e "dcb" b b d
6 f "edc" c c e
k equivalent of nprev
k)nprev:{$[0h>#y;'`rank;y(!#y)-\:1+!x]}
and similarly nnext would look like
k)nnext:{$[0h>#y;'`rank;y(!#y)+\:1+!x]}
See the code and error. I have already tried Do, For,...and it is not working.
CODE + Error from Mathematica:
Import of survival probabilities _{k}p_x and _{k}p_y (calculated in excel)
px = Import["C:\Users\Eva\Desktop\kpx.xlsx"];
px = Flatten[Take[px, All], 1];
NOTE: The probability _{k}p_x can be found on the position px[[k+2, x -16]
i = 0.04;
v = 1/(1 + i);
JointLifeIndep[x_, y_, n_] = Sum[v^k*px[[k + 2, x - 16]]*py[[k + 2, y - 16]], {k , 0, n - 1}]
Part::pkspec1: The expression 2+k cannot be used as a part specification.
Part::pkspec1: The expression 2+k cannot be used as a part specification.
Part::pkspec1: The expression 2+k cannot be used as a part specification.
General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.
Part of dataset (left corner of the dataset):
k\x 18 19 20
0 1 1 1
1 0.999478086278185 0.999363078716059 0.99927911905056
2 0.998841497412202 0.998642656911039 0.99858030519133
3 0.998121451605207 0.99794428814123 0.99788275311401
4 0.997423447323642 0.997247180349674 0.997174407432264
5 0.996726703362208 0.996539285828369 0.996437857252448
6 0.996019178300768 0.995803204773039 0.99563600297737
7 0.995283481416241 0.995001861216016 0.994823584922968
8 0.994482556091416 0.994189960607964 0.99405569519175
9 0.993671079225432 0.99342255996206 0.993339856748282
10 0.992904079096455 0.992707177451333 0.992611817294026
11 0.992189069953677 0.9919796017009 0.991832027835091
Without having the exact same data files to work with it is often easy for each of us to make mistakes that the other cannot reproduce or understand.
From your snapshot of your data set I used Export in Mathematica to try to reproduce your .xlsx file. Then I tried the following
px = Import["kpx.xlsx"];
px = Flatten[Take[px, All], 1];
py = px; (* fake some py data *)
i = 0.04;
v = 1/(1 + i);
JointLifeIndep[x_, y_, n_] := Sum[v^k*px[[k+2,x-16]]*py[[k+2,y-16]], {k,0,n-1}];
JointLifeIndep[17, 17, 12]
and it displays 362.402
Notice I used := instead of = in my definition of JointLifeIndep. := and = do different things in Mathematica. = will immediately evaluate the right hand side of that definition. This is possibly the reason that you are getting the error that you do.
You should also be careful with your subscript values and make sure that every subscript is between 1 and the number of rows (or columns) in your matrix.
So see if you can try this example with an Excel sheet containing only the snapshot of data that you showed and see if you get the same result that I do.
Hopefully that will be enough for you to make progress.
I know how to convert first and second term to the first term of the simplified expression, but I don't know how to convert the rest.
By simplifying, I can get rid of A_Bar in the third term and A in the fifth term and get =B*C_bar
How is it that B*C_bar + the fourth term = becomes XOR(B,C) ?
The two expressions are clearly the same. This can be easily proven by truth tables.
The first one is:
And the second one:
However, this does not fully answer your question.
B*C_bar + the fourth term = becomes XOR(B,C)
This is clearly true if A is true, since per definitionem, B XOR C = B_bar and C OR B and C_bar.
If A is false, these terms are always false and you cannot simplify these two to B XOR C! They are not equal!
Note: Tables generated with http://web.stanford.edu/class/cs103/tools/truth-table-tool/
Note2: ^= OR, ¬ = NOT, ∨ = AND
let play a game.
Let a=not(A), b=not(B) and c=not(C) and *=xor
Y = ab + (B*C)
Y = ab + Bc + bC
Y = ab(1) + Bc(1) + bC(1)
Y = ab(c+C) + Bc(a+A) + bC(a+A)
Y = abc + abC + Bca + BcA + bCa + bCA
Y = abc + abC + aBc + ABc + abC + AbC
Y = abc + abC + aBc + ABc + AbC
That is the first equ.