Tucsky / aggr

Cryptocurrency trades aggregator
https://charts.aggr.trade/
GNU General Public License v3.0
801 stars 232 forks source link

Feature/custom script options #364

Closed Tucsky closed 10 months ago

Tucsky commented 10 months ago

Until now the script options were detected from the script itself, if you write options.anything, anything will be visible in the indicator settings, and will show differently depending on it's value and name

Defining options with the new option() function will allow choose the appropriate input type, set default value, help text and more.

Basic example


// text input
somevalue = option(type=text)
console.log(somevalue) // undefined, unless you set it yourself in the indicator settings

// input with default value, label and placeholder
safevalue = option(
  type=text, // type of input
  default=123, // default value
  label="Safe value", // text above input
  placeholder="Some help text" // text within empty input 
)
console.log(safevalue) // 123

The xxx = option(...) code is removed from the script during the build. It's not a variable per definition. You shouldn't try to change it during execution In above example, I can access the option value by using both options.safevalue and safevalue. it all become options.safevalue during execution anyway.

Number input

threshold = option(type=number, min=0, max=10, step=0.1)

Range input

// regular scale
threshold = option(type=range, min=0, max=10, step=0.1)

// log scale
threshold = option(type=range, min=0, max=1000, step=0.1, log=true)

List input

// regular list
quote = option(
  type=list,
  options=[null, "USD", "USDT", "TUSD", "USDC", "BUSD"],
  rebuild=true // not specifc to list but will trigger a full indi rebuild when change
)

// custom list item labels
quote = option(
  type=list,
  options={
    "": "Pick something",
    "USD": "United State Dollar",
    "USDT": "Tether",
    "TUSD": "TrueUSD",
    "USDC": "Coinbase USD",
    "BUSD": "Binance USD"
  },
  default=USD
)

// same as list but with predefined exchanges options
exchange = option(type=exchange, rebuild=true)

options must be valid JSON format all option values must be wrapped in double-quotes "

Color input

// regular color with default value
color = option(type=color,default="red")

// other color format are allowed
color = option(type=color,default="rgba(0, 255, 0, 0.5)")

missing a , in the color code, a " in the list options, WILL break the whole chart without notice.

Usage with source() function

type = option(type=list, options=[null, "spot", "perp"])
src = source(quote=quote)
candlestick(avg_ohlc(src))