Skip to content

Instantly share code, notes, and snippets.

@larrybotha
Last active September 30, 2019 03:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save larrybotha/b81011555f5e01f13fdc249255ebc7c0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const tradingStrategyMachine = Machine({
id: 'three-step-trading-process',
initial: 'backtesting',
context: {
timesRefactored: 0,
},
states: {
backtesting: {
on: {
CONSISTENT_GAINS: 'paperTrading',
CONSISTENT_LOSSES: [
{
cond: 'withinMaxRefactors',
target: 'refactoring',
},
{
target: 'abandoned',
}
],
}
},
refactoring: {
exit: 'incrementRefactors',
on: {
REFACTORED: 'backtesting',
}
},
paperTrading: {
entry: 'resetRefactorCount',
on: {
CONSISTENT_GAINS: 'cautiousTrading',
CONSISTENT_LOSSES: 'refactoring',
}
},
cautiousTrading: {
on: {
CONSISTENT_GAINS: 'activeTrading',
CONSISTENT_LOSSES: 'paperTrading',
}
},
activeTrading: {
type: 'final',
},
abandoned: {
type: 'final',
},
}
},
{
guards: {
withinMaxRefactors: ({timesRefactored}) => timesRefactored < 3,
},
actions: {
incrementRefactors: assign(context => {
const {timesRefactored} = context;
return {
...context,
timesRefactored: timesRefactored + 1,
};
}),
resetRefactorCount: assign(context => ({...context, timesRefactored: 0}))
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment