Nuggets Quantification—Python SDK Documentation—3. Variable Conventions

 

Table of contents

Python SDK Documentation

3. Variable conventions

  3.1 symbol - code identification

    3.1.1 Exchange Code

    3.1.2 Transaction target code

    3.1.3symbol example

    3.1.4 Futures main continuous contracts

  3.2mode - mode selection

    3.2.1 Real-time mode

    3.2.2 Backtesting Mode

  3.3context - context object

    3.3.1context.symbols - subscription code collection

    3.3.2context.now - current time

    3.3.3context.data - data sliding window

    3.3.4context.account - account information

    3.3.5context.parameters - dynamic parameters

    3.3.6context.xxxxx - Custom Properties


Python SDK Documentation

3. Variable conventions

3.1 symbol - code identification

The nugget code ( symbol ) is the code used by the nugget platform to uniquely identify the transaction target,

The format is: exchange code. transaction code , such as the symbol of Shenzhen Ping An , for example: SZSE.000001(Note that it is case-sensitive). The code indicates that it can be queried in the simulated transaction or transaction tool of the Nuggets terminal.

3.1.1 Exchange Code

Nuggets currently supports 7 domestic exchanges, and the codes of each exchange are abbreviated as follows:

Market Chinese name market code
Shanghai Stock Exchange SHSE
Shenzhen Stock Exchange SSE
Nakagane place CFFEX
First half office SHFE
Dashang DCE
Zheng Shang Institute CZCE
Shanghai International Energy Exchange Center INE
3.1.2 Transaction target code

The transaction table code refers to the transaction target code given by the exchange, including stock (such as 600000), futures (such as rb2011), option (such as 10002498), index (such as 000001), fund (such as 510300) and other codes.

For the specific code, please refer to the definition of the securities code given by the exchange.

3.1.3symbol example
Market Chinese name market code sample code Securities Abbreviation
Shanghai Stock Exchange SHSE SHSE.600000 Shanghai Pudong Development Bank
Shenzhen Stock Exchange SSE SZSE.000001 Ping An Bank
Nakagane place CFFEX CFFEX.IC2011 CSI 500 Index November 2020 Futures Contract
First half office SHFE SHFE.rb2011 Rebar November 2020 Futures Contract
Dashang DCE DCE.m2011 Soybean Meal November 2020 Futures Contract
Zheng Shang Institute CZCE CZCE.FG101 Glass January 2021 Futures Contract
Shanghai International Energy Exchange Center INE INE.sc2011 Crude Oil November 2020 Futures Contract
3.1.4 Futures main continuous contracts

It is only used in the backtest mode. 期货主力连续合约It is a simple splicing of volume and price data without smoothing. For example, SHFE.RB is the main continuous contract of rebar. For other main contracts, please refer to the futures main continuous contract

3.2mode - mode selection

The strategy supports two operating modes, which need to run()be specified in it, namely the real-time mode and the backtest mode.

3.2.1 Real-time mode

Live mode needs to specify  mode = MODE_LIVE

Subscribe to the real-time quotes pushed by the quotes server, that is, the real-time quotes of the exchange, which are only provided during trading hours and are often used for simulation and firm offers.

3.2.2 Backtesting Mode

The backtest mode needs to specify  mode = MODE_BACKTEST

Subscribe to the historical quotations of the specified period, specified transaction code, and specified data type, and the quotation server will play back the corresponding quotation data at full speed according to the specified conditions. The applicable scenario is the strategy backtesting stage to quickly verify whether the performance of the strategy meets expectations.

3.3context - context object

context is the strategy execution context object, which will be passed between any methods of your algorithm strategy. Users can define a variety of properties they need through context, and can also view the intrinsic properties of context. The context structure is as follows:

3.3.1context.symbols - subscription code collection

Through the subscribe market subscription function, the subscription code will generate a code collection

Function prototype:

context.symbols

return value:

type illustrate
set(str) Subscription code collection

Example:

subscribe(symbols=['SHSE.600519', 'SHSE.600419'], frequency='60s')
context.symbols

return:

{'SHSE.600519', 'SHSE.600419'}

3.3.2context.now - current time

The real-time mode returns the current local time, and the backtest mode returns the current backtest time

Function prototype:

context.now

return value:

type illustrate
datetime.datetime Current time (the current historical time of strategy backtesting in backtesting mode, and the user's system local time in real-time mode)

Example:

context.now

return:

2020-09-01 09:40:00+08:00

3.3.3context.data - data sliding window

Get the subscribed tick object  or  bar objecttick sliding window, and the data is the previous count or bardata including the tick or bar pushed at the current moment

prototype:

context.data(symbol,frequency,count,fields)

parameter:

parameter name type illustrate
symbol str Target code (only a single target code string is allowed)
frequency str Frequency, the filled frequency should be included in subscribe subscribed frequency.
count int Sliding window size, a positive integer, where the count value should be less than or equal to the count value specified in subscribe
fields str The required bar or tick fields, if there are multiple attributes, are separated by ,. For specific fields, see:  tick object  and  bar object

return value:

type illustrate
dataframe tick dataframe or bar dataframe

Example when subscribing to tick  :

Subcribe_data = context.data(symbol='SHSE.600000', frequency='tick', count=2)

output:

[{'symbol': 'SHSE.600000', 'open': 9.680000305175781, 'high': 9.720000267028809, 'low': 9.619999885559082, 'price': 9.630000114440918, 'quotes': [{'bid_p': 9.630000114440918, 'bid_v': 360197, 'ask_p': 9.640000343322754, 'ask_v': 124200}, {'bid_p': 9.619999885559082, 'bid_v': 1265300, 'ask_p': 9.649999618530273, 'ask_v': 172859}, {'bid_p': 9.609999656677246, 'bid_v': 1030400, 'ask_p': 9.65999984741211, 'ask_v': 233400}, {'bid_p': 9.600000381469727, 'bid_v': 1200000, 'ask_p': 9.670000076293945, 'ask_v': 150700}, {'bid_p': 9.59000015258789, 'bid_v': 208000, 'ask_p': 9.680000305175781, 'ask_v': 199543}], 'cum_volume': 29079145, 'cum_amount': 280888066.0, 'last_amount': 963.0, 'last_volume': 100, 'created_at': datetime.datetime(2020, 11, 20, 11, 30, 1, 400000, tzinfo=tzfile('PRC')), 'cum_position': 0, 'trade_type': 0}, {'quotes': [{'bid_p': 9.630000114440918, 'bid_v': 315497, 'ask_p': 9.640000343322754, 'ask_v': 125900}, {'bid_p': 9.619999885559082, 'bid_v': 1291300, 'ask_p': 9.649999618530273, 'ask_v': 177959}, {'bid_p': 9.609999656677246, 'bid_v': 1035000, 'ask_p': 9.65999984741211, 'ask_v': 233400}, {'bid_p': 9.600000381469727, 'bid_v': 1213300, 'ask_p': 9.670000076293945, 'ask_v': 150700}, {'bid_p': 9.59000015258789, 'bid_v': 212100, 'ask_p': 9.680000305175781, 'ask_v': 173943}, {'bid_p': 0, 'bid_v': 0, 'ask_p': 0, 'ask_v': 0}, {'bid_p': 0, 'bid_v': 0, 'ask_p': 0, 'ask_v': 0}, {'bid_p': 0, 'bid_v': 0, 'ask_p': 0, 'ask_v': 0}, {'bid_p': 0, 'bid_v': 0, 'ask_p': 0, 'ask_v': 0}, {'bid_p': 0, 'bid_v': 0, 'ask_p': 0, 'ask_v': 0}], 'symbol': 'SHSE.600000', 'created_at': datetime.datetime(2020, 11, 20, 13, 0, 2, 430000, tzinfo=tzfile('PRC')), 'price': 9.630000114440918, 'open': 9.680000305175781, 'high': 9.720000267028809, 'low': 9.619999885559082, 'cum_volume': 29171845, 'cum_amount': 281780897.0, 'cum_position': 0, 'last_amount': 892831.0, 'last_volume': 92700, 'trade_type': 0, 'receive_local_time': 1605863292.163}]

Example when subscribing to bar  :

Subcribe_data = context.data(symbol='SHSE.600000', frequency='60s', count=2, fields='symbol,open,close,volume,eob')

output:

  symbol         open         close        volume         eob
SHSE.600000    12.64000     12.65000      711900   2017-06-30 15:00:00
SHSE.600000    12.64000     12.62000      241000   2017-07-03 09:31:00

Note: 1.  The obtained data are arranged in the positive order of eob time. 2.  It is not supported to pass in multiple symbols or frequencies, if more than one is entered, an empty dataframe will be returned. 3.  If the fields query field contains invalid fields, KeyError will be returned.

Tips: The difference and connection when context.data() is used with bar

Take subscribing to the stock daily frequency data of 'SHSE.600519' as an example, output bar and context.data() in on_bar() at the same time.

  • When the subscription sliding window size (count) is 1, the data returned by bar is the same as the data returned by context.data
  • When the sliding window size (count) of the subscription is greater than 1, the data returned by bar is the latest one; while the data returned by context.data() is count pieces, and the last one is the same as the data returned by bar, that is, no matter the subscription How to set the size of the sliding window, bar only returns one piece of latest data each time, while context.data() returns the number of data pieces equal to count, and the last piece is the latest data.
3.3.4context.account - account information

This function can be used to obtain account fund information and position information.

prototype:

context.account(account_id=None)

parameter:

parameter name type illustrate
account_id str Account information, the default account will be returned by default, if multiple accounts need to specify account_id

return value:

The return type is account - the account object .

Example - get current positions:

# 所有持仓
Account_positions = context.account().positions()
# 指定持仓
Account_position = context.account().position(symbol='SHSE.600519',side = PositionSide_Long)

return value:

type illustrate
list[position] List of position objects

Note: When there is no position, use context.account().positions() to check the total position and return an empty list, use context.account().position() to check a single position and return None

output

# 所有持仓输出
[{'account_id': 'd7443a53-f65b-11ea-bb9d-484d7eaefe55', 'symbol': 'SHSE.600419', 'side': 1, 'volume': 2200, 'volume_today': 100, 'vwap': 16.43391600830338, 'amount': 36154.61521826744, 'fpnl': -2362.6138754940007, 'cost': 36154.61521826744, 'available': 2200, 'available_today': 100, 'created_at': datetime.datetime(2020, 9, 1, 9, 40, tzinfo=tzfile('PRC')), 'updated_at': datetime.datetime(2020, 9, 30, 9, 40, tzinfo=tzfile('PRC')), 'account_name': '', 'vwap_diluted': 0.0, 'price': 0.0, 'order_frozen': 0, 'order_frozen_today': 0, 'available_now': 0, 'market_value': 0.0, 'last_price': 0.0, 'last_volume': 0, 'last_inout': 0, 'change_reason': 0, 'change_event_id': '', 'has_dividend': 0}, {'account_id': 'd7443a53-f65b-11ea-bb9d-484d7eaefe55', 'symbol': 'SHSE.600519', 'side': 1, 'volume': 1100, 'vwap': 1752.575242219682, 'amount': 1927832.7664416502, 'fpnl': -110302.84700805641, 'cost': 1927832.7664416502, 'available': 1100, 'created_at': datetime.datetime(2020, 9, 1, 9, 40, tzinfo=tzfile('PRC')), 'updated_at': datetime.datetime(2020, 9, 15, 9, 40, tzinfo=tzfile('PRC')), 'account_name': '', 'volume_today': 0, 'vwap_diluted': 0.0, 'price': 0.0, 'order_frozen': 0, 'order_frozen_today': 0, 'available_today': 0, 'available_now': 0, 'market_value': 0.0, 'last_price': 0.0, 'last_volume': 0, 'last_inout': 0, 'change_reason': 0, 'change_event_id': '', 'has_dividend': 0}]
# 指定持仓输出
{'account_id': 'd7443a53-f65b-11ea-bb9d-484d7eaefe55', 'symbol': 'SHSE.600519', 'side': 1, 'volume': 1100, 'vwap': 1752.575242219682, 'amount': 1927832.7664416502, 'fpnl': -110302.84700805641, 'cost': 1927832.7664416502, 'available': 1100, 'created_at': datetime.datetime(2020, 9, 1, 9, 40, tzinfo=tzfile('PRC')), 'updated_at': datetime.datetime(2020, 9, 15, 9, 40, tzinfo=tzfile('PRC')), 'account_name': '', 'volume_today': 0, 'vwap_diluted': 0.0, 'price': 0.0, 'order_frozen': 0, 'order_frozen_today': 0, 'available_today': 0, 'available_now': 0, 'market_value': 0.0, 'last_price': 0.0, 'last_volume': 0, 'last_inout': 0, 'change_reason': 0, 'change_event_id': '', 'has_dividend': 0}

Example - get current account funds:

context.account().cash

return value:

type illustrate
dict[cash] Fund Object Dictionary

output

{'account_id': 'd7443a53-f65b-11ea-bb9d-484d7eaefe55', 'nav': 1905248.2789094353, 'pnl': -94751.72109056474, 'fpnl': -94555.35135529494, 'frozen': 1963697.3526980684, 'available': 36106.277566661825, 'cum_inout': 2000000.0, 'cum_trade': 1963697.3526980684, 'cum_commission': 196.3697352698069, 'last_trade': 1536.1536610412597, 'last_commission': 0.153615366104126, 'created_at': datetime.datetime(2020, 9, 1, 8, 0, tzinfo=tzfile('PRC')), 'updated_at': datetime.datetime(2020, 9, 30, 9, 40, tzinfo=tzfile('PRC')), 'account_name': '', 'currency': 0, 'order_frozen': 0.0, 'balance': 0.0, 'market_value': 0.0, 'cum_pnl': 0.0, 'last_pnl': 0.0, 'last_inout': 0.0, 'change_reason': 0, 'change_event_id': ''}

Example - get account connection status:

context.account().status

output

state: 3
​​​​​​​​3.3.5context.parameters - dynamic parameters

Get all dynamic parameters

Function prototype:

context.parameters

return value:

type illustrate
dict key is the key of the dynamic parameter, and the value is the dynamic parameter object, see dynamic parameter

Example - Adding dynamic parameters and querying all set dynamic parameters

add_parameter(key='k_value', value=context.k_value, min=0, max=100, name='k值阀值', intro='k值阀值',group='1', readonly=False)

context.parameters

output

{'k_value': {'key': 'k_value', 'value': 80.0, 'max': 100.0, 'name': 'k值阀值', 'intro': 'k值阀值', 'group': '1', 'min': 0.0, 'readonly': False}}

​​​​​​​​3.3.6context.xxxxx - Custom properties

Set parameters through custom attributes, and pass them into each event of the strategy along with context global variables

context.my_value = 100000000

return value:

type illustrate
any type custom attributes

Example - Output Custom Attributes

print(context.my_value)

output

100000000

Guess you like

Origin blog.csdn.net/weixin_42219751/article/details/131717577