Public
Edited
Nov 5, 2022
Insert cell
# Trading System
Insert cell
data = [1, 2, 3]
Insert cell
Insert cell
Insert cell
Insert cell
function rule1Fn(ctx, params) {
const { $today, $symbol } = ctx
const { value } = params
return $today.close > value
}
Insert cell
rule1 = (params) => ({
name: "rule1",
description: 'Symbol close price is above value',
params,
fn: rule1Fn
})
Insert cell
Insert cell
function rule2Fn(ctx, params) {
const { $bars } = ctx

const { symbol, value, days } = params

if ($bars.length < 2 || !$bars[days]) {
return false
}
return ($bars[0].close - $bars[days].close) / $bars[days].close > value
}
Insert cell
rule2 = (params) => ({
name: "rule2",
description: 'Symbol cumulative return is above value%',
params,
fn: rule2Fn
})
Insert cell
entryTree = ({
value: rule1({ symbol: 'MSFT', value: 200 }),
type: 'decision',
yes: {
value: rule1({ symbol: 'SPY', value: 385 }), //rule2({ symbol: 'MSFT', days: 2, value: 0.01 }),
type: 'decision',
yes: {
value: { symbol: 'AAPL' },
type: 'open'
},
no: null
},
no: {
type: 'notification',
value: { msg: 'Send bla bla' }
}
})
Insert cell
exitTree = ({
})
Insert cell
function evaluateTree({ value, type, ...rest }, cb) {
switch(type) {
case 'decision': {
const { result } = cb(value, type)
if (result) {
return rest.yes ? evaluateTree(rest.yes, cb) : null
} else {
return rest.no ? evaluateTree(rest.no, cb) : null
}
}

case 'notification':
case 'open': {
return cb(value, type)
}
}
// const { result } = cb(rule)
}
Insert cell
# Data
Insert cell
system1 = ({
name: 'System 1',
capital: 10000,
risk: 0.05,
symbol: 'AAPL',
distribution: 'equal',
distributionFixed: {
AAPL: 0.25,
META: 0.5,
},
entryTree: entryTree,
exitTree: exitTree
})
Insert cell
Insert cell
SystemManager = {
function structureBars(bars) {
let result = {}
let window = -1;
for(let key in bars) {
result[key] = []
let arr = bars[key].concat().reverse()
for (let i = 0; i < arr.length; i++) {
result[key][i] = arr.slice(i)
}
if (window === -1) {
window = result[key].length
} else {
window = Math.min(window, result[key].length)
}
}
return {
symbols: Object.keys(result),
bars: result,
window,
}
}

function run(s, bars) {
const d = structureBars(bars)
console.log(d)

console.clear()
for (let i = d.window - 1; i >= 0; i--) {
const barsPerWindow = _.map(v => v[i], d.bars)
// {MSFT:[], SPY:[]}
const today = barsPerWindow['MSFT'][0].dateFormatted

const rulesEval = []
const openTrade = evaluateTree(s.entryTree, (value, type) => {
switch (type) {
case 'decision': {
const symbol = value.params.symbol

const barsForRule = barsPerWindow[symbol]
const ctx = RulesManager.createCtx(symbol, barsForRule)
const result = value.fn(ctx, value.params)

rulesEval.push([value.name, result])
return { result }
}

case 'open': {
console.log('open trade:', value)
return
}

default:
return
}
})

console.log(today)
console.info(...rulesEval)
if (openTrade && TradeManager.canOpenTrade()) {

//TradeManager.openTrade("AAPL"
// const trade = TradeManager.openTrade(ctx)
// console.log("open trade: ", trade)
}

console.log('\n')
}
return {}
}

return {
run,
structureBars
}
}
Insert cell
Insert cell
Insert cell
# Test
Insert cell
{
inputs: [a, b, c],
outpus: [],
fn: (symbol, bars) {
return inputs[0] + inputs[1]
}
}
Insert cell
SystemManager.run(system1, bars)
Insert cell
Insert cell
_ = require('ramda')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more