Skip to content

Commit 3dd9b88

Browse files
committed
Scripts: add ts _id system
1 parent 66694f1 commit 3dd9b88

File tree

8 files changed

+169
-39
lines changed

8 files changed

+169
-39
lines changed

.babelrc

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
22
"presets": ["@babel/env"],
3-
"plugins": ["@babel/plugin-transform-runtime"]
3+
"plugins": [
4+
"@babel/plugin-transform-runtime",
5+
[
6+
"@babel/plugin-transform-parameters",
7+
{ "loose": true }
8+
]
9+
]
410
}

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"devDependencies": {
4141
"@ava/babel": "^1.0.1",
4242
"@babel/core": "^7.10.5",
43+
"@babel/plugin-transform-parameters": "^7.10.5",
4344
"@babel/plugin-transform-runtime": "^7.10.5",
4445
"@babel/preset-env": "^7.10.4",
4546
"ava": "^3.10.1",

src/helpers/script_engine.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ScriptEngine {
2626
s.src.upd_src = this.get_raw_src(s.src.update)
2727
}
2828

29-
this.env = new ScriptEnv(s.src, {
29+
this.env = new ScriptEnv(s, {
3030
open: this.open,
3131
high: this.high,
3232
low: this.low,

src/helpers/script_env.js

+83-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66

77
import ScriptStd from './script_std.js'
88

9+
const FDEFS = /(function |)([$A-Z_][0-9A-Z_$\.]*)[\s]*?\((.*?)\)/gmi
10+
911
export default class ScriptEnv {
1012

11-
constructor(src, data) {
13+
constructor(s, data) {
1214

1315
this.std = new ScriptStd(this)
16+
this.src = s
1417
this.output = []
1518
this.data = []
1619
this.tss = {}
17-
this.output.box_maker = this.make_box(src)
20+
this.output.box_maker = this.make_box(s.src)
1821
this.output.box_maker(this, data)
1922
delete this.output.box_maker
2023

@@ -35,6 +38,7 @@ export default class ScriptEnv {
3538
for (var k in src.props) {
3639
props += `var ${k} = ${src.props[k].def}\n`
3740
}
41+
// TODO: add argument values to _id
3842

3943
return Function('self,tsdata', `
4044
'use strict';
@@ -49,7 +53,7 @@ export default class ScriptEnv {
4953
const close = tsdata.close
5054
const vol = tsdata.vol
5155
52-
// Dierct data ts
56+
// Direct data ts
5357
const data = self.data
5458
const ohlcv = tsdata.ohlcv
5559
@@ -60,10 +64,84 @@ export default class ScriptEnv {
6064
${src.init_src}
6165
}
6266
63-
this.update = () => {
64-
${src.upd_src}
67+
this.update = (_id = 'root') => {
68+
${this.prep(src.upd_src)}
6569
}
6670
`)
6771
}
6872

73+
// Preprocess the update function.
74+
// Replace functions with the full arguments list +
75+
// generate & add tsid
76+
prep(src) {
77+
78+
//console.log('Before -----> \n', src)
79+
80+
let h = this.src.use_for[0] // TODO: add props here
81+
src = '\t\t let _pref = `${_id}<-'+h+'<-`\n' + src
82+
83+
FDEFS.lastIndex = 0
84+
let call_id = 0 // Function call id (to make each call unique)
85+
86+
do {
87+
var m = FDEFS.exec(src)
88+
if (m) {
89+
let fkeyword = m[1].trim()
90+
let fname = m[2]
91+
let fargs = m[3]
92+
93+
if (fkeyword === 'function') {
94+
// TODO: addinf _ids to inline functions
95+
} else {
96+
if (this.std[fname]) {
97+
src = this.postfix(src, m, ++call_id)
98+
}
99+
}
100+
}
101+
} while (m)
102+
103+
//console.log('After ----->\n', src)
104+
105+
return src
106+
}
107+
108+
// Postfix function calls with ts _ids
109+
postfix(src, m, call_id) {
110+
111+
let target = this.get_args(this.fdef(m[2])).length
112+
let args = this.get_args(m[0])
113+
114+
for (var i = args.length; i < target; i++) {
115+
args.push('void 0')
116+
}
117+
118+
// Add an unique time-series id
119+
args.push(`_pref+"f${call_id}"`)
120+
121+
return src.replace(m[0], `${m[2]}(${args.join(', ')})`)
122+
123+
}
124+
125+
// Get the function definition
126+
// TODO: parse path names like 'this.module1.method(1,2,3)'
127+
fdef(fname) {
128+
return this.std[fname].toString()
129+
}
130+
131+
// Get args in the function's definition
132+
get_args(src) {
133+
let reg = this.regex_clone(FDEFS)
134+
reg.lastIndex = 0
135+
136+
let m = reg.exec(src)
137+
if (!m[3].trim().length) return []
138+
let arr = m[3].split(',')
139+
.map(x => x.trim())
140+
.filter(x => x !== '_id')
141+
return arr
142+
}
143+
144+
regex_clone(rex) {
145+
return new RegExp(rex.source, rex.flags)
146+
}
69147
}

src/helpers/script_std.js

+38-24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export default class ScriptStd {
1111
this.SWMA = [1/6, 2/6, 2/6, 1/6]
1212
}
1313

14+
// Generate the next timeseries id
15+
_tsid(prev, next) {
16+
return `${prev}<-${next}`
17+
}
18+
1419
// Wait for a value !== undefined
1520
nw(x) {
1621
if (x === undefined || x !== x) {
@@ -45,23 +50,17 @@ export default class ScriptStd {
4550

4651
// Creates a new time-series & records each x.
4752
// Return the an array. Id is auto-genrated
48-
ts(x, id) {
49-
let ts = this.env.tss[id]
53+
ts(x, _id) {
54+
let ts = this.env.tss[_id]
5055
if (!ts) {
51-
ts = this.env.tss[id] = [x]
52-
ts.__id__ = id
56+
ts = this.env.tss[_id] = [x]
57+
ts.__id__ = _id
5358
} else {
5459
ts[0] = x
5560
}
5661
return ts
5762
}
5863

59-
// Generate the next timeseries id
60-
_tsid(args, next) {
61-
let prev = args[args.length - 1]
62-
return `${prev}<-${next}`
63-
}
64-
6564
abs(x) {
6665
return Math.abs(x)
6766
}
@@ -195,8 +194,14 @@ export default class ScriptStd {
195194
// TODO: this
196195
}
197196

198-
ema() {
199-
// TODO: this
197+
ema(src, len, _id) {
198+
let id = this._tsid(_id, `ema(${len})`)
199+
let a = 2 / (len + 1)
200+
let ema = this.ts(id, 0)
201+
ema[0] = this.na(ema[1]) ?
202+
this.sma(src, len, id)[0] :
203+
a * src[0] + (1 - a) * this.nz(ema[1])
204+
return ema
200205
}
201206

202207
exp(x) {
@@ -215,8 +220,8 @@ export default class ScriptStd {
215220
Math.floor(x)
216221
}
217222

218-
highest(src, len) {
219-
let id = this._tsid(arguments, `highest(${len})`)
223+
highest(src, len, _id) {
224+
let id = this._tsid(_id, `highest(${len})`)
220225
let high = -Infinity
221226
for (var i = 0; i < len; i++) {
222227
if (src[i] > high) high = src[i]
@@ -260,8 +265,8 @@ export default class ScriptStd {
260265
Math.log10(x)
261266
}
262267

263-
lowest(src, len) {
264-
let id = this._tsid(arguments, `lowest(${len})`)
268+
lowest(src, len, _id) {
269+
let id = this._tsid(_id, `lowest(${len})`)
265270
let low = Infinity
266271
for (var i = 0; i < len; i++) {
267272
if (src[i] < low) low = src[i]
@@ -372,8 +377,8 @@ export default class ScriptStd {
372377
return Math.sin(x)
373378
}
374379

375-
sma(src, len) {
376-
let id = this._tsid(arguments, `sma(${len})`)
380+
sma(src, len, _id) {
381+
let id = this._tsid(_id, `sma(${len})`)
377382
let sum = 0
378383
for (var i = 0; i < len; i++) {
379384
sum = sum + src[i]
@@ -393,8 +398,8 @@ export default class ScriptStd {
393398
// TODO: this
394399
}
395400

396-
sum(src, len) {
397-
let id = this._tsid(arguments, `sum(${len})`)
401+
sum(src, len, _id) {
402+
let id = this._tsid(_id, `sum(${len})`)
398403
let sum = 0
399404
for (var i = 0; i < len; i++) {
400405
sum = sum + src[i]
@@ -406,8 +411,8 @@ export default class ScriptStd {
406411
// TODO: this
407412
}
408413

409-
swma(src) {
410-
let id = this._tsid(arguments, `swma`)
414+
swma(src, _id) {
415+
let id = this._tsid(_id, `swma`)
411416
let sum = src[3] * this.SWMA[0] + src[2] * this.SWMA[1] +
412417
src[1] * this.SWMA[2] + src[0] * this.SWMA[3]
413418
return this.ts(sum, id)
@@ -454,8 +459,17 @@ export default class ScriptStd {
454459
// TODO: this
455460
}
456461

457-
wma(src, len) {
458-
// TODO: this
462+
wma(src, len, _id) {
463+
// TODO: not precise
464+
let id = this._tsid(_id, `wma(${len})`)
465+
let norm = 0
466+
let sum = 0
467+
for (var i = 0; i < len - 1; i++) {
468+
let w = (len - i) * len
469+
norm += w
470+
sum += src[i] * w
471+
}
472+
return this.ts(sum / norm, id)
459473
}
460474

461475
wpr(len) {

test/tests/Scripts.vue

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<trading-vue :data="chart" :width="this.width" :height="this.height"
33
:toolbar="true" :overlays="ov"
4+
ref="tv"
45
:color-back="colors.colorBack"
56
:color-grid="colors.colorGrid"
67
:color-text="colors.colorText">
@@ -31,6 +32,7 @@ export default {
3132
window.addEventListener('resize', this.onResize)
3233
this.onResize()
3334
window.dc = this.chart
35+
window.tv = this.$refs.tv
3436
},
3537
computed: {
3638
colors() {

test/tests/Scripts/ScriptOverlay.vue

+30-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,38 @@ export default {
3333
length: { def: 50, range: [2, 200, 1] }
3434
},
3535
init() {
36+
this.extf = function(a, b=500, flag = true) {
37+
return 1
38+
}
3639
},
3740
update(length) {
38-
return sma(swma(close, '123'), 10, '123')[0]
41+
42+
function regf(a = 7, b = 2000) {
43+
44+
}
45+
46+
let f = (a, b = 1000) => {
47+
return a + b
48+
}
49+
50+
let xxx = ts (1)
51+
52+
let yyy = ts (1)
53+
54+
let ya = this.extf(123)
55+
56+
let zzz = f(
57+
58+
1,
59+
2
60+
61+
)
62+
63+
stoch(1)
64+
65+
bb()
66+
67+
return wma(close, 10)[0]
3968
}
4069
}
4170
}

0 commit comments

Comments
 (0)