Australian (ASX) Stock Market Forum

Amibroker FAQ

It's my first post, so I want say Hello, from Poland ;)

I have a little question. Is possible set position size of 10% equity by not less than 1600? (damn high provision :mad: )

e.g
Initial Equity: 10000
max open positions: 5 or whatever

In my code I set:
SetPositionSize( 10, spsPercentOfEquity ); thats great but how set no less than 1600?

Remove SetPositionSize( 10, spsPercentOfEquity ); and add the following at the top of your code

Code:
SetCustomBacktestProc("");
if ( Status( "action" ) == actionPortfolio )
{
    // retrieve the interface to portfolio backtester
    bo = GetBacktesterObject();
    bo.PreProcess();

    for ( bar = 0; bar < BarCount; bar++ )
    {
        // this retrieves current value of portfolio-level equity
        eq = bo.Equity;

        // this for loop iterates through all trade signals and adjust pos size
        for ( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
        {
            // set 10% of equity but minimum 1600
            sig.PosSize = Max( 0.1 * eq, 1600 );
        }
        bo.ProcessTradeSignals( bar );
    }

    bo.PostProcess();
}
 
I have not tested it but maybe the following one could work too:

Code:
PositionSize = Max( -10, 1600 );
 
Upper one doesn't work. So use the other (CBT) one. That one definitely works

Hey! Thanks a lot! very helpful



I've got other problem. I write simply code which I want backtest but they doesn't work fine and I don't know why.

Code:
_SECTION_BEGIN("test");
sr = MA(C, 150);
Plot(sr, "Srednia kroczaca", ParamColor("Color", colorViolet), ParamStyle("Style") );

//Stochastic

slow = StochD(15,3,3);
quick = StochK(15,3);

// Buy and Sell

Buy = slow > 20 AND Ref(slow,-1) < 20 AND sr > Ref(sr,-1);
Sell = 0;

// Stop Loss

StopLevel = 1 - Param("TS %", 10, 1, 200, 1)/100;

trailArray = Null;
trailstop = 0;

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

if(trailstop > 0 AND Low < trailstop){
Sell = 1;
SellPrice = trailstop;
trailstop = 0;
}

if(trailstop > 0){
trailstop = Max(High*stoplevel, trailstop);
trailArray = trailstop;
}
}

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

PlotShapes(Buy*shapeDownArrow,colorBlue, 0, H, -25);
PlotShapes(Sell*shapeDownArrow,colorOrange, 0, H, -25);
Plot(trailArray,"trailing stop level", colorRed);



_SECTION_END();

Problem:
V3ukPjX.png

I write "test1.afl" and overlay this on charts and all works fine but in backtest not. Look at image.
Blue arrow is Buy signal by overlay code and oragne is sell, red line is traling stop. Green arrow is buy signal by backtest and sell is red.
problem, I have Buy = ExRem( buy, sell ); Sell = ExRem( sell, buy ); so why backtest open positions beetwen blue and orange arrow and why don't respect sell signal and not close position in oragne arrow. Position went on and close in 2009-07-03 where generate -91% Drop down :D in place where is next buy signal.
any clue what I do wrong?
j2Js9mk.png
 

Attachments

  • V3ukPjX.png
    V3ukPjX.png
    238.4 KB · Views: 4
  • V3ukPjX.png
    V3ukPjX.png
    238.4 KB · Views: 3
  • j2Js9mk.png
    j2Js9mk.png
    267.5 KB · Views: 5
Yes, the reason is pretty simple. It is a user error.

You are using param. But if you change parameter in chart pane then it does not get changed in analysis at the same time et vice versa. That's why if you click analysis with different params applied then the end result of plot shapes functions of your code and arrow plots of analysis in chart pane will differ (so in that case result list of analysis will also differ from PlotShapes plot). That's why in analysis always use hard coded parameters if you want to compare analysis result list with chart pane of same code. But even if not comparing with chart pane it is highly recommended to not use param in backtest/ optimization or in analysis in general except you know what you do. If you use hard coded params in analysis then you will ensure that those hard-coded params are/were actually used in current or recent analysis and you avoid confusion.

Second thing to keep in mind is that analysis time frame is independent from set chart time frame. So always ensure (if comparing chart with analysis) that in backtest setting > General tab > Periodicity there is the same time frame set as in chart pane.


Here is the modified code with hardcoded percent and some other minor modifications.

Next time please also use code tags. They are provided in forums for a reason! Thanks!

Code:
_SECTION_BEGIN( "test" );
SetBarsRequired( 1000, 0 );

percent = 10;//Param( "TS %", 10, 1, 200, 1 );

sr = MA( C, 150 );

//Stochastic
slow = StochD( 15, 3, 3 );
quick = StochK( 15, 3 );

// Buy and Sell
Buy = slow > 20 AND Ref( slow, -1 ) < 20 AND sr > Ref( sr, -1 );
Sell = 0;
Short = Cover = 0;

// Stop Loss
StopLevel = 1 - percent / 100;

trailArray = Null;
trailstop = 0;

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

    if ( trailstop > 0 AND Low[i] < trailstop )
    {
        Sell[i] = 1;
        SellPrice[i] = trailstop;
        trailstop = 0;
    }

    if ( trailstop > 0 )
    {
        trailstop = Max( High[i] * stoplevel, trailstop );
        trailArray[i] = trailstop;
    }
}

if ( Status( "actionex" ) == actionIndicator )
{
    Buy = ExRem( buy, sell );
    Sell = ExRem( sell, buy );

    GraphXSpace = 5;
    SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

    Plot( C, "Price", colorDefault, GetPriceStyle() );
    Plot( sr, "Srednia kroczaca", ParamColor( "Color", colorViolet ), ParamStyle( "Style" ) );

    dist = 15;
    PlotShapes( Buy*shapeUpArrow, colorBlue, 0, L, -dist );
    PlotShapes( Sell*shapeDownArrow, colorOrange, 0, H, -dist );

    Plot( trailArray, "trailing stop level", colorRed );
}
_SECTION_END();
 
Here is the picture showing that if using it properly then PlotShapes arrows and Analysis arrows are perfectly inline at same positions.

click to enlarge
image.jpg
 

Attachments

  • image.jpg
    image.jpg
    2.9 KB · Views: 9
Hi all,

I am looking to trade 2 systems using Amibroker. Are others doing this, and if so what have you found to be the best approach?

At the moment, I have setup 3 charts for system A and another 3 charts for system B, each using the formula/indicators specific to the system. I will load up the afl file to perform scans etc and check each signal for the corresponding chart for that system

My only concern is how I manage my active trades. With one system I have been using favourites to keep track of them, however I don't want to have 2 sets of stocks for different systems in the one place. Is there anyway to have 2 favourites or folders under favourites? I know my fallback is using a watchlist, however I am using Premium Data and have 100's of watchlists already.

Cheers
 
hello everyone i have some problem i want test turtle system 20hi buy /10low sell

-then please help me check program below wrong or not?
period1=20;
period2=10;

period1=Param("Maxperiod",20,20,60,5);
period2=Param("Minperiod",10,10,20,5);

HighChannel=Ref(HHV(H,period1),-1);
LowChannel=Ref(LLV(L,period2),-1);


Buy = Close >= Highchannel;
Sell = Close <= Lowchannel;


-do u have program scan roc260(ibd rs ranking) code amibroker ?


thank you :D
 
Is it possible to build an array composed of a combination of the high and low of each bar?

Example (for last 3 bars) - original data in 2 separate arrays, H & L:
H: 1.02, 1.04, 1.01
L: 0.98, 0.99, 0.94

Composed into a new array:
X: 1.02, 0.98, 1.04, 0.99, 1.01, 0.94

Cheers
 
Is it possible to build an array composed of a combination of the high and low of each bar?

Example (for last 3 bars) - original data in 2 separate arrays, H & L:
H: 1.02, 1.04, 1.01
L: 0.98, 0.99, 0.94

Composed into a new array:
X: 1.02, 0.98, 1.04, 0.99, 1.01, 0.94

Cheers

Hi RB --

All AmiBroker arrays have exactly the same number of elements -- BarCount.

You can create an array as easily as:
NewArray = 0 * Close;
You can store anything you want in it.
If, as it appears from your example, the elements of the new array are not aligned with the elements of the primary data series, you will need to supply whatever functions are necessary to manipulate and / or display the data.

Any attempt to refer to an element outside the limits of 0 to BarCount-1 will result in a runtime error and program halt.

---------------

AmiBroker syntax rules aside, what are you planning to store and how will it be used?

Best regards,
Howard
 
Thanks for the reply Howard. I want to find the median value of the last n-bar Highs and Lows. I suspect this will involve a loop of some sort. I found a .dll plugin through a Google search but couldn't get AB to recognise it.
 
Thanks for the reply Howard. I want to find the median value of the last n-bar Highs and Lows. I suspect this will involve a loop of some sort. I found a .dll plugin through a Google search but couldn't get AB to recognise it.


AmiBroker has a median function

SYNTAX Median( array, period )
RETURNS ARRAY

FUNCTION The Median function - finds median (middle element) value of the array over period elements. Note that LOWER median is returned when 'period' is an even number. If you want to get average of upper and lower median for even 'periods' you need to use Percentile( array, period, 50 ) instead. It will do the averaging for you but runs slower.

EXAMPLE // list only symbols which volume is greater than
// median Volume from past 50 days
Filter = Volume > Median( Volume, 50 );
AddColumn( V, "Volume" );

SEE ALSO Percentile() function
 
Yes but it only works on a single array. I need to find the median of two arrays of data combined.

Hi RB --

The median of two arrays? The two arrays are highs and lows? The median is not either. Are you sure the mean would not do as well, at less computational cost?

Best regards,
Howard
 
Is it possible to build an array composed of a combination of the high and low of each bar?

Example (for last 3 bars) - original data in 2 separate arrays, H & L:
H: 1.02, 1.04, 1.01
L: 0.98, 0.99, 0.94

Composed into a new array:
X: 1.02, 0.98, 1.04, 0.99, 1.01, 0.94

Cheers
If you could be more specific as to how you visualise the array then I could learn too. Is your X: example leading to a sum of H + L, a sum of H or a sum of L or not a sum? One combination of the H & L of each bar is H + L / 2.
 
Median of a list of numbers -

The median of a finite list of numbers can be found by arranging all the observations from lowest value to highest value and picking the middle one (e.g., the median of {3, 3, 5, 9, 11} is 5).
 
You need some meat in the sandwich. That being an open and close value otherwise the visual is nonsensical. This chart gives you an adjustable median of H, L as well as O, C to create a price chart.

Code:
_SECTION_BEGIN("Median Bar Values");

BarPeriod = Param("Bar Median Period", 3, 1, 100, 1);
OpenValueMed = Median(Open, BarPeriod);
HighValueMed = Median(High, BarPeriod);
LowValueMed = Median(Low, BarPeriod);
CloseValueMed = Median(Close, BarPeriod);

ColorPlot = IIf(OpenValueMed > Ref(OpenValueMed, -1), colorYellow, colorBlack);
PlotOHLC(OpenValueMed, HighValueMed, LowValueMed, CloseValueMed, "OHLC 3 Bar Median", ColorPlot, styleBar); 

_SECTION_END();

Chart Top to Bottom Median = 1, 3, 100 period

Untitled.png.
 
Howard - I want to take both arrays and concatenate them (is that the right term? join them end to end) and then find the median of that group of numbers. The mean is no good for what I need to do.

Wysiwyg - the above might clear up what I'm after. There is no addition, just tacking one array onto the end of the other, sorting it and finding the median.

Its an easy concept but I find it difficult to code. It's really easy in excel and this might clear it up even further for everyone. Set up two columns, the first with High values (A1 to A6), the second with Low values (B1 to B6). Find the median of the two columns =MEDIAN(A1:B6)

This is quite an interesting problem it seems...
 
Top