PYTHON写代码 连接MYSQL host='localhost', user='root', password='123456', database='STOCK_CODES', port=3306, charset='utf8mb4' 读取MYSQL表格“stock_k_data” 里面包含的字段有date, time, code, open, high, low, close, volume, amount,price,status price 用来记录成交价格 status 来记录状态“买入”或“卖出”或“保持买入”或“空仓”或“保持卖入” 第一行price=close,status=“买入” 从第二行开始,当股票从最高点冲高回踩5%,(status=“卖出,price=high*0.95),否则是“保持”, 循环直到卖出, 当状态=“空仓”时 从第二行开始,价格=close,当股票从最低点冲高回踩5%,(status=买入,否则是“保持买入”记录最高值
时间: 2024-01-21 15:18:26 浏览: 40
zip-codes-database-FREE-csv_database_zip_zipcodes_
以下是Python代码实现:
```python
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
database="STOCK_CODES",
port=3306,
charset='utf8mb4'
)
# 获取数据表
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM stock_k_data")
myresult = mycursor.fetchall()
# 初始化数据
price = myresult[0][6]
status = "买入"
highest_price = price
lowest_price = myresult[0][5]
# 循环计算
for row in myresult[1:]:
if status == "卖出":
price = row[5] * 0.95
if price >= highest_price:
highest_price = price
else:
status = "保持卖出"
elif status == "空仓":
price = row[6]
if row[5] <= lowest_price * 0.95:
status = "买入"
lowest_price = row[5]
else:
status = "保持买入"
else:
if row[4] >= highest_price:
highest_price = row[4]
if row[5] <= highest_price * 0.95:
price = highest_price * 0.95
status = "卖出"
else:
status = "保持"
# 输出结果
print(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], price, status)
```
该代码会从 MySQL 数据库中读取 `stock_k_data` 表格,然后根据上述规则计算价格和状态,并输出结果。
阅读全文