如何编程获得上一个交易日的最低价?

新手专区
标签: #<Tag:0x00007f73e209b688>

(will_bao) #1

history_data = D.history_data(instruments, start_date, end_date, fields=[‘amount’,‘low’, ‘open’,‘close’])

low_0 = float(context.options[‘selected_data’].ix[today].low) #今天的最低价

如何获得上一个交易日的最低价呢?


(iQuant) #2
克隆策略

如何获取上一个交易日的收盘价

In [15]:
instruments = ['600519.SHA']  
start_date = '2017-08-24'  
end_date = '2017-08-27'

def initialize(context):
    pass 

def handle_data(context, data):
    date = data.current_dt
    print('该交易日为: ', date)
    k = instruments[0] # 标的为字符串格式
    sid = context.symbol(k) # 将标的转化为equity格式
    price = data.current(sid, 'price') 
    print('该交易日收盘价为: ', price)
    short_mavg = data.history(sid, 'price', 7, '1d').mean()  
    print('7日均价为: ', short_mavg)
    recently_7_low = data.history(sid, 'low', 7, '1d')
    print('最近七天最低价为: ', recently_7_low)
    today_low = data.history(sid, 'low', 7, '1d')[-1] 
    yesterday_low = data.history(sid, 'low', 7, '1d')[-2] 
    print('今日最低价为: ', today_low)
    print('昨日最低价为: ', yesterday_low)

m=M.trade.v2(
    instruments=instruments,
    start_date=start_date,
    end_date=end_date,
    initialize=initialize,
    handle_data=handle_data,
    order_price_field_buy='open',
    order_price_field_sell='open',
    capital_base=1000000,
    )
[2017-08-28 12:32:04.841651] INFO: bigquant: backtest.v7 start ..
该交易日为:  2017-08-24 19:00:00+00:00
该交易日收盘价为:  3504.3
7日均价为:  3497.121857142857
最近七天最低价为:  2017-08-16 00:00:00+00:00    3505.655
2017-08-17 00:00:00+00:00    3482.900
2017-08-18 00:00:00+00:00    3470.101
2017-08-21 00:00:00+00:00    3462.563
2017-08-22 00:00:00+00:00    3384.770
2017-08-23 00:00:00+00:00    3443.933
2017-08-24 00:00:00+00:00    3498.686
Freq: C, Name: Equity(0 [600519.SHA]), dtype: float64
今日最低价为:  3498.686
昨日最低价为:  3443.933
该交易日为:  2017-08-25 19:00:00+00:00
该交易日收盘价为:  3505.58
7日均价为:  3493.566428571429
最近七天最低价为:  2017-08-17 00:00:00+00:00    3482.900
2017-08-18 00:00:00+00:00    3470.101
2017-08-21 00:00:00+00:00    3462.563
2017-08-22 00:00:00+00:00    3384.770
2017-08-23 00:00:00+00:00    3443.933
2017-08-24 00:00:00+00:00    3498.686
2017-08-25 00:00:00+00:00    3479.487
Freq: C, Name: Equity(0 [600519.SHA]), dtype: float64
今日最低价为:  3479.487
昨日最低价为:  3498.686
[2017-08-28 12:32:05.114841] INFO: Performance: Simulated 2 trading days out of 2.
[2017-08-28 12:32:05.116526] INFO: Performance: first open: 2017-08-24 13:30:00+00:00
[2017-08-28 12:32:05.117694] INFO: Performance: last close: 2017-08-25 19:00:00+00:00
  • 收益率0.0%
  • 年化收益率0.0%
  • 基准收益率1.06%
  • 阿尔法-0.04
  • 贝塔0.0
  • 夏普比率n/a
  • 收益波动率0.0%
  • 信息比率-15.74
  • 最大回撤0.0%
[2017-08-28 12:32:05.439608] INFO: bigquant: backtest.v7 end [0.597933s].