> 文章列表 > python - 记录从0开始学量化交易

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

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

爬取当前BTCUSDT永续价格

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_priceexcept: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, QPushButtonclass App(QWidget):def __init__(self):super().__init__()self.title = '计算股票做T盈利'self.left = 10self.top = 10self.width = 400self.height = 300self.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_profitself.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