Teach you how to debug programs

Posted by jawinn on Tue, 01 Feb 2022 11:29:19 +0100

We often encounter all kinds of errors in writing strategies, which is very cumbersome, but it is an essential step in procedural trading.

In fact, there are only a few reasons and types of errors in JoinQuant operation.

  1. python code format error, such as indentation and unequal usage - this can be basically solved by Baidu or google error prompt
  2. Problems in data processing, such as errors in the operation of DataFrame, list and dict - just learn the relevant processing methods, and you can learn the relevant basic knowledge in Python teaching in quantitative classroom.
  3. There are also some strange problems. As you write more programs and see more, you can solve them by yourself.

The following two figures show the error prompt:

The debugging methods can be summarized as follows:
1, First look at the first line of the error code and check it by yourself. First eliminate simple errors such as spelling or indentation.

2, Baidu or google error prompt code, such as querying python unexpected indent, will have a lot of results for reference.
Of course you can Stack Overflow Query, very easy to use, strongly recommended.

3, Print out the variables before and after the error to see if it is caused by different data object formats.
You can use print or try excpt... , Take a look at the relevant variable output.

Example:

The following code:

1 def initialize(context):
2    g.security = '000001.XSHE'
3    set_universe([g.security])
4    
5 def handle_data(context, data):
6     get_lists(context)
7     print 'done'
8    
9 def get_lists(context):
10    lists = get_all_securities('stock')
11    log.info(lists)
12    h = history(3, '1d', field='close', security_list = lists, df=False)
13    return [stock for stock in lists if h[stock][-1] < h[stock][0]]

An error will be reported after running:

2016-06-01 09:30:00 - INFO -             display_name  name  start_date    end_date   type
000001.XSHE         Ping An Bank  PAYH  1991-04-03  2200-01-01  stock
000002.XSHE         VankeA   WKA  1991-01-29  2200-01-01  stock
000004.XSHE         National Agricultural Science and technology  GNKJ  1990-12-01  2200-01-01  stock
........

2016-06-01 09:30:00 - ERROR - Traceback (most recent call last):
  File "kuanke/user_space.py", line 146, in exec_msg
    return getattr(self, func)(*msg['args'], **msg['kwargs'])
  File "kuanke/user_space.py", line 319, in handle_data
    self.func_handle_data(self.user_context, user_space_api.SecuritiesData(context.current_dt))
  File "user_code.py", line 6, in handle_data
    get_lists(context)
  File "user_code.py", line 12, in get_lists
    h = history(60, '1d', field = ('close'), security_list = lists)
  File "kuanke/user_space_api.py", line 978, in history
    HistoryOptions(fq=fq, skip_paused=bool(skip_paused), df=bool(df)))
  File "/home/server/y/envs/kuanke/lib/python2.7/site-packages/functools32/functools32.py", line 380, in wrapper
    result = user_function(*args, **kwds)
  File "kuanke/user_space_api.py", line 943, in history_daily
    options=options,
  File "kuanke/user_space_utils.py", line 465, in get_price_daily_single
    a = get_all_day_data(security)
  File "/home/server/y/envs/kuanke/lib/python2.7/site-packages/functools32/functools32.py", line 380, in wrapper
    result = user_function(*args, **kwds)
  File "kuanke/user_space_utils.py", line 169, in get_all_day_data
    api_proxy.prepare_all_day_data(security)
  File "kuanke/user_space.py", line 73, in func
    raise ret
Exception: shares"display_name"non-existent

The log printing is as shown above. It looks dazzling and has wood and wood~~~~
Although the program gives an Exception, it is obvious that it should not be this problem (the machine is not so intelligent).

Take another look at the error prompt and find that there is an error on the sixth line of the prompt program.
On the sixth line, we call get_lists(context) function, an error occurs here.
So we need to check get_list(contest) function.
Then look at the error prompt, which indicates that there are errors in line 12, and then a lot of errors.
Next, we need to check whether there is a problem with the line 12, history function. The history function is as follows:

history(count, unit='1d', field='avg', security_list=None, df=True, skip_paused=False, fq='pre')

The general error of this function is that the format of the incoming parameters is wrong, which may be due to the wrong order of the parameters or the format of the incoming parameters.
We compare the programs and find that there are no errors in count, unit and field, and the error is basically security_list. With doubt, we should print lists to see if it is a list.

For convenience, my program has printed the incoming lists on line 11.
The discovery result is a multi column DataFrame. Because I want to return the index data of the result. So you should change line 10 to lists = get_all_securities('stock').index and output lists. The results of lists are as follows:

2015-06-01 09:30:00 - INFO - Index([u'000001.XSHE', u'000002.XSHE', u'000004.XSHE', u'000005.XSHE',
       u'000006.XSHE', u'000007.XSHE', u'000008.XSHE', u'000009.XSHE',
       u'000010.XSHE', u'000011.XSHE', 
       ...
       u'603969.XSHG', u'603979.XSHG', u'603986.XSHG', u'603988.XSHG',
       u'603989.XSHG', u'603993.XSHG', u'603996.XSHG', u'603997.XSHG',
       u'603998.XSHG', u'603999.XSHG'],
      dtype='object', length=3010)

2015-06-01 09:30:00 - INFO - done

2015-06-02 09:30:00 - INFO - Index([u'000001.XSHE', u'000002.XSHE', u'000004.XSHE', u'000005.XSHE',
       u'000006.XSHE', u'000007.XSHE', u'000008.XSHE', u'000009.XSHE',
       u'000010.XSHE', u'000011.XSHE', 
       ...
       u'603969.XSHG', u'603979.XSHG', u'603986.XSHG', u'603988.XSHG',
       u'603989.XSHG', u'603993.XSHG', u'603996.XSHG', u'603997.XSHG',
       u'603998.XSHG', u'603999.XSHG'],

2015-06-02 09:30:00 - INFO - done

......................

The program runs successfully

Of course, in the process of code debugging, you will encounter various other errors, which you need to accumulate slowly.

As https://www.joinquant.com/post/1936 For the questions in this post, the program will prompt:

  File "user_code.py", line 101
    def chk_position(context):
      ^
SyntaxError: invalid syntax

But after careful inspection, there is no error. The real error is due to the lack of a bracket at the end of line 98. The program believes that the following is the content in the bracket of line 98.

Topics: Python Machine Learning debug