0bps

Strategy vault / Other

Moving Average Strategy Python Version

Moving average strategy (Python version) is for instructional purposes; use with caution in live trading.

PythonOther
Author
发明者量化-小小梦
Complexity
Intermediate · 26 lines
Parameters
8

Moving average strategy (Python version) is for instructional purposes; use with caution in live trading.

Parameters

NameDefaultWhat it controls
FastPeriod3Market Entry Express Cycle
SlowPeriod7Slow market entry cycle
EnterPeriod3Market Entry Observation Period
ExitFastPeriod3Exit express cycle
ExitSlowPeriod7Exit slow cycle
ExitPeriodtrueMarket Exit Observation Period
PositionRatio0.8Position ratio
Interval10Polling Interval

Source Python

import types
def main():
    STATE_IDLE = -1
    state = STATE_IDLE
    initAccount = ext.GetAccount()
    while True:
        if state == STATE_IDLE :
            n = ext.Cross(FastPeriod,SlowPeriod) # Indicator cross function
            if abs(n) >= EnterPeriod :
                opAmount = _N(initAccount.Stocks * PositionRatio,3)
                Dict = ext.Buy(opAmount) if n > 0 else ext.Sell(opAmount)
                if Dict :
                    opAmount = Dict['amount']
                    state = PD_LONG if n > 0 else PD_SHORT
                    Log("Open position details",Dict,"Cross period",n)
        else:
            n = ext.Cross(ExitFastPeriod,ExitSlowPeriod) # Indicator cross function
            if abs(n) >= ExitPeriod and ((state == PD_LONG and n < 0) or (state == PD_SHORT and n > 0)) :
                nowAccount = ext.GetAccount()
                Dict2 = ext.Sell(nowAccount.Stocks - initAccount.Stocks) if state == PD_LONG else ext.Buy(initAccount.Stocks - nowAccount.Stocks)
                state = STATE_IDLE
                nowAccount = ext.GetAccount()
                LogProfit(nowAccount.Balance - initAccount.Balance,'Money: ',nowAccount.Balance,' Coins: ',nowAccount.Stocks,' Closing Details: ',Dict2,' Cross Cycle:',n)
        Sleep(Interval * 1000)

Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2016-09-30. 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/Moving-Average-Strategy-Python-Version.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