SWITCH with clauses differing by a constant increment - for-loop

Below is my SWITCH. I want to put it in a loop. I want to constantly increase my SWITCH by 2.5, because I have a lot of different values from 0 to 1000.
Order Amount =
SWITCH (
TRUE(),
Order[Amount Order] >= 0 && Order[Amount Order] < 5 , "< 5",
Order[Amount Order] >= 5.1 && Order[Amount Order] <= 7.5, "5 - 7.5",
Order[Amount Order] >= 7.51 && Order[Amount Order] <= 10, "7.51 - 10",
Order[Amount Order] >= 10.01 && Order[Amount Order] <= 12.5, "10.01 - 12.5",
Order[Amount Order] >= 12.51 && Order[Amount Order] <= 15, "12.51 - 15",Blank())

Maybe This can help you, just let me know if it does. Thanks.

This formula succeeded for me:
Order Amount =
VAR _ceil = CEILING('Order'[Amount Order] / 2.5, 1) * 2.5
VAR _big_result = CONVERT(_ceil-2.49, STRING) & " - " & CONVERT(_ceil, STRING)
RETURN SWITCH(TRUE(),
'Order'[Amount Order] >= 0 && 'Order'[Amount Order] < 5 , "< 5",
'Order'[Amount Order] >= 5 && 'Order'[Amount Order] <= 7.5, "5 - 7.5",
_big_result)

Related

Pine Script outputting weird for loop error

Everything works except for loop. It's been outputting weird error:
Script could not be translated from: for i = 1 to 10
study("RSI Below 25.3", overlay=true)
rsiValue = rsi(close, 14)
rsiValLow= rsi(low, 14)
x =low[1]
//for i = 1 to 10
// if x > low[i]
// x := low[i]
isDivergent = abs(x-low[0])/low[0]*100 < .05 and abs(x- rsiValLow[0])>=1.5
isRsiOS = rsiValue <= 25
//plot signals to chart
plotshape(isRsiOS, title="Oversold", location=location.belowbar, transp=0, style=shape.triangleup, text="Buy")
plotshape(isDivergent, title="Divergence", location=location.belowbar, transp=0, style=shape.triangleup, text= "Divergence" )
I have been working on this for a while but cannot figure out why it won't work. Can you help?
Thanks!
If you need to find the minimum value, then it is better to replace the loop with a function lowest.
//#version=4
study("Help (RSI Below 25.3)", overlay=true)
rsiValue = rsi(close, 14)
rsiValLow= rsi(low, 14)
//x = low[1]
// for i = 1 to 10
// if x > low[i]
// x := low[i]
x = lowest(low[1], 10) // to replace the cycle
isDivergent = abs(x-low[0])/low[0]*100 < .05 and abs(x- rsiValLow[0])>=1.5
isRsiOS = rsiValue <= 25
//plot signals to chart
plotshape(isRsiOS, title="Oversold", location=location.belowbar, transp=0, style=shape.triangleup, text="Buy")
plotshape(isDivergent, title="Divergence", location=location.belowbar, transp=0, style=shape.triangleup, text= "Divergence" )
//plot(x)
I was missing:
//#version=4
That's all and nothing else was the problem and there for it concludes this question

Tradingview Pine-Script: How to plot only the last x periods

I'd like to plot an indicator only for the last x periods.
How do I do that?
If I could do time operations (substract x * period from plotStartDate), maybe I could use this code:
period = timeframe.ismonthly or timeframe.isweekly ? "12M" : "M"
plotStartDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), 00, 00)
isPlotDate = time >= plotStartDate
plot(isPlotDate ? mydata : na, color=mydata != mydata[1]:na, style=plot.style_line, linewidth=2)
Version 1
Not sure this is what you're looking for. It uses plot()'s show_last= parameter to restrict the number of last bars plotted after your isPlotDate constraint has been satisfied:
//#version=4
study("", "", true)
xPeriods = input(10)
plotStartDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), 00, 00)
isPlotDate = time >= plotStartDate
plot(isPlotDate ? close : na, show_last = xPeriods)
Version 2
//#version=4
study("Plot starting n months back", "", true)
monthsBack = input(3, minval = 0)
monthsExtra = monthsBack % 12
monthsExcedent = month(timenow) - monthsExtra
yearsBack = floor(monthsBack / 12) + (monthsExcedent <= 0 ? 1 : 0)
targetMonth = monthsExcedent <= 0 ? 12 + monthsExcedent : monthsExcedent
targetYearMonth = year == year(timenow) - yearsBack and month == targetMonth
beginMonth = not targetYearMonth[1] and targetYearMonth
var float valueToPlot = na
if beginMonth
valueToPlot := high
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na)
Version 3
Simpler:
//#version=4
study("Plot starting n months back", "", true)
monthsBack = input(3, minval = 0)
targetDate = time >= timestamp(year(timenow), month(timenow) - monthsBack, 1, 0, 0, 0)
beginMonth = not targetDate[1] and targetDate
var float valueToPlot = na
if beginMonth
valueToPlot := high
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na)
Version 4
At v4 you can set the variable show_last in the plot() function.
In "PineScript language reference manual" says:
show_last (input integer) If set, defines the number of bars (from the
last bar back to the past) to plot on chart.
https://www.tradingview.com/pine-script-reference/#fun_plot

Julia MAC Installation

I am trying to install Julia on my MAC. I came up with the following error during installation:
Any idea is appreciated about what the problem can be.
Edit:
"test.jl is like this:
using JuMP
using Gurobi
m = Model(solver=GurobiSolver())
m = Model()
#variable(m, x ) # No bounds
#variable(m, x >= lb ) # Lower bound only (note: 'lb <= x' is not valid)
#variable(m, x <= ub ) # Upper bound only
#variable(m, lb <= x <= ub ) # Lower and upper bounds
#variable(m, x == fixedval ) # Fixed to a value (lb == ub)
#variable(m, x[1:M,1:N] >= 0 )
aff = AffExpr([x, z], [3.0, 4.0], 2.0) # 3x + 4z + 2
quad = QuadExpr([x,y],[x,z],[3.0,4.0],aff) # 3x^2 + 4yz + 3x + 4z + 2
#constraints(m, begin
x >= 1
y - w <= 2
sum_to_one[i=1:3], z[i] + y == 1
end)
#expression(m, shared, sum(i*x[i] for i=1:5))
#constraint(m, shared + y >= 5)
#constraint(m, shared + z <= 10)
expr = #expression(m, [i=1:3], i*sum(x[j] for j=1:3))
A few suggestions:
- use the full path in your code to be sure where the file is located
- Julia is case sensitive, so Test.jl and test.jl are different
- check the permissions on the file
- Julia makes heavy use of "test.jl" type names, potential confusion?

How to fix Errors with lists Length?

Help me please!
On the 3-rd step I got such errors as
Part 41 of ...... does not exist.
though on the previous steps it worked and returned results.
I've got lists of 40 elements in spkn,spkw,spmn,spmw and 41 in spx,spfn,spfw.
Code:
spx = {-2, -1.90577, -1.81153, -1.59327, -1.375, -1.35785, -1.3407, -1.24655, -1.22941, -1.11811, -0.934054, -0.80167, -0.75, -0.625,-0.5, -0.25, -0.0981238, 0.303752, 0.651876, 0.94833, 1, 1.5, 1.75,2.11731, 2.5, 2.5625, 2.625, 3.3125, 3.75, 4, 4.00964, 4.01928,4.25964, 4.36731, 4.5, 4.75, 5, 5.25, 5.5, 5.75, 6}
spkw = {105.056, 89.2249, 17.7361, 7.25929, 7.25929, 7.25929, 7.25929,1.09386, 1.09386, -7.35382, -12.5073, -11.929, -11.929, -15.429, -8.63312,-6.34314, -14.3807, -16.7907, -18.933, -12.3896, -3.021, -22.0262,-25.7865, -18.8033, -9.07591, -9.18036, -8.49959, -9.24378, -7.32337,-0.271835, -0.270096, 0.123206, 0.156523, 0.465142, 4.12922, 4.23318,8.03654, 8.20981, 12.1518, 12.3944}
spkn = {73.5426, 66.8007, 24.6942, 16.4029, 0.726929,0.314512, -1.23002, -1.23002, -3.90668, -10.8276, -14.2065,-13.0895, -18.656, -20.1709, -8.79676, -8.79676, -11.2319, -13.9771, -15.1407, -2.50312, -4.72374, -32.4496, -34.2958, -21.0455, -2.45882,-2.45882, -2.45882, -2.45882, -2.45882, -2.45882, -2.45882, -1.70357, -1.70357, -1.11799, 6.1251, 6.36752, 6.36752, 6.60995, 14.0955,14.5803}
spmw = {243.475, 213.305, 83.8004, 67.1081, 67.1081, 67.1081, 67.1081,59.4226, 59.4226, 49.9772, 45.1635, 45.6272, 45.6272, 43.4397,46.8376, 47.4101, 46.6214, 47.3535, 48.75, 42.5447, 33.1761,61.6839, 68.2644, 53.4787, 29.1603, 29.4279, 27.6409,30.1061,22.9045,-5.30161,-5.30859,-6.88938,-7.0313,-8.37913,-24.8675,-25.3613, -44.3781, -45.2877, -66.9686, -68.3634}
spmn = {180.448, 167.6, 91.3225, 78.1123, 56.5579, 55.9978, 53.9271,53.9271, 50.6364, 42.898, 39.742, 40.6374, 36.4626, 35.5158,41.2028, 41.2028, 40.9639, 41.7978, 42.5563, 30.5716, 32.7923,74.3811, 77.6119, 49.5569, 3.09017, 3.09017, 3.09017, 3.09017,3.09017, 3.09017, 3.09017, 0.0546329, 0.0546329, -2.5028,-35.0967, -36.2482, -36.2482, -37.5209,-78.6912, -81.4791}
spfn[[i]] = spkn[[i]]*spx[[i]] + spmn[[i]];
spfw[[i]] = spkw[[i]]*spx[[i]] + spmw[[i]];
spfw = {33.3632, 43.263, 51.6709, 55.5421, 57.1266, 57.2511, 57.3756,58.059, 58.0778, 58.1995, 56.846, 55.1903, 54.5739, 53.0828,51.1542, 48.9959, 48.0325, 42.2533, 36.408,30.7952,30.1551,28.6446,23.138,19.4168,6.47053,5.90328,5.32951, -0.513959, -0.750527, -6.38895, -6.39157, -6.39418,-6.36456, -6.09357, -6.28599, -5.25369, -4.19539, -2.18625, -0.133803,2.90414, 6.171}
spfn = {33.3632, 40.2933, 46.5882, 51.9781, 55.5583, 55.5708, 55.5762,55.4604, 55.4393, 55.0045, 530116,51.1309,50.4546,48.1226,45.6012,43.402,42.066,37.5522, 32.6864, 28.1979, 28.0685,25.7067, 17.5943,13.5547, -2.97428, -3.21054,-3.36422, -5.05466, -5.1301, -6.4392,-6.76879, -6.48231, -7.20196, -7.00719, -7.53373, -6.00246, -4.41058,-2.8187, -1.16621, 2.35765, 6.04694}
1-st step:
For[i = 1, i < Length#spfn, i++,
If[spfn[[i]]*spfn[[i + 1]] < 0 && spfw[[i]]*spfw[[i + 1]] < 0,
Print["1) exist roots: ", xnz[i] = -spmn[[i]]/spkn[[i]], ", ",
xwz[i] = -spmw[[i]]/spkw[[i]]] ;
Break[]
]
]
2-nd step:
For[i = 1, i < Length#spfn, i++,
If[(0 < spfn[[i]]) && (spfn[[i + 1]] < 0) && (0 < spfw[[i]]) && (0 <
spfw[[i + 1]]),
Print["2) exist roots:", xnz[i] = -spmn[[i]]/spkn[[i]], ", ",
spx[[i + 1]]] ;
Break[]
]
]
3-rd step(DOESN'T WORK):
For[i = 1, i < Length#spfn, i++,
If[(spfn[[i]] < 0) && (0 < spfn[[i + 1]]) && (0 < spfw[[i]]) && (0 <
spfw[[i + 1]]),
Print["3) exist roots:", xnz[i] = -spmn[[i]]/spkn[[i]], ", ",
spx[[i]]];
Break[]
]
]
THE RESULTS are:
1) exist roots: 5.58272, 5.511
2) exist roots:2.35475, 2.5
and errors:
Part::partw: Part 41 of {73.5426,66.8007,24.6942,16.4029,0.726929,0.314512,-1.23002,-1.23002,-3.90668,-10.8276,-14.2065,-13.0895,-18.656,-20.1709,-8.79676,-8.79676,<<8>>,-2.45882,-2.45882,-2.45882,-2.45882,-2.45882,-2.45882,-2.45882,-1.70357,-1.70357,-1.11799,6.1251,6.36752,6.36752,6.60995,14.0955,14.5803} does not exist. >>
Part::partw: Part 41 of {-2,-1.90577,-1.81153,-1.59327,-1.375,-1.35785,-1.3407,-1.24655,-1.22941,-1.11811,-0.934054,-0.80167,-0.75,-0.625,-0.5,-0.25,-0.0981238,0.303752,0.651876,0.94833,1,1.5,1.75,2.11731,2.5,2.5625,2.625,3.3125,3.75,4,4.00964,4.01928,4.25964,4.36731,4.5,4.75,5,5.25,5.5,5.75,6} does not exist. >>
and some more similar..
If you take the following out of your 'Code' section the rest executes without error messages.
spfn[[i]] = spkn[[i]]*spx[[i]] + spmn[[i]];
spfw[[i]] = spkw[[i]]*spx[[i]] + spmw[[i]];

What should be the recurrence relation?

I am trying to solve this problem using dynamic programming but getting wrong answer. I think the recurrence i am using is incorrect. What should be the recurrence relation for the problem and what information should the dp state hold? At present, i am using a 2-dimensional array where dp[i][j] denotes the maximum number of scoops for rectangle of size (i x j), So answer will be dp[n - 1][n - 1].-> Problem Statement
My Code:
1) s[n][n] is the grid that is given in the problem.
2) end[i][j].first is 1 if s[i][j] is used in the solution for (i x j) rectangle and 0 otherwise.
3) end[i][j].second is 1 if s[i][j] is joined with upper-# and 2 if left-# and 0 if s[i][j] is not used.
int dp[n][n];
pair<int, int> end[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
// w is the answer for (i x j) without using s[i][j]
w = (i - 1 >= 0 ? dp[i - 1][j] : 0) + (j - 1 >= 0 ? dp[i][j - 1] : 0) - (i - 1 >= 0 && j - 1 >= 0 ? dp[i - 1][j - 1] : 0);
if (i - 1 >= 0 && j - 1 >= 0 && end[i - 1][j].first == 1 && end[i - 1][j].second == 2 && end[i][j - 1].first == 1 && end[i][j - 1].second == 1) w--;
x = y = 0;
if (s[i][j] == '#') {
if (i > 0) {
// using the upper # if present
if (s[i - 1][j] == '#') x = 1 + (i - 2 >= 0 ? dp[i - 2][j] : 0) + (j - 1 >= 0 ? dp[i][j - 1] : 0) - (i - 2 >= 0 && j - 1 >= 0 ? dp[i - 2][j - 1] : 0);
if (i - 2 >= 0 && j - 1 >= 0 && end[i - 1][j - 1].first == 1 && end[i - 1][j - 1].second == 1 && end[i - 2][j].first == 1 && end[i - 2][j].second == 2) x--;
if (x <= w) x = 0;
}
if (j > 0) {
//using the left # if present
if (s[i][j - 1] == '#') y = 1 + (i - 1 >= 0 ? dp[i - 1][j] : 0) + (j - 2 >= 0 ? dp[i][j - 2] : 0) - (i - 1 >= 0 && j - 2 >= 0 ? dp[i - 1][j - 2] : 0);
if (i - 1 >= 0 && j - 2 >= 0 && end[i - 1][j - 1].first == 1 && end[i - 1][j - 1].second == 2 && end[i][j - 2].first == 1 && end[i][j - 2].second == 1) y--;
if (y <= w) y = 0;
}
}
// choosing the maximum of the three and accordingly assigning end[i][j]
if (w >= x && w >= y) {
dp[i][j] = w;
end[i][j] = make_pair(0, 0);
} else if (x > w && x > y) {
dp[i][j] = x;
end[i][j] = make_pair(1, 1);
} else {
dp[i][j] = y;
end[i][j] = make_pair(1, 2);
}
}
}
This problem is not about dynamic programming. It's about the maximum matching. Let's paint the grid with black and white colors(like a chess board). Now we have a bipartite graph(black cells are in the first part and white cells are in the second one). We should add edges between adjacent cells if both of them contain oil. The answer is the size of the maximum matching in this graph.

Resources