0bps

Strategy vault / Other

Python Version of MACD Indicator Drawing Example

Python strategy by 发明者量化-小小梦.

PythonOtherMACD
Author
发明者量化-小小梦
Complexity
Starter · 76 lines
Parameters
0

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

Source Python

'''backtest
start: 2020-01-28 00:00:00
end: 2020-02-26 00:00:00
period: 1d
exchanges: [{"eid":"OKEX","currency":"BTC_USDT"}]
'''

ChartCfg = {
    '__isStock': True,
    'title': {
        'text': 'PythonDrawing'
    },
    'yAxis': [{
        'title': {'text': 'KLine'},
        'style': {'color': '#4572A7'},
        'opposite': False
    }, {
        'title': {'text': 'Indicator axis'},
        'opposite': True
    }],
    'series': [{
        'type': 'candlestick',
        'name': 'Current cycle',
        'id': 'primary',
        'data': []
    }, {
        'type': 'line',
        'id': 'dif',
        'name': 'DIF',
        "yAxis" : 1,
        'data': []
    }, {
        'type': 'line',
        'id': 'dea',
        'name': 'DEA',
        "yAxis" : 1,
        'data': []
    }, {
        'type': 'line',
        'id': 'macd',
        'name': 'MACD',
        "yAxis" : 1,
        'data': []
    }]
}

def main():
    global ChartCfg
    preTime = 0
    chart = Chart(ChartCfg)
    chart.reset()
    while True:
        while True:
            r = _C(exchange.GetRecords)
            if len(r) > 50:
                break
        # Calculate indicator
        macd = TA.MACD(r)
        
        LogStatus(_D(), len(r))
        
        # Drawing
        for i in range(len(r)):
            if r[i]["Time"] == preTime:
                chart.add(0, [r[i]["Time"], r[i]["Open"], r[i]["High"], r[i]["Low"], r[i]["Close"]], -1)
                chart.add(1, [r[i]["Time"], macd[0][i]], -1)
                chart.add(2, [r[i]["Time"], macd[1][i]], -1)
                chart.add(3, [r[i]["Time"], macd[2][i]], -1)
            elif r[i]["Time"] > preTime:
                chart.add(0, [r[i]["Time"], r[i]["Open"], r[i]["High"], r[i]["Low"], r[i]["Close"]])
                chart.add(1, [r[i]["Time"], macd[0][i]])
                chart.add(2, [r[i]["Time"], macd[1][i]])
                chart.add(3, [r[i]["Time"], macd[2][i]])
                preTime = r[i]["Time"]
        Sleep(500)

Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2020-02-27. 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/Python-Version-of-MACD-Indicator-Drawing-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