{"id":66,"date":"2007-10-11T18:34:43","date_gmt":"2007-10-11T23:34:43","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/2007\/10\/11\/low-level-gfx-example-yearlymonthly-profit-chart\/"},"modified":"2014-12-04T08:25:08","modified_gmt":"2014-12-04T13:25:08","slug":"low-level-gfx-example-yearlymonthly-profit-chart","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2007\/10\/11\/low-level-gfx-example-yearlymonthly-profit-chart\/","title":{"rendered":"Low-level gfx example: Yearly\/monthly profit chart"},"content":{"rendered":"
The code below is an little bit more complex example of Low Level Graphics functions (see http:\/\/www.amibroker.com\/guide\/a_lowlevelgfx.html)<\/p>
It allows to display three kinds of charts:<\/p>
The type of chart is switchable from Parameters dialog.<\/p>
It should be applied to ~~~EQUITY – portfolio equity symbol (so it only produces output if you run backtest before using it). <\/p>
SetBarsRequired<\/span>(<\/span>1000000<\/span>,<\/span>1000000<\/span>);
<\/span>eq <\/span>= <\/span>Foreign<\/span>(<\/span>"~~~EQUITY"<\/span>, <\/span>"C" <\/span>);
<\/span>yr <\/span>= <\/span>Year<\/span>();
<\/span>mo <\/span>= <\/span>Month<\/span>();
<\/span>YearChange <\/span>= <\/span>yr <\/span>!= <\/span>Ref<\/span>( <\/span>yr<\/span>, -<\/span>1 <\/span>);
<\/span>MonChange <\/span>= <\/span>mo <\/span>!= <\/span>Ref<\/span>( <\/span>mo<\/span>, -<\/span>1 <\/span>);
<\/span>FirstYr <\/span>= <\/span>0<\/span>;
<\/span>LastYr <\/span>= <\/span>0<\/span>;
<\/span>startbar <\/span>= <\/span>0<\/span>;
<\/span>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
\/\/ SKIP non-trading bars
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
<\/span>for( <\/span>i <\/span>= <\/span>0<\/span>; <\/span>i <\/span>< <\/span>BarCount<\/span>; <\/span>i<\/span>++ )
{
if( <\/span>eq<\/span>[ <\/span>i <\/span>] )
{
<\/span>startbar <\/span>= <\/span>i<\/span>;
break;
}
}
<\/span>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
\/\/ collect yearly \/ monthly changes in equity
\/\/ into dynamic variables
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
<\/span>LastYrValue <\/span>= <\/span>eq<\/span>[ <\/span>startbar <\/span>];
<\/span>LastMoValue <\/span>= <\/span>eq<\/span>[ <\/span>startbar <\/span>];
<\/span>MaxYrProfit <\/span>= <\/span>MinYrProfit <\/span>= <\/span>0<\/span>;
<\/span>MaxMoProfit <\/span>= <\/span>MinMoProfit <\/span>= <\/span>0<\/span>;
for( <\/span>i <\/span>= <\/span>startbar <\/span>+ <\/span>1<\/span>; <\/span>i <\/span>< <\/span>BarCount<\/span>; <\/span>i<\/span>++ )
{
if( <\/span>YearChange<\/span>[ <\/span>i <\/span>] || <\/span>i <\/span>== <\/span>BarCount <\/span>- <\/span>1 <\/span>)
{
<\/span>Chg <\/span>= <\/span>100 <\/span>* ( -<\/span>1 <\/span>+ <\/span>eq<\/span>[ <\/span>i <\/span>] \/ <\/span>LastYrValue <\/span>);
<\/span>VarSet<\/span>(<\/span>"ChgYear"<\/span>+ <\/span>yr<\/span>[ <\/span>i <\/span>- <\/span>1 <\/span>], <\/span>Chg <\/span>);
<\/span>MaxYrProfit <\/span>= <\/span>Max<\/span>( <\/span>MaxYrProfit<\/span>, <\/span>Chg <\/span>);
<\/span>MinYrProfit <\/span>= <\/span>Min<\/span>( <\/span>MinYrProfit<\/span>, <\/span>Chg <\/span>);
if( <\/span>FirstYr <\/span>== <\/span>0 <\/span>) <\/span>FirstYr <\/span>= <\/span>yr<\/span>[ <\/span>i <\/span>- <\/span>1 <\/span>];
<\/span>LastYr <\/span>= <\/span>yr<\/span>[ <\/span>i <\/span>];
<\/span>LastYrValue <\/span>= <\/span>eq<\/span>[ <\/span>i <\/span>];
}
if( <\/span>MonChange <\/span>[ <\/span>i <\/span>] || <\/span>i <\/span>== <\/span>BarCount <\/span>- <\/span>1 <\/span>)
{
<\/span>mon <\/span>= <\/span>mo<\/span>[ <\/span>i <\/span>- <\/span>1 <\/span>];
<\/span>Chg <\/span>= <\/span>100 <\/span>* ( -<\/span>1 <\/span>+ <\/span>eq<\/span>[ <\/span>i <\/span>] \/ <\/span>LastMoValue <\/span>);
<\/span>VarSet<\/span>(<\/span>"ChgMon" <\/span>+ <\/span>yr<\/span>[ <\/span>i <\/span>- <\/span>1 <\/span>] + <\/span>"-" <\/span>+ <\/span>mon<\/span>, <\/span>Chg <\/span>);
<\/span>VarSet<\/span>(<\/span>"SumChgMon"<\/span>+ <\/span>mon<\/span>, <\/span>Chg <\/span>+ <\/span>Nz<\/span>( <\/span>VarGet<\/span>(<\/span>"SumChgMon"<\/span>+ <\/span>mon <\/span>) ) );
<\/span>VarSet<\/span>(<\/span>"SumMon" <\/span>+ <\/span>mon<\/span>