首先,我们先对网格交易进行一个简单的介绍
网格交易是一种被广泛使用的交易策略,其基本思想是建立一系列买入价和卖出价的网格,并在这些价位之间进行买卖操作。网格交易旨在在市场波动性较大的情况下稳定盈利,尤其适合范围市场。
在网格交易中,投资者首先设定一个交易区间,包括一个最高价和一个最低价。然后,将这个区间划分成多个小网格,并在每个小网格内设置买入价和卖出价。例如,在一个价格区间为100到110的交易中,可以将其划分成10个等分的价格网格,并在每个小网格内设置买入价和卖出价。
当市场价格接近或突破买入价时,投资者将买入一定数量的资产,当价格接近或突破卖出价时,则将卖出资产,以获得盈利。当资产在价格区间内波动时,投资者可以通过不断地买入和卖出,在网格中获取稳定的盈利。
网格交易的优点在于,它可以在市场波动性较大时保持稳定的收益率,并避免因价格波动导致的交易风险。同时,网格交易还能够在市场价格持续波动时增加交易机会,提高交易效率。
但是,网格交易也存在一定的风险。如果市场价格持续向上或向下波动,买入或卖出的资产数量也将不断增加,从而导致交易成本的增加。此外,当市场价格发生大幅波动时,网格交易可能无法及时反应,从而导致盈利机会的损失。
综上所述,网格交易是一种常见的交易策略,它能够在市场波动性较大时稳定盈利,但也需要投资者掌握一定的技巧和经验才能取得好的效果。
现在,正文开始,以下代码可以直接运行
首先需要配置json
{
"测试":"股票",
"自动网格":"真",
"运行策略":"可转债",
"股票名称":["三一重工","北方稀土"],
"股票代码":["600031","600111"],
"ETF名称":["传媒ETF","光伏ETF"],
"ETF代码":["159805","515790"],
"可转债名称":["小康转债","莱克转债"],
"可转债代码":["113016","113659"],
"期间天数":20,
"期间上限":20,
"期间下限":16,
"价格中枢":18,
"格子总数":8,
"单位格子":0.5,
"buy_1_price":17.5,
"buy_1_volume":100,
"buy_2_price":17,
"buy_2_volume":100,
"buy_3_price":16.5,
"buy_3_volume":100,
"buy_4_price":16,
"buy_4_volume":100,
"sell_1_price":18.5,
"sell_1_volume":100,
"sell_2_price":19,
"sell_2_volume":100,
"sell_3_price":19.5,
"sell_3_volume":100,
"sell_4_price":20,
"sell_4_volume":100,
"同花顺下单路径":"C:/同花顺软件/同花顺/xiadan.exe",
"qq":"65133625@qq.com",
"定时运行":"09:40",
"配置声明":"如果自动网格采用为真,自己配置的网格无效,程序会自动生产网格区间,用最近20日的最高点最为价格上限,最低点最为价格下限,期间平均分为8份,买卖各4份",
"测试声明":"需要测试为是进行测试,不然直接运行程序交易,测试类型支持全部,股票,ETF,可转债,无",
"其他声明":"如果自动网格为假,代码列表智能输入一个代码,在推其他参数进行配置",
"运行策略说明":"可以选择全部/股票/ETF/可转债/无"}
网格计算的盈利股票为例子
def cacal_stock_grid_data(self,stock='600031',start_date='20210101',end_date='20500101',data_type='D',n=20):
'''
计算股票网格策略
'''
df=self.get_stock_hist_data_em(stock=stock,start_date=start_date,end_date=end_date,data_type=data_type)
df['最高价']=df['close'].rolling(window=n).max()
df['最低价']=df['close'].rolling(window=n).min()
data_dict={}
data_dict['股票代码']=stock
data_dict['期间天数']=n
data_dict['期间上限']=df['最高价'].tolist()[-1]
data_dict['期间下限']=df['最低价'].tolist()[-1]
data_dict['价格中枢']=(df['最高价'].tolist()[-1]+df['最低价'].tolist()[-1])/2
data_dict['格子总数']=8
data_dict['单位格子']=(df['最高价'].tolist()[-1]-df['最低价'].tolist()[-1])/8
for i in range(1,5):
data_dict['buy_{}_price'.format(i)]=data_dict['价格中枢']-i*data_dict['单位格子']
data_dict['buy_{}_volume'.format(i)]=100
for i in range(1,5):
data_dict['sell_{}_price'.format(i)]=data_dict['价格中枢']+i*data_dict['单位格子']
data_dict['sell_{}_volume'.format(i)]=100
print(data_dict)
with open(r'股票分析数据\{}.txt'.format(stock),'w+',encoding='utf-8') as f:
f.write(str(data_dict))
return data_dict
网格策略的交易程序
import time
from datetime import datetime
import schedule
#核心数据
from stock_data import stock_data
import pandas as pd
#easytrader交易,改进
from easytrader_trader import trader_frame
import numpy as np
#可转债数据
from bond_cov_data import bond_cov_data
import json
#import tarder_bound
class trader_strategy:
def __init__(self,qq='1029762153@qq.com',path=r'C:\同花顺软件\同花顺\xiadan.exe'):
self.qq=qq
self.exe=path
self.tarder=''
self.stock_data=stock_data(qq=qq)
#可转债数据
self.bond_cov_data=bond_cov_data()
def connect(self):
'''
连接同花顺
'''
try:
a=trader_frame(exe=self.exe)
a.connect()
self.tarder=a
print('连接同花顺成功')
return True
except:
print('连接同花顺失败')
return False
def run_stock_trader_strategy(self):
'''
运行交易策略 股票
'''
if self.stock_data.check_is_trader_date()==True:
with open(r'交易配置.json','r+',encoding='utf-8') as f:
text=f.read()
trader_set=json.loads(text)
if trader_set['自动网格']=='真':
for stock in trader_set['股票代码']:
trader_set=self.stock_data.cacal_stock_grid_data(stock=stock,start_date='20210101',end_date='20500101')
price=self.stock_data.get_stock_now_data(code=stock)['最新价'].tolist()[-1]
#买入的情况
if price<trader_set['价格中枢'] and price>=trader_set['buy_1_price']:
amount=trader_set['buy_1_volume']
text='买入一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_1_price'] and price>=trader_set['buy_2_price']:
amount=trader_set['buy_2_volume']
text='买入二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_3_price'] and price>=trader_set['buy_4_price']:
amount=trader_set['buy_4_volume']
text='买入四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
#卖出的情况
elif price>trader_set['价格中枢'] and price<=trader_set['sell_1_price']:
amount=trader_set['sell_1_volume']
text='卖出一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_1_price'] and price<=trader_set['sell_2_price']:
amount=trader_set['sell_2_volume']
text='卖出二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_2_price'] and price<=trader_set['sell_3_price']:
amount=trader_set['sell_3_volume']
text='卖出三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_3_price'] and price<=trader_set['sell_4_price']:
amount=trader_set['sell_4_volume']
text='卖出四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
else:
pass
else:
stock=trader_set['股票代码'][0]
price=self.stock_data.get_stock_now_data(code=stock)['最新价'].tolist()[-1]
#买入的情况
if price<trader_set['价格中枢'] and price>=trader_set['buy_1_price']:
amount=trader_set['buy_1_volume']
text='买入一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_1_price'] and price>=trader_set['buy_2_price']:
amount=trader_set['buy_2_volume']
text='买入二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_3_price'] and price>=trader_set['buy_4_price']:
amount=trader_set['buy_4_volume']
text='买入四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
#卖出的情况
elif price>trader_set['价格中枢'] and price<=trader_set['sell_1_price']:
amount=trader_set['sell_1_price']
text='卖出一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_1_price'] and price<=trader_set['sell_2_price']:
amount=trader_set['sell_2_price']
text='卖出二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_2_price'] and price<=trader_set['sell_3_price']:
amount=trader_set['sell_3_price']
text='卖出三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_3_price'] and price<=trader_set['sell_4_price']:
amount=trader_set['sell_4_price']
text='卖出四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
else:
pass
else:
print('目前不是交易时间')
def run_ETF_trader_strategy(self):
'''
运行交易策略,ETF基金
'''
if self.stock_data.check_is_trader_date()==True:
with open(r'交易配置.json','r+',encoding='utf-8') as f:
text=f.read()
trader_set=json.loads(text)
if trader_set['自动网格']=='真':
for stock in trader_set['ETF代码']:
trader_set=self.stock_data.cacal_ETF_grid_data(stock=stock,end_date='20500101',limit=10000,data_type='D')
price=self.stock_data.get_ETF_spot_em(stock=stock)['最新价'].tolist()[-1]
#买入的情况
if price<trader_set['价格中枢'] and price>=trader_set['buy_1_price']:
amount=trader_set['buy_1_volume']
text='买入一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_1_price'] and price>=trader_set['buy_2_price']:
amount=trader_set['buy_2_volume']
text='买入二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_3_price'] and price>=trader_set['buy_4_price']:
amount=trader_set['buy_4_volume']
text='买入四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
#卖出的情况
elif price>trader_set['价格中枢'] and price<=trader_set['sell_1_price']:
amount=trader_set['sell_1_price']
text='卖出一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_1_price'] and price<=trader_set['sell_2_price']:
amount=trader_set['sell_2_volume']
text='卖出二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_2_price'] and price<=trader_set['sell_3_price']:
amount=trader_set['sell_3_volumee']
text='卖出三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_3_price'] and price<=trader_set['sell_4_price']:
amount=trader_set['sell_4_volume']
text='卖出四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
else:
pass
else:
stock=trader_set['ETF代码'][0]
price=price=self.stock_data.get_ETF_spot_em(stock=stock)['最新价'].tolist()[-1]
#买入的情况
if price<trader_set['价格中枢'] and price>=trader_set['buy_1_price']:
amount=trader_set['buy_1_volume']
text='买入一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_1_price'] and price>=trader_set['buy_2_price']:
amount=trader_set['buy_2_volume']
text='买入二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_3_price'] and price>=trader_set['buy_4_price']:
amount=trader_set['buy_4_volume']
text='买入四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
#卖出的情况
elif price>trader_set['价格中枢'] and price<=trader_set['sell_1_price']:
amount=trader_set['sell_1_volume']
text='卖出一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_1_price'] and price<=trader_set['sell_2_price']:
amount=trader_set['sell_2_volume']
text='卖出二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_2_price'] and price<=trader_set['sell_3_price']:
amount=trader_set['sell_3_volume']
text='卖出三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_3_price'] and price<=trader_set['sell_4_price']:
amount=trader_set['sell_4_volume']
text='卖出四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
else:
pass
else:
print('目前不是交易时间')
def run_cov_bond_trader_strategy(self):
'''
运行交易策略,可转债
'''
if self.stock_data.check_is_trader_date()==True:
with open(r'交易配置.json','r+',encoding='utf-8') as f:
text=f.read()
trader_set=json.loads(text)
if trader_set['自动网格']=='真':
for stock in trader_set['可转债代码']:
trader_set=self.stock_data.cacal_bond_cov_grid_data(stock=stock,end_date='20500101',limit=10000,data_type='D')
price=self.stock_data.get_cov_bond_spot(stock=stock)['最新价']
#买入的情况
if price<trader_set['价格中枢'] and price>=trader_set['buy_1_price']:
amount=trader_set['buy_1_volume']
text='买入一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_1_price'] and price>=trader_set['buy_2_price']:
amount=trader_set['buy_2_volume']
text='买入二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_3_price'] and price>=trader_set['buy_4_price']:
amount=trader_set['buy_4_volume']
text='买入四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
#卖出的情况
elif price>trader_set['价格中枢'] and price<=trader_set['sell_1_price']:
amount=trader_set['sell_1_volume']
text='卖出一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_1_price'] and price<=trader_set['sell_2_price']:
amount=trader_set['sell_2_volume']
text='卖出二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_2_price'] and price<=trader_set['sell_3_price']:
amount=trader_set['sell_3_volume']
text='卖出三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_3_price'] and price<=trader_set['sell_4_price']:
amount=trader_set['sell_4_volume']
text='卖出四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
else:
pass
else:
stock=trader_set['可转债代码'][0]
price=self.stock_data.get_cov_bond_spot(stock=stock)['最新价']
#买入的情况
if price<trader_set['价格中枢'] and price>=trader_set['buy_1_price']:
amount=trader_set['buy_1_volume']
text='买入一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_1_price'] and price>=trader_set['buy_2_price']:
amount=trader_set['buy_2_volume']
text='买入二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_2_price'] and price>=trader_set['buy_3_price']:
amount=trader_set['buy_3_volume']
text='买入三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
elif price<trader_set['buy_3_price'] and price>=trader_set['buy_4_price']:
amount=trader_set['buy_4_volume']
text='买入四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
#卖出的情况
elif price>trader_set['价格中枢'] and price<=trader_set['sell_1_price']:
amount=trader_set['sell_1_volume']
text='卖出一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_1_price'] and price<=trader_set['sell_2_price']:
amount=trader_set['sell_2_volume']
text='卖出二档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_2_price'] and price<=trader_set['sell_3_price']:
amount=trader_set['sell_3_volume']
text='卖出三档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
elif price>trader_set['sell_3_price'] and price<=trader_set['sell_4_price']:
amount=trader_set['sell_4_volume']
text='卖出四档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.sell(security=stock,price=price,amount=amount)
else:
amount=trader_set['buy_1_volume']
text='买入一档 时间{} 代码{} 价格{} 数量{}'.format(str(datetime.now()),stock,price,amount)
print(text)
self.tarder.buy(security=stock,price=price,amount=amount)
else:
print('目前不是交易时间')
def seed_qq_email(self):
if self.stock_data.check_is_trader_date()==True:
if self.connect()==True:
now=str(datetime.now())
text=now+'程序连接正常'
print(text)
else:
self.connect()
else:
print('目前不是交易时间')
print(datetime.now())
if __name__=='__main__':
'''
交易策略
'''
print('程序准备运行')
time.sleep(5)
with open(r'交易配置.json','r+',encoding='utf-8') as f:
text=f.read()
trader_set=json.loads(text)
qq=trader_set['qq']
path=trader_set['同花顺下单路径']
run_date=trader_set['定时运行']
test=trader_set['测试']
run=trader_set['运行策略']
strategy=trader_strategy(qq=qq,path=r'{}'.format(path))
strategy.connect()
#下面2个测试用,去调#就可以运行,用在调试
if test=='全部':
strategy.run_stock_trader_strategy()
strategy.run_ETF_trader_strategy()
strategy.run_cov_bond_trader_strategy()
elif test=='股票':
strategy.run_stock_trader_strategy()
elif test=='ETF':
strategy.run_ETF_trader_strategy()
elif test=='可转债':
strategy.run_cov_bond_trader_strategy()
else:
pass
#strategy.seed_qq_email()
#strategy.get_hold_stock()
#strategy.adjust_position()
#strategy.run_hold_stock_T_stategy()
#保存持股
if run=='全部':
schedule.every().day.at('{}'.format(run_date)).do(strategy.run_ETF_trader_strategy)
schedule.every().day.at('{}'.format(run_date)).do(strategy.run_stock_trader_strategy)
schedule.every().day.at('{}'.format(run_date)).do(strategy.run_cov_bond_trader_strategy)
elif run=='股票':
schedule.every().day.at('{}'.format(run_date)).do(strategy.run_ETF_trader_strategy)
elif run=='ETF':
schedule.every().day.at('{}'.format(run_date)).do(strategy.run_stock_trader_strategy)
elif run=='可转债':
schedule.every().day.at('{}'.format(run_date)).do(strategy.run_cov_bond_trader_strategy)
else:
pass
#schedule.every().day.at('14:40').do(tarder_bound.tarder_bound)
#schedule.every().day.at('14:50').do(tarder_bound.tarder_bound)
schedule.every(10).minutes.do(strategy.seed_qq_email)
while True:
schedule.run_pending()
time.sleep(60)
#pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package opencv-python