小米听说百度开放了文心一言AI,好奇的跑去问大麦:“文心一言都放开了,什么代码都可以写,你还学Python干嘛呀?”
大麦:“怎么不学?不学你知道代码怎么回事儿!”
“你学了这么久,你敢说有我的AI写得快么?”,小米一脸鄙夷地斜视着大麦,似乎在等着大麦哥哥洋相出尽。
“我不信,AI能写出我的策略”,大麦反驳道,小米心里有些失望,但更有些高兴。
“量化交易,不是一个AI就可以搞定的,AI对通用的算法也许很强,但涉及到个性的API调用,还是需要调试吧!”
小米一听,立马挑战道:“是骡子是马,拉出来溜溜!”
“这谁怕谁啊?!”大麦不甘示弱,不服气的反驳道。
“我让文心一言给你写段代码”,小麦认真起来,也是巾帼不让须眉。
“来,来,来,写写就写写!”,大麦也来劲了。
只见小米打开Mac,就给文心写了一段话,最后一个优雅的回车。AI一字一句斟酌着,很快,代码就如蹦豆子一般,潇潇洒洒地写出来了。
“来,拿去试试”,小米大方将代码送给大麦哥哥,迫不及待地想得到结果。
“这个只是个片段,但是可以参考,等下我给它改改,运行试试”,大麦并不在意,而是认真的修改起来,就好像策略代码是自己写的。
“快快,咋样啊?”,小米迫不及待的盯着大麦哥哥的电脑屏幕,恨不得立马看见结果。
“好了,还不赖啊!百度不负等待啊!这代码可以运行,不错不错!”,大麦哥哥再也不吝啬夸赞之词,同时对这小米微微一笑,频频点头示意,好似在说:“妹妹带来了宝贝啊!”
“这以后你不用愁了吧”,小米也为能帮到大麦哥哥心生欢喜。
大麦看着妹妹,眼中透漏着光:“嗯嗯,妹妹眼睛是真亮啊!”
AI已经发到到可以写代码了,那是不是也可以写写量化交易代码呢?
带着这个问题,我们试试文心一言。
提示:以下是本篇文章正文内容,下面案例可供参考
文心一言:有用、有趣、有温度,既能写文案、读文档,又能脑洞大开、答疑解惑,还能倾听你的故事、感受你的心声。快来和我对话吧!
这是百度AI的介绍,为了试一试AI的代码书写能力,我们准备让其根据给出的文字策略,编写python代码。
根据以下内容用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
这段代码同样使用了TA-Lib库中的BBANDS函数来计算布林线指标。首先计算出上轨、中轨和下轨,然后检查前一天的收盘价是否在中轨之下,当天的收盘价是否在中轨之上,如果是,则为买入信号。接着检查前一天的收盘价是否在上轨之下,当天的收盘价是否在上轨的0.5%之上,如果是,则为卖出信号。最后检查当天的收盘价是否在中轨之下,如果是,则为卖出信号。最后将买入和卖出信号添加到数据框中并返回。请注意,这里同样假设输入数据框df已经包含了收盘价数据,并且使用了np.where函数来实现条件判断。
由于AI代码并不包含数据提取和展示过程,我们这里增加一些代码,验证一下策略是否可执行,以及是否按要求执行策略。
'''
根据以下内容用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')
运行结果如下,Buy为1表示买入,Sell1为止盈卖出,Sell2为止损卖出:
+----+--------+--------+--------+--------+------------+---------+---------+---------+-----+-------+-------+
| 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 |
+----+--------+--------+--------+--------+------------+---------+---------+---------+-----+-------+-------+
请按任意键继续. . .
通过运行结果可以看出,确实可以生成买卖信号,而且也可以及时止损。当然这也还只是个片段。量化交易还需要结合资金和持仓情况才可以进行买卖操作。
通过演示和运行测试,可以看出,使用AI已可以辅助我们书写策略代码,但代码仍有优化的空间。之前还测试过一些AI写出的代码,还需要进行debug。不过至少AI给我们提供了一些将文字策略转变为代码的思路,再结合各种量化交易API就可以进行模拟测试了。
如果要进行实盘,大家还是要谨慎再谨慎,毕竟真金白银可不是闹着玩的额,AI可不承担损失!
还是那句话:入市有风险,交易需谨慎!