{"id":413,"date":"2014-09-29T04:03:53","date_gmt":"2014-09-29T09:03:53","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=413"},"modified":"2014-12-04T08:21:04","modified_gmt":"2014-12-04T13:21:04","slug":"debugging-techniques-part-1-exploration","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/09\/29\/debugging-techniques-part-1-exploration\/","title":{"rendered":"Debugging techniques – Part 1 – Exploration"},"content":{"rendered":"

From time to time people send us their formulas asking what happens in their own code. Or they do not know why given trade is taken or not. These questions are usually caused by the fact that people lack the insight what is happening inside and what values values their variables hold.<\/p>

The first general-purpose debugging technique is using Exploration<\/a><\/strong>. <\/p>

You need to add several AddColumn<\/strong> statements and run your code as Exploration<\/strong>, so you can actually see the values of all variables. This will reveal whenever you really have values that you expect and would make it easier for you to understand what is happening inside your code.<\/p>

In simplest form add this code to your system formula:<\/p>Filter <\/span>= <\/span>1<\/span>; <\/span>\/\/ show all bars
<\/span>AddColumn<\/span>( <\/span>Buy<\/span>, <\/span>"Buy" <\/span>)<\/code>

and it will show you if you are getting expected values in Buy<\/strong> array. You can use the same technique to track the content of any variable. Add as many columns as you want. You would be surprised how much insight into your own code you will get.<\/p>

You can use Exploration<\/strong> to learn how particular function works, for example, if you don’t understand how ValueWhen<\/strong> works, you can display its results this way:<\/p>Filter <\/span>= <\/span>1<\/span>; <\/span>\/\/ show all bars
\/\/
<\/span>m <\/span>= <\/span>MA<\/span>( <\/span>C<\/span>, <\/span>10 <\/span>);
<\/span>cond <\/span>= <\/span>Cross<\/span>( <\/span>C<\/span>, <\/span>m <\/span>);
<\/span>bi <\/span>= <\/span>BarIndex<\/span>();
<\/span>\/\/
<\/span>AddColumn<\/span>( <\/span>C<\/span>, <\/span>"Close" <\/span>);
<\/span>AddColumn<\/span>( <\/span>m<\/span>, <\/span>"Mov Avg" <\/span>);
<\/span>AddColumn<\/span>( <\/span>cond<\/span>, <\/span>"Condition"<\/span>);
<\/span>AddColumn<\/span>( <\/span>bi<\/span>, <\/span>"BarIndex" <\/span>);
<\/span>AddColumn<\/span>( <\/span>ValueWhen<\/span>( <\/span>cond<\/span>, <\/span>bi <\/span>), <\/span>"ValueWhen( cond, BarIndex() )" <\/span>);
<\/span>AddColumn<\/span>( <\/span>ValueWhen<\/span>( <\/span>cond<\/span>, <\/span>Close<\/span>), <\/span>"ValueWhen( cond, Close )" <\/span>)<\/code>

If you run above code you will clearly see how ValueWhen picks the value when condition is true and “holds” it for all other bars (when condition is false).<\/p>

\"Debug<\/p>

Once you get this level of insight into your code you will be better equipped to fix any errors.
Exploration is number one choice in getting detailed view on what is happening inside your code.<\/p>

For more information about Exploration see http:\/\/www.amibroker.com\/guide\/h_exploration.html<\/a><\/p>","protected":false},"excerpt":{"rendered":"

From time to time people send us their formulas asking what happens in their own code. Or they do not know why given trade is taken or not. These questions are usually caused by the fact that people lack the insight what is happening inside and what values values their variables hold.The first general-purpose debugging […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[53,14,15,37],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/413"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=413"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/413\/revisions"}],"predecessor-version":[{"id":496,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/413\/revisions\/496"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}