Tucsky / aggr

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

[FEATURE] Built-in variables to fetch/filter available sources (Exchange, Type, Currency) #321

Open nl8-gh opened 1 year ago

nl8-gh commented 1 year ago

Ask | Request: Seeking built-in variable(s) to get / filter sources from within an indicator --> 'Exchange(s)' | 'Type' | 'Currency'

Context: For custom indicators I want to do some math on an e.g. USD Perp for BTC on every supported Exchange. Rather adding them manually / hardcode related sources, a built-in var allows better flexibillity & more.

Benefit: a) Kinda "optimistic" & unified User eXperience since less skilled noobs / users often aren't aware that they need to tweak things (especially on older scripts they might have found). b) developers can provide better defaults (or even, if supported, an option in settings to either use a custom selection only or "all available" (Exchanges | Types | Currencies)

lmvdz commented 1 year ago

maybe something like this?

script variables:

sources - for all sources.perp - for all sources with perp type sources.futures - for all sources with futures type sources.spot - for all sources with spot type sources.exchange.BINANCE - for all sources from BINANCE exchange sources.filter(source => source.exchange === 'BINANCE' && source.type === 'PERP') - maybe for customization

or just have a filter tab where you can select which sources similar to how the sources search works

nl8-gh commented 1 year ago

Thats a great starter! I even didn't think about how to actually implement it.

Following is a brain dump of my wet dreams. Please expand the below & sorry for the text wall.

Setting the stage | exists | definition | result | comments |:-:|:-|:-|:- | ✅ |`exchanges` \ `enabledExchanges` | BINANCE, BYBIT, PHEMEX, ... | | ✅ |`baseSymbol` | BTC, ETH, ADA, XRP, ... | | ✅ |`quoteSymbol` | USD, USDT, USDC, ... | ✅ |`type` \ `pair` | | see [^*1] | ✅ |`diesabledExchanges` | Any, Other, Whatever | see [^*2] | ❌ |`activatedExchanges` | (internal function) `enabledExchanges reduced by diesabledExchanges` | see [^*3] | ❌ |`assetClass` | Crypto | see [^*4] | ❌ |`use_baseCurrency` |```true ? : na``` | see [^*5] | ❌ | `baseCurrency` | ``` use_baseCurrency ? : quoteSymbol ``` [^*1]: Consider to rename `type` to `assetType` and `pair` to `ticker` for better code readability/context by using commonly used definitions [^*2]: Allow setting override to specify one ore more Exchanges to exclude [^*3]: If an Exchange listed is not supported yet, ignore it. But this way the developer can ensure that, once the defined exchange(s) gets added, remains disabled by default - for whatever reason but at least to ensure the indicator remains working as designed. [^*4]: Get future proof so you can add `Stocks`, `CFD`, `Forex`, ... later [^*5]: Allow conversion of different quoteSymbols to a user defined baseCurrency (unless already done?!). Example: if enabled do convert BUSD, USDC (whatever is not matching the defined baseCurrency to e.g USD which would increase the accuracy of any related math (price, volume, ....). To take it to the max, an option to perform calculation to be done by either a) converting to the underlaying exchanges' USD value (to sstick with USD for this example) prior aggregting the numbers or b) convert to the baseSymbol (e.g. #BTC) , then aggregate the numbers from all selcted exchanges against the real global USD price)
Example json for overrides Could easily be extended to e.g. override chart settings amongst other stuff you might want developers allow to hook into by code. ```json { "Settings_Override": { "Exchange_Overrides": { "enabledExchange": [ "ALL || Default || Array (e.g. ['Binance_US', 'ANY', 'Other']" ], "disabledExchange": "ALL || Default || Array (e.g. ['Binance_US', 'ANY', 'Other'])" }, "AssetType_Overrides": { "Futures": false, "Perpetual": true, "Spot": true }, "Symbols": { "baseSymbol": "BTC", "quoteSymbol": [ "ALL || Array (e.g. 'USD', 'USDT', 'USDC')" ] }, "baseCurrency": { "enabled": true, "quoteSymbol": "USD" } } } ``` This way, its easy to refer to `enabledExchange`to get e.g. `activatedExchanges.Open`price
Tucsky commented 1 year ago

maybe something like this?

script variables:

Not like this because someone might want to filter on multiple exchange, or combine multiple filters (type + exchange)

What you want is a source() function taking parameter likesource({ type: 'perp', quote: 'USDT' }) source({ type: 'spot', exchange: 'BITFINEX' })

From there, in the script transpilation parse these function calls and store the filters somewhere then we are going to need a second step to the transpilation dedicated to transpile these calls into actual markets data path (bars.renderer.sources[...]) so that the script is not retranspiled in it's entierty each time we change the pane's sources

by default should result in the average of the ohlc based on the given filters but in a more advanced version of that implementation we could imagine a second parameter mathing one of the source field (close, high, vbuy etc) to aggregate into

spotcvd = source({type: 'spot'}, 'vbuy') - source({type: 'spot'}, 'vsell')

How useful would that be 😄

Tucsky commented 1 year ago

draw close of spot markets available on the pane

line(source(close, type='spot'))

draw candlestick of spot markets available

candlestick(avg_ohlc(source(type='spot')))

cvd per type

line(
  cum(source(vbuy, type=spot) - source(vsell, type=spot)),
  title=spot cvd,
  color=limegreen
)
line(
  cum(source(vbuy, type=perp) - source(vsell, type=perp)), 
  title=perp cvd,
  color=slateblue
)

draw heikinashi of BITFINEX spot markets only

candlestick(avg_heikinashi(source(exchange=BITFINEX, type=spot)))
Tucsky commented 1 year ago

@nl8-gh the feature is available on https://tucsky.github.io/aggr for testing

adeacetis commented 1 year ago

Just a reminder that when, or if, this is approved, documentation will need to be updated here: https://github.com/Tucsky/aggr/wiki/Introduction-to-scripting