Australian (ASX) Stock Market Forum

Amibroker FAQ

This is the profit stop.

Profit = 2 * ATR(20);
ApplyStop(stopTypeProfit, stopModePoint, Profit, 2);

This is the stop loss.

Loss = 1 * ATR(20);
ApplyStop(stopTypeLoss, stopModePoint, Loss, 2);

Thanks wysiwyg. Is there anyway that can be plotted on a chart?
 
Thanks wysiwyg. Is there anyway that can be plotted on a chart?

The buy price has to be known so that a number can become a Y co-ordinate to plot the ATR multiples with. Since this price is only known at buy time I have added a slide Parameter so this price can be added once at buy and will stay at these levels. Also much simpler is viewing the ATR levels in Exploration by adding the column. Both are below and feel free to better them to suit your needs.

On Chart.

PHP:
BuyPrice = Param("Enter Buyprice", 0, 0, 100, 0.005);
Profit = 2 * ATR(14);
ExitA = SelectedValue(BuyPrice + Profit); 
Loss = 1 * ATR(14);
ExitB = SelectedValue(BuyPrice - Loss);

ProfitLine = LineArray(BarCount - 100, ExitA, BarCount-1, ExitA);
StopLossLine = LineArray(BarCount - 100, ExitB, BarCount-1, ExitB);
Plot(ProfitLine, "Profit", colorBlue, styleLine|styleThick);
Plot(StopLossLine, "Loss", colorDarkRed, styleLine|styleThick);

In Column.

PHP:
Buy = A Winner;
Sell = A Loser;

Profit = 2 * ATR(14);
ExitWin = BuyPrice + Profit; 
Loss = 1 * ATR(14);
ExitLoss = BuyPrice - Loss;

Filter = Buy;
AddColumn(Close, "Price", 1.3, colorSeaGreen);
AddColumn(ExitWin, "Profit Price", 1.3, colorBlue);
AddColumn(ExitLoss, "Loss Price", 1.3, colorDarkRed);
 
To extend the lines beyond last bar simply add the x-shift onto the end of the plot code. So min = 0, max = 0 and x-shift = 20 bars.

Plot(ProfitLine, "Profit", colorBlue, styleLine|styleThick, 0, 0, 20);
Plot(StopLossLine, "Loss", colorDarkRed, styleLine|styleThick, 0, 0, 20);
 
Hi all,

I need help understanding arrays and ADX. Can someone please help with an example below?

Here is some ADX code I am using - the object is to use the ADX value to rank shares that meet my buy condition - I assign the FinalADX value to PositionScore i.e. I want the strongest trending shares.


MIperiod = 14;
ADXperiod = 14;
PDIValue=PDI(MIperiod);
MDIValue=MDI(MIperiod);
DX=100*abs(PDIValue-MDIValue)/(PDIValue+MDIValue);
FinalADX=EMA(DX,ADXperiod);

PositionScore = FinalADX;

What value gets assigned to PositionScore? The last value in the array?

Many thanks :)
 
MIperiod = 14;
ADXperiod = 14;
PDIValue=PDI(MIperiod);
MDIValue=MDI(MIperiod);
DX=100*abs(PDIValue-MDIValue)/(PDIValue+MDIValue);
FinalADX=EMA(DX,ADXperiod);

PositionScore = FinalADX;

What value gets assigned to PositionScore? The last value in the array?

The value of FinalADX at the last bar of the backtest/scan/exploration period is assigned to PositionScore. To see the value use an exploration.

Code:
MIperiod = 14;
ADXperiod = 14;
PDIValue=PDI(MIperiod);
MDIValue=MDI(MIperiod);
DX=100*abs(PDIValue-MDIValue)/(PDIValue+MDIValue);
FinalADX=EMA(DX,ADXperiod);

PositionScore = FinalADX;

Filter = 1;
AddColumn(FinalADX , "FinalADX");
This will output the value of FinalADX for each ticker. To see it for only those tickers where Buy is true then assign a condition to Buy function and use Filter = Buy; rather than Filter = 1;

PositionScore values rank from lowest to highest so tickers with lowest FinalADX are ranked before higher FinalADX. Backtest output shows positionscore values as well.
 
Thanks Capt'n,

I am yet to use the 'explore' feature of AB - maybe this is a good opportunity.

Also, thanks for the heads up on the positionscore order of preference - I have only used it in conjunction with the random function (pseudo monte carlo testing) and assumed the higher value took preference.

Cheers
:)
 
T
PositionScore values rank from lowest to highest so tickers with lowest FinalADX are ranked before higher FinalADX. Backtest output shows positionscore values as well.

Hi Capt'n,

I tested the position score again and it seems as though the highest value takes precedence. Here is an extract from an AB help file -

"The score (PositionScore) for all securities is calculated first. Then all scores are sorted according to absolute value of PositionScore. Then top N are choosen to be traded. N depends on available funds and "max. open positions" setting. Backtester successively enters the trades starting from highest ranked security until the number of positions open reaches "max. open positions" or there are no more funds available. The score has the following meaning:

higher positive score means better candidate for entering long trade
lower negative score means better candidate for entering short trade "

Unfortunately, there does not seem to be a lot of information about positionscore. Does the above make sense? Have I mis-interpreted the help?

Cheers :)
 
I tested the position score again and it seems as though the highest value takes precedence.

Yes, you're quite correct, ranking is from highest to lowest. One of my systems uses the lowest score to rank systems so I have it set to an inverse relationship which led me to post the wrong info. Apologies.

Unfortunately, there does not seem to be a lot of information about positionscore.

There seems to be a bit of angst about it in some quarters. It's simply there to enable a non-discretionary way of deciding which trades to take in a portfolio when there isn't enough capital to take all trades. eg. You have capital to take 5 trades but your system triggers 10 trades that day. If you have a "mechanical" system then there needs to be a "mechanical" way to decide which trades to take. By all means use random positionscore for monte carlo testing but when it comes to trading in the real world you need some way to rank signals in a mechanical fashion.

If you search Howard Bandy's posts you'll find a good explanation of positionscore in another thread whose location escapes me atm.
 
G'day all

I have been trading for only 3 years, mainly Darvas box set-ups, so far only long and only ASX. I haven't lost my shirt yet but there are a couple of loose buttons so there is definitely room to improve.

I'm wondering if anyone knows if Amibroker (or anything else) can backtest a Darvas-based system. I am keen to compare my current method and results with the results I might have achieved using a couple of different stop-loss and profit-target approaches - eg, stop at 1 tick below the top of the box, exit if not in profit after 3 days, profit target set at the height of the box above the top plus a couple of others. I'm keen to put some numbers on these theories before I change my approach.

I have spotted a couple of Darvas formulas in the Amibroker formula library and have downloaded Amibroker to trial but can't find anything about Darvas backtesting.

I have started going through all of my completed trades manually to compare the result if I had included a different approach as above - this will take a while. I'm keen to test this on other charts apart from just the trades I have taken if there is a way to do it.

Any assistance will be greatly appreciated.

Cheers

Paul
 
Hello to all AB folk,

Well having roam around this thread I've decided to finally backtest a few ideas by writing a few lines of code in AB. My particular area of interest is Ichimoku Kinko Hyo system. So after spending about two day reading and exploring I've downloaded trial version of AB (ver. 5.30) and gave it a try.

I was able to code a Ichi indicator (thanks to some code in AFL Library and my own limited knowledge in programming). I even was able to plot signal arrows on the chart to give an indication for buy/sell signal - albeit with limited success. However after encouraging advance I hit a wall. I've decided to code a simple buy signal to see what happens during backtesting. However, Auto Analyser keeps telling me that there is no buy/sell variable assegments...:banghead:My code (simplified) as follows:

KS=(HHV(H,26)+LLV(L,26))/2;
TS=(HHV(H,9)+LLV(L,9))/2;
Buy = Cross(TS,KS);


Now unless I'm missing something (or forgot to tick some boxes in Analyser - I did went through the tut on AB site) I'm starting to believe that this is the limitation of trial version.

Did anyone encounter similar problem or can anyone provide a direction in which I can venture on to find a solution? :confused:
 
However, Auto Analyser keeps telling me that there is no buy/sell variable assegments...:banghead:My code (simplified) as follows:

KS=(HHV(H,26)+LLV(L,26))/2;
TS=(HHV(H,9)+LLV(L,9))/2;
Buy = Cross(TS,KS);

You need to add:

Sell = your sell statement here;

to your formula.
 
You need to add:

Sell = your sell statement here;

to your formula.

I see. I thought that setting SL and TP in Settings tab will take of it - the order will be close at either stop loss or take profit targets...

If I need to specify sell do I also need to specify stop loss level?

Thanks in advance Cap.
 
I see. I thought that setting SL and TP in Settings tab will take of it - the order will be close at either stop loss or take profit targets...

If I need to specify sell do I also need to specify stop loss level?

Thanks in advance Cap.

If you only want to exit using built-in stops then add:

Code:
Sell = 0;
to your formula.
 
If you only want to exit using built-in stops then add:

Code:
Sell = 0;
to your formula.

Thanks again Cap. Lunch time is over but I was able to do a quick run with what I had. One thing that I've realised is that it is better to define our own rules rather than using fixed stops. Anyway lots to try tonight.

One question though. What are the commands to get a price at buy - buyprice? And also the date of the candle at which buy signal occured?

Cheers
 
What are the commands to get a price at buy - buyprice?

BuyPrice defines the price at which you buy.
eg.
Code:
SetTradedelays(1,1,0,0);
BuyPrice = Open;
This tells the backtester to buy at the Open price the day after the buy signal is generated.

If you want to access the value of the buy price then use the valuewhen function.

http://www.amibroker.com/guide/afl/afl_view.php?id=163

And also the date of the candle at which buy signal occured?

Take a look at the Barssince function to find the date when various array conditions are true.

http://www.amibroker.com/guide/afl/afl_view.php?id=23

Can I suggest a read of the Portfolio backtest tutorial before going too much further down the backtesting path.

http://www.amibroker.com/guide/h_portfolio.html
 
Can I suggest a read of the Portfolio backtest tutorial before going too much further down the backtesting path.

http://www.amibroker.com/guide/h_portfolio.html

Captain,

I went through tutorial and still confused about the difference between raw and actual signals :confused:

I've managed to put a small amount of code together for backtesting Ichimoku system however even in the presence of clear buy signals (I'm only backtesting long now) the system does not generate any buy signals (although some raw signals are visible)... So at the moment I'm trying to figure out why the system does not buy.
 
Captain,

I went through tutorial and still confused about the difference between raw and actual signals :confused:

I've managed to put a small amount of code together for backtesting Ichimoku system however even in the presence of clear buy signals (I'm only backtesting long now) the system does not generate any buy signals (although some raw signals are visible)... So at the moment I'm trying to figure out why the system does not buy.

Post your code and we'll have a look and try to help :)
 
Post your code and we'll have a look and try to help :)

No problem Captain

//import Ichimoku indicator
#include "E:\Internet Apps\Amibroker\Formulas\Custom\Ichimoku.afl";

Buy = strongbuy;//strongbuy when Tenkan Sen crosses Kijin Sen while close is above the Cloud
Sell=0;
priceatbuy = 0;
highsincebuy = 0;
exit = 0;

//for loop to check if price reached twice the amount of stop loss
for(i = 0; i < BarCount; i++)
{
trailstop = SAR();
if (priceatbuy == 0 AND Buy)
{
priceatbuy = BuyPrice;
stoploss = KS;
PlotText("Buy price" + priceatbuy, i,(L-0.25),colorBlue);//line to check the flow
}
if (priceatbuy > 0)
{
if (exit == 0 AND High >= (2*stoploss))
{
exit = 1;
PlotText("Target reached "+(2*stoploss),i,(L-0.35),colorBlue);//line to check the flow
Buy = sigScaleOut;
stoploss = priceatbuy;
}
if (exit == 1 AND Low < trailstop)
{
exit = 2;
SellPrice = Max(Low,trailstop);
}
if (exit = 2)
{
Buy = 0;
priceatbuy = 0;
highsincebuy = 0;
}
}
}
SetPositionSize(50,spsPercentOfEquity);
SetPositionSize(50,spsPercentOfPosition*(Buy==sigScaleOut));

I was trying to use some flow indicators (text which shows me which stages of the code are executed and which are not). The first if statement gets executed exactly at the signal. But the rest of the code does not :banghead: By the way the code is not originally mine - I've taken the code from AFL Reference library and modified it a bit for my own purpose.

Thanks in advance Cap
 
Top