Is it possible to draw a pattern (XABCD) with Tradingview api? - trading

I am trying to build a harmonic pattern detection system. I would like to use Tradingview UI for frontend but I am wondering if it is possible to draw these patterns programmatically via their API.
On the chart, we can draw it manually, choosing tool from pattern toolbox. For example:
bat pattern
Can we manage to draw it with code ?

Yes, you can draw complex shapes/drawings when using the TradingView Charting Library by using createMultipointShape():
tradingViewWidget.chart.createMultipointShape(
// indicator points
[
{time: pointTime1, price: pointPrice1},
{time: pointTime2, price: pointPrice2},
{time: pointTime3, price: pointPrice3},
{time: pointTime4, price: pointPrice4},
{time: pointTime5, price: pointPrice5}
],
// settings/overrrides
{
shape: 'xabcd_pattern'
}
)

Related

How does one serialize a digraph in Erlang or Elixir for export purposes

I am using Erlang's digraph module for storing Directed Acyclic Graphs (DAGs). So for argument's sake here is a super simple graph (using Elixir):
gr = :digraph.new()
:digraph.add_vertex(gr, "A")
:digraph.add_vertex(gr, "B")
:digraph.add_vertex(gr, "C")
:digraph.add_edge(gr, "A", "B")
:digraph.add_edge(gr, "A", "C")
which looks like this:
We can see it's all worked:
iex(7)> :digraph.vertices(gr)
["A", "C", "B"]
iex(8)> :digraph.edges(gr)
[[:"$e" | 0], [:"$e" | 1]]
iex(9)> :digraph.out_neighbours(gr, "A")
["C", "B"]
iex(10)> :digraph.out_neighbours(gr, "B")
[]
iex(11)> :digraph.out_neighbours(gr, "C")
[]
iex(12)> :digraph_utils.is_acyclic(gr)
true
Now I'm going to be adding and removing more vertices and edges, but I would like to transmit these graphs to applications outside of the Elixir/Erlang ecosystem such as Cytoscape.js. Is there a standardized way to serialize digraphs into some industry standard readable format (json or xml for example), such as JGF, Netlix's Falcor JSON Graph format, or other?
I could write my own serializer but I'd prefer something pre-existing. I can't find anything that does this in digraph or digraph_utils.
Searching there are various solutions for exporting digraph data to various popular formats, but nothing canonical Two of the most popular of these formats are DOT and GraphML.
Some Elixir and Erlang libraries for exporting digraphs to various formats:
https://github.com/mikowitz/graphvix
https://github.com/jabarszcz/digraph_viz
https://github.com/fenollp/erlang-dot
Some Elixir and Erlang examples of updating the front-end in soft realtime using js libs such as vis.js and web sockets:
http://blog.songsaboutsnow.com/elixir/graphviz/2018/06/26/graphvix-part1.html
https://github.com/swelham/digraph_viewer
https://rshestakov.wordpress.com/2013/03/17/extended-example-with-graphviz-updates-via-websockets/

Linear Regression Using CEP

What is the best approach to achieve a linear regression using CEP ?. We have tried two different options.
We do want to have the algorithm working in real time.
Basic code for both approach :
create context IntervalSpanning3Seconds start #now end after 30 sec;
create schema measure (
temperature float,
water float,
_hours float,
persons float,
production float
);
#Name("gattering_measures")
insert into measure
select
cast(getNumber(m,"measurement.bsk_mymeasurement.temperature.value"),
float) as temperature,
cast(getNumber(m, "measurement.bsk_mymeasurement.water.value"), float) as water,
cast(getNumber(m, "measurement.bsk_mymeasurement._hours.value"), float) as _hours,
cast(getNumber(m, "measurement.bsk_mymeasurement.persons.value"), float) as persons,
cast(getNumber(m, "measurement.bsk_mymeasurement.production.value"),float) as production
from MeasurementCreated m
where m.measurement.type = "bsk_mymeasurement";
1. Using the function stat:linest
#Name("get_data")
context IntervalSpanning3Seconds
select * from measure.stat:linest(water,production,_hours,persons,temperature)
output snapshot when terminated;
EDIT: The problem here is that it seems like the "get_data" is getting execute by each measurement and not by the entire collection of measurement.
2. Get data and passed a javascript function.
create expression String exeReg(data) [
var f = f(data)
function f(d){
.....
// return the linear regression as a string
}
return f
];
#Name("get_data")
insert into CreateEvent
select
"bsk_outcome_linear_regression" as type,
exeReg(m) as text,
....
from measure m;
EDIT: Here, I would like to know what is the type of the variable that is passed to the exeReg() function and how I should iterate it ? example would be nice.
I'll appreciate any help.
Using JavaScript would mean that the script computes a new result (recomputes) for each collection it receives. Instead of recomputing it the #lineest data window is a good choice. Or you can add a custom aggregation function or custom data window to the engine if there is certain code your want to use. Below is how the script can receive multiple events for the case when a script is desired.
create expression String exeReg(data) [
.... script here...
];
select exeReg(window(*)) ....
from measure#time(10 seconds);

Remove legend bar in R using Image.Plot

This is the test code im using
x_coord <- c(1,2,3,4)
y_coord <- c(1,2,3,4)
value <- c(12,15,19,30)
foo <- data.frame(x_coord, y_coord, value)
library(MBA)
foo=foo[ order(foo[,1], foo[,2],foo[,3]), ]
mba.int <- mba.surf(foo, 300, 300, extend=T)$xyz.est
library(fields)
fields::image.plot(mba.int,legend.only = FALSE, axes=FALSE)
The axes part deletes the axis, but when i try to remove the legend bar, the vertical bar indicating the color measurements, it will not go away.
i have tried smallplot = 1, but that gives me an error but it gets rid of the vertical legend,
Anyone have any idea of how to get rid of the legend without any errors produced ?
If you don't want the color legend, just use the built-in image function instead. The function you are using is designed specifically for adding a legend easily.
If you want to keep the same color scheme as the fields image.plot function:
image(<your data>, ..., col = tim.colors())
Using this creates the exact same image without a legend.
The image and image.plot functions actually have quite different plotting functionality.
One problem (at least for me, trying to plot regional climate model data) with using the built-in image is that it cannot handle irregular grids. The image.plot function uses the poly.image function internally to create the plot. Both are included in the fields package. The good thing is that the poly.image function also can be used on its own, for example like this:
library("fields")
# create an example of an irregular 3x3 grid by adding random perturbations up to ±0.6
x <- matrix(rep(1:3,each=3) + 0.6*runif(9), ncol=3)
y <- matrix(rep(7:9,times=3) + 0.6*runif(9), ncol=3)
# 9 values, from 1 to 9
z <- matrix(1:9,nrow=3)
# Please avoid the default rainbow colours, see e.g.
# https://www.climate-lab-book.ac.uk/2014/end-of-the-rainbow/
# Other examples of colour schemes: https://colorbrewer2.org/
col <- colorRampPalette(c("#2c7bb6","#abd9e9","#ffffbf","#fdae61","#d7191c"))(9)
par(mfrow=c(1,3), mar=c(4,4,4,1), oma=c(0,0,0,0))
image(x=7:9,y=1:3,z,col=col, main="image()\n Only regular grid. Also note transposition.")
image.plot(x,y,z,col=col, main="image.plot()\n Always with colorbar.")
poly.image(x,y,z,col=col, main="poly.image()\n Does not include colorbar.")

Charting and Comparing Prices in F# - Chart.Combine will not produce graph

I'm working through the charting and comparing prices tutorial on tryfsharp.org and my Chart.Combine function in Fsharp.Charting library will not work, but, other charts, such as Chart.Line will work! Code below.
// Helper function returns dates & closing prices from 2012
let recentPrices symbol =
let data = stockData symbol (DateTime(2012,1,1)) DateTime.Now
[ for row in data.Data -> row.Date.DayOfYear, row.Close ]
Chart.Line(recentPrices "AAPL", Name="Apple") //These two guys work when I try to plot them.
Chart.Line(recentPrices "MSFT", Name="Microsoft")
Chart.Combine( // This guy will not plot. Syntax found here: http://fsharp.github.io/FSharp.Charting/PointAndLineCharts.html
[ Chart.Line(recentPrices "AAPL", Name="Apple")
Chart.Line(recentPrices "MSFT", Name="Microsoft")])
I'd suggest you substituting your data generator function with something simpler and achieving correct plotting with this mockup first. For example, the following script:
#load #"<your path here>\Fsharp.Charting.fsx"
open System
open FSharp.Charting
let rand = System.Random
let recentPricesMock symbol =
[for i in 1..12 -> DateTime(2012,i,1),rand.Next(100)]
Chart.Combine (
[ Chart.Line(recentPricesMock "AAPL", Name="Apple")
Chart.Line(recentPricesMock "MSFT", Name="Microsoft")])
must plot combined mockup chart without any problems, as it does on my local box. From here you may drill down for the cause of original problem comparing your recentPrices with recentPricesMock.
EDIT: after getting to the full problematic source code I can point out two problems there that, as I was expecting, are in your choice of data rather, than in charting per se:
First, your definition of recentPrices converts dates into sequential day of year (row.Date.DayOfYear), so transition from 2012 into 2013 messes up your data and, consequently, charts. If you want to preserve your current functionality then it makes sense to redefine recentPrices as below
let recentPrices symbol =
let data = stockData symbol (DateTime(2012,1,1)) DateTime.Now
[ for row in data.Data -> row.Date, row.Close ]
Second, you chose a pair of stocks that doesn't scale well being combined on the single chart (AAPL in high hundreds $$, while MSFT in low tens $$), which adds to repetition of data points from first problem. After changing in your code AAPL to YHOO in addition to the recentPrices definition change described above
Chart.Combine ([
Chart.Line(recentPrices "YHOO", Name="Yahoo")
Chart.Line(recentPrices "MSFT", Name="Microsoft")
])
yields a beautiful smooth chart combo:

Generating printable output within Squeak

I'd like to create a printable output file from within Squeak, for instance to create a report.
I've done a little Googling and I'm surprised by how little material in the way of examples relating to creating printable files exist. However, I've found a couple of classes class called PostscriptCanvas and EPSCanvas and a method within it called morphAsPostscript.
To try these classes out I created a tiny code example and my first workspace example was:
p := PasteUpMorph new.
p extent: 300#300.
p position: 20#20.
p borderColor: Color black.
p setProperty: #cornerStyle toValue: #rounded.
p openInWorld.
(FileStream newFileNamed: 'test1.ps') nextPutAll: (PostscriptCanvas morphAsPostscript: p)
unfortunately the above doesn't work and halts with doesnotUnderstand #pageBBox.
when I try the example again but this time using the EPSCanvas class:
p := PasteUpMorph new.
p extent: 300#300.
p position: 20#20.
p borderColor: Color black.
p setProperty: #cornerStyle toValue: #rounded.
p openInWorld.
(FileStream newFileNamed: 'test2.eps') nextPutAll: (EPSCanvas morphAsPostscript: p).
this time I generate output but the corners of the box aren't rounded in the eps file (they are rounded on the screen).
So, my questions are:
Am I on the right track as far as generating printable output or should I be using an alternative technique?
Why does the first example crash with doesnotUnderstand #pageBBox?
Why does the second example almost work but does not render the rounded corners?
Thanks
Kevin
It's not just Squeak - producing printable output is fearsomely difficult in any programming language. Whenever I've done project planning and people mention reports, I immediatel double (at least) the project estimates. Personally, I would recommend writing the data to a file in some well-known format such as XML or CSV and then use a report-writing package to produce the actual reports.
Sorry not to be more helpful!

Resources