Proposal for runtime snapshot saving / restoring
BusinessDuck opened this issue · 5 comments
Requirements:
Debut strategy instance should have a method for getting current runtime state e.g. plugins state calculated at work and current orders.
Plugins and Types
New hooks PluginHook.onSnapshot
and PluginHook.onRestore
, must return plugin runtime calculated value for savings
Plugin Driver implementation example
public async getSnapshot() {
const snapshot: unknown = {};
for (const plugin of this.plugins) {
if (PluginHook.onSnapshot in plugin) {
snapshot[plugin.name] = plugin.onSnapshot();
}
}
return Object.freeze(snapshot);
}
public async restoreFromSnapshot(snapshot: Record<string, unknown) {
for (const plugin of this.plugins) {
if (PluginHook.onRestore in plugin && plugin.name in snapshot) {
plugin.onRestore(snapshot[plugin.name]);
}
}
}
Core implementations example
public async createSnapshot() {
const pluginsSnapshot = await this.pluginDriver.asyncReduce(PluginHook.onSnapshot);
return { orders: this.orders, marketTick: this.marketTick, plugins: this.pluginDriver.getSnapshot() };
}
public async restoreFromSnapshot(snapshot: Record<string, unknown>) {
return this.pluginDriver.restoreFromSnapshot(snapshot);
}
Strategy implementations example
class MyStrategy extends Debut {
constructor(...) {
// Restore saved snapshot
const snapshot = fs.readFileSync('./snapshot.json', 'utf-8');
if (snapshot) {
this.restoreFromSnapshot(JSON.parse(snapshot)):
}
// Scheduled runtime snapshot savings
setInterval(this._saveSnapshot, 1000);
}
private _saveSnapshot() {
const snapshot = this.createSnapshot();
fs.saveFileSync('./snapshot.json', snapshot);
}
}
this.createSnapshot() must accept object with strategy parameters that need to be saved
i think that we must create folder with structure like saves > broker > ticker
saving data in one file can cause some problems, including file reading speed
we can save our strategy options in json and change it during runtime
we will run into problems trying to change options in plugins like grid
we can leave this feature for later releases
i think restoring data in constructor can cause problems with plugins
so we probably need to restore it after learning stage
getSnapshot
and hydrateSnapshot
added