How to create fixed lines that do not change as the timeframe changes - line

I am developing a strategy that allows me to buy long on certain price levels set on the past daily timeframe (close[1]). My algorithm builds a grid of 5 lines starting from the closing price of the dayli timeframe (D). I do not think I have done anything wrong in the code, I have been checking for several hours. The problem is that the lines in my grid do not remain fixed on the dayli levels but change as the timeframe changes. So if I switch from D to 1H or less my grid adjusts to that timeframe. I wonder how this is possible as all the inputs I have written refer exclusively to the Dayli timeframe ?
//#version=5
strategy("",
overlay = true,
calc_on_every_tick = true,
initial_capital = 1000,
commission_type = strategy.commission.percent,
commission_value = 0.03,
pyramiding = 1,
default_qty_value = 100,
default_qty_type = strategy.percent_of_equity,
process_orders_on_close = true,
close_entries_rule = 'ANY')
openingDayPrice = request.security(syminfo.ticker,
"D", open[1],
gaps = barmerge.gaps_off)
closingDayPrice = request.security(syminfo.ticker,
"D", close[1],
gaps = barmerge.gaps_off)
var float dcaLongStart = 0
var float dcaLongEnd = 0
isDcaLong = if closingDayPrice > close[2]
dcaLongStart := closingDayPrice
dcaLongEnd := openingDayPrice
else
dcaLongStart := 0
dcaLongEnd := 0
startDcaLong = float(dcaLongStart)
endDcaLong = float(dcaLongEnd)
numbersOfLevels = 5
difference = float(startDcaLong - endDcaLong)
valuePerDCA = difference / numbersOfLevels
valueDCA1 = valuePerDCA * 1
level1 = startDcaLong - valueDCA1
valueDCA2 = valuePerDCA * 2
level2 = startDcaLong - valueDCA2
valueDCA3 = valuePerDCA * 3
level3 = startDcaLong - valueDCA3
valueDCA4 = valuePerDCA * 4
level4 = startDcaLong - valueDCA4
valueDCA5 = valuePerDCA * 5
level5 = startDcaLong - valueDCA5
var line _firstLine = na
var line _secondLine = na
var line _thirdLine = na
var line _fourthLine = na
var line _fifthLine = na
var line _sixLine = na
var line _sevenLine = na
if isDcaLong
_firstLine := line.new(bar_index, startDcaLong, bar_index + 1, startDcaLong, extend = extend.right, color = color.purple, width = 1)
line.delete(_firstLine[1])
_secondLine := line.new(bar_index, endDcaLong, bar_index + 1, endDcaLong, extend = extend.right, color = color.purple, width = 1)
line.delete(_secondLine[1])
_thirdLine := line.new(bar_index, level1, bar_index + 1, level1, extend = extend.right, color = color.purple, width = 1)
line.delete(_thirdLine[1])
_fourthLine := line.new(bar_index, level2, bar_index + 1, level2, extend = extend.right, color = color.purple, width = 1)
line.delete(_fourthLine[1])
_fifthLine := line.new(bar_index, level3, bar_index + 1, level3, extend = extend.right, color = color.purple, width = 1)
line.delete(_fifthLine[1])
_sixLine := line.new(bar_index, level4, bar_index + 1, level4, extend = extend.right, color = color.purple, width = 1)
line.delete(_sixLine[1])

Related

Why are Y axis labels offset in ggplot?

The Y axis labels are offset
The Y axis labels from 100 to the top are aligned to the right, while from 90 to the bottom are aligned to the left. I've looked at many paramaters and I couldn't find one to be causing this. Also, I haven't found anyone else with this same issue.
Here's my code:
test <- ggplot(data,
aes(x=Month, y=Value, color=Name, group=Name, fill=Name))+
geom_line(size=3)+
geom_point(size=5)+
scale_color_manual(values=c("#FBE785","#0F5F00","#FFC300","#1BFFAA"))+
ylab ("")+
xlab ("")+
labs(caption = paste("Fonte: Fred e IBGE.")) +
scale_x_date(date_labels = "%b/%y", breaks = "6 month", expand=c(0,0))+
coord_cartesian(clip = "off")+
theme_minimal() +
guides(fill=guide_legend())+
theme(panel.background = element_rect(fill= "#122929",color = "#122929"),
plot.background = element_rect(fill = "#122929"),
panel.grid.major = element_line(color = "#4D4B55", size =0.1),
panel.grid.minor = element_line(color= "#4D4B55", size =0.1),
panel.grid = element_blank(),
axis.text.y = element_text(vjust = 1, hjust=-1),
axis.text.x = element_text(vjust = -1, hjust=0),
legend.title = element_blank(),
legend.position = "bottom",
legend.key.width = unit(1.5, "cm"),
plot.caption = element_text(family = "Abel",vjust = -1, hjust = 0,colour="#4D4B55", size= 30),
text = element_text(family = "Abel", color = "#4D4B55",size = 35),
plot.margin = margin(1,1,1.5,1.2, "cm"))
ggsave("./test.png", width = 21, height = 15, dpi = 300)
PS: Not sharing the data itself because I guess that's not where the problem is.
Thanks!
Your text is misaligned because of your axis.text.y argument. Change hjust to 1 and it will be properly aligned. I have provided a minimal reproducible example below.
library(tidyverse)
Month <- rep(x = month.abb, times = 10)
Value <- sample(x = 10:120, size = 120, replace = TRUE)
Name <- sample(x = LETTERS[1:4], size = 120, replace = TRUE)
data <- data.frame(Month, Value, Name)
ggplot(data, aes(x = Month, y = Value, color = Name, group = Name, fill = Name)) +
geom_line(size = 1) + geom_point(size = 2) +
theme(axis.text.y = element_text(vjust = 1, hjust = 1)) # <- problem here

Automatically assigning p-value position in ggplot loop

I am running an mapply loop on a huge set of data to graph 13 parameters for 19 groups. This is working great except the p-value position. Due to the data varying for each plot I cannot assign position using label.y = 125 for example, in some plots it is in the middle of the bar/error bar. However, I can't assign it higher without having it way to high on other graphs. Is there a way to adjust to the data and error bars?
This is my graphing function and like I said the graph is great, except p-value position. Specifically, the stat compare means anova line.
ANOVA_plotter <- function(Variable, treatment, Grouping, df){
Inputdf <- df %>%
filter(Media == treatment, Group == Grouping) %>%
ggplot(aes_(x = ~ID, y = as.name(Variable))) +
geom_bar(aes(fill = ANOVA_Status), stat = "summary", fun = "mean", width = 0.9) +
stat_summary(geom = "errorbar", fun.data = "mean_sdl", fun.args = list(mult = 1), size = 1) +
labs(title = paste(Variable, "in", treatment, "in Group", Grouping, sep = " ")) +
theme(legend.position = "none",axis.title.x=element_blank(), axis.text = element_text(face="bold", size = 18 ), axis.text.x = element_text(angle = 45, hjust = 1)) +
stat_summary(geom = "errorbar", fun.data = "mean_sdl", fun.args = list(mult = 1), width = 0.2) +
stat_compare_means(method = "anova", label.y = 125) +
stat_compare_means(label = "p.signif", method = "t.test", paired = FALSE, ref.group = "Control")
}
I get graphs that look like this
(https://i.stack.imgur.com/hV9Ad.jpg)
But I can't assign it to label.y = 200 because of plots like this
(https://i.stack.imgur.com/uStez.jpg)

Strange Issues with ggplot?

PLEASE HELP, something seems not to be working here,I previously ploted this figure and it was working fine, however, now it seems something is off.
The categories are no longer orderly and some of them appear twice...
...
ggplot(data = data_c, aes(x = reorder(Sektor, -Preis), y = Preis)) +
geom_bar(stat = "identity", width = data_c$Menge/26) +
geom_line(data = data_c, aes(group = 1, x = Sektor,
y = Preis, colour = "40 € pro MWh")) +
geom_line(data = data_d, aes(group = 1, x = sektoren2,
y = preise2, colour = "60 € pro MWh")) +
geom_label(aes(label = preise2a), nudge_y = 30, size = 4) +
geom_label(aes(label = twh2), nudge_y = 6, size = 4) +
geom_text(aes(label = euros), vjust = 1.5, colour = "white", size = 2) +
scale_y_continuous(labels = dollar_format(suffix = " €", prefix = "")) +
labs(title = "Merrit Order Curve Wasserstoff",
subtitle = "Mengen in TWh und Preise in Euro pro MWh",
x = "Sektoren",
y = "Preise in Euro pro MWh") +
theme_bw()
´´´

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?

Error bar not showing all the values in mschart

I am using VB.NET with mschart. I am using ErroBar chartype but i am not able to label all the values (medium, upper and lower). When i set chart.Series("ErrorBar").IsValueShownAsLabel = True, only upper value is shown.
I want to show the center, upper and lower values.
Thanks in advance
The solution adopted by me was a simpler one using annotations as can be seen in the code below
Dim Media1 As New RectangleAnnotation()
Media1.BackColor = Color.Yellow
Media1.Text = FormatNumber(D20, 4)
Dim point As PointF = New PointF(chart.ChartAreas(0).AxisX.ValueToPosition(1), chart.ChartAreas(0).AxisY.ValueToPosition(D20))
Media1.AnchorX = point.X + 10
Media1.AnchorY = point.Y + 2
Media1.AllowMoving = True
Dim L1 As New RectangleAnnotation()
L1.BackColor = Color.Yellow
L1.Text = FormatNumber(D20 - result * My.Settings("IncertezaDensidade20") / 2, 4)
point = New PointF(chart.ChartAreas(0).AxisX.ValueToPosition(1), chart.ChartAreas(0).AxisY.ValueToPosition(D20 - result * My.Settings("IncertezaDensidade20") / 2))
L1.AnchorX = point.X
L1.AnchorY = point.Y + 10
L1.AllowMoving = True
Dim L2 As New RectangleAnnotation()
L2.BackColor = Color.Yellow
L2.Text = FormatNumber(D20 + result * My.Settings("IncertezaDensidade20") / 2, 4)
point = New PointF(chart.ChartAreas(0).AxisX.ValueToPosition(1), chart.ChartAreas(0).AxisY.ValueToPosition(D20 + result * My.Settings("IncertezaDensidade20") / 2))
L2.AnchorX = point.X
L2.AnchorY = point.Y - 5
L2.AllowMoving = True
The chart now, can be seen below