[DaMaiXiaomiLearning Quantification] Use Wenxinyiyan AI to write stock quantitative trading strategy codes (including demo code and advanced demos)


AI is a treasure

Xiaomi heard that Baidu had opened up Wenxin Yiyan AI, and curiously ran to ask Damai: "Wenxin Yiyan has opened up and can write any code. Why are you still learning Python?" Damai: "Why don't you learn it?
" ? If you don’t learn, you know what’s going on with the code!”
“You’ve been studying for so long, do you dare to say that my AI can write faster?” Xiaomi squinted at Damai with disdain, as if waiting for Brother Damai to make a fool of himself. All.
"I don't believe that AI can write my strategy," Damai retorted. Xiaomi was a little disappointed, but even more happy.
"Quantitative trading cannot be done by just one AI. AI may be very strong on general algorithms, but when it comes to personalized API calls, you still need to debug it!" When Xiaomi heard this, he immediately challenged: "It's a mule or a horse, pull it.
" Come out and run around!"
"Who is afraid of whom?!" Damai retorted unconvinced, not to be outdone.
"I asked Wen Xinyiyan to write a piece of code for you." When Xiaomai got serious, he was like a woman.
"Come on, come on, come on, just write and write!" Damai was also very excited.
I saw Xiaomi open her Mac, write a paragraph to Wen Xin, and press Enter gracefully at the end. AI considered every word, and soon, the code was written out like a jumping bean.
"Here, let's try it." Xiaomi generously gave the code to Brother Damai, eager to get the results.
"This is just a fragment, but it can be used as a reference. I will modify it later and try running it." Damai didn't care, but seriously modified it, as if the strategy code was written by himself.
"Come on, how's it going?" Xiaomi stared at Brother Damai's computer screen impatiently, eager to see the results immediately.
"Okay, it's not bad! Baidu has lived up to the wait! This code can run, not bad!" Brother Damai no longer hesitated to praise him. At the same time, he smiled at Xiaomi and nodded frequently, as if to say: " My sister brought a baby!"
"You don't have to worry about it from now on." Xiaomi was also happy to be able to help Brother Damai.
Damai looked at his sister with light in his eyes: "Yeah, my sister's eyes are so bright!"

Preface

AI has been released to the point where it can write code. Can it also be used to write quantitative trading code?
With this question, let’s try Wen Xinyiyan.


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is Wen Xin Yi Yan?

Wenxinyiyan: Useful, interesting, and warm. It can not only write copywriting and read documents, but also open up ideas, answer questions, and listen to your stories and feel your voice. Come and talk to me!

This is an introduction to Baidu AI. In order to test the code writing ability of AI, we are going to let it write python code according to the given text strategy.

2. Let AI write code according to the strategy

1. Strategy prompt words

Use python to write the strategy code based on the following content:
buy when the upper limit of the boll is exceeded, sell when the upper limit of the boll is exceeded by 0.5%, and sell when the upper limit of the boll is exceeded.

2. Strategy code and description given by AI

Here is the strategy code written based on the content you provided:

import pandas as pd  
import numpy as np  
import talib  
  
def run_strategy(df):  
    # 计算boll指标  
    df['upper'], df['middle'], df['lower'] = talib.BBANDS(df['Close'], timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)  
      
    # 检查是否上穿boll中轨  
    buy_signal = np.where((df['Close'].shift(1) < df['middle'].shift(1)) & (df['Close'] > df['middle']), 1, 0)  
      
    # 检查是否超过boll上轨0.5%  
    sell_signal1 = np.where((df['Close'].shift(1) < df['upper'].shift(1)) & (df['Close'] > df['upper'] * 1.005), 1, 0)  
      
    # 检查是否下穿布林中轨  
    sell_signal2 = np.where(df['Close'] < df['middle'], 1, 0)  
      
    df['Buy'] = buy_signal.astype(int)  
    df['Sell1'] = sell_signal1.astype(int)  
    df['Sell2'] = sell_signal2.astype(int)  
      
    return df

This code also uses the BBANDS function in the TA-Lib library to calculate the Bollinger Bands indicator. First calculate the upper rail, middle rail and lower rail, and then check whether the closing price of the previous day is below the middle rail, and whether the closing price of the day is above the middle rail. If so, it is a buy signal. Then check whether the closing price of the previous day is below the upper rail, and whether the closing price of the current day is above 0.5% of the upper rail. If so, it is a sell signal. Finally, check whether the closing price of the day is below the middle rail. If so, it is a sell signal. Finally add buy and sell signals to the data frame and return. Please note that it is also assumed here that the input data frame df already contains closing price data, and the np.where function is used to implement conditional judgment.

3. Advanced debugging

Since the AI ​​code does not include the data extraction and display process, we add some code here to verify whether the strategy is executable and whether the strategy is executed as required.

'''
根据以下内容用python写出策略代码:
上破boll中轨买入,上破boll上轨0.5%卖出,下破boll中轨卖出
'''

import pandas as pd  
import numpy as np  
import talib  
  
def run_strategy(df):  
    # 计算boll指标  
    df['upper'], df['middle'], df['lower'] = talib.BBANDS(df['Close'], timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)  
      
    # 检查是否上穿boll中轨  
    buy_signal = np.where((df['Close'].shift(1) < df['middle'].shift(1)) & (df['Close'] > df['middle']), 1, 0)  
      
    # 检查是否超过boll上轨0.5%  
    sell_signal1 = np.where((df['Close'].shift(1) < df['upper'].shift(1)) & (df['Close'] > df['upper'] * 1.005), 1, 0)  
      
    # 检查是否下穿布林中轨  
    sell_signal2 = np.where(df['Close'] < df['middle'], 1, 0)  
      
    df['Buy'] = buy_signal.astype(int)  
    df['Sell1'] = sell_signal1.astype(int)  
    df['Sell2'] = sell_signal2.astype(int)  
      
    return df
    
# ===============表格美化输出===============
def df_table(df,index):
    import prettytable as pt
    #利用prettytable对输出结果进行美化,index为索引列名:df_table(df,'market')
    tb = pt.PrettyTable()
    # 如果为trade_time为index转换为日期类型,其它不用管。
    if index == "trade_time":
        df = df.set_index(index)
        df.index = pd.DatetimeIndex(df.index)
    # df.reset_index(level=None, drop=True, inplace=True, col_level=0, col_fill='')
    df = df.reset_index(drop = True)
    tb.add_column(index,df.index)#按date排序
    for col in df.columns.values:#df.columns.values的意思是获取列的名称
        # print('col',col)
        # print('df[col]',df[col])
        tb.add_column(col, df[col])
    print(tb)


if __name__=='__main__':
    # 使用pip install prettytable 先安装这个库
    import prettytable as pt
	# 下载Ashare放到和本文件一个目录即可引用
    from Ashare import *
    df=get_price('sz300750',frequency='1d',count=500)
    print(df)
    # df.columns = [l.lower() for l in df.columns] # 将所有列名小写
    df.columns = [l.capitalize() for l in df.columns] # 将所有列名首字母改为大写
    print(df)
    ret = run_strategy(df)
    print(ret)
    df_table(ret.round(3),'AI')
    
    

The operation results are as follows. Buy is 1 to buy, Sell1 is to sell at profit, Sell2 is to sell at loss:

+----+--------+--------+--------+--------+------------+---------+---------+---------+-----+-------+-------+
| AI |  Open  |  High  |  Low   | Close  |   Volume   |  upper  |  middle |  lower  | Buy | Sell1 | Sell2 |
+----+--------+--------+--------+--------+------------+---------+---------+---------+-----+-------+-------+
| 0  | 225.8  | 234.25 | 221.21 | 232.52 | 28956810.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 1  | 232.55 | 242.98 | 231.2  | 240.4  | 34747371.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 2  | 241.99 | 244.56 | 237.98 | 237.98 | 22309979.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 3  | 241.01 | 244.0  | 235.1  | 238.29 | 26706471.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 4  | 238.4  | 240.97 | 236.77 | 239.0  | 13973953.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 5  | 239.0  | 241.68 | 236.06 | 240.0  | 18246432.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 6  | 241.3  | 241.37 | 231.04 | 232.4  | 24123880.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 7  | 231.75 | 234.18 | 230.0  | 230.0  | 19472168.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 8  | 229.0  | 232.5  | 227.0  | 230.3  | 14679114.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 9  | 230.88 | 231.71 | 225.15 | 225.42 | 19613196.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 10 | 224.8  | 228.51 | 223.72 | 225.25 | 14785735.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 11 | 224.85 | 227.68 | 222.49 | 224.09 | 13753272.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 12 | 224.2  | 224.5  | 217.5  | 220.0  | 18211695.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 13 | 222.2  | 222.22 | 215.6  | 217.06 | 16537161.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 14 | 217.0  | 224.99 | 216.01 | 224.99 | 26467083.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 15 | 225.05 | 226.89 | 220.16 | 221.17 | 18869744.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 16 | 222.64 | 228.0  | 221.51 | 222.9  | 15941049.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 17 | 224.9  | 229.99 | 224.0  | 228.44 | 23181483.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 18 | 225.9  | 225.9  | 216.39 | 219.8  | 33595114.0 |   nan   |   nan   |   nan   |  0  |   0   |   0   |
| 19 | 218.01 | 222.3  | 216.98 | 219.28 | 17242526.0 | 243.347 | 228.465 | 213.582 |  0  |   0   |   1   |
| 20 | 216.38 | 216.5  | 204.45 | 207.3  | 50093939.0 | 244.565 | 227.204 | 209.842 |  0  |   0   |   1   |
| 21 | 212.75 | 213.3  | 207.0  | 208.01 | 23857940.0 | 243.744 | 225.584 | 207.424 |  0  |   0   |   1   |
| 22 | 208.55 | 213.13 | 208.55 | 212.04 | 24261728.0 | 242.425 | 224.287 | 206.149 |  0  |   0   |   1   |
| 23 | 211.95 | 214.0  | 208.9  | 211.95 | 18131632.0 |  240.67 |  222.97 |  205.27 |  0  |   0   |   1   |
| 24 | 211.01 | 213.93 | 210.68 | 213.1  | 13243699.0 | 238.248 | 221.675 | 205.102 |  0  |   0   |   1   |
| 25 | 217.15 | 217.63 | 213.0  | 213.4  | 16362927.0 | 234.978 | 220.345 | 205.712 |  0  |   0   |   1   |
| 26 | 214.99 | 230.88 | 214.89 | 230.88 | 50495682.0 | 234.665 | 220.269 | 205.873 |  1  |   0   |   0   |
| 27 | 232.0  | 234.17 | 228.3  | 231.1  | 29639828.0 | 234.876 | 220.324 | 205.772 |  0  |   0   |   0   |
| 28 | 232.99 | 232.99 | 226.0  | 227.88 | 18707081.0 | 234.458 | 220.203 | 205.948 |  0  |   0   |   0   |
| 29 | 227.87 | 231.31 | 225.77 | 228.0  | 17960501.0 | 234.818 | 220.332 | 205.846 |  0  |   0   |   0   |
| 30 | 228.81 | 230.5  | 224.9  | 224.91 | 17118438.0 | 234.779 | 220.315 | 205.851 |  0  |   0   |   0   |
| 31 | 219.91 | 226.95 | 219.68 | 225.17 | 15910609.0 | 234.897 | 220.369 | 205.841 |  0  |   0   |   0   |
| 32 | 223.98 | 226.3  | 221.18 | 223.47 | 13420648.0 | 235.131 | 220.543 | 205.954 |  0  |   0   |   0   |
| 33 | 222.22 | 226.5  | 222.0  | 224.5  | 13558644.0 | 235.509 | 220.915 |  206.32 |  0  |   0   |   0   |
| 34 | 223.51 | 226.91 | 222.5  | 222.5  | 9930233.0  | 235.285 |  220.79 | 206.295 |  0  |   0   |   0   |
| 35 | 223.99 | 229.99 | 220.35 | 228.79 | 21551522.0 | 236.081 | 221.171 | 206.261 |  0  |   0   |   0   |
| 36 | 231.95 | 233.95 | 228.55 | 229.88 | 15554352.0 | 236.895 |  221.52 | 206.145 |  0  |   0   |   0   |
| 37 | 228.5  | 230.05 | 227.68 | 229.25 | 10155673.0 | 237.012 | 221.561 | 206.109 |  0  |   0   |   0   |
| 38 | 229.26 | 230.41 | 225.5  | 227.51 | 8636414.0  | 237.586 | 221.946 | 206.306 |  0  |   0   |   0   |
| 39 | 227.0  | 228.48 | 223.97 | 224.85 | 10283568.0 | 237.863 | 222.225 | 206.586 |  0  |   0   |   0   |
| 40 | 224.8  | 224.85 | 221.6  | 222.25 | 9808377.0  | 237.036 | 222.972 | 208.908 |  0  |   0   |   1   |
| 41 | 223.0  | 230.0  | 223.0  | 228.8  | 16093831.0 | 236.481 | 224.012 | 211.542 |  1  |   0   |   0   |
| 42 | 229.65 | 229.99 | 226.58 | 229.09 | 9158787.0  | 236.225 | 224.864 | 213.503 |  0  |   0   |   0   |
| 43 | 228.5  | 229.4  | 226.5  | 226.99 | 8693260.0  |  235.33 | 225.616 | 215.902 |  0  |   0   |   0   |
| 44 | 228.21 | 235.0  | 228.21 | 230.0  | 22257672.0 | 234.462 | 226.461 |  218.46 |  0  |   0   |   0   |
| 45 | 230.15 | 230.93 | 226.55 | 228.28 | 13004091.0 | 232.529 | 227.205 | 221.881 |  0  |   0   |   0   |
| 46 | 227.01 | 228.0  | 223.6  | 225.65 | 10452522.0 | 232.028 | 226.944 | 221.859 |  0  |   0   |   1   |
| 47 | 227.8  | 227.88 | 224.5  | 225.5  | 8575776.0  | 231.407 | 226.664 |  221.92 |  0  |   0   |   1   |
| 48 | 224.0  | 224.95 | 218.58 | 219.5  | 17600182.0 | 231.881 | 226.245 | 220.608 |  0  |   0   |   1   |
| 49 | 219.0  | 226.69 | 219.0  | 221.05 | 14441648.0 | 231.902 | 225.897 | 219.892 |  0  |   0   |   1   |
| 50 | 220.85 | 222.98 | 219.76 | 221.59 | 8393526.0  | 232.013 | 225.731 | 219.449 |  0  |   0   |   1   |
| 51 | 217.15 | 220.88 | 214.14 | 218.98 | 13719339.0 |  232.36 | 225.422 | 218.483 |  0  |   0   |   1   |
| 52 | 224.9  | 227.64 | 221.02 | 226.35 | 19286245.0 | 232.455 | 225.566 | 218.676 |  1  |   0   |   0   |
| 53 | 225.12 | 227.0  | 223.66 | 226.66 | 15456786.0 | 232.561 | 225.674 | 218.786 |  0  |   0   |   0   |
| 54 | 231.0  | 232.88 | 226.88 | 226.98 | 20859235.0 | 232.647 | 225.898 | 219.148 |  0  |   0   |   0   |
| 55 | 226.98 | 231.0  | 225.05 | 230.05 | 18803095.0 | 232.839 | 225.961 | 219.082 |  0  |   0   |   0   |
| 56 | 234.0  | 239.5  | 233.1  | 237.75 | 29902754.0 | 234.805 | 226.354 | 217.903 |  0  |   1   |   0   |
| 57 | 237.4  | 239.1  | 234.5  | 235.5  | 15615168.0 | 235.945 | 226.667 | 217.388 |  0  |   0   |   0   |
| 58 | 237.0  | 239.49 | 235.5  | 236.5  | 14147185.0 | 237.337 | 227.116 | 216.895 |  0  |   0   |   0   |
| 59 | 236.0  | 239.97 | 235.53 | 239.14 | 16180526.0 | 239.246 | 227.831 | 216.415 |  0  |   0   |   0   |
| 60 | 245.0  | 249.96 | 241.11 | 245.51 | 31569675.0 | 242.454 | 228.994 | 215.533 |  0  |   1   |   0   |
| 61 | 244.5  | 246.94 | 243.31 | 246.49 | 14927749.0 | 245.347 | 229.878 | 214.409 |  0  |   0   |   0   |
| 62 | 245.98 | 249.33 | 244.88 | 247.14 | 15066676.0 | 247.971 | 230.781 |  213.59 |  0  |   0   |   0   |
| 63 | 245.5  | 247.68 | 243.14 | 247.0  | 11684661.0 | 250.254 | 231.781 | 213.308 |  0  |   0   |   0   |
| 64 | 247.9  | 251.33 | 244.12 | 246.5  | 14040964.0 | 252.131 | 232.606 | 213.081 |  0  |   0   |   0   |
| 65 | 248.95 | 248.95 | 241.5  | 241.5  | 17204064.0 | 253.054 | 233.267 |  213.48 |  0  |   0   |   0   |
| 66 | 239.2  | 239.98 | 230.21 | 232.9  | 25693218.0 | 253.109 |  233.63 |  214.15 |  0  |   0   |   1   |
| 67 | 232.88 | 234.6  | 229.4  | 231.3  | 14610073.0 | 253.076 |  233.92 | 214.763 |  0  |   0   |   1   |
| 68 | 231.52 | 234.38 | 230.11 | 232.6  | 14009045.0 | 252.575 | 234.575 | 216.574 |  0  |   0   |   1   |
| 69 | 234.99 | 236.5  | 228.88 | 235.25 | 20442773.0 | 252.181 | 235.285 | 218.388 |  0  |   0   |   1   |
| 70 | 235.29 | 239.12 | 234.42 | 235.87 | 16769998.0 | 251.684 | 235.999 | 220.313 |  0  |   0   |   1   |
| 71 | 233.99 | 235.87 | 232.2  | 232.96 | 10780439.0 | 250.409 | 236.698 | 222.986 |  0  |   0   |   1   |
| 72 | 234.8  | 235.18 | 227.51 | 230.75 | 15188941.0 | 250.088 | 236.918 | 223.747 |  0  |   0   |   1   |
| 73 | 230.7  | 231.9  | 227.0  | 227.5  | 11874911.0 | 250.004 |  236.96 | 223.915 |  0  |   0   |   1   |
| 74 | 228.88 | 231.54 | 226.5  | 228.95 | 14157716.0 | 249.826 | 237.058 |  224.29 |  0  |   0   |   1   |
| 75 | 227.93 | 229.97 | 225.81 | 228.34 | 11185129.0 | 249.948 | 236.973 | 223.997 |  0  |   0   |   1   |
| 76 | 241.5  | 243.96 | 229.5  | 230.5  | 22311515.0 |  249.88 |  236.61 |  223.34 |  0  |   0   |   1   |
| 77 | 231.2  | 243.0  | 230.92 | 238.5  | 29174995.0 | 250.045 |  236.76 | 223.475 |  1  |   0   |   0   |
| 78 | 239.4  | 242.0  | 237.5  | 237.5  | 14591637.0 | 250.098 |  236.81 | 223.522 |  0  |   0   |   0   |
| 79 | 237.1  | 238.5  | 236.13 | 236.55 | 9671527.0  | 249.925 | 236.681 | 223.436 |  0  |   0   |   1   |
| 80 | 236.6  | 239.51 | 234.6  | 236.4  | 9101132.0  | 248.835 | 236.225 | 223.615 |  1  |   0   |   0   |
| 81 | 238.21 | 238.76 | 233.4  | 237.65 | 14052387.0 | 247.512 | 235.783 | 224.054 |  0  |   0   |   0   |
| 82 | 237.0  | 239.5  | 236.16 | 236.82 | 11581665.0 | 245.799 | 235.267 | 224.735 |  0  |   0   |   0   |
| 83 | 236.05 | 237.17 | 232.8  | 234.42 | 12474783.0 | 243.691 | 234.638 | 225.585 |  0  |   0   |   1   |
| 84 | 233.0  | 233.75 | 226.51 | 226.51 | 19266090.0 | 241.578 | 233.639 | 225.699 |  0  |   0   |   1   |
| 85 | 225.0  | 227.2  | 220.5  | 222.94 | 16499443.0 | 241.084 | 232.711 | 224.337 |  0  |   0   |   1   |
| 86 | 222.9  | 223.55 | 219.68 | 221.53 | 17060897.0 | 241.828 | 232.142 | 222.456 |  0  |   0   |   1   |
| 87 | 222.0  | 222.41 | 217.54 | 217.93 | 18231209.0 | 242.975 | 231.474 | 219.972 |  0  |   0   |   1   |
| 88 | 217.0  | 219.15 | 215.03 | 216.6  | 15135693.0 | 243.854 | 230.674 | 217.493 |  0  |   0   |   1   |
| 89 | 213.68 | 216.3  | 213.03 | 214.93 | 18294104.0 | 244.319 | 229.658 | 214.996 |  0  |   0   |   1   |
| 90 | 212.0  | 214.0  | 206.6  | 207.76 | 33679992.0 | 245.435 | 228.252 | 211.069 |  0  |   0   |   1   |
| 91 | 206.0  | 214.69 | 206.0  | 213.06 | 26593485.0 | 245.506 | 227.257 | 209.008 |  0  |   0   |   1   |
| 92 | 213.06 | 213.48 | 210.6  | 212.32 | 15024395.0 | 245.618 | 226.336 | 207.053 |  0  |   0   |   1   |
| 93 | 213.0  | 213.33 | 208.68 | 209.9  | 13087485.0 | 246.009 | 225.456 | 204.902 |  0  |   0   |   1   |
| 94 | 210.01 | 210.95 | 206.67 | 206.73 | 16214929.0 | 246.372 | 224.345 | 202.317 |  0  |   0   |   1   |
| 95 | 205.5  | 211.45 | 205.5  | 210.93 | 22509065.0 | 246.167 | 223.474 | 200.781 |  0  |   0   |   1   |
| 96 | 213.02 | 213.62 | 208.71 | 209.28 | 13605355.0 |  245.67 | 222.413 | 199.156 |  0  |   0   |   1   |
| 97 | 206.67 | 207.35 | 203.5  | 204.5  | 20253782.0 | 243.988 | 220.713 | 197.438 |  0  |   0   |   1   |
| 98 | 206.3  | 208.49 | 202.45 | 203.66 | 21848959.0 | 242.088 | 219.021 | 195.954 |  0  |   0   |   1   |
| 99 | 204.0  | 206.5  | 202.58 | 203.03 | 15809524.0 |  239.94 | 217.345 |  194.75 |  0  |   0   |   1   |
+----+--------+--------+--------+--------+------------+---------+---------+---------+-----+-------+-------+
请按任意键继续. . .

It can be seen from the running results that buying and selling signals can indeed be generated, and losses can also be stopped in time. Of course this is just a fragment. Quantitative trading also requires a combination of funds and positions to carry out buying and selling operations.


Summarize

Through demonstrations and running tests, we can see that using AI can assist us in writing strategy code, but there is still room for optimization of the code. I have tested some codes written by AI before, and they still need to be debugged. But at least AI has provided us with some ideas for converting text strategies into codes, and then combined with various quantitative trading APIs, we can conduct simulation tests.

If you want to make a real offer, everyone should be more cautious. After all, real money is not a joke, and AI will not bear losses!

Still the same sentence: There are risks in entering the market, so be cautious when trading!

Guess you like

Origin blog.csdn.net/popboy29/article/details/133266651