def get_datas(s): init('a', s) symbol = '002192' exchange = Exchange('SZ') day = Interval.DAILY start = datetime(2019, 1, 1) end = datetime(2019, 6, 3) data = MongoManager() datas = data.load_bar_data(symbol, exchange, day, start, end) bars = pd.DataFrame(columns=['open', 'close', 'low', 'high', 'volume', 'openInterest']) opens = [data.open_price for data in datas] closes = [data.close_price for data in datas] low = [data.low_price for data in datas] high = [data.high_price for data in datas] volume = [data.volume for data in datas] datetimes=[data.datetime for data in datas] bars['open'] = opens bars['close'] = closes bars['low'] = low bars['high'] = high bars['volume'] = volume bars['openInterest'] = 0 bars['datetime'] = datetimes return bars
def generate_bar_from_row(row, symbol, exchange): """""" bar = BarData(symbol=symbol, exchange=Exchange(exchange), interval=Interval.DAILY, open_price=row["open"], high_price=row["high"], low_price=row["low"], close_price=row["close"], volume=row["vol"], datetime=str2datatime(row['trade_date']), gateway_name="DB") return bar
def to_tick(self): """ Generate TickData object from DbTickData. """ tick = TickData( symbol=self.symbol, exchange=Exchange(self.exchange), datetime=self.datetime, name=self.name, volume=self.volume, last_price=self.last_price, last_volume=self.last_volume, limit_up=self.limit_up, limit_down=self.limit_down, open_price=self.open_price, high_price=self.high_price, low_price=self.low_price, pre_close=self.pre_close, bid_price_1=self.bid_price_1, ask_price_1=self.ask_price_1, bid_volume_1=self.bid_volume_1, ask_volume_1=self.ask_volume_1, gateway_name="DB", ) if self.bid_price_2: tick.bid_price_2 = self.bid_price_2 tick.bid_price_3 = self.bid_price_3 tick.bid_price_4 = self.bid_price_4 tick.bid_price_5 = self.bid_price_5 tick.ask_price_2 = self.ask_price_2 tick.ask_price_3 = self.ask_price_3 tick.ask_price_4 = self.ask_price_4 tick.ask_price_5 = self.ask_price_5 tick.bid_volume_2 = self.bid_volume_2 tick.bid_volume_3 = self.bid_volume_3 tick.bid_volume_4 = self.bid_volume_4 tick.bid_volume_5 = self.bid_volume_5 tick.ask_volume_2 = self.ask_volume_2 tick.ask_volume_3 = self.ask_volume_3 tick.ask_volume_4 = self.ask_volume_4 tick.ask_volume_5 = self.ask_volume_5 return tick
def to_bar(self): """ Generate BarData object from DbBarData. """ bar = BarData( symbol=self.symbol, exchange=Exchange(self.exchange), datetime=self.datetime, interval=Interval(self.interval), volume=self.volume, open_price=self.open_price, open_interest=self.open_interest, high_price=self.high_price, low_price=self.low_price, close_price=self.close_price, build_time=self.build_time, remove_time=self.remove_time, gateway_name="DB", ) return bar
def getReturn(stocks, startday, endday, peried=1): if isinstance(startday, datetime): start_day = startday else: start_day = datetime.strptime(startday, '%Y%m%d').date() if isinstance(endday, datetime): end_day = endday else: end_day = datetime.strptime(endday, '%Y%m%d').date() results = pd.DataFrame() for code in stocks: code, exchange = code.split('.') datas = dbm.load_bar_dataframe_data(code, Exchange(exchange), Interval.DAILY, start_day, end_day) close = datas['close_price'] lagclose = close.shift(peried) results[code] = (close - lagclose) / lagclose # returns = datas.close_price.pct_change().dropna() # print('%s: %f' % (code, annualize(returns, 1))) return fillInfNaN(results)
def set_parameters(self, vt_symbol: str, interval: Interval, start: datetime, rate: float, slippage: float, size: float, pricetick: float, capital: int = 0, end: datetime = None, mode: BacktestingMode = BacktestingMode.BAR, days: int = 30): """""" self.mode = mode self.vt_symbol = vt_symbol self.interval = Interval(interval) self.rate = rate self.slippage = slippage self.size = size self.pricetick = pricetick self.start = start self.symbol, exchange_str = self.vt_symbol.split(".") self.exchange = Exchange(exchange_str) if capital: self.capital = capital if end: self.end = end if mode: self.mode = mode if days: self.days = days
import pprint from datetime import datetime from base_database.database_mongo import init, MongoManager from base_utils.constant import Interval, Exchange from settings.setting import Settings s = Settings() init('a', s) symbol = '002192' data = MongoManager() exchange = Exchange('SZ') day = Interval.DAILY # 最新价 # newest_bar = data.get_newest_bar_data('002192',Exchange.SZ,Interval.DAILY) # print(newest_bar) # 指定日期价 start = datetime(2019, 1, 1) end = datetime(2019, 6, 3) # date = start.strftime('%Y-%m-%d') # print(date) datas = data.load_bar_data(symbol, exchange, day, start, end) # # print(datas) # # # # d =d # print(datas[0]) print(pprint.pformat(datas))
s = Settings() dbm = init('_', s) ''' ''' start = datetime(2019, 1, 1) end = datetime(2020, 1, 1) bank_path = s['PATH_BANK'] codes = pd.read_csv(bank_path) codes.columns = ['name', 'code'] result_list = [] result_name = [] # 整合bank的收盘价 for index, codes in codes.iterrows(): code, exchange = codes[1].split('.') result = dbm.load_bar_dataframe_data(code, Exchange(exchange), Interval.DAILY, start, end) result_name.append(codes[1]) result_list.append(result['close_price']) all_banks = pd.concat(result_list, axis=1, keys=result_name) fill_banks = all_banks.dropna() st = PairTrading() # up_ssd,down_ssd = st.calBound('SSD') # print(up_ssd) # print(down_ssd) st.calAllCointegration(fill_banks) print(st.resultCoint) print(st.resultNotCoint) st.to_csv()
def get_closeprice(codes, start, end): code, exchange = codes.split('.') price = dbm.load_bar_dataframe_data(code, Exchange(exchange), Interval.DAILY, start, end) return price['close_price']
from datetime import datetime import pandas as pd import matplotlib.pyplot as plt from base_database.initialize import database_manager from base_utils.constant import Interval, Exchange from settings.setting import Settings """ 多对象的相关系数计算 """ s = Settings() start = datetime(2018, 1, 1) end = datetime(2020, 3, 31) results = dict() stocks_TMT = { '蓝思':'300433.SZ', '立讯':'002475.SZ', # '歌尔':'002241.SZ' # } for name, codes in stocks_TMT.items(): code,exchange = codes.split('.') result = database_manager.load_bar_dataframe_data(code,Exchange(exchange),Interval.DAILY,start, end) results[name] = result['close_price'] df = pd.DataFrame(results) plt.scatter(df['歌尔'],df['蓝思']) plt.legend() plt.show() corr = df.corr(method = 'pearson', min_periods = 1) print(corr)