Python编程股票自动交易实现盈利30% ChatGPT + TradingView + PineScript交易自动化

前言

貌似油管上使用ChatGPT制定交易策略及编程,从而实现股票交易自动化并获得盈利的视频很火爆。今天看到的这款视频采用ChatGPT+TradingView+PineScript编程模拟交易并评估盈利状况。本文简单介绍该视频内容后,详细介绍如何用Python实现并改进该视频中的策略,用特斯拉股票分时数据来模拟股票交易并获得盈利30%。

目录

ChatGPT+TradingView+PineScript股票交易自动化

Python编程股票自动交易实现盈利30%

名词解释

VWAP成交量加权平均价格(Volume Weighted Average Price)

RSI相对强弱指数(Relative Strength Index)

Trading View

ChatGPT+TradingView+PineScript股票交易自动化

视频:ChatGPT AI Made Me A $100,000 TRADING STRATEGY, Humbled Trader, 关注人数:102万人,视频播放量:61万次。

网址:https://www.youtube.com/watch?v=Jh5rJskkEkU

主要交易工具:

Trading View + Pine script programming language

Thinkorswim by TDAmeritrade

Trading strategy: VWAP and volumn 交易策略:VWAP 和 交易量

- sell the long when position increased 3%, close the short when position also increased 3% 当位置增加3%时卖出多头,当位置同样增加3%时平仓空头 → 关于这个策略作者有疑问!!!

- buy when the price crosses over VWAP, short when the price crosses below VWAP 当价格穿过 VWAP 时买入,当价格跌破 VWAP 时卖出(买入时机就是所谓的golden cross 金叉!)

- ChatGPT to create entries and exits on Trading View Pine script 用 ChatGPT生成Trading View的Pine Script程序,在其中包含了卖出和买入的点

Python编程股票自动交易实现盈利30%

Source: ChatGPT Trading Strategy Fully Backtested with Python, Lachezar Haralampiev, MSc, Quant Factory https://medium.com/quant-factory/chatgpt-trading-strategy-fully-backtested-with-python-70e6769ab550

使用特斯拉股票分时数据,用VWAP及RSI来判断应该卖出还是买进。基本策略是基于上一章介绍的油管视频,加上了RSI作为判断材料。加上RSI之前模拟结果为年亏损-52.6%,加上RSI后模拟结果为年盈利30.7%。

加上RSI之前年亏损-52.6%:

图片

图片

加上RSI作为判断材料后年盈利30.7%:

图片

图片

图片

特斯拉分时数据免费下载网址:

https://www.quantfactory.ai/p/tesla-hourly-data

Python程序(加入RSI之前和之后):

# Load the packages we would need for the whole project.

import datetime as dt

import pandas as pd

import numpy as np

import pandas_ta

tsla_prices_df = pd.read_csv(‘tsla_1h_prices.csv’)

# Calculate VWAP indicator and then first strategy signal.

tsla_prices_df[‘typical_price’] = (tsla_prices_df[‘high’]+tsla_prices_df[‘low’]+tsla_prices_df[‘close’])/3

tsla_prices_df[‘typical_price_volume’] = tsla_prices_df[‘typical_price’]*tsla_prices_df[‘volume’]

# 在一个名为tsla_prices_df的DataFrame中创建了一个新的列cumm_price_volume

# 这个新的列的值是由typical_price_volume列的滚动20行的总和计算而来(rolling(20).sum()函数)

tsla_prices_df[‘cumm_price_volume’] = tsla_prices_df[‘typical_price_volume’].rolling(20).sum()

tsla_prices_df[‘cumm_volume’] = tsla_prices_df[‘volume’].rolling(20).sum()

tsla_prices_df[‘vwap’] = tsla_prices_df[‘cumm_price_volume’]/tsla_prices_df[‘cumm_volume’]

tsla_prices_df

# Close/VWAP crossover signal.

# shift()函数在名为tsla_prices_df的DataFrame中创建了一个新的列close_lag_1

# 这个新列的值是上一行的close列的值

tsla_prices_df[‘close_lag_1’] = tsla_prices_df[‘close’].shift()

tsla_prices_df[‘vwap_lag_1’] = tsla_prices_df[‘vwap’].shift()

tsla_prices_df[‘signal_1’] = tsla_prices_df.apply(lambda x: 1

​ if (x[‘close_lag_1’]<x[‘vwap_lag_1’])&(x[‘close’]>x[‘vwap’])

​ else (-1 if (x[‘close_lag_1’]>x[‘vwap_lag_1’])&

​ (x[‘close’]<x[‘vwap’]) else np.nan),

​ axis=1)

#加入RSI作为判断指标

# Improve the strategy by adding RSI crossover condition — above/below 50.

# RSI Above/Below 50 crossover signal.

# 使用pandas_ta库(一个为pandas库增加技术分析功能的库)来计算相对强弱指数(Relative Strength Index, RSI)

tsla_prices_df[‘rsi’] = pandas_ta.rsi(close=tsla_prices_df[‘close’],

​ length=20)

tsla_prices_df[‘rsi_lag_1’] = tsla_prices_df[‘rsi’].shift()

tsla_prices_df[‘signal_2’] = tsla_prices_df.apply(lambda x: 1

​ if (x[‘rsi’]>50) & (x[‘rsi_lag_1’]<50)

​ else (-1 if (x[‘rsi’]<50) & (x[‘rsi_lag_1’]>50) else np.nan),

​ axis=1)

tsla_prices_df[[‘close’, ‘vwap’]].plot(figsize=(16,4), lw=2)

tsla_prices_df.apply(lambda x: x[‘open’]

​ if (x[‘signal_1’]==1)&(x[‘signal_2’]==1)

​ else np.nan, axis=1).plot(marker=‘^’,

​ color=‘green’,

​ ms=10)

tsla_prices_df.apply(lambda x: x[‘close’]

​ if (x[‘signal_1’]-1)*(x[‘signal_2’]-1)

​ else np.nan, axis=1).plot(marker=‘v’,

​ color=‘red’,

​ ms=10)

plt.title(‘TSLA Stock Price’)

plt.show()

名词解释

VWAP成交量加权平均价格(Volume Weighted Average Price)

图片

RSI相对强弱指数(Relative Strength Index)

图片

Trading View交易软件

图片

-END-


学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、机器学习、自动化测试带你从零基础系统性的学好Python!

👉[CSDN大礼包:《python安装工具&全套学习资料》免费分享]安全链接,放心点击

👉Python学习大礼包👈

在这里插入图片描述

👉Python学习路线汇总👈

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(全套教程文末领取哈)
在这里插入图片描述

👉Python必备开发工具👈

在这里插入图片描述

温馨提示:篇幅有限,已打包文件夹,获取方式在:文末

👉Python实战案例👈

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

在这里插入图片描述

👉Python书籍和视频合集👈

观看零基础学习书籍和视频,看书籍和视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

在这里插入图片描述

👉Python面试刷题👈

👉Python副业兼职路线👈

在这里插入图片描述
在这里插入图片描述
这份完整版的Python全套学习资料已经上传CSDN,朋友们如果需要可以点击链接免费领取或者保存图片到wx扫描二v码免费领取保证100%免费

👉[CSDN大礼包:《python安装工具&全套学习资料》免费分享]安全链接,放心点击

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值