The function is used for custom drawing at strategy runtime using a drawing method similar to the Pine
language.
The chart object. The KLineChart()
function returns a chart object with several methods, among which you need to pay attention to begin()
and close()
. The drawing operation must start with a begin()
function call and end with a close()
function call when traversing over the KLine data to perform the drawing operation.
object
KLineChart(options)
The options
parameter is the chart configuration.
options
true
object, object array
function main() {
// Call the KLineChart function to create a chart control object c
let c = KLineChart({
overlay: true
})
// Use the Spot Exchange object test to get K-line data. If you use the futures exchange object test, you need to set up the contract first.
let bars = exchange.GetRecords()
if (!bars) {
return
}
// Execute the drawing operation by traversing over the K-line data. The drawing operation must start with the ```c.begin(bar)``` function call and end with the ```c.close()``` function call.
bars.forEach(function(bar, index) {
c.begin(bar)
c.barcolor(bar.Close > bar.Open ? 'rgba(255, 0, 0, 0.2)' : 'rgba(0, 0, 0, 0.2)')
if (bar.Close > bar.Open) {
c.bgcolor('rgba(0, 255, 0, 0.5)')
}
let h = c.plot(bar.High, 'high')
let l = c.plot(bar.Low, 'low')
c.fill(h, l, {
color: bar.Close > bar.Open ? 'rgba(255, 0, 0, 0.2)' : 'rgba(255, 0, 0, 0.2)'
})
c.hline(bar.High)
c.plotarrow(bar.Close - bar.Open)
c.plotshape(bar.Low, {
style: 'diamond'
})
c.plotchar(bar.Close, {
char: 'X'
})
c.plotcandle(bar.Open*0.9, bar.High*0.9, bar.Low*0.9, bar.Close*0.9)
if (bar.Close > bar.Open) {
// long/short/closelong/closeshort
c.signal("long", bar.High, 1.5)
} else if (bar.Close < bar.Open) {
c.signal("closelong", bar.Low, 1.5)
}
c.close()
})
}
def main():
# Call the KLineChart function to create a chart control object c
c = KLineChart({
"overlay": True
})
# Use the Spot Exchange object test to get K-line data. If you use the futures exchange object test, you need to set up the contract first.
bars = exchange.GetRecords()
if not bars:
return
for bar in bars:
c.begin(bar)
c.barcolor('rgba(255, 0, 0, 0.2)' if bar.Close > bar.Open else 'rgba(0, 0, 0, 0.2)')
if bar.Close > bar.Open:
c.bgcolor('rgba(0, 255, 0, 0.5)')
h = c.plot(bar.High, 'high')
l = c.plot(bar.Low, 'low')
c.fill(h, l, 'rgba(255, 0, 0, 0.2)' if bar.Close > bar.Open else 'rgba(255, 0, 0, 0.2)')
c.hline(bar.High)
c.plotarrow(bar.Close - bar.Open)
c.plotshape(bar.Low, style = 'diamond')
c.plotchar(bar.Close, char = 'X')
c.plotcandle(bar.Open*0.9, bar.High*0.9, bar.Low*0.9, bar.Close*0.9)
if bar.Close > bar.Open:
# long/short/closelong/closeshort
c.signal("long", bar.High, 1.5)
elif bar.Close < bar.Open:
c.signal("closelong", bar.Low, 1.5)
c.close()
// Not supported for now
If a chart control object is necessary to draw in the strategy custom drawing area, use the KLineChart()
function to create the object. The parameter to the KLineChart()
function is a chart configuration structure, the one used in the reference code is simple: {overlay: true}
. This chart configuration structure only sets the drawing content to be output on the main chart. If overlay
is set to a false value, e.g. false
, the content on the chart is output on the secondary chart. If you need to specify a drawing function to draw on the main chart, you can also specify the parameter overlay
as a true value in the specific function call, for example: true
.
c.barcolor(bar.Close > bar.Open ? 'rgba(255, 0, 0, 0.2)' : 'rgba(0, 0, 0, 0.2)') // Use the example illustrated in the reference code in this example, without further ado
c.barcolor('rgba(255, 0, 0, 0.2)' if bar.Close > bar.Open else 'rgba(0, 0, 0, 0.2)')
// Not supported for now
The drawing interface functions of the Pine
language supported in the drawing operation are:
barcolor
, which sets the K-line color.
barcolor(color, offset, editable, show_last, title, display) display parameters are optional: “none”, “all”
c.bgcolor('rgba(0, 255, 0, 0.5)')
c.bgcolor('rgba(0, 255, 0, 0.5)')
// Not supported for now
bgcolor
, fills the background of the K-line with the specified color.
bgcolor(color, offset, editable, show_last, title, display, overlay) display parameters are optional: “none”, “all”
c.plot(bar.High, 'high')
c.plot(bar.Open < bar.Close ? NaN : bar.Close, "Close", {style: "linebr"}) // Support for drawing discontinuous data lines
h = c.plot(bar.High, 'high')
h = c.plot(None if bar.Open < bar.Close else bar.Close, "Close", style = "linebr") # Support for drawing discontinuous data lines
// Not supported for now
plot
, plot a series of data on a chart.
plot(series, title, color, linewidth, style, trackprice, histbase, offset, join, editable, show_last, display) style parameters are optional: “stepline_diamond”, “stepline”, “cross”, “areabr”, “area”, “circles”, “columns”, “histogram”, “linebr”, “line” display parameters are optional: “none”, “all”
let h = c.plot(bar.High, 'high')
let l = c.plot(bar.Low, 'low')
c.fill(h, l, {color: bar.Close > bar.Open ? 'rgba(255, 0, 0, 0.2)' : 'rgba(255, 0, 0, 0.2)'})
h = c.plot(bar.High, 'high')
l = c.plot(bar.Low, 'low')
c.fill(h, l, color = 'rgba(255, 0, 0, 0.2)' if bar.Close > bar.Open else 'rgba(255, 0, 0, 0.2)'})
// Not supported for now
fill
, fill the background between two plots or hline
with the provided colors.
fill(hline1, hline2, color, title, editable, fillgaps, display) display parameters are optional: “none”, “all”
Since the JavaScript
language cannot specify incoming parameters based on the names of function formal parameters, to solve this problem, you can use a {key: value}
structure to specify the parameters to be passed to a certain formal parameter name.
For example, the reference code uses {color: bar.Close > bar.Open ? 'rgba(255, 0, 0, 0, 0.2)' : 'rgba(255, 0, 0, 0, 0.2)'}
specifies the color
parameter of the fill
function.
If you need to specify multiple parameters with consecutive form parameter names, you can use {key1: value1, key2: value2, key3: value3}
.
For example, in this example, an additional title
parameter is specified: {color: bar.Close > bar.Open ? 'rgba(255, 0, 0, 0, 0.2)' : 'rgba(255, 0, 0, 0, 0.2)', title: 'fill'}
.
For the color value, you can set it with 'rgba(255, 0, 0, 0, 0.2)'
or with '#FF0000'
.
c.hline(bar.High)
c.hline(bar.High)
// Not supported for now
hline
, the horizontal line is presented at a given fixed price level.
hline(price, title, color, linestyle, linewidth, editable, display) linestyle parameters are optional: “dashed”, “dotted”, “solid” display parameters are optional: “none”, “all”
c.plotarrow(bar.Close - bar.Open)
c.plotarrow(bar.Close - bar.Open)
// Not supported for now
plotarrow
, plot up and down arrows on the chart.
plotarrow(series, title, colorup, colordown, offset, minheight, maxheight, editable, show_last, display) display parameters are optional: “none”, “all”
c.plotshape(bar.Low, {style: 'diamond'})
c.plotshape(bar.Low, style = 'diamond')
// Not supported for now
plotshape
, draw visual shapes on the chart.
plotshape(series, title, style, location, color, offset, text, textcolor, editable, size, show_last, display) style parameters are optional: “diamond”, “square”, “label_down”, “label_up”, “arrow_down”, “arrow_up”, “circle”, “flag”, “triangle_down”, “triangle_up”, “cross”, “xcross” location parameters are optional: “abovebar”, “belowbar”, “top”, “bottom”, “absolute” size parameters are optional: “10px”, “14px”, “20px”, “40px”, “80px”, comparing size.tiny, size.small, size.normal, size.large, size.huge in Pine language. size.auto is size.small. display parameters are optional: “none”, “all”
c.plotchar(bar.Close, {char: 'X'})
c.plotchar(bar.Close, char = 'X')
// Not supported for now
plotchar
, draw visual shapes on the chart using any given Unicode character.
plotchar(series, title, char, location, color, offset, text, textcolor, editable, size, show_last, display) location parameters are optional: “abovebar”, “belowbar”, “top”, “bottom”, “absolute” size parameters are optional: “10px”, “14px”, “20px”, “40px”, “80px”, comparing size.tiny, size.small, size.normal, size.large, size.huge in Pine language. size.auto is size.small. display parameters are optional: “none”, “all”
c.plotcandle(bar.Open*0.9, bar.High*0.9, bar.Low*0.9, bar.Close*0.9)
c.plotcandle(bar.Open*0.9, bar.High*0.9, bar.Low*0.9, bar.Close*0.9)
// Not supported for now
plotcandle
, plot a K-line chart on a chart.
plotcandle(open, high, low, close, title, color, wickcolor, editable, show_last, bordercolor, display) display parameters are optional: “none”, “all”
c.signal("long", bar.High, 1.5)
c.signal("long", bar.High, 1.5)
// Not supported for now
signal
, a function not available on the Pine language, is used to draw buy and sell signals here.
signal(direction, price, qty, id) The parameter ‘‘long’’ is passed in to indicate the direction of the transaction, you can choose ‘‘long’’, ‘‘closelong’’, ‘‘short’’, ‘‘closeshort’’. The parameter
bar.High
is the Y-axis position of the marker signal. The passed parameter 1.5 indicates the number of transactions of the signal. The fourth parameter can be passed to replace the default text content drawn, and the default text of the drawn signal marker is the direction of the transaction, e.g. ‘‘closelong’’.
c.reset()
c.reset()
// Not supported for now
reset
, a function not available on the Pine language, is used to empty the chart data.
reset(remain) The
reset()
method can take one parameter,remain
, to specify the number of data to keep. Not passingremain
means clearing all data.
Strategy custom drawing can only use one of the ways of KLineChart()
function or Chart()
function. For some color and style settings used in the KLineChart()
function call, please refer to the Use the KLineChart function to make strategy drawing design easier.
{@fun/Log/Chart Chart}
Chart LogReset