0bps

Strategy vault / Other

Variable Chart Example

Python strategy by FawkesPan.

PythonOther
Author
FawkesPan
Complexity
Intermediate · 181 lines
Parameters
0

The original spec has no written description. Read the source below.

Source Python

PS = ext.PersistentStorage()
ALPHA = 0.0008
class GraphManager:

    diffs_chart = {
        '__isStock': False,
        'extension': {
            'layout': 'single',
            'height': 300,
            'col': 6
        },
        'chart': {
            'zoomType': 'x'
        },
        'title': {
            'text': 'Deviation values of each currency price and index'
        },
        'subtitle': {
            'text': 'Alpha: %s' % ALPHA
        },
        'xAxis': {
            'type': 'datetime'
        },
        'series': []
    }
    index_chart = {
        '__isStock': False,
        'extension': {
            'layout': 'single',
            'height': 300,
            'col': 6
        },
        'chart': {
            'zoomType': 'x'
        },
        'title': {
            'text': 'Reference index value'
        },
        'xAxis': {
            'type': 'datetime'
        },
        'series': [{
            'type': 'line',
            'name': 'INDEX',
            'data': []
        }]
    }
    pos_chart = {
        '__isStock': False,
        'extension': {
            'layout': 'single',
            'height': 300,
            'col': 6
        },
        'chart': {
            'zoomType': 'x'
        },
        'title': {
            'text': 'Current holdings value of each currency'
        },
        'subtitle': {
            'text': 'Calculate in USDT'
        },
        'xAxis': {
            'type': 'datetime'
        },
        'series': []
    }
    upnl_chart = {
        '__isStock': False,
        'extension': {
            'layout': 'single',
            'height': 300,
            'col': 6
        },
        'chart': {
            'zoomType': 'x'
        },
        'title': {
            'text': 'Current unrealized profit and loss of each currency'
        },
        'subtitle': {
            'text': 'Calculate in USDT'
        },
        'xAxis': {
            'type': 'datetime'
        },
        'series': []
    }

    def __init__(self, reset: bool):
        self.indices = {}
        self.diffs_data = {}
        self.pos_data = {}
        self.upnl_data = {}
        self.index = 1
        self.not_manual_reset = True
        if PS['symbols'] is None or reset:
            echo('Chart Manager initialized for the first time')
            if reset:
                PS['symbols'] = None
                self.not_manual_reset = False

    def initialize(self, symbols):
        self.symbols = symbols
        self.create_chart()
        if self.symbols != PS['symbols']:
            if self.not_manual_reset:
                echo('Coins have changed, and the chart has been reinitialized')
            self.reset_graph()
        PS['symbols'] = self.symbols

    def reset_graph(self):
        self.chart.reset(0)

    def create_chart(self):
        i = 0
        for symbol in self.symbols:
            # DIFF Chart
            self.diffs_chart['series'].append({
                'type': 'line',
                'name': symbol,
                'data': []
            })
            self.indices[symbol+'_diff'] = i
            i+=1
        # INDEX Chart
        self.indices['INDEX_CHART'] = i
        i+=1
        for symbol in self.symbols:
            # POS Chart
            self.pos_chart['series'].append({
                'type': 'line',
                'name': symbol,
                'data': []
            })
            self.indices[symbol+'_pos'] = i
            i+=1
        for symbol in self.symbols:
            # UPNL Chart
            self.upnl_chart['series'].append({
                'type': 'line',
                'name': symbol,
                'data': []
            })
            self.indices[symbol+'_upnl'] = i
            i+=1
        self.chart = Chart([self.diffs_chart, self.index_chart, self.pos_chart, self.upnl_chart])

    def add_diff_data(self, symbol, diff):
        self.diffs_data[symbol] = diff

    def add_pos_data(self, symbol, pos):
        self.pos_data[symbol] = pos

    def add_upnl_data(self, symbol, upnl):
        self.upnl_data[symbol] = upnl

    def add_index_data(self, index):
        self.index = index

    def update_chart(self, timestamp):
        for symbol in self.diffs_data.keys():
            index = self.indices[symbol+'_diff']
            data = self.diffs_data[symbol]
            self.chart.add(index, [timestamp, data])

        for symbol in self.pos_data.keys():
            index = self.indices[symbol+'_pos']
            data = self.pos_data[symbol]
            self.chart.add(index, [timestamp, data])

        for symbol in self.upnl_data.keys():
            index = self.indices[symbol+'_upnl']
            data = self.upnl_data[symbol]
            self.chart.add(index, [timestamp, data])

        index = self.indices['INDEX_CHART']
        self.chart.add(index, [timestamp, self.index])
        self.chart.update([self.diffs_chart, self.index_chart, self.pos_chart, self.upnl_chart])

Published by FawkesPan on the FMZ strategy square · last modified 2020-06-17. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Variable-Chart-Example.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open