first question here. Just starting to learn PineScript and I just want to add this to my charts. The first error is the usual "end of line without continuation" at line 69 (I tried on that particular line in different ways but with no result), I believe once that's solved there's gonna be another errors related to the use of tabular space right?
If anyone is willing to try to add this to any crypto and tell me what's wrong I would appreciate it.
Here's the actual code (also the original code was in version 4, the change of version affects it?)
Thanks!
study(title="El Loco", overlay=true)//, commission_type=strategy.commission.percent,
commission_value=0.025, default_qty_type=strategy.cash, default_qty_value=10000,
initial_capital=10000, slippage=0
// === INPUT BACKTEST RANGE ===
// useDate = input(true, title='---------------- Use Date ----------------', type=input.bool)
// FromMonth = input(defval=7, title="From Month", minval=1, maxval=12)
// FromDay = input(defval=25, title="From Day", minval=1, maxval=31)
// FromYear = input(defval=2019, title="From Year", minval=2017)
// ToMonth = input(defval=1, title="To Month", minval=1, maxval=12)
// ToDay = input(defval=1, title="To Day", minval=1, maxval=31)
// ToYear = input(defval=9999, title="To Year", minval=2017)
// start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
// finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
// window() => // create function "within window of time"
// time >= start and time <= finish ? true : false
// === INPUT BACKTEST RANGE ===
sources = input(defval=close, title="Source")
isHA = input(false, "Use HA Candles", input.bool)
heikenashi_1 = heikinashi(syminfo.tickerid)
security_1 = security(heikenashi_1, timeframe.period, sources)
src = isHA ? security_1 : sources
// Sampling Period
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
per = input(defval=27, minval=1, title="Sampling Period")
// Range Multiplier
mult = input(defval=1.0, minval=0.1, title="Range Multiplier")
// Smooth Average Range
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ema(abs(x - x[1]), t)
smoothrng = ema(avrng, wper) * m
smoothrng
smrng = smoothrng(src, per, mult)
// Range Filter
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r :
x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
rngfilt
filt = rngfilt(src, smrng)
// Filter Direction
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
// Target Bands
hband = filt + smrng
lband = filt - smrng
// Colors
filtcolor = upward > 0 ? color.lime : downward > 0 ? color.red : color.orange
barcolor = src > filt and src > src[1] and upward > 0 ? color.lime :
src > filt and src < src[1] and upward > 0 ? color.green :
src < filt and src < src[1] and downward > 0 ? color.red :
src < filt and src > src[1] and downward > 0 ? color.maroon : color.orange
//filtplot = plot(filt, color=filtcolor, linewidth=3, title="Range Filter")
// Target
hbandplot = plot(hband, color=color.aqua, transp=100, title="High Target")
lbandplot = plot(lband, color=color.fuchsia, transp=100, title="Low Target")
// Fills
//fill(hbandplot, filtplot, color=color.aqua, title="High Target Range")
//fill(lbandplot, filtplot, color=color.fuchsia, title="Low Target Range")
// Bar Color
//barcolor(barcolor)
// Break Outs
longCond = bool(na)
shortCond = bool(na)
longCond := src > filt and src > src[1] and upward > 0 or
src > filt and src < src[1] and upward > 0
shortCond := src < filt and src < src[1] and downward > 0 or
src < filt and src > src[1] and downward > 0
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1
//Alerts
plotshape(longCondition, title="Buy Signal", text="⁎", textcolor=color.white,
style=shape.labelup, size=size.large, location=location.belowbar, color=color.black, transp=10)
plotshape(shortCondition, title="Sell Signal", text="⁎", textcolor=color.white,
style=shape.labeldown, size=size.large, location=location.abovebar, color=color.black,
transp=10)
//strategy.entry("Long", strategy.long, stop = hband, when = window() , comment="Long")
//strategy.entry("Short", strategy.short, stop = lband, when = window() , comment="Short")
// strategy.entry("Long", strategy.long, when=longCondition and window(), comment="Long")
// strategy.entry("Short", strategy.short, when=shortCondition and window(),
comment="Short")
// // === Stop LOSS ===
// useStopLoss = input(false, title='----- Use Stop Loss / Take profit -----', type=input.bool)
// sl_inp = input(100, title='Stop Loss %', type=input.float, step=0.25) / 100
// tp_inp = input(1.5, title='Take Profit %', type=input.float, step=0.25) / 100
// stop_level = strategy.position_avg_price * (1 - sl_inp)
// take_level = strategy.position_avg_price * (1 + tp_inp)
// stop_level_short = strategy.position_avg_price * (1 + sl_inp)
// take_level_short = strategy.position_avg_price * (1 - tp_inp)
// // === Stop LOSS ===
// if useStopLoss
// strategy.exit("Stop Loss/Profit Long", "Long", stop=stop_level, limit=take_level)
// strategy.exit("Stop Loss/Profit Short", "Short", stop=stop_level_short,
limit=take_level_short)
// EMA
showEMA = input(false, title="EMA - Enabled")
emaConfig1 = input(21, "EMA 1", input.integer)
emaConfig2 = input(50, "Ema 2", input.integer)
emaConfig3 = input(200, "Ema 3", input.integer)
ema1 = ema(close, emaConfig1)
ema2 = ema(close, emaConfig2)
ema3 = ema(close, emaConfig3)
plot(showEMA ? ema1 : na, title="EMA", color=#FFFF00, linewidth=1)
plot(showEMA ? ema2 : na, title="EMA", color=#FF84D0, linewidth=1)
plot(showEMA ? ema3 : na, title="EMA", color=#9700FF, linewidth=1)
// BB
showBB = input(false, title="BB - Enabled")
length = input(20, minval=1, title="BB - Length")
multBB = input(2.0, minval=0.001, maxval=50, title="BB - StdDev")
basis = sma(close, length)
dev = multBB * stdev(close, length)
upper = basis + dev
lower = basis - dev
offset = input(0, "BB - Offset", type = input.integer, minval = -500, maxval = 500)
plot(showBB ? basis : na, "Basis", color=#872323, offset = offset)
p1 = plot(showBB ? upper : na, "Upper", color=color.teal, offset = offset)
p2 = plot(showBB ? lower : na, "Lower", color=color.teal, offset = offset)
fill(p1, p2, title = "Background", color=#198787, transp=95)
Try putting the lines in one line only like below
//#version=4
study(title="El Loco", overlay=true)//, commission_type=strategy.commission.percent,
commission_value=0.025, default_qty_type=strategy.cash, default_qty_value=10000,
initial_capital=10000, slippage=0
// === INPUT BACKTEST RANGE ===
// useDate = input(true, title='---------------- Use Date ----------------', type=input.bool)
// FromMonth = input(defval=7, title="From Month", minval=1, maxval=12)
// FromDay = input(defval=25, title="From Day", minval=1, maxval=31)
// FromYear = input(defval=2019, title="From Year", minval=2017)
// ToMonth = input(defval=1, title="To Month", minval=1, maxval=12)
// ToDay = input(defval=1, title="To Day", minval=1, maxval=31)
// ToYear = input(defval=9999, title="To Year", minval=2017)
// start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
// finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
// window() => // create function "within window of time"
// time >= start and time <= finish ? true : false
// === INPUT BACKTEST RANGE ===
sources = input(defval=close, title="Source")
isHA = input(false, "Use HA Candles", input.bool)
heikenashi_1 = heikinashi(syminfo.tickerid)
security_1 = security(heikenashi_1, timeframe.period, sources)
src = isHA ? security_1 : sources
// Sampling Period
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
per = input(defval=27, minval=1, title="Sampling Period")
// Range Multiplier
mult = input(defval=1.0, minval=0.1, title="Range Multiplier")
// Smooth Average Range
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ema(abs(x - x[1]), t)
smoothrng = ema(avrng, wper) * m
smoothrng
smrng = smoothrng(src, per, mult)
// Range Filter
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r :
x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
rngfilt
filt = rngfilt(src, smrng)
// Filter Direction
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
// Target Bands
hband = filt + smrng
lband = filt - smrng
// Colors
filtcolor = upward > 0 ? color.lime : downward > 0 ? color.red : color.orange
barcolor = src > filt and src > src[1] and upward > 0 ? color.lime : src > filt and src < src[1] and upward > 0 ? color.green : src < filt and src < src[1] and downward > 0 ? color.red : src < filt and src > src[1] and downward > 0 ? color.maroon : color.orange
//filtplot = plot(filt, color=filtcolor, linewidth=3, title="Range Filter")
// Target
hbandplot = plot(hband, color=color.aqua, transp=100, title="High Target")
lbandplot = plot(lband, color=color.fuchsia, transp=100, title="Low Target")
// Fills
//fill(hbandplot, filtplot, color=color.aqua, title="High Target Range")
//fill(lbandplot, filtplot, color=color.fuchsia, title="Low Target Range")
// Bar Color
//barcolor(barcolor)
// Break Outs
longCond = bool(na)
shortCond = bool(na)
longCond := src > filt and src > src[1] and upward > 0 or src > filt and src < src[1] and upward > 0
shortCond := src < filt and src < src[1] and downward > 0 or src < filt and src > src[1] and downward > 0
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1
//Alerts
plotshape(longCondition, title="Buy Signal", text="⁎", textcolor=color.white,style=shape.labelup, size=size.large, location=location.belowbar, color=color.black, transp=10)
plotshape(shortCondition, title="Sell Signal", text="⁎", textcolor=color.white,style=shape.labeldown, size=size.large, location=location.abovebar, color=color.black,transp=10)
//strategy.entry("Long", strategy.long, stop = hband, when = window() , comment="Long")
//strategy.entry("Short", strategy.short, stop = lband, when = window() , comment="Short")
// strategy.entry("Long", strategy.long, when=longCondition and window(), comment="Long")
// strategy.entry("Short", strategy.short, when=shortCondition and window(),comment="Short")
// // === Stop LOSS ===
// useStopLoss = input(false, title='----- Use Stop Loss / Take profit -----', type=input.bool)
// sl_inp = input(100, title='Stop Loss %', type=input.float, step=0.25) / 100
// tp_inp = input(1.5, title='Take Profit %', type=input.float, step=0.25) / 100
// stop_level = strategy.position_avg_price * (1 - sl_inp)
// take_level = strategy.position_avg_price * (1 + tp_inp)
// stop_level_short = strategy.position_avg_price * (1 + sl_inp)
// take_level_short = strategy.position_avg_price * (1 - tp_inp)
// // === Stop LOSS ===
// if useStopLoss
// strategy.exit("Stop Loss/Profit Long", "Long", stop=stop_level, limit=take_level)
// strategy.exit("Stop Loss/Profit Short", "Short", stop=stop_level_short,limit=take_level_short)
// EMA
showEMA = input(false, title="EMA - Enabled")
emaConfig1 = input(21, "EMA 1", input.integer)
emaConfig2 = input(50, "Ema 2", input.integer)
emaConfig3 = input(200, "Ema 3", input.integer)
ema1 = ema(close, emaConfig1)
ema2 = ema(close, emaConfig2)
ema3 = ema(close, emaConfig3)
plot(showEMA ? ema1 : na, title="EMA", color=#FFFF00, linewidth=1)
plot(showEMA ? ema2 : na, title="EMA", color=#FF84D0, linewidth=1)
plot(showEMA ? ema3 : na, title="EMA", color=#9700FF, linewidth=1)
// BB
showBB = input(false, title="BB - Enabled")
length = input(20, minval=1, title="BB - Length")
multBB = input(2.0, minval=0.001, maxval=50, title="BB - StdDev")
basis = sma(close, length)
dev = multBB * stdev(close, length)
upper = basis + dev
lower = basis - dev
offset = input(0, "BB - Offset", type = input.integer, minval = -500, maxval = 500)
plot(showBB ? basis : na, "Basis", color=#872323, offset = offset)
p1 = plot(showBB ? upper : na, "Upper", color=color.teal, offset = offset)
p2 = plot(showBB ? lower : na, "Lower", color=color.teal, offset = offset)
fill(p1, p2, title = "Background", color=#198787, transp=95)
im new to R and im trying to solve for the minimum number of moves for a knight visit all the moves in a chess board.
I got the python code from:
https://www.geeksforgeeks.org/the-knights-tour-problem-backtracking-1/
and i tried to translate it to r.
But i am always getting the error and I don't know where I went wrong.
This is my code:
chess = rep(-1, times = 64)
board = matrix(data = chess, nrow = 8, ncol = 8, byrow = TRUE)
move_x = c(2, 1, -1, -2, -2, -1, 1, 2)
move_y = c(1, 2, 2, 1, -1, -2, -2, -1)
board[1, 1] = 0
pos = 1
valid_move <- function (x, y, board) {
if (x >= 1 & y >= 1 & x <= 8 & y <= 8 & board[x, y] == -1) {
return (T)
}
return (F)
}
solve <- function (board, curr_x, curr_y, move_x, move_y, pos) {
if (pos == 64) {
return (T)
}
for (i in seq(1:8)) {
new_x = curr_x + move_x[i]
new_y = curr_y + move_y[i]
if (valid_move(new_x, new_y, board)) {
board[new_x, new_y] = pos
if (solve(board, new_x, new_y, move_x, move_y, pos+1)) {
return (TRUE)
}
board[new_x, new_y] = -1
}
}
}
main <- function() {
sims = 10
ctr = 0
number_of_moves = c()
solve(board, 1, 1, move_x, move_y, pos)
print(paste("Minimum number of moves: ", pos))
}
main()
Thanks!
The problem is that the python code relies on short-circuiting to prevent out-of-bounds errors. & will not short-circuit so you need to use &&.
Here is an example
FALSE && stop()
#> [1] FALSE
FALSE & stop()
#> Error:
Update valid_move to this
valid_move <- function (x, y, board) {
# Changed to && to allow short-circuiting
# if (x >= 1 & y >= 1 & x <= 8 & y <= 8 & board[x, y] == -1) {
if (x >= 1 && y >= 1 && x <= 8 && y <= 8 && board[x, y] == -1) {
return (T)
}
return (F)
}
I have a vector of start and end angels values between 0 and 360.
I would like to have an additional column that specifies in which sector(consider 12 sectors) are my variables.
sectors should be defined as : (15:45], (45,75],(75,105], ..., (345,15]
test = structure(list(start = c(4, 67, 13, 35, 54, 0), end = c(23, 84,
30, 52, 71, 0)), row.names = 2:7, class = "data.frame")
For my test example I thought I have to loop over the number of rows :
for( i in 1:nrow(test)){
if(test$start[i] <= 15 | test$start[i] >345){test$sector_start[i] = 12}
else if(test$start[i] > 15 & test$start[i] <= 45){test$sector_start[i] = 1}
else if(test$start[i] > 45 & test$start[i] <= 75){test$sector_start[i] = 2}
else if(test$start[i] > 75 & test$start[i] <= 105){test$sector_start[i] = 3}
else if(test$start[i] > 105 & test$start[i] <= 135){test$sector_start[i] = 4}
else if(test$start[i] > 135 & test$start[i] <= 165){test$sector_start[i] = 5}
else if(test$start[i] > 165 & test$start[i] <= 195){test$sector_start[i] = 6}
else if(test$start[i] > 195 & test$start[i] <= 225){test$sector_start[i] = 7}
else if(test$start[i] > 225 & test$start[i] <= 255){test$sector_start[i] = 8}
else if(test$start[i] > 255 & test$start[i] <= 285){test$sector_start[i] = 9}
else if(test$start[i] > 285 & test$start[i] <= 315){test$sector_start[i] = 10}
else if(test$start[i] > 315 & test$start[i] <= 345){test$sector_start[i] = 11}
if(test$end[i] <= 15 | test$end[i] >345){test$sector_end[i] = 12}
else if(test$end[i] > 15 & test$end[i] <= 45){test$sector_end[i] = 1}
else if(test$end[i] > 45 & test$end[i] <= 75){test$sector_end[i] = 2}
else if(test$end[i] > 75 & test$end[i] <= 105){test$sector_end[i] = 3}
else if(test$end[i] > 105 & test$end[i] <= 135){test$sector_end[i] = 4}
else if(test$end[i] > 135 & test$end[i] <= 165){test$sector_end[i] = 5}
else if(test$end[i] > 165 & test$end[i] <= 195){test$sector_end[i] = 6}
else if(test$end[i] > 195 & test$end[i] <= 225){test$sector_end[i] = 7}
else if(test$end[i] > 225 & test$end[i] <= 255){test$sector_end[i] = 8}
else if(test$end[i] > 255 & test$end[i] <= 285){test$sector_end[i] = 9}
else if(test$end[i] > 285 & test$end[i] <= 315){test$sector_end[i] = 10}
else if(test$end[i] > 315 & test$end[i] <= 345){test$sector_end[i] = 11}
}
here I could add 2 column to test which is telling me my angles are in which sector. I'm looking for a smarter way of doing this that I could have an option to vary the number of sectors, for example to 24 sectors.
As Roman says, you can use cut. The last step is for angles > 345 or <= 15.
library(dplyr)
test %>%
mutate(sector_start = cut(start, 15 + 30*(0:11), 1:11)
, sector_end = cut(end, 15 + 30*(0:11), 1:11)) %>%
mutate_at(vars(contains('sector')), ~ifelse(is.na(.), 12, .))
In base R:
test[paste0('sector_', names(test))] <-
lapply(test, function(x){
labs <- cut(x, 15 + 30*(0:11), 1:11)
ifelse(is.na(labs), 12, labs)
})
I'm doing an IF statement in R to create a variable in R.
I'm having an error that I can't detect to what it refers exactly so I can't fix it. Can somebody help me?
library(install.load)
install_load("checkmate", "expss")
amostra$escol <- NA
educd003 <- data.frame("d003" = 1:9, "codeduc" = c(1,1,3,3,5,5,7,9,9))
educd0091 <- data.frame("d009" = c(1,2,3,4,5,6,7,8,9,10,11,12),
"codeduc" = c(2,2,4,4,4,4,6,6,6,8,10,10))
educd0092 <- data.frame("d009" = c(1,2,3,4,5,6,7,8,9,10,11,12),
"codeduc" = c(1,1,3,3,3,3,5,5,5,7,9,9))
for (i in 1:nrow(amostra)) {
if (is.na(amostra$d001[i]) == TRUE) {
amostra$escol[i] <- 99
} else if (amostra$d001[i] == 2) {
amostra$escol[i] <- 0
} else if (amostra$d002[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d003[i], educd003, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 2) {
amostra$escol[i] <- 2
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0091, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 2) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & is.na(amostra$d014[i]) == TRUE) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else {
amostra$escol[i] <- NA
}
}
Error:
Error in if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == :
missing value where TRUE/FALSE needed
Thanks,
Wagner
I solved it.
The problem, apparently, was the order.
The code below ran ok:
for (i in 1:nrow(amostra)) {
if (is.na(amostra$d001[i]) == TRUE) {
amostra$escol[i] <- 99
} else if (amostra$d001[i] == 2) {
amostra$escol[i] <- 0
} else if (amostra$d002[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d003[i], educd003, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 2) {
amostra$escol[i] <- 2
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & is.na(amostra$d014[i]) == TRUE) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0091, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 2) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else {
amostra$escol[i] <- NA
}
}
Thank you very much anyway!