Australian (ASX) Stock Market Forum

Amibroker FAQ

Does any one have or seen any code for a rolling 12 month returns profit table?

i.e instead of each month displaying that months return it would display a rolling 12 month CAR

This would be a great tool for looking into a systems consistency. I've tried to build my own no luck as yet.
Don't know if this is any help but a quick google I located this https://www.marketcalls.in/amibroker/profit-table-afl-code-yearly-max-drawdown.html
It might lead you along the path you wanted, hope it helps.

Edit: Sorry @Roller_1 after re-reading I misunderstood what you were after. Sorry mate
 
Last edited:
No problems mate, starting to get above my level, but I will keep an eye out for you.

Thanks, i thought i might've been able to manipulate the standard table but the solution obviously isn't so straight fwd.
 
@Roller_1 well there goes my Friday night....googling some more AmiBroker stuff.

Yes I lead an exciting life :laugh::laugh::laugh:

Haha don't worry i've been there, easy to waste hours tinkering #iso

The idea come from another group i'm in but he uses excel to create a pivot table. Would be good to have it straight away
 
I couldn't find a table like @Roller_1 wanted

The way I understand the back test report feature is that the file '3. Profit Table' is called and that generates the table in HTML.

Now for someone smarter than me they could manipulate the existing file to use any calculation (like the one below from your original post) to populate the table.

/////////////////////////////////////////////////
// CAR% calculation
/////////////////////////////////////////////////
YearCount = MonthCount / 12;
turnover = EQ[ BarCount - 1 ] / EQ[ startbar ];
CAR = ( -1 + turnover ^ ( 1 / YearCount ) ) * 100;

So the existing file (report) uses equity to start with then calculates the change monthly / yearly etc.

upload_2020-5-29_16-56-25.png

Build another section of code below the existing, replace equity with required data then wah-lah magic all done .. :roflmao::roflmao::roflmao: easy -peasy
 
I am the point of setting my trailing stops:

1. Since buy, if index filter continues true, Stop loss at 70% of high, until
2. Index filter has become not true, change stop loss of 90% of high since buy, until
3. Index filter returns true, then 70% of high subject to this stop loss being above stop loss in Condition 2

I have created some rudimentary rules, but these I know are way off. I would welcome some guidance on writing the AFL to implement the above trailing stop


Code:
SetForeign( "XAO" );


 Filter1 = C> MA( C, 10 );
 Filter3 =  Ref(MA( C, 10 ),-1)> Filter1;
 
RestorePriceArrays();


Filter = Filter1;


_SECTION_BEGIN("Buy_sell");
Hh = Ref(HHV(H,20),-1);


Buy = Iif(Filter,Cross(C,Hh),0);

//Trailing Stops:

//        1. Since buy, if index filter continues true, Stop loss at 70% of high, until
//        2. Index filter has become not true, change stop loss of 90% of high since buy, until
//        3. Index filter returns true, then 70% of high subject to this stop loss being above stop loss in Condition  2
 

Stop1 = IIf(Filter1,C<= Hh*.7,0);            //stop condition1
Stop2 = IIf(Filter3, C<= Hh*.9,0);           //stop condition2
Stop3 = IIf(Ref(filter3,-1), C<= Hh*.9,0);  //stop condition3


Sell = C<Stop1 OR C<Stop2 OR C<Stop3;

_SECTION_END();

Hi CNH, did you ever have any luck with this?
 
Hi CNH, did you ever have any luck with this?

I might just jump in here as I knocked up a trail stop system this arvo based on the above post from @wasp and thought that it would be a good talking point for all of us AmiBroker users.

so basically the below comprises of the following

- Index Filter using $XAO
- $XAO (Grey) plotted against $XAO Moving Average (10 period) (Pink)
- Test is Close > MA == Healthy
- Ribbon on bottom Green for Healthy ** note scaling appears to be out for some reason​

- Price Chart / System
- Standard Chart from AmiBroker
- EMA (20) (Aqua) Cross from snippet library
- Trail Stop
- Based on AmiBroker standard Trailing Stop Loop with a couple of minor mod's
- 2 x stop levels Standard 30% (Yellow) and Index Filter 10% (Red) switched via the Index Filter
- Position Parameters
- Standard AmiBroker Equal % Allocation added
- Exploration
- As per below used to help verify code

upload_2020-5-30_14-55-36.png
** DO NOT TRADE THIS - FOR EDUCATIONAL PURPOSES ONLY **

upload_2020-5-30_14-42-37.png

Code:
SetForeign("$xao"); // Replaces current stock code with XAO

IndexFilter = Close > MA(Close,10);  // Basic moving average test
Plot( Close, "$XAO", colorGrey40, styleLine | styleOwnScale ); // Plot XAO close price
Plot( MA(Close,10), "$XAO MA", colorPink, styleLine | styleOwnScale ); // Plot XAO Moving Average
IndexFilterHealthy = IIf( IndexFilter == 1 , colorGreen, Null );
    Plot( 1, "Index Healthy", IndexFilterHealthy, styleOwnScale| styleArea| styleNoLabel,-.5, 100);

RestorePriceArrays();

maxpos = 10; // maximum number of open positions
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 100 / maxpos, spsPercentOfEquity );


// Standard buy / sell system

period = 20; // number of averaging periods
m = EMA( Close, period ); // exponential moving average

Buy = Sell = 0;
Short = Cover = 0;

Buy = Cross( Close, m ); // buy when close crosses ABOVE moving average

// Trailing Stop loop

TrailStopPercent =  1 - 30/100; // Normal Trail Stop set @ 30 % nice and loose
IndexStopPercent =  1 - 10/100;  // Index Trail Stop set @ 10 % now tighter due to index filter mood change

StopLevel = IIf( IndexFilter == 0, IndexStopPercent, TrailStopPercent); // Selects which value to use in trailing stop

trailARRAY = Null;
trailstop = 0;
TrailStopSell = 0;

for( i = 1; i < BarCount; i++ )
{
   if( trailstop == 0 AND Buy[ i ] )
   {
      trailstop = High[ i ] * StopLevel [ i ] ;
   }
   else Buy[ i ] = 0;

   if( trailstop > 0 AND Close[ i ] < trailstop)
   {
      TrailStopSell[ i ] = 1;
      SellPrice[ i ] = trailstop;
      trailstop = 0;
    }
       
   if( trailstop > 0 AND TrailStopSell [i] == 0)
   {  
      trailstop = Max( High[ i ] * StopLevel [ i ], trailstop );
      trailARRAY[ i ] = trailstop;
   }
}

Sell = TrailStopSell OR Cross( m, Close ); // sell when closes crosses BELOW moving average

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

// plots price chart and trail stop

Plot( C, "Price", colorDefault, styleBar );
Plot( m, "MA 20", colorAqua, styleLine );

InTrade = Flip(Buy,Sell); // used to turn off trail stop plot
Plot( IIf(InTrade, trailARRAY, Null),"trailing stop level", IIf( IndexFilter == 0, colorRed, colorYellow ), styleLine, Null, Null, 0, -1, 1);

// Plots buy and sell signals

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeupArrow, shapeNone),colorWhite, 0,L, Offset=-45);

PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, L, Offset=-60);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,L, Offset=-70);
PlotShapes(IIf(Sell, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-65);


Filter = Buy;

AddColumn(Close,"Close",1.2); // current bar close
AddColumn(High,"High",1.2); // Current bar high
AddColumn(trailARRAY,"TS Value",1.2); // Based on % of High ( high select )
AddColumn(100*(1-StopLevel),"Stop %",1.2); // displays which % value we are using depending on index filter
AddColumn(IndexFilter, "Index Filter Active",1,
    IIf( IndexFilter, colorGreen, colorRed),
    IIf( IndexFilter, colorGreen, colorRed), 80);
 
Nice, thanks for sharing. That's really impressive.
I can't figure out how to have a trailing stop stay at 10% after the market turns up again (which is what the WTT does).
 
I can't figure out how to have a trailing stop stay at 10% after the market turns up again (which is what the WTT does).

This part of the code should lock in the high value so when the market turns up the SP has to increase past the previous high level.

trailstop = Max( High[ i ] * StopLevel [ i ], trailstop );

upload_2020-5-30_16-30-25.png

As can be seen below the trailing stop array doesn't move down with the SP.....basically holds last value

upload_2020-5-30_16-28-25.png

 
- Index Filter using $XAO
- $XAO (Grey) plotted against $XAO Moving Average (10 period) (Pink)
- Test is Close > MA == Healthy
- Ribbon on bottom Green for Healthy ** note scaling appears to be out for some reason​

The issue with the ribbon not aligning with a cross of the MA and xao looks like it's due to using the "styleOwnScale" in the plot function. If you change it to "styleLeftAxis" it seems to work as they are now both aligned to the same reference point. I haven't used styleLeftAxis before.

Code:
Plot( Close, "$XAO", colorGrey40, styleLine | styleLeftAxisScale ); // Plot XAO close price
Plot( MA(Close,10), "$XAO MA", colorPink, styleLine | styleLeftAxisScale ); // Plot XAO Moving Average
 
@Lone Wolf awesome stuff mate, works great and thanks for finding a fix.

updated version of the AFL attached with fix for aligning foreign plots , Date on X axis, ATH in chart title and another column in the exploration called "Signal" to display buy / sell signal.

Enjoy
 

Attachments

  • 2 stage trail stop.afl
    3.9 KB · Views: 51
As you know that there are many free systems out there that generate good results but end of the day they are only as good as the person using them.

I have overridden a sell signal many times as I thought that I knew best but turns out my emotions are my weakness and I need to follow the system rules

Some sample code attached which is profitable in a weekly mode on the XAO but would you be brave enough to follow it.

Please don't blindly follow others as you need to understand what the system is doing or you might as well not buy AmiBroker and just pay someone to manage your account.

 

Attachments

  • The CAM Indicator For Trends And Countertrends.afl
    9.1 KB · Views: 34
  • Double Donchain Trading System.afl
    3.2 KB · Views: 28
I have a system that generated a buy signal last week for z1p based on a breakout (signal came out after it's price spiked). I am not trading this system, but if I was I would have been hesitant to place a "buy" order after such a huge spike. Does anyone have a simple code or rule that identifies price spikes like this?
 
Top