I'm trying to include a plot using the PlotlyJS package in a small application using Blink.
So far, whenever I use the plot function, it opens a new window, which I want to avoid..
Furthermore, when updating the plot, another window pops up - also not wanted.
Does anyone know how to avoid these new windows? Thanks in advance!
Sample code:
using PlotlyJS,Blink
scatter_1 = scatter(;x=rand(10),y=rand(10))
p = plot(scatter_1) # first unwanted window
w = Window()
body!(w,p)
scatter_2 = scatter(;x = 2*rand(10), y = 2*rand(10))
addtraces(p, scatter_2) #second unwated window
Try adding ; at the end of the line.
using PlotlyJS,Blink
scatter_1 = scatter(;x=rand(10),y=rand(10))
p = plot(scatter_1);
w = Window()
body!(w,p)
scatter_2 = scatter(;x = 2*rand(10), y = 2*rand(10))
addtraces(p, scatter_2);
Related
Can the following expression of numpy arrays be vectorized for speed-up?
k_lin1x = [2*k_lin[i]*k_lin[i+1]/(k_lin[i]+k_lin[i+1]) for i in range(len(k_lin)-1)]
Is it possible to vectorize this calculation in numpy?
x1 = k_lin
x2 = k_lin
s = len(k_lin)-1
np.roll(x2, -1) #do this do bring the column one position right
result1 = x2[:s]+x1[:s] #your divider. You add everything but the last element
result2 = x2[:s]*x1[:s] #your upper part
# in one line
result = 2*x2[:s]*x1[:s] / (x2[:s]+x1[:s])
You last column wont be added or taken into the calculations and you can do this by simply using np.roll to shift the columns. x2[0] = x1[1], x2[1] = x1[2].
This is barely a demo of how you should approach google numpy roll. Also instead of using s on x2 you can simply drop the last column since it's useless for the calculations.
I am trying to make a Standard Deviation overlay using only the daily inputs, and have it overlay that info on any time frame chart. So, even if I look at an hourly chart, I will still see the daily deviations overlayed on the chart. I made one that changes with whatever time frame I am looking at. I started with another public one to make this:
study(title="Standard Deviation",shorttitle="SD",overlay=true)
length = input(20, minval=1)
src = input(open, title="Source")
sd = stdev(src, length)
piv=open
plotOpen = plot(piv,title="Open",color=black,trackprice=true,linewidth=2)
plotR05 = plot(piv+(0.5*sd),title="+0.5", color=red,trackprice=true,linewidth=2)
plotS05 = plot(piv-(0.5*sd),title="-0.5", color=red,trackprice=true,linewidth=2)
plotR10 = plot(piv+sd,title="1", color=blue,trackprice=true,linewidth=2)
plotS10 = plot(piv-sd,title="-1", color=blue,trackprice=true,linewidth=2)
plotR15 = plot(piv+(1.5*sd),title="+1.5", color=green,trackprice=true,linewidth=2)
plotS15 = plot(piv-(1.5*sd),title="-1.5", color=green,trackprice=true,linewidth=2)
plotR20 = plot(piv+(2*sd),title="+2", color=orange,trackprice=true,linewidth=2)
plotS20 = plot(piv-(2*sd),title="-2", color=orange,trackprice=true,linewidth=2)
I am trying to make a Standard Deviation overlay using only the daily inputs, and have it overlay that info on any time frame chart.
You can use TradingView's security() function for that. That function can load price data from any time frame and/or instrument, including the daily data from the current instrument.
With security() your code can thus calculate the daily standard deviation regardless of which time frame the script currently runs on.
For example:
study(title="Standard Deviation",shorttitle="SD",overlay=true)
length = input(20, minval=1)
src = input(open, title="Source")
// Load daily stddev
dailyStd = security(tickerid, "D", stddev(src, length))
piv=open
plotOpen = plot(piv,title="Open",color=black,trackprice=true,linewidth=2)
plotR05 = plot(piv+(0.5*dailyStd),title="+0.5", color=red,trackprice=true,linewidth=2)
plotS05 = plot(piv-(0.5*dailyStd),title="-0.5", color=red,trackprice=true,linewidth=2)
plotR10 = plot(piv+dailyStd,title="1", color=blue,trackprice=true,linewidth=2)
plotS10 = plot(piv-dailyStd,title="-1", color=blue,trackprice=true,linewidth=2)
plotR15 = plot(piv+(1.5*dailyStd),title="+1.5", color=green,trackprice=true,linewidth=2)
plotS15 = plot(piv-(1.5*dailyStd),title="-1.5", color=green,trackprice=true,linewidth=2)
plotR20 = plot(piv+(2*dailyStd),title="+2", color=orange,trackprice=true,linewidth=2)
plotS20 = plot(piv-(2*dailyStd),title="-2", color=orange,trackprice=true,linewidth=2)
Give it a try to see if this approach better serves your goal.
for version 5
//#version=5
indicator("Standart Deviation", shorttitle="SD", overlay=true)
length = input.int(30, minval=1)
src = input.source(open,"Source")
//load daily stdev
dailyStd = request.security(syminfo.tickerid,"D",ta.stdev(src,length))
piv=open
plotOpen = plot(piv,title="Open", color=color.white, trackprice = true, linewidth=2)
plotStdResistance = plot(piv+dailyStd,title="RESISTANCE",color=color.red,trackprice=true,linewidth=1)
plotStdSupport = plot(piv-dailyStd,title="SUPPORT", color=color.green, trackprice = true, linewidth=1)
I have a Box2DWeb sketch working ok but I am unable to figure out how to use the ApplyForce method with a body. I have attached the working codepen. On line 85, I have commented out the line that I thought would work but everything disappears when I include it.
If anyone could let me know the correct way to use it, I would be very happy. I have RTFM and seen similar posts on StackO but I still cannot work it out.
http://codepen.io/anon/pen/vOJByN?editors=101
Thanks a lot,
Steven
// single dynamic object----------------------
var fixDef2 = new b2FixtureDef;
fixDef2.density = 1.0
fixDef2.friction = 0.2;
fixDef2.restitution = 0.5;
var bodyDef2 = new b2BodyDef;
bodyDef2.type = b2Body.b2_dynamicBody;
fixDef2.shape = new b2PolygonShape;
fixDef2.shape.SetAsBox((300/SCALE)/2, (60/SCALE) / 2);
bodyDef2.position.x = canvas.width/4/SCALE;
bodyDef2.position.y = canvas.height/2/SCALE;
bodyDef2.angle = 5;
world.CreateBody(bodyDef2).CreateFixture(fixDef2);
// Apply force to object----------------------
/*bodyDef2.ApplyForce(new b2Vec2(500,50) , bodyDef2.GetWorldCenter());
*/
You should call ApplyForce method of b2Body, not of b2BodyDef. You can get b2Body object as result of world.CreateBody(bodyDef2) method.
I've changed your codepen here: http://codepen.io/anon/pen/NqZvqG
Your code:
world.CreateBody(bodyDef2).CreateFixture(fixDef2);
// Apply force to object----------------------
/*bodyDef2.ApplyForce(new b2Vec2(500,50) , bodyDef2.GetWorldCenter());
*/
My code:
var myBody = world.CreateBody(bodyDef2);
var myFixture = mybody.CreateFixture(fixDef2);
// Apply force to object
myBody.ApplyForce(new b2Vec2(500,50), myBody.GetWorldCenter());
I am trying to somehow replicate the range bar chart here.
I've found this reference but I don't fully grasp the code.
What I have is a series of task (sometimes accomplished in different periods).
let d = [("task1", DateTime.Parse("11/01/2014 08:30"), DateTime.Parse("12/01/2014 10:30"));
("task2", DateTime.Parse("15/01/2014 09:30"), DateTime.Parse("16/01/2014 10:30"));
("task3", DateTime.Parse("11/01/2014 08:30"), DateTime.Parse("16/01/2014 10:30"))]
let chart = d |> FSharp.Charting.Chart.RangeBar
chart.ShowChart()
I am struggling to understand the logic of the API.
I have also tried:
let chart = new Windows.Forms.DataVisualization.Charting.Chart(Dock = DockStyle.Fill)
let area = new ChartArea("Main")
chart.ChartAreas.Add(area)
let mainForm = new Form(Visible = true, TopMost = true, Width = 700, Height = 500)
mainForm.Controls.Add(chart)
let seriesColumns = new Series("NameOfTheSerie")
seriesColumns.ChartType <- SeriesChartType.RangeBar
type SupportToChart(serieVals: Series) =
member this.addPointXY(lbl, [<ParamArray>] yVals: Object[]) =
serieVals.Points.AddXY(lbl, yVals) |> ignore
let supporter = SupportToChart(seriesColumns)
supporter.addPointXY("AAA", DateTime.Parse("11/01/2014 08:30"), DateTime.Parse("12/01/2014 10:30") )
which results in
System.ArgumentOutOfRangeException: You can only set 1 Y values for
this data point.
Has something changed in the API since then?
I'm not entirely sure that F# Charting is currently powerful enough to be able to reconstruct the above chart. However, one of the problems seems to be that it treats dates as float values (for some reason) and incorrectly guesses the ranges. You can at least see the chart if you use:
Chart.RangeBar(d)
|> Chart.WithYAxis(Min=41650.0, Max=41660.0)
Please submit this as an issue on GitHub. If you want to dig deeper into how F# Charting works and help us get this fixed, that would be amazing :-)
The trick is initializing the Series with
let serie = new Series("Range", yValues)
where yValues defines the max number of "Y-values".
this code (lua) gives you a random value from the table "local a" by pressing the text "new". Unfortunately the new random value just appears above the old one. I've tried to remove the old value e.g. with display.remove(mmDis), but it doesn't work.
The second problem is that sometimes I also get back the value "nil" and not only the four entries from the table.
Both things must be easy to solve, but as newbie to lua and working on these small things for almost 4 hours now I just don't get what to change to make it work.
-- references
local mmDis
-- functions
function randomText(event)
display.remove(mmDis)
local a = {"Banana!","Apple!","Potato","Pie"}
com = (a[math.random(0.5,#a)])
local mmDis = display.newText(tostring(com),
display.contentWidth*0.57, display.contentHeight*0.7,
display.contentWidth*0.9, display.contentHeight*0.8, "Calibri", 60)
end
-- menu button
local textnew = display.newText("New", 0, 0, "Calibri", 40)
textnew.x = display.contentWidth*0.2
textnew.y = display.contentHeight*0.9
textnew:addEventListener ("tap", randomText )
It's hard to understand what you are trying to do because when you create textnew you have it in one position and size, while in randomeText() you seem to want to replace that text object with a new one, but you put it at a different pos and size. It appears you want to change the object's text every time you press on tap; in this case, you don't need to replace the text object, you just replace its text.
Also:
you have two "local mmDis", the second one will hide the first, not sure what you're after there
read carefully the docs for math.random
com is already a string so you don't need tostring
Try this code and let me know if this is not what you are after:
local menuTextOptions = {
text = "New",
x = display.contentWidth*0.2,
y = display.contentHeight*0.9,
align = 'left',
font = "Calibri",
fontSize = 40,
}
local textnew = display.newText(menuTextOptions)
-- functions
function randomText(event)
local a = {"Banana!","Apple!","Potato","Pie"}
local com = a[math.random(1,#a)]
textnew.text = com
-- if you want to change position too:
-- textnew.x = display.contentWidth*0.2
-- textnew.y = display.contentHeight*0.9
-- if you want to change size too, but only used for multiline text:
-- textnew.width = display.contentWidth*0.9
-- textnew.height = display.contentHeight*0.8
end
textnew:addEventListener ("tap", randomText )
Sometimes it looks like the button doesn't do anything when you tap but that's because the random number happens to be same as previous, you could put a loop to guard against that. Ig you actually want to change the pos and/or width, then the above code makes it clear where you should do that.
So, that's the basic code (without any extras at the moment :)) how it should be. Big thanks to Scholli!:
local menuTextOptions = {
text = "New",
x = display.contentWidth*0.2,
y = display.contentHeight*0.9,
align = 'left',
font = "Calibri",
fontSize = 40,
}
local textnew = display.newText(menuTextOptions)
local replacement = display.newText(menuTextOptions)
replacement.y = display.contentHeight*0.5
-- functions
function randomText(event)
local a = {"Banana!","Apple!","Potato","Pie"}
local com = a[math.random(1,#a)]
replacement.text = com
end
textnew:addEventListener ("tap", randomText )