Moving Average Strategy Python Version
Moving average strategy (Python version) is for instructional purposes; use with caution in live trading.
Moving average strategy (Python version) is for instructional purposes; use with caution in live trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| FastPeriod | 3 | Market Entry Express Cycle |
| SlowPeriod | 7 | Slow market entry cycle |
| EnterPeriod | 3 | Market Entry Observation Period |
| ExitFastPeriod | 3 | Exit express cycle |
| ExitSlowPeriod | 7 | Exit slow cycle |
| ExitPeriod | true | Market Exit Observation Period |
| PositionRatio | 0.8 | Position ratio |
| Interval | 10 | Polling 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.
/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- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- 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
- Dynamic Balancing Strategy Python VersionPython · Other
- Exchange Wash Trading and Mutual BrushingPython · Other
- Leek Protection Program Tang Anqi Channel Position Balancing StrategyPython · Other
- Paul The Gambler Lévy Gold EditionPython · Other
- Porting OKCoin Leek Harvester Multi Variety Version Annotated Python VersionPython · Other
- Python Example Using asyncio http to Obtain Market DataPython · Other