Tucsky / aggr

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

Add custom script options #365

Closed Tucsky closed 9 months ago

Tucsky commented 10 months ago

Until now the script options were detected from the script itself, if you write options.anything, an option anything will magicaly appear in the indicator's 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.

export enum ALLOWED_OPTION_TYPES {
  text,
  number,
  range,
  list,
  lineType,
  lineStyle,
  color,
  checkbox,
  exchange
}

Basic example

image
// default type is number
MyNumber = option(
  default=123, // default value
  label="Number value", // text above input
  placeholder="Type something" // text within empty input 
)
console.log(MyNumber) // 123

// text input
MyText = option(
  type=text,
  label="Text value",
  placeholder="Fill me",
  description="Small description to help understand the option"
)
console.log(MyNumber) // ""

[!NOTE] 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 becomes options.safevalue during execution anyway.

// number input with `min`, `max` and `step` attribute
threshold = option(type=number, min=0, max=10, step=0.1)

Range input

image
smallrange = option(
  type=range,
  label="Small range",
  min=0,
  max=1
)

bigrange = option(
  type=range,
  label="Big range",
  gradient=["red", "limegreen"], // colorize slider
  min=0,
  max=1000000,
  log=true // slider will ajust displayed value logarithmic scale
)

List input

image
// 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)

[!WARNING] 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)")

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

Usage with source() function

// let user choose spot, perp or both in indicator's settings
type = option(type=list, options=[null, "spot", "perp"])

// get list of markets matching given type 
src = source(type=type)

// aggregate ohlc from markets & print into candlestick serie
candlestick(avg_ohlc(src))
adeacetis commented 10 months ago

Assuming in the doc's last section is an executable example, I don't see where type is passed to either source or line:

type = option(type=list, options=[null, "spot", "perp"])
src = source(quote=quote)
candlestick(avg_ohlc(src))
Tucsky commented 10 months ago

Assuming in the doc's last section is an executable example, I don't see where type is passed to either source or line:

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

You are absolutely right. Thank you. Fixed now.

Tucsky commented 9 months ago

Current implementation might suggest this is a possible usage, while it currently doesn't work like that

option1 = option()
option2 = option() * option1
adeacetis commented 9 months ago

Current implementation might suggest this is a possible usage, while it currently doesn't work like that

option1 = option()
option2 = option() * option1

Where it does suggest it ? I am not sure I understand what it means.