1. 首页
  2. 易语言 - EasyLanguage
  3. 用打印语句调试 Debugging with Print Statements

用打印语句调试 Debugging with Print Statements

当验证并运行 EasyLanguage代码,但是图表却绘制了意外的对象时,可以使用print语句进行调试。本文提供一个最简单的例子加以说明。

When EasyLanguage code verifies and runs, but unexpected objects get plotted on your chart, a useful habit is debugging with print statements. Here is the easiest example print statement:

Print( "Hello World" );

To see the result, take a look at the Print Log tab of the EasyLanguage Output Bar. If the EasyLanguage Output Bar is not visible, use the View – EasyLanguage Output Bar menu sequence to display it. A new panel will open at the bottom of the window. It will have 3 tabs. Select the ‘Print’ tab and the message “Hello World” will appear.

Each Print statement determines the format of the printed items. The most recent print output is always added to the bottom of the items in the Print Log. The Print Log will be blank until an analysis technique or strategy sends output through a Print statement. It will include items from any analysis technique or strategy that includes print statements. The number of lines displayed is controlled by the ‘Maximum number of line’ option in the general tab of the print tab preferences. Once the buffer is full, the oldest line scrolls off when a new line is added. To access the preferences, right click on the output pane and select ‘preferences’.

Our “Hello World” example is simple, but won’t help reveal the source of coding problems. To get some useful information, we need to print out the values of specific variables on specific bars. Here is the general form of a debugging print statement. This line of code can be placed anywhere within your indicator or strategy.

If Condition1 then
Print( "Label ", Date, " ", Time, " ", Variable ) ;

“Condition1” is a logical test to minimize the number of useless printouts. After the EasyLanguage terms “Print”, we have a set of terms separated by commas. The commas separate the terms. In this case there are 5 terms:
Label (something to remind us what the numbers mean) Date (the bar’s date) Time (the bar’s time) Variable (the value of the variable we suspect is a problem) ” ” (this just prints a space so we can read the output)

For example, consider a case where a strategy fails to make the desired covering exits. We suspect the EasyLanguage keyword AvgEntryPrice is the issue. Here is some code to test the theory.

AEP = AvgEntryPrice
Buy to Cover ("LS SX") PositionSize contracts next bar at AEP + 5 * onetick stop;
If  marketPosition = -1 then
	Print( "AEP test ", Date," ", Time, " ", AEP ) ;

If our print output window displays AEP values, we have something to work with. If it is zero, we have a problem.

Other ways to inspect intermediate calculations include writing to files (FileAppend) and writing to the commentary windows (AtCommentaryBar). We will look at these in the future.

更新于 2020年5月25日

这篇文章对您有帮助吗?

相关的话题

留言评论