Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of AkShare Policy

Shulou Source: shulou.com Published: 2022-05-31 13:28:48 10月03日 Update

In this article, the editor introduces "AkShare strategy example analysis" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this "AkShare strategy example analysis" article can help you solve your doubts.

AkShare Policy sample Stock Policy

Download and install BackTrader

From datetime import datetime

Import backtrader as bt

Import matplotlib.pyplot as plt

Import akshare as ak

Plt.rcParams ["font.sans-serif"] = ["SimHei"]

Plt.rcParams ["axes.unicode_minus"] = False

Stock_hfq_df = ak.stock_zh_a_daily (symbol= "sh700000", adjust= "hfq") # use AkShare to obtain complex weight data per row

Class MyStrategy (bt.Strategy):

"

Master policy program

"

Params = ("maperiod", 20),) # globally set the parameters of the trading strategy

Def _ init__ (self):

"

Initialization function

"

Self.data_close = self.datas [0] .close # specify price sequence

# initialize trading orders, buying and selling prices and handling fees

Self.order = None

Self.buy_price = None

Self.buy_comm = None

# add moving average metrics

Self.sma = bt.indicators.SimpleMovingAverage (

Self.datas [0], period=self.params.maperiod

)

Def next (self):

"

: return:

: rtype:

"

If self.order: # check if there are instructions waiting to be executed

Return

# check whether you are in a position

If not self.position: # No position

If self.data_close [0] > self.sma [0]: # execute the buy condition judgment: the closing price rises above the 20-day moving average

Self.order = self.buy (size=100) # execute buy

Else:

If self.data_close [0] < self.sma [0]: # execute selling condition judgment: closing price falls below the 20-day moving average

Self.order = self.sell (size=100) # execute sell

Cerebro = bt.Cerebro () # initialize the backtest system

Start_date = datetime (2000, 1, 1) # retest start time

End_date = datetime (2020, 4, 21) # end time of backtest

Data = bt.feeds.PandasData (dataname=stock_hfq_df, fromdate=start_date, todate=end_date) # load data

Cerebro.adddata (data) # pass the data into the back test system

Cerebro.addstrategy (MyStrategy) # loads the trading strategy into the back test system

Start_cash = 1000000

Cerebro.broker.setcash (start_cash) # sets the initial capital to 100000

Cerebro.broker.setcommission (commission=0.002) # sets the transaction fee to 0.2%

Cerebro.run () # run the back test system

Port_value = cerebro.broker.getvalue () # get the total funds after the return test

Pnl = port_value-start_cash # profit and loss statistics

Print (f "initial funds: {start_cash}\ nRetest period: {start_date.strftime ('% Y% m% d')}: {end_date.strftime ('% Y% m% d')}")

Print (f "Total funds: {round (port_value, 2)}")

Print (f "net income: {round (pnl, 2)}")

Cerebro.plot (style='candlestick') # drawing result initial funding: 1000000

During the retest period: 20000101purl 20200421

Total funds: 1010238.65

Net income: 10238.65 read here, this "AkShare strategy sample analysis" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more related articles, welcome to follow the industry information channel.

Tags: Policies funds examples prices systems transactions analysis moving averages data articles content formalities fees instructions returns time conditions checks proper one line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi Shulou Tech Info MariaDB macOS