Fundamentals 10 min read

Build a Simple Moving‑Average Stock Strategy on Ricequant in Minutes

This step‑by‑step guide shows how to implement, backtest, and run a single‑stock 5‑day versus 30‑day moving‑average trading strategy on the Ricequant platform, covering code setup, cash handling, order execution, and both daily and minute‑level simulations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a Simple Moving‑Average Stock Strategy on Ricequant in Minutes

Overview

This article demonstrates implementing a single‑stock moving‑average strategy on the Ricequant quant platform, helping beginners get started and create their own strategy code.

Strategy Framework

The rule is simple: when the 5‑day moving average is above the 30‑day moving average, buy the stock with the full cash amount; otherwise, sell all holdings.

1. Initialization

Set the target stock to "300059.XSHE" (Dongfang Caifu on the Shenzhen exchange).

def init(context):
    context.stock = "300059.XSHE"  # target stock

Stock code suffixes: XSHE for Shenzhen, XSHG for Shanghai.

2. Retrieve Moving Averages

# fast = 5‑day average, slow = 30‑day average
fast = bar_dict[context.stock].mavg(5, frequency='day')
slow = bar_dict[context.stock].mavg(30, frequency='day')

3. Determine Cash

cash = context.portfolio.cash  # current cash amount

4. Buy / Sell Logic

if fast > slow:
    order_value(context.stock, cash)          # buy with all cash
elif fast < slow:
    order_target_percent(context.stock, 0)    # sell all holdings

5. Full Daily‑Frequency Code

def init(context):
    context.stock = "300059.XSHE"

def handle_bar(context, bar_dict):
    fast = bar_dict[context.stock].mavg(5, frequency='day')
    slow = bar_dict[context.stock].mavg(30, frequency='day')
    cash = context.portfolio.cash
    if fast > slow:
        order_value(context.stock, cash)
    elif fast < slow:
        order_target_percent(context.stock, 0)

6. Backtesting

Run a daily backtest from 2015‑01‑04 to 2016‑10‑04 with an initial capital of 100,000 CNY. The platform will display performance charts and risk‑return metrics.

7. Minute‑Level Backtesting

To switch to minute frequency, modify the handler and add a before_trading function that resets a flag each day, ensuring only one order per day.

def init(context):
    context.stock = "300059.XSHE"
    context.fired = 0

def before_trading(context):
    context.fired = 0

def handle_bar(context, bar_dict):
    if context.fired == 0:
        fast = bar_dict[context.stock].mavg(5, frequency='day')
        slow = bar_dict[context.stock].mavg(30, frequency='day')
        cash = context.portfolio.cash
        if fast > slow:
            order_value(context.stock, cash)
        elif fast < slow:
            order_target_percent(context.stock, 0)
        context.fired = 1

8. Simulation Trading

After a successful minute backtest, enable simulation trading in the platform to see real‑time order execution on minute‑level data.

9. WeChat Notifications

Activate the WeChat notification switch in the strategy settings, scan the QR code to bind your account, and receive trade signals instantly via WeChat.

Pythonquantitative tradingmoving averagebacktestingAlgorithmic TradingRicequant
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.