修改调整
This commit is contained in:
@@ -1,9 +1,4 @@
|
||||
'''
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
|
||||
该代码的主要目的是处理Tick数据并生成交易信号。代码中定义了一个tickcome函数,它接收到Tick数据后会进行一系列的处理,包括构建Tick字典、更新上一个Tick的成交量、保存Tick数据、生成K线数据等。其中涉及到的一些函数有:
|
||||
|
||||
on_tick(tick): 处理单个Tick数据,根据Tick数据生成K线数据。
|
||||
@@ -11,10 +6,6 @@ tickdata(df, symbol): 处理Tick数据,生成K线数据。
|
||||
orderflow_df_new(df_tick, df_min, symbol): 处理Tick和K线数据,生成订单流数据。
|
||||
GetOrderFlow_dj(kData): 计算订单流的信号指标。
|
||||
除此之外,代码中还定义了一个MyTrader类,继承自TraderApiBase,用于实现交易相关的功能。
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
'''
|
||||
from multiprocessing import Process, Queue
|
||||
from AlgoPlus.CTP.MdApi import run_tick_engine
|
||||
@@ -44,6 +35,8 @@ import re # 新增
|
||||
import logging # 新增
|
||||
from logging.handlers import RotatingFileHandler # 新增
|
||||
|
||||
# from vnpy_tts import TtsGateway
|
||||
|
||||
|
||||
# 在文件顶部定义全局变量
|
||||
tickdatadict = {} # 存储Tick数据的字典
|
||||
@@ -60,6 +53,21 @@ AI_SIGNAL_QUEUE = ThreadQueue()
|
||||
AI_THREAD_RUNNING = False
|
||||
# 不再需要单独的K线时间粒度全局变量,它已经被整合到GLOBAL_LLM_CONFIG中
|
||||
|
||||
# 20250509: add feishu
|
||||
def send_feishu_message(text):
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
data = {
|
||||
"msg_type": "text",
|
||||
"content": {
|
||||
"text": text
|
||||
}
|
||||
}
|
||||
response = requests.post("https://open.feishu.cn/open-apis/bot/v2/hook/8608dfa4-e599-462a-8dba-6ac72873dd27", headers=headers, json=data)
|
||||
if response.status_code != 200:
|
||||
print(f"飞书消息发送失败,状态码: {response.status_code}, 响应内容: {response.text}")
|
||||
|
||||
def setup_logging(log_folder='logs'):
|
||||
# 确保日志文件夹存在
|
||||
if not os.path.exists(log_folder):
|
||||
@@ -191,10 +199,6 @@ def on_tick(tick):
|
||||
# 直接调用tickdata,不传递resample_rule参数,让其自行从全局配置获取
|
||||
tickdata(tick_dt, sym)
|
||||
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
|
||||
def data_of(df):
|
||||
global trader_df
|
||||
@@ -237,12 +241,7 @@ def process(bidDict, askDict, symbol):
|
||||
df = {'bidDictResult': bidDictResult, 'askDictResult': askDictResult}
|
||||
quotedict[symbol] = df
|
||||
|
||||
return bidDictResult, askDictResult
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
|
||||
return bidDictResult, askDictResult
|
||||
|
||||
def tickdata(df, symbol):
|
||||
tickdata =pd.DataFrame({'datetime':df['datetime'],'symbol':df['symbol'],'lastprice':df['lastprice'],
|
||||
@@ -305,10 +304,6 @@ def tickdata(df, symbol):
|
||||
orderflow_df_new(tickdata,bardata,symbol)
|
||||
# time.sleep(0.5)
|
||||
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
|
||||
def orderflow_df_new(df_tick,df_min,symbol):
|
||||
startArray = pd.to_datetime(df_min['starttime']).values
|
||||
@@ -380,11 +375,7 @@ def orderflow_df_new(df_tick,df_min,symbol):
|
||||
#df['ticktime']=tTickArray[0]
|
||||
df['dj'] = GetOrderFlow_dj(df)
|
||||
ofdatadict[symbol]=df
|
||||
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
|
||||
|
||||
def GetOrderFlow_dj(kData):
|
||||
Config = {
|
||||
@@ -973,12 +964,7 @@ class MyTrader(TraderApiBase):
|
||||
|
||||
def OnRspOrderInsert(self, pInputOrder, pRspInfo, nRequestID, bIsLast):
|
||||
print("||OnRspOrderInsert||", pInputOrder, pRspInfo, nRequestID, bIsLast)
|
||||
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
|
||||
|
||||
#注意:运行前请先安装好algoplus,
|
||||
# pip install AlgoPlus
|
||||
#http://www.algo.plus/ctp/python/0103001.html
|
||||
@@ -1173,13 +1159,7 @@ class MyTrader(TraderApiBase):
|
||||
AI_LOGGER.error(log_message)
|
||||
finally:
|
||||
# 标记线程已完成
|
||||
AI_THREAD_RUNNING = False
|
||||
|
||||
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
AI_THREAD_RUNNING = False
|
||||
|
||||
#注意:运行前请先安装好algoplus,
|
||||
# pip install AlgoPlus
|
||||
@@ -1220,6 +1200,11 @@ class MyTrader(TraderApiBase):
|
||||
for line in log_lines:
|
||||
print(line)
|
||||
SIGNAL_LOGGER.info(line)
|
||||
if action != '不操作':
|
||||
send_feishu_message(line)
|
||||
|
||||
if action != '不操作':
|
||||
send_feishu_message(f"开始执行交易,当前价格: 买一{data['BidPrice1']} / 卖一{data['AskPrice1']}")
|
||||
|
||||
# 确保合约在止损止盈字典中
|
||||
self._ensure_stop_order_dict(instrument_id)
|
||||
@@ -1236,6 +1221,7 @@ class MyTrader(TraderApiBase):
|
||||
|
||||
print(f"开始执行交易,当前价格: 买一{data['BidPrice1']} / 卖一{data['AskPrice1']}")
|
||||
|
||||
|
||||
# 根据不同交易行为执行相应操作
|
||||
if action == '开多' and current_stops['long']['position'] <= 0:
|
||||
self._handle_open_long(data, instrument_id, stop_loss, take_profit)
|
||||
@@ -1476,12 +1462,7 @@ class MyTrader(TraderApiBase):
|
||||
elif current_stops['short']['position'] > 0: # 空头持仓
|
||||
self.update_stop_order_dict(instrument_id, 'short', None, None, None, take_profit, None)
|
||||
print(f'已调整空头止盈价: {take_profit}')
|
||||
self.save_to_csv(instrument_id)
|
||||
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
self.save_to_csv(instrument_id)
|
||||
|
||||
#注意:运行前请先安装好algoplus,
|
||||
# pip install AlgoPlus
|
||||
@@ -1928,6 +1909,7 @@ class MyTrader(TraderApiBase):
|
||||
# 检查止损
|
||||
if stops['long']['stop_loss'] > 0 and current_bid <= stops['long']['stop_loss']:
|
||||
print(f"触发多头止损: {instrument_id}, 价格: {current_bid}, 止损价: {stops['long']['stop_loss']}")
|
||||
send_feishu_message(f"触发多头止损: {instrument_id}, 价格: {current_bid}, 止损价: {stops['long']['stop_loss']}")
|
||||
self.insert_order(data['ExchangeID'], data['InstrumentID'], current_bid-self.py,
|
||||
stops['long']['position'], b'1', b'3')
|
||||
# 清空多头持仓信息
|
||||
@@ -1937,6 +1919,7 @@ class MyTrader(TraderApiBase):
|
||||
# 检查跟踪止损 - 确保只在价格高于开仓价且低于跟踪止损价时触发
|
||||
elif stops['long']['trailing_stop'] > 0 and current_bid < stops['long']['trailing_stop'] and current_bid > entry_price:
|
||||
print(f"触发多头跟踪止损: {instrument_id}, 价格: {current_bid}, 跟踪止损价: {stops['long']['trailing_stop']}, 开仓价: {entry_price}")
|
||||
send_feishu_message(f"触发多头跟踪止损: {instrument_id}, 价格: {current_bid}, 跟踪止损价: {stops['long']['trailing_stop']}, 开仓价: {entry_price}")
|
||||
self.insert_order(data['ExchangeID'], data['InstrumentID'], current_bid-self.py,
|
||||
stops['long']['position'], b'1', b'3')
|
||||
# 清空多头持仓信息
|
||||
@@ -1946,6 +1929,7 @@ class MyTrader(TraderApiBase):
|
||||
# 检查止盈
|
||||
elif stops['long']['take_profit'] > 0 and current_bid >= stops['long']['take_profit']:
|
||||
print(f"触发多头止盈: {instrument_id}, 价格: {current_bid}, 止盈价: {stops['long']['take_profit']}")
|
||||
send_feishu_message(f"触发多头止盈: {instrument_id}, 价格: {current_bid}, 止盈价: {stops['long']['take_profit']}")
|
||||
self.insert_order(data['ExchangeID'], data['InstrumentID'], current_bid-self.py,
|
||||
stops['long']['position'], b'1', b'3')
|
||||
# 清空多头持仓信息
|
||||
@@ -1977,6 +1961,7 @@ class MyTrader(TraderApiBase):
|
||||
# 检查止损
|
||||
if stops['short']['stop_loss'] > 0 and current_ask >= stops['short']['stop_loss']:
|
||||
print(f"触发空头止损: {instrument_id}, 价格: {current_ask}, 止损价: {stops['short']['stop_loss']}")
|
||||
send_feishu_message(f"触发空头止损: {instrument_id}, 价格: {current_ask}, 止损价: {stops['short']['stop_loss']}")
|
||||
self.insert_order(data['ExchangeID'], data['InstrumentID'], current_ask+self.py,
|
||||
stops['short']['position'], b'0', b'3')
|
||||
# 清空空头持仓信息
|
||||
@@ -1986,6 +1971,7 @@ class MyTrader(TraderApiBase):
|
||||
# 检查跟踪止损 - 确保只在价格低于开仓价且高于跟踪止损价时触发
|
||||
elif stops['short']['trailing_stop'] > 0 and current_ask > stops['short']['trailing_stop'] and current_ask < entry_price:
|
||||
print(f"触发空头跟踪止损: {instrument_id}, 价格: {current_ask}, 跟踪止损价: {stops['short']['trailing_stop']}, 开仓价: {entry_price}")
|
||||
send_feishu_message(f"触发空头跟踪止损: {instrument_id}, 价格: {current_ask}, 跟踪止损价: {stops['short']['trailing_stop']}, 开仓价: {entry_price}")
|
||||
self.insert_order(data['ExchangeID'], data['InstrumentID'], current_ask+self.py,
|
||||
stops['short']['position'], b'0', b'3')
|
||||
# 清空空头持仓信息
|
||||
@@ -1995,6 +1981,7 @@ class MyTrader(TraderApiBase):
|
||||
# 检查止盈
|
||||
elif stops['short']['take_profit'] > 0 and current_ask <= stops['short']['take_profit']:
|
||||
print(f"触发空头止盈: {instrument_id}, 价格: {current_ask}, 止盈价: {stops['short']['take_profit']}")
|
||||
send_feishu_message(f"触发空头止盈: {instrument_id}, 价格: {current_ask}, 止盈价: {stops['short']['take_profit']}")
|
||||
self.insert_order(data['ExchangeID'], data['InstrumentID'], current_ask+self.py,
|
||||
stops['short']['position'], b'0', b'3')
|
||||
# 清空空头持仓信息
|
||||
@@ -2501,10 +2488,6 @@ def clean_log_directory(log_dir="./log", timeout_seconds=5):
|
||||
return success
|
||||
|
||||
if __name__ == '__main__':
|
||||
#公众号:松鼠Quant
|
||||
#主页:www.quant789.com
|
||||
#本策略仅作学习交流使用,实盘交易盈亏投资者个人负责!!!
|
||||
#版权归松鼠Quant所有,禁止转发、转卖源码违者必究。
|
||||
|
||||
# 清理日志目录
|
||||
clean_log_directory(log_dir="./log", timeout_seconds=10) # 可以调整超时时间
|
||||
@@ -2518,7 +2501,7 @@ if __name__ == '__main__':
|
||||
# 配置大模型参数 o3 代理地址: https://2233.ai/i/9ONWNBDK
|
||||
# deepseek模型地址:https://www.deepseek.com/ 右上角进入API开放平台创建APIKEY
|
||||
|
||||
api_key = "" #配置大模型API密钥
|
||||
api_key = "sk-6a28007843344b44b028297bf349459c" #配置大模型API密钥
|
||||
api_url = "https://api.deepseek.com" #配置大模型API地址
|
||||
api_model = "deepseek-chat" # 大模型名称
|
||||
|
||||
@@ -2538,10 +2521,10 @@ if __name__ == '__main__':
|
||||
|
||||
# simnow的future_account字典,如果实盘请注释掉simnow的future_account字典
|
||||
future_account = get_simulate_account(
|
||||
investor_id='', #Simnow的账号,注意不是注册账号,是网站登录SIMNOW后,显示的投资者ID
|
||||
password='', #Simnow的密码,你注册时填写的密码
|
||||
investor_id='223828', #Simnow的账号,注意不是注册账号,是网站登录SIMNOW后,显示的投资者ID
|
||||
password='Zj1234!@#%', #Simnow的密码,你注册时填写的密码
|
||||
server_name='电信1',# 电信1、电信2、移动、TEST、N视界
|
||||
subscribe_list= [b'au2506'], # 订阅合约列表,注意是bytes类型,例如b'au2506'
|
||||
subscribe_list= [b'IM2506'], # 订阅合约列表,注意是bytes类型,例如b'IM2505'
|
||||
md_flow_path=temp_md_dir,
|
||||
td_flow_path=temp_td_dir,
|
||||
)
|
||||
@@ -2556,7 +2539,22 @@ if __name__ == '__main__':
|
||||
# password='', # 密码
|
||||
# app_id='simnow_client_test', # 认证使用AppID
|
||||
# auth_code='0000000000000000', # 认证使用授权码
|
||||
# subscribe_list=[b'au2506'], # 订阅合约列表
|
||||
# subscribe_list=[b'IM2505'], # 订阅合约列表
|
||||
# md_flow_path=temp_md_dir, # MdApi流文件存储地址,默认MD_LOCATION
|
||||
# td_flow_path=temp_td_dir, # TraderApi流文件存储地址,默认TD_LOCATION
|
||||
# )
|
||||
|
||||
#TTS系统CTP程序只要替换一下ctp动态库即可接入TTS系统。BrokerID: 不用填,AppID: 不用填,AuthCode: 不用填:
|
||||
# 1、 openctp-7x24:TradeFront:tcp://121.37.80.177:20002;MarketFront:tcp://121.37.80.177:20004;1、 openctp-仿真:TradeFront:tcp://121.37.90.193:20002
|
||||
# future_account = FutureAccount(
|
||||
# broker_id='', # 期货公司BrokerID
|
||||
# server_dict={'TDServer': "121.37.80.177:20002", 'MDServer': '121.37.80.177:20004'}, # TDServer为交易服务器,MDServer为行情服务器。服务器地址格式为"ip:port。"
|
||||
# reserve_server_dict={}, # 备用服务器地址
|
||||
# investor_id='1148', # 账户
|
||||
# password='123456', # 密码
|
||||
# app_id='', # 认证使用AppID
|
||||
# auth_code='', # 认证使用授权码
|
||||
# subscribe_list=[b'IM2505'], # 订阅合约列表
|
||||
# md_flow_path=temp_md_dir, # MdApi流文件存储地址,默认MD_LOCATION
|
||||
# td_flow_path=temp_td_dir, # TraderApi流文件存储地址,默认TD_LOCATION
|
||||
# )
|
||||
|
||||
Reference in New Issue
Block a user