Type/to search
Built-in Functions
Global
Log
Log
LogStatus
LogProfit
LogProfitReset
LogReset
LogVacuum
EnableLog
Chart
KLineChart
console.log
console.error
exchange.Log
Market
Trade
Account
Futures
Exchange
IO
Network
Storage
Threads
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

This function is used to perform custom drawing while a strategy is running, using a drawing approach similar to the Pine language.

KLineChart(options)

Examples

  • Draw the K-lines with a close-price line overlaid.

    javascript
    function main() { var c = KLineChart({overlay: true}) var records = exchange.GetRecords() if (!records) return for (var bar of records) { c.begin(bar) // each bar starts with begin c.plot(bar.Close, "Close") c.close() // and ends with close } }
    python
    def main(): c = KLineChart({"overlay": True}) records = exchange.GetRecords() if not records: return for bar in records: c.begin(bar) # each bar starts with begin c.plot(bar["Close"], "Close") c.close() # and ends with close
    rust
    fn main() { let mut c = KLineChart::new(r#"{"overlay": true}"#); let Ok(records) = exchange.GetRecords(None, None, None) else { return }; for bar in &records { c.begin(bar); // each bar starts with begin c.plot(bar.Close, r#"{"title": "Close"}"#); c.close(); // and ends with close } }
  • If you need to draw on the strategy's custom chart area, you must first create a chart control object, which can be created using the KLineChart() function. The argument of the KLineChart() function is a chart configuration structure. The chart configuration structure used in the reference code is very 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 falsy value (for example false), then all the chart content will be output on the sub-chart; if you need to specify that a certain drawing function draws on the main chart, you can also specify the argument overlay as a truthy value (for example true) in the specific function call.

    javascript
    function main() { // Call the KLineChart function to create the chart control object c let c = KLineChart({ overlay: true }) // Test with a spot exchange object to obtain K-line data. If testing with a futures exchange // object, you need to set the contract first let bars = exchange.GetRecords() if (!bars) { return } // Iterate over the K-line data to perform drawing operations. Each drawing operation must start // with a ```c.begin(bar)``` function call and end with a ```c.close(bar)``` 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(bar) }) }
    python
    def main(): # Call the KLineChart function to create the chart control object c c = KLineChart({ "overlay": True }) # Test with a spot exchange object to obtain K-line data. If testing with a futures exchange # object, you need to set 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(bar)
    rust
    fn main() { // Call KLineChart::new to create the chart control object c let mut c = KLineChart::new(r#"{"overlay": true}"#); // Test with a spot exchange object to obtain K-line data. If testing with a futures exchange // object, you need to set the contract first let bars = exchange.GetRecords(None, None, None).unwrap(); // Iterate over the K-line data to perform drawing operations. Each drawing operation must start // with a c.begin(bar) function call and end with a c.close() function call. for bar in &bars { c.begin(bar); c.barcolor(if bar.Close > bar.Open { "rgba(255, 0, 0, 0.2)" } else { "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, r#"{"title": "high"}"#); let l = c.plot(bar.Low, r#"{"title": "low"}"#); c.fill(h, l, if bar.Close > bar.Open { r#"{"color": "rgba(255, 0, 0, 0.2)"}"# } else { r#"{"color": "rgba(255, 0, 0, 0.2)"}"# }); c.hline(bar.High, "{}"); c.plotarrow(bar.Close - bar.Open, "{}"); c.plotshape(bar.Low > 0.0, r#"{"style": "diamond"}"#); c.plotchar(bar.Close > 0.0, r#"{"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, "long"); } else if bar.Close < bar.Open { c.signal("closelong", bar.Low, 1.5, "closelong"); } c.close(); } }
  • Use the pricePrecision and volumePrecision parameters to control the display precision of the chart data. You can set the display precision for price and volume according to your actual needs. For example, for instruments with large price fluctuations, you can set the precision to 0 to display integers; for instruments with finer price granularity, you can set it to 2 or a higher precision.

    javascript
    function main() { // Create the chart control object, setting both price precision and volume precision to 0 // (i.e., display integers) let c = KLineChart({ overlay: true, pricePrecision: 0, // Price data precision; set to 2 to keep 2 decimal places volumePrecision: 0 // Volume data precision }) // Select the appropriate trading pair based on the exchange type let symbol = exchange.GetName().includes("Futures_") ? "ETH_USDT.swap" : "ETH_USDT" Log("Test symbol:", symbol) // Get K-line data let bars = exchange.GetRecords(symbol) if (!bars) { return } // Iterate over the K-line data and draw the chart 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)') c.plot(bar.High, 'high') c.plot(bar.Low, 'low') c.close(bar) }) }
    python
    def main(): # Create the chart control object, setting both price precision and volume precision to 0 (i.e., # display integers) c = KLineChart({ "overlay": True, "pricePrecision": 0, # Price data precision; set to 2 to keep 2 decimal places "volumePrecision": 0 # Volume data precision }) # Select the appropriate trading pair based on the exchange type exName = exchange.GetName() symbol = "ETH_USDT.swap" if "Futures_" in exName else "ETH_USDT" Log("Test symbol:", symbol) # Get K-line data bars = exchange.GetRecords(symbol) if not bars: return # Iterate over the K-line data and draw the chart 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)') c.plot(bar.High, 'high') c.plot(bar.Low, 'low') c.close(bar)
    rust
    fn main() { // Create the chart control object, setting both price precision and volume precision to 0 // (i.e., display integers) // pricePrecision is the price data precision; set to 2 to keep 2 decimal places; // volumePrecision is the volume data precision let mut c = KLineChart::new(r#"{"overlay": true, "pricePrecision": 0, "volumePrecision": 0}"#); // Select the appropriate trading pair based on the exchange type let symbol = if exchange.GetName().contains("Futures_") { "ETH_USDT.swap" } else { "ETH_USDT" }; Log!("Test symbol:", symbol); // Get K-line data let bars = exchange.GetRecords(symbol, None, None).unwrap(); // Iterate over the K-line data and draw the chart for bar in &bars { c.begin(bar); c.barcolor(if bar.Close > bar.Open { "rgba(255, 0, 0, 0.2)" } else { "rgba(0, 0, 0, 0.2)" }, "{}"); c.plot(bar.High, r#"{"title": "high"}"#); c.plot(bar.Low, r#"{"title": "low"}"#); c.close(); } }

Returns

TypeDescription

object

A chart object; the parameters of its drawing methods (barcolor, bgcolor, plot, fill, hline, plotarrow, plotshape, plotchar, plotcandle, signal, reset) are described in KLineChart-return. When drawing over K-line data, the drawing calls for each bar must start with begin(bar) and end with close(bar).

Arguments

NameTypeRequiredDescription

options

object / object array

Yes

Chart configuration object: overlay draws on the main chart when true and on a sub-chart when false; pricePrecision and volumePrecision set the decimals shown for prices and volumes (0 rounds to integers). See KLineChart-options for other fields.

See Also

Remarks

For custom drawing in a strategy, you can only choose one of the two methods: the KLineChart() function or the Chart() function. For settings such as colors and styles involved when calling the KLineChart() function, please refer to the topic article on drawing with the KLineChart function.

The pricePrecision and volumePrecision parameters are used to control the display precision of data in the chart. When these parameters are not set, the chart displays data using the default precision. After the precision parameters are set, the price and volume data in the chart will be rounded and displayed according to the specified number of decimal places, which helps simplify the chart display and improve readability.