Conditional BG Color in Pinescript - pine-script-v5

I am trying to plot a table on my indicator and conditionally color the text color depending on the value "c1_1" (if c1_1 >= 0.0 then green else red)
var table XYZ = table.new(position.top_right, 1, 1, bgcolor = color.white, frame_width = 4, frame_color = color.black)
if barstate.islast
table.cell(XYZ, 0, 0, str.tostring(c1_1, format.mintick), text_color = c1_1 >= 0.0 ? color.green : color.red)
The problem is that sometimes the color is as expected while other times it is not, I do not understand what I am doing incorrectly, would appreciate any inputs on what could be the issue or if there's a better way of doing this.

Related

Compare bool and float to determine plot (pine script v5)

You folks helped me with another part of this script and I'm making good progress. However, I'm stuck again.
I added a code to produce a dynamic overbought/oversold line on the indicator. That part works great. Now, I'm wanting to add a plotshape when conditions are met to signal long or short. Unfortunately, I can't figure out how to compare a bool variable and a float variable. The dynamic OB/OS lines are floats. The crossover lines are bool.
What I want is for the (green/long) shape to plot when a crossover happens below the dynamic oversold line and the (red/short) shape to plot when the crossunder happens above the dynamic overbought line.
I've tried all kinds of things (var, loop with "while" using sb value as a trigger). I either break the code or I get every crossover plotted. Here is what I have that is stable. I took all my junk that didn't work out:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HammerGeek
//#version=5
indicator('Impulse MACD [HammerGeek - LazyBear]', shorttitle='IMACD_HG_LB', overlay=false)
lengthMA = input(34)
lengthSignal = input(9)
//OverBought = input.float(0.1)
//OverSold = input.float(-0.1)
calc_smma(src, len) =>
smma = 0.0
sma_1 = ta.sma(src, len)
smma := na(smma[1]) ? sma_1 : (smma[1] * (len - 1) + src) / len
smma
calc_zlema(src, length) =>
ema1 = ta.ema(src, length)
ema2 = ta.ema(ema1, length)
d = ema1 - ema2
ema1 + d
src = hlc3
hi = calc_smma(high, lengthMA)
lo = calc_smma(low, lengthMA)
mi = calc_zlema(src, lengthMA)
md = mi > hi ? mi - hi : mi < lo ? mi - lo : 0
sb = ta.sma(md, lengthSignal)
sh = md - sb
mda = ta.sma(md, lengthSignal)
OverBoughtLine = ta.highest(sb * 0.5, lengthMA*100)
OverSoldLine = ta.lowest(sb * 0.5, lengthSignal*100)
shsbCrossOver = ta.crossover(md, sb)
shsbCrossUnder = ta.crossunder(md, sb)
//mdc = src > mi ? src > hi ? color.lime : color.green : src < lo ? color.red : color.orange
mdc = color.green
plot(0, color=color.new(color.gray, 0), linewidth=1, title='MidLine')
plot(md, color=mdc, linewidth=2, title='ImpulseMACD', style=plot.style_line)
plot(sh, color=color.rgb(122, 5, 168, 40), linewidth=2, title='ImpulseHisto', style=plot.style_area)
plot(sb, color=color.rgb(255, 255, 255, transp = 40), linewidth=2, title='ImpulseMACDCDSignal')
plot(OverBoughtLine, color=color.new(#f3e032, 0), linewidth=1, title = 'Overbought Line')
plot(OverSoldLine, color=color.new(color.yellow, 0), linewidth=1, title = 'Oversold Line')
plotshape(shsbCrossOver, "LONG", shape.triangleup, location.bottom, color.new(color.green, 0))
plotshape(shsbCrossUnder, "SHORT", shape.triangledown, location.top, color.new(#cd0808, 0))
//ebc = input(false, title='Enable bar colors')
//barcolor(ebc ? mdc : na)
You cannot compare a bool with a float. It doesn't make any sense and it is not legal in pinescript.
What I want is for the (green/long) shape to plot when a crossover
happens below the dynamic oversold line and the (red/short) shape to
plot when the crossunder happens above the dynamic overbought line.
You want to check if the values you use in that crossover is under your dynamic oversold line at the time of crossover.
OverSoldLine = ta.lowest(sb * 0.5, lengthSignal*100)
shsbCrossOver = ta.crossover(md, sb)
is_good = shsbCrossOver and (md < OverSoldLine) and (sb < OverSoldLine)

Plotshape and label

In below code, I am getting syntax error for plotshape and several for label. Even for max / min i am getting too many arguments. I used label to get the value from intraburst formula as text. Will need help to resolve these.
//#version=4
study("Intraburst", overlay=true)
truemove = abs(open[2] - close) dayTrueHigh = max(high[1], high,
close[2]) dayTrueLow = min(low[1], low, close[2]) dayTrueRange =
dayTrueHigh - dayTrueLow intraburstLevel = truemove / dayTrueRange *
100
if intraburstLevel > 30
plotshape(series=intraburstLevel, text="ON", style=shape.circle, location=location.abovebar, color=color.green, size=size.normal) else
plotshape(series=intraburstLevel, text="OFF", style=shape.circle, location=location.abovebar, color=color.red, size=size.normal)
label = label.new(bar_index=0, yloc=y_top, xloc=xloc.bar_index,
text=tostring(intraburstLevel, "0.00"), size=size.large,
color=color.black) label.location = location.top label.y = y_top
label.x = x_right label.text = tostring(intraburstLevel, "0.00")
In series, use a ternary
Something like that:
plotshape(
intraburstLevel > 30 ? intraburstLevel : na,
text = "ON",
style = shape.circle,
location = location.abovebar,
color = color.green,
size = size.normal)
plotshape(
intraburstLevel <= 30 ? intraburstLevel : na,
text = "OFF",
style = shape.circle,
location = location.abovebar,
color = color.red,
size = size.normal)
and the label
label.new(
bar_index,
close,
tostring (intraburstLevel, "##.00"),
color = color.black,
textcolor = color.white)

Im trying to make an animation trigger on roblox studio, can someone tell me how?

So when I try touch the trigger, the thing I'm trying to animate doesn't so the animation, I tried the animation id, anything else, can someone send me a model that has this, it will be nice if you can.
I made this with the gui elements already done u fill in what u need.
local library = {}
local function onClicked(frame, gui)
gui:SetVisible(false)
gui:ClearAllChildren()
end
function library:Create(parent)
local frame = Instance.new("Frame")
frame.Size = UDim2.new(1, 0, 1, 0)
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.BorderSizePixel = 0
local gui = Instance.new("TextLabel")
gui.Size = UDim2.new(0, 200, 0, 50)
gui.BackgroundColor3 = Color3.new(0.5, 0.5, 0.5)
gui.Position = UDim2.new(0.5, -100, 0.5, -25)
gui.Text = "Click the Button"
gui.TextColor3 = Color3.new(1, 1, 1)
gui.TextXAlignment = Enum.TextXAlignment.Center
gui.TextYAlignment = Enum.TextYAlignment.Center
gui.Font = Enum.Font.SourceSans
gui.TextSize = 24
gui.Parent = frame
local button = Instance.new("TextButton")
button.Size = UDim2.new(0, 50, 0, 25)
button.BackgroundColor3 = Color3.new(1, 0, 0)
button.Position = UDim2.new(0.5, -25, 0.85, 0)
button.Text = "X"
button.TextColor3 = Color3.new(1, 1, 1)
button.Parent = frame
button.MouseButton1Click:Connect(function()
onClicked(frame, gui)
end)
frame.Parent = parent
return frame
end

Max Drawdown in my Table is wrong, not same as in Overview (Tradingview)

Can you help me fix Drawdown in this table ? Is not the same as in Overview (Tradingview)
It shows a few percent difference and cannot figure a solution.
To measure an order’s drawdown, we take the difference between the entry price and worst price. Then multiply with the order’s size and increase the outcome with commission costs.
That price difference is for long orders the distance between the entry price and lowest price while the order was open. For short orders, it’s the distance between the entry price and the highest price during the order.
var balance = strategy.initial_capital
var totalTrades = strategy.closedtrades
var totalWins = strategy.wintrades
var totalLosses = strategy.losstrades
var maxDrawdown = 0.0
var maxBalance = 0.0
var drawdown = 0.0
var winsInRow = 0
var maxWinsInRow = 0
var lossesInRow = 0
var maxLossesInRow = 0
if strategy.wintrades > strategy.wintrades[1]
balance := strategy.initial_capital + strategy.netprofit
// totalWins := totalWins + 1 < not necessary as we're just using strategy.wintrades now
if balance > maxBalance
maxBalance := balance
winsInRow := winsInRow + 1
if winsInRow > maxWinsInRow
maxWinsInRow := winsInRow
lossesInRow := 0 // reset loss counter as we've just taken a win
if strategy.losstrades > strategy.losstrades[1]
balance := strategy.initial_capital + strategy.netprofit
// totalLosses := totalLosses same as above
drawdown := (maxBalance - balance) / maxBalance // I've changed this, but left your original here > (balance / maxBalance) - 1
if drawdown > maxDrawdown // drawdown will now hold, as a decimal, the max % drop from maxBalance
maxDrawdown := drawdown
lossesInRow := lossesInRow + 1
if lossesInRow > maxLossesInRow
maxLossesInRow := lossesInRow
winsInRow := 0 // reset the win counter as we've just taken a loss
var table testTable = table.new(position.bottom_right, 2, 4 , border_width=1, border_color=color.white, frame_color=color.white, frame_width=1)
fillCell(table, column, row, title, value, bgcolor, txtcolor) =>
cellText = title + "\n" + value
table.cell(table, column, row, cellText, bgcolor=bgcolor, text_color=txtcolor)
var bgcolor = color.new(color.blue,50)
green = color.new(color.green,50)
red = color.new(color.red,50)
if barstate.islastconfirmedhistory
dollarReturn = strategy.netprofit
fillCell(testTable, 0, 0, "Total Trades:", str.tostring(strategy.closedtrades), bgcolor, color.white)
fillCell(testTable, 1, 0, "Win Rate:", str.tostring(math.round((strategy.wintrades/strategy.closedtrades)*100,2))+"%", strategy.wintrades/strategy.closedtrades >= 0.5 ? green : red, color.white)
fillCell(testTable, 0, 1, "Starting:", "$" + str.tostring(strategy.initial_capital), bgcolor, color.white)
fillCell(testTable, 1, 1, "Ending:", "$" + str.tostring(math.round(balance, 2)),strategy.netprofit > 0 ? green :red, color.white)
fillCell(testTable, 0, 3, "Wins In Row", str.tostring(maxWinsInRow), maxWinsInRow > maxLossesInRow ? green : red , color.white)
fillCell(testTable, 1, 3, "Losses In Row", str.tostring(maxLossesInRow), maxWinsInRow > maxLossesInRow ? green : red, color.white)
fillCell(testTable, 1, 2, "Max Drawdown", str.tostring(math.round(maxDrawdown * 100, 2))+"%", strategy.netprofit > maxDrawdown ? green : red, color.white)
fillCell(testTable, 0, 2, "Net Profit", str.tostring(math.round(strategy.netprofit/strategy.initial_capital*100,2))+"%", strategy.netprofit/strategy.initial_capital *100 > 0 ? green : red, color.white)```

Script for indicating order history - Tradeview Pine

I want to indicate historical trades in tradingview charts via a script based on information on time and price for entry and close.
My best idea is to search through "time" to find matches for entry and close, and then change the background color according to short or long position or draw a horizontal line. However, this seems not optimal. Any suggestions?
I'd implement that in the next way:
//#version=3
strategy("Background changing", overlay=true)
NONE = 0
LONG = 1000
SHORT = -1000
position = NONE
position := nz(position[1], NONE)
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("LongEntryId", strategy.long)
position := LONG
if (close < high[1])
strategy.close("LongEntryId")
position := NONE
getColor(state) =>
state == LONG ? green :
state == SHORT ? red :
white
bgcolor(color=getColor(position))
Or you can put arrows to the chart:
//#version=3
study("My Script", overlay=true)
order = 0
if time >= timestamp(2018, 1, 10, 0, 0)
order := 1
if time == timestamp(2018, 1, 17, 0, 0)
order := -1
plotarrow(order)