Last time simply displayed the stock price of 9984 (Softbank). This time, let's calculate the moving average of the stock price.
There are various types of moving averages, but in the case of a simple moving average, if $ p_t $ is the price $ t $ days ago, the n-day moving average will be the average of the closing prices for the past n days.
The moving average is one of the most basic indicators in the technical analysis of stock prices and is used to judge the conditions such as golden cross and dead cross. The golden cross is the opposite of the dead cross, as the 5-day moving average is "good to buy" when it exceeds the 25-day moving average.
[Wikipedia: Moving Average](https://ja.wikipedia.org/wiki/Moving Average)
This time, let's calculate the moving average with the closing price after adjusting the stock price.
Suddenly, it's a code.
def _mavg_signal(data):
m5 = data["close_price_adj"].fillna(method='ffill').rolling(window=5, center=False).mean()
m25 = data["close_price_adj"].fillna(method='ffill').rolling(window=25, center=False).mean()
return {
"mavg_5:price": m5,
"mavg_25:price": m25,
}
#Signal registration
ctx.regist_signal("mavg_signal", _mavg_signal)
In ʻinitialize (), register the function
_mavg_signalwith
ctx.regist_signal ()`.
In _mavg_signal ()
, two types of calculation are performed: 5-day moving average and 25-day moving average.
The parameter data
is of type pandas.Panel and has a three-dimensional structure. The three-dimensional structure is
axis-0 (items): Data items (close_price, volume, etc.) axis-1 (major): Date (datetime.datetime type) axis-2 (minor): Brand name (symbol object type) Quoted from https://quantx.io/handbook/ja/#initialize
It seems that it has a structure like this. It's hard to imagine, but it may be easier to understand if you think like Excel (3D with "rows, columns, sheets"?) The actual internal image is written in handbook, but after deciding the data item of axis-0 and dropping it to 2D, pandas The moving average is calculated by the function of .DataFrame and returned as the value of dict as the return value.
It came out! mavg_5 is the 5-day moving average and mavg_25 is the 25-day moving average. The 5-day moving average is not much different from the closing price (close_price_adj), so it's hard to see.
I wonder if I can really calculate, so I will check the actual calculation result on the tab called RawData
on the chart.
The value of the 5-day moving average of 2014-01-10 is $ \ frac {8880 + 8920 + 8990 + 8920 + 9020} 5 = 8946 $, so it seems that it can be calculated! Mavg_5: price is 5 Since it is a daily moving average, it is blank because it cannot be calculated until the 4th day after the back test is started.
In _mavg_signal ()
, which calculates the moving average,
return {
"mavg_5:price": m5,
"mavg_25:price": m25,
}
I returned the dict. The value of dict is pandas.DataFrame which is the calculation result of moving average.
The key is the axis name of the chart, but the point is : price
, which specifies the scale in the chart.
: price
is a predefined scale, which is the stock price scale of the stock. By making the axes of mavg_5 and mavg_25 the same, it is possible to compare on the chart for the first time.
as a trial,
return {
"mavg_5:mavg_5": m5,
"mavg_25:mavg_25": m25,
}
With that feeling, when I try to execute it,
Since the scale of the vertical axis of each moving average value is different, the degree of overlap of the charts has changed slightly (difficult to understand). Therefore, it is difficult to judge the golden cross or dead cross from this chart, so let's align the axes.
This is the code for this time.
def initialize(ctx):
ctx.configure(
target="jp.stock.daily",
channels={
"jp.stock": {
"symbols": [ "jp.stock.9984" ],
"columns": [ "close_price_adj" ]
}
}
)
def _mavg_signal(data):
m5 = data["close_price_adj"].fillna(method='ffill').rolling(window=5, center=False).mean()
m25 = data["close_price_adj"].fillna(method='ffill').rolling(window=25, center=False).mean()
return {
"mavg_5:price": m5,
"mavg_25:price": m25,
}
#Signal registration
ctx.regist_signal("mavg_signal", _mavg_signal)
def handle_signals(ctx, date, current):
'''
current: pd.DataFrame
'''
pass
Next time, I will try to calculate and display more complicated indicators.
2018/1/11 postscript Official version URL:
Recommended Posts