Created
August 31, 2020 07:23
-
-
Save shussekaido/fc3c0d7da34922f9f3e8506a416f1ccf to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const fetchMachine = Machine({ | |
id: 'test', | |
initial: 'idle', | |
context: { | |
H: 0, | |
L: 0, | |
trend: 0, | |
direction: '', | |
}, | |
states: { | |
idle: { | |
exit: ['saveCandle', 'showInfo'], | |
on: { | |
CANDLE: [ | |
{ | |
target: 'trendingUp', | |
cond: 'isGoingUp', | |
actions: ['incUptrend'] | |
}, | |
{ | |
target: 'trendingDown', | |
cond: 'isGoingDown', | |
actions: ['incDowntrend'] | |
}, | |
{ | |
target: 'idle', | |
actions: ['resetTrend'] | |
}, | |
] | |
} | |
}, | |
trendingUp: { | |
on: { | |
'': [ | |
{ target: 'trend', cond: 'triedEnough' }, | |
{ target: 'idle' } | |
] | |
} | |
}, | |
trendingDown: { | |
on: { | |
'': [ | |
{ target: 'trend', cond: 'triedEnough' }, | |
{ target: 'idle' } | |
] | |
} | |
}, | |
trend: { | |
entry: ['emitTrend'], // emit start of trend | |
exit: ['emitTrend', 'showInfo'], // should be 'STOP' after the 'resetTrend' action | |
on: { | |
CANDLE: [ | |
{ | |
target: 'idle', | |
cond: 'isNotSameEvent', | |
actions: ['resetTrend', 'saveCandle'], | |
}, | |
{ | |
// actions: ['saveCandle', 'emitTrend'], // re-emit the trend signal each time (in case it was missed by trade machine) | |
actions: ['saveCandle'], | |
} | |
] | |
} | |
}, | |
} | |
}, | |
{ | |
actions: { | |
saveCandle: assign( | |
{ | |
H: (ctx, event) => event.H, | |
L: (ctx, event) => event.L | |
} | |
), | |
incUptrend: assign( | |
{ | |
trend: ctx => { | |
if (ctx.direction === 'bullish') { | |
return ctx.trend + 1 | |
} else { | |
return 1 | |
} | |
}, | |
direction: 'bullish' | |
} | |
), | |
incDowntrend: assign( | |
{ | |
trend: ctx => { | |
if (ctx.direction === 'bearish') { | |
return ctx.trend + 1 | |
} else { | |
return 1 | |
} | |
}, | |
direction: 'bearish' | |
} | |
), | |
resetTrend: assign( | |
{ | |
trend: (ctx, event) => { | |
if ((event.H > ctx.H && event.L > ctx.L) || (event.H < ctx.H && event.L < ctx.L)) { | |
return 1 | |
} else { | |
return 0 | |
} | |
}, | |
direction: (ctx, event) => { | |
if (event.H > ctx.H && event.L > ctx.L) { | |
return 'bullish' | |
} else if (event.H < ctx.H && event.L < ctx.L) { | |
return 'bearish' | |
} else { | |
return 'STOP' | |
} | |
} | |
} | |
), | |
emitTrend: (ctx, event) => { | |
console.log(`* * * TREND: ${ctx.direction}`) | |
}, | |
showInfo: (context, event) => { | |
console.log(`🚦[THREECANDLE FSM] event: ${event.type} H:${event.H} L:${event.L}`) | |
}, | |
}, | |
guards: { | |
triedEnough: ctx => (ctx.trend > 2), | |
isNotDowntrend: ctx => (ctx.direction !== 'bearish'), | |
isNotUptrend: ctx => (ctx.direction !== 'bullish'), | |
isGoingUp: (ctx, event) => { | |
return ctx.H && ctx.L && event.H > ctx.H && event.L > ctx.L | |
}, | |
isGoingDown: (ctx, event) => { | |
return ctx.H && ctx.L && event.H < ctx.H && event.L < ctx.L | |
}, | |
isNotSameEvent: (ctx, event) => { | |
return ((ctx.direction === 'bullish' && event.H > ctx.H && event.L > ctx.L) || (ctx.direction === 'bearish' && event.H < ctx.H && event.L < ctx.L)) ? false : true | |
}, | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment