0bps

Strategy vault / Other

Python Version of Chasing Up and Killing Down Strategy Teaching

https://www.fmz.com/bbs-topic/4908

PythonOther
Author
发明者量化-小小梦
Complexity
Starter · 47 lines
Parameters
0

https://www.fmz.com/bbs-topic/4908

Source Python

'''backtest
start: 2019-02-20 00:00:00
end: 2020-01-10 00:00:00
period: 1m
exchanges: [{"eid":"OKEX","currency":"BTC_USDT"}]
'''

import time

basePrice = -1
ratio = 0.05
acc = _C(exchange.GetAccount)
lastCancelAll = 0
minStocks = 0.01

def CancelAll():
    while True : 
        orders = _C(exchange.GetOrders)
        for i in range(len(orders)) :
            exchange.CancelOrder(orders[i]["Id"], orders[i])
        if len(orders) == 0 :
            break
        Sleep(1000)

def main():
    global basePrice, acc, lastCancelAll
    exchange.SetPrecision(2, 3)
    while True:
        ticker = _C(exchange.GetTicker)
        if basePrice == -1 :
            basePrice = ticker.Last
        if ticker.Last - basePrice > 0 and (ticker.Last - basePrice) / basePrice > ratio :
            acc = _C(exchange.GetAccount)
            if acc.Balance * ratio / ticker.Last > minStocks :
                exchange.Buy(ticker.Last, acc.Balance * ratio / ticker.Last)
                basePrice = ticker.Last
        if ticker.Last - basePrice < 0 and (basePrice - ticker.Last) / basePrice > ratio : 
            acc = _C(exchange.GetAccount)
            if acc.Stocks * ratio > minStocks :
                exchange.Sell(ticker.Last, acc.Stocks * ratio)
                basePrice = ticker.Last
        ts = time.time()
        if ts - lastCancelAll > 60 * 5 :
            CancelAll()
            lastCancelAll = ts 
        LogStatus(_D(), "\n", "Market information:",Tiker,"\n", "Account information.":", acc)
        Sleep(500)

Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2020-01-11. 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-Chasing-Up-and-Killing-Down-Strategy-Teaching.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