A股网格交易策略

首先,我们先对网格交易进行一个简单的介绍

网格交易是一种被广泛使用的交易策略,其基本思想是建立一系列买入价和卖出价的网格,并在这些价位之间进行买卖操作。网格交易旨在在市场波动性较大的情况下稳定盈利,尤其适合范围市场。

在网格交易中,投资者首先设定一个交易区间,包括一个最高价和一个最低价。然后,将这个区间划分成多个小网格,并在每个小网格内设置买入价和卖出价。例如,在一个价格区间为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 timefrom datetime import datetimeimport schedule#核心数据from stock_data import stock_dataimport pandas as pd#easytrader交易,改进from easytrader_trader import trader_frameimport numpy as np#可转债数据from bond_cov_data import bond_cov_dataimport json#import tarder_boundclass 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
交叉持股是指两个或更多的公司相互持有对方股份的情况。在Stata代码中,我们可以使用以下方法来计算交叉持股。 首先,我们需要准备一份包含两个公司的股份信息的数据集。假设我们有一个名为“stock”(股份)的数据集,其中包含了公司A和公司B的股份信息。 我们可以使用以下代码计算交叉持股的比例: ``` gen cross_ownership = . bysort company: egen total_stock = total(stock) replace cross_ownership = stock / total_stock if company != say_company & say_company == "A" ``` 在上述代码中,我们首先创建了一个名为“cross_ownership”的变量,用于存储交叉持股的比例。然后,我们使用egen函数针对公司变量进行了排序,并使用total()函数创建了一个名为“total_stock”的变量,用于存储该公司的总股份数。 接下来,我们使用replace命令计算了交叉持股的比例。通过if语句,我们检查了公司变量是否与"say_company"(例如公司A)相同,并且与目标公司变量(例如公司B)不同。在这种情况下,我们将交叉持股比例设置为当前的股份数除以总股份数。 类似地,我们也可以计算公司B持有公司A股份的交叉持股比例: ``` replace cross_ownership = stock / total_stock if company != say_company & say_company == "B" ``` 在这种情况下,我们只需将目标公司变量“say_company”设置为公司B,并按照相同的方法计算交叉持股比例。 最后,我们可以使用sort命令将数据集按照公司变量和交叉持股比例进行排序: ``` sort company cross_ownership ``` 通过执行上述代码,我们就能够计算出交叉持股的比例,并将数据集按照公司和交叉持股比例进行排序。 这样,我们就可以使用Stata代码来计算并分析交叉持股的关系了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做量化的智明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值