python - 记录从0开始学量化交易

爬取当前价格

import requests
import json

# Binance API endpoint for BTCUSDT ticker price
url = 'https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT'

# Function to get real-time BTCUSDT price
def get_btcusdt_price():
    try:
        response = requests.get(url)
        data = json.loads(response.text)
        btcusdt_price = float(data['price'])
        return btcusdt_price
    except:
        print('Error retrieving BTCUSDT price')
        return None

# Example usage
btcusdt_price = get_btcusdt_price()
if btcusdt_price:
    print('BTCUSDT price:', btcusdt_price)

简单的股票做T小程序

  • 提供了window界面
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QTextEdit, QPushButton


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = '计算股票做T盈利'
        self.left = 10
        self.top = 10
        self.width = 400
        self.height = 300
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # 输入框
        self.buy_price_label = QLabel('买入价格:', self)
        self.buy_price_label.move(20, 20)
        self.buy_price = QLineEdit(self)
        self.buy_price.move(120, 20)

        self.buy_quantity_label = QLabel('买入股数:', self)
        self.buy_quantity_label.move(20, 50)
        self.buy_quantity = QLineEdit(self)
        self.buy_quantity.move(120, 50)

        self.sell_price_label = QLabel('卖出价格:', self)
        self.sell_price_label.move(20, 80)
        self.sell_price = QLineEdit(self)
        self.sell_price.move(120, 80)

        self.sell_quantity_label = QLabel('卖出股数:', self)
        self.sell_quantity_label.move(20, 110)
        self.sell_quantity = QLineEdit(self)
        self.sell_quantity.move(120, 110)

        # 文本框
        self.result = QTextEdit(self)
        self.result.move(20, 150)
        self.result.setReadOnly(True)

        # 计算按钮
        self.calculate_button = QPushButton('计算', self)
        self.calculate_button.setGeometry(20, 270, 80, 30)
        self.calculate_button.clicked.connect(self.calculate_profit)
        self.calculate_button.setStyleSheet("background-color: #4CAF50; color: white; font-weight: bold;")

        #self.calculate_button.mousePressEvent = self.calculate_profit

        self.show()

    def calculate_profit(self, event):
        try:
            buy_price = float(self.buy_price.text())
            buy_quantity = float(self.buy_quantity.text())
            sell_price = float(self.sell_price.text())
            sell_quantity = float(self.sell_quantity.text())
            profit = sell_price * sell_quantity - buy_price * buy_quantity - (sell_price * sell_quantity * 0.0016 + buy_price * buy_quantity * 0.0016)
            self.result.setText(f'盈利:{
      
      profit:.2f}元')
        except ValueError:
            self.result.setText('请输入正确的数字')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
  • 安装 PyInstaller
 pip install pyinstaller
  • 生成 exe文件
pyinstaller --onefile main.py

安装TA-LIB

TA-Lib,全称“Technical Analysis Library”, 即技术分析库,是Python金融量化的高级库,涵盖了150多种股票、期货交易软件中常用的技术分析指标,如MACD、RSI、KDJ、动量指标、布林带等等。

  1. 下载 TA_Lib-0.4.24-cp39-cp39-win_amd64.whl
    39即python的版本为3.9,然后操作系统为64位
    在这里插入图片描述
  2. 下载后在cmd打开对应文件夹并安装
cd D:\LHJY
d:
pip install TA_Lib-0.4.24-cp39-cp39-win_amd64.whl

猜你喜欢

转载自blog.csdn.net/qq_43382350/article/details/130186077
今日推荐