多线程监视多个turtle
This commit is contained in:
parent
fb18ccc986
commit
c9841014c1
@ -5,12 +5,11 @@ import os
|
||||
from datetime import datetime, timedelta, date
|
||||
import pandas as pd
|
||||
import mplfinance as mpf
|
||||
import sqlite3
|
||||
import stock_database
|
||||
import mysql_database
|
||||
from EmailTest import send_email, parse_return_email
|
||||
from dataclasses import dataclass
|
||||
import time
|
||||
import threading
|
||||
|
||||
'''
|
||||
todo
|
||||
@ -346,9 +345,10 @@ class TurtleTrading_OnTime(object):
|
||||
3、实时监测主流程
|
||||
'''
|
||||
|
||||
def __init__(self, turtle: TurtleTrading, user_email):
|
||||
self.turtle = turtle
|
||||
def __init__(self, turtles: list[TurtleTrading], user_email):
|
||||
self.turtles = turtles # List of TurtleTrading instances
|
||||
self.user_email = user_email
|
||||
self.email_events = {} # Track email response events for each turtle
|
||||
|
||||
def get_stocks_data(self):
|
||||
"""获取实时股票、基金数据,不保存
|
||||
@ -717,18 +717,22 @@ class TurtleTrading_OnTime(object):
|
||||
|
||||
# ------------------准备阶段--------------------
|
||||
# 获取数据或读取数据 -- 计算ATR Donchian 20 50 up, 20 down
|
||||
self.turtle.get_ready(100)
|
||||
self.turtle.N = float(self.turtle.CurrentData['ATR'].iloc[-1])
|
||||
self.turtle.prev_heigh = float(self.turtle.CurrentData['最高价'].iloc[-1])
|
||||
self.turtle.Donchian_20_up = float(self.turtle.CurrentData['Donchian_20_upper'].iloc[-1])
|
||||
self.turtle.Donchian_50_up = float(self.turtle.CurrentData['Donchian_50_upper'].iloc[-1])
|
||||
self.turtle.Donchian_10_down = float(self.turtle.CurrentData['Donchian_10_lower'].iloc[-1])
|
||||
self.turtle.CalPositionSize()
|
||||
# 初始化所有turtle
|
||||
for turtle in self.turtles:
|
||||
# 准备数据
|
||||
turtle.get_ready(100)
|
||||
turtle.N = float(turtle.CurrentData['ATR'].iloc[-1])
|
||||
turtle.prev_heigh = float(turtle.CurrentData['最高价'].iloc[-1])
|
||||
turtle.Donchian_20_up = float(turtle.CurrentData['Donchian_20_upper'].iloc[-1])
|
||||
turtle.Donchian_50_up = float(turtle.CurrentData['Donchian_50_upper'].iloc[-1])
|
||||
turtle.Donchian_10_down = float(turtle.CurrentData['Donchian_10_lower'].iloc[-1])
|
||||
turtle.CalPositionSize()
|
||||
# ------------------实时监测阶段--------------------
|
||||
# 9:00 1、判断是否是新的一周,是则重新计算Position Size
|
||||
# 判断是否是新的一周
|
||||
if datetime.now().weekday() == 0:
|
||||
self.turtle.CalPositionSize()
|
||||
for turtle in self.turtles:
|
||||
turtle.CalPositionSize()
|
||||
|
||||
# 每分钟获取一次数据,判断是否触发条件 9:30-11:30 13:00-15:00
|
||||
while True:
|
||||
@ -757,8 +761,9 @@ class TurtleTrading_OnTime(object):
|
||||
break
|
||||
|
||||
# 获取股票和ETF数据
|
||||
stock_data, etf_data = self.get_stocks_data()
|
||||
self.run_short_trading_loop(stock_data, etf_data)
|
||||
self.monitor_all_turtles()
|
||||
# 等待一段时间后再次检查
|
||||
time.sleep(60) # 每分钟检查一次
|
||||
# ------------------结束阶段--------------------
|
||||
# 数据库更新当天数据,增加ATR、donchian数据
|
||||
# 直接做个新表
|
||||
@ -766,6 +771,66 @@ class TurtleTrading_OnTime(object):
|
||||
self.turtle.get_ready(100)
|
||||
time.sleep(16.5*600)
|
||||
|
||||
|
||||
def monitor_all_turtles(self):
|
||||
"""主监控循环"""
|
||||
# 获取实时数据
|
||||
stock_data, etf_data = self.get_stocks_data()
|
||||
|
||||
# 遍历所有turtle进行监控
|
||||
for turtle in self.turtles:
|
||||
self.monitor_single_turtle(turtle, stock_data, etf_data)
|
||||
|
||||
|
||||
|
||||
def monitor_single_turtle(self, turtle: TurtleTrading, stock_data, etf_data):
|
||||
"""监控单个turtle的交易条件"""
|
||||
# 获取当前价格
|
||||
if turtle.TradeCode in stock_data['代码'].values:
|
||||
price_now = stock_data[stock_data['代码'] == turtle.TradeCode]['最新价'].values[0]
|
||||
elif turtle.TradeCode in etf_data['代码'].values:
|
||||
price_now = etf_data[etf_data['代码'] == turtle.TradeCode]['最新价'].values[0]
|
||||
else:
|
||||
return # 未找到对应标的
|
||||
|
||||
# 检查买入条件
|
||||
if self.system1EnterNormal(price_now, turtle.Donchian_20_ups, turtle.BreakOutLog):
|
||||
self.start_email_thread(turtle, "买入", price_now)
|
||||
|
||||
# 检查加仓条件
|
||||
if self.add(price_now):
|
||||
self.start_email_thread(turtle, "加仓", price_now)
|
||||
|
||||
# 检查止损条件
|
||||
if self.system_1_stop(price_now):
|
||||
self.start_email_thread(turtle, "止损", price_now)
|
||||
|
||||
def start_email_thread(self, turtle, action, price_now):
|
||||
"""启动邮件处理线程"""
|
||||
event = threading.Event()
|
||||
self.email_events[turtle.TradeCode] = event
|
||||
|
||||
thread = threading.Thread(
|
||||
target=self.handle_email_response,
|
||||
args=(turtle, action, price_now, event)
|
||||
)
|
||||
thread.start()
|
||||
|
||||
def handle_email_response(self, turtle, action, price_now, event):
|
||||
"""处理邮件响应的线程"""
|
||||
# 发送邮件
|
||||
if action == "买入":
|
||||
self.Buy_stock(price_now)
|
||||
elif action == "加仓":
|
||||
self.add_stock(price_now)
|
||||
elif action == "止损":
|
||||
self.stop_sale_stock(price_now)
|
||||
elif action == "止盈":
|
||||
self.out_sale_stock(price_now)
|
||||
|
||||
# 等待邮件响应
|
||||
event.wait()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
user_email = "guoyize2209@163.com"
|
||||
|
Loading…
x
Reference in New Issue
Block a user