Australian (ASX) Stock Market Forum

AmiBroker > Backtesting > BuyPrice is not the entry price?

the entryprice is the latest buyprice of the raw signal and what I would like is to recall the buyprice of the trade (open position)

Yep, very similar to what I'm trying to do. You'd think they'd have a function built in to Amibroker to do this, as it should be a very common thing that people would want to do in their code. I'm sure there'd be a way to do it, but there doesn't appear to be any easy solution by the sounds of it.
 
agreed, I've also posted this problem to the amibroker yahoo group and will keep you posted when I discover a good answer.

It is a pity, everything else I've tried works great and fast but somehow recording the first buy signal is not easy...
 
agreed, I've also posted this problem to the amibroker yahoo group and will keep you posted when I discover a good answer.

It is a pity, everything else I've tried works great and fast but somehow recording the first buy signal is not easy...

Hi guys, I have been struggling with this for MONTHS!!! Then I got really excited when I found the start of this thread and saw that CH90 and AlterEgo were asking the exact same question that I have.
But then I got to the end and there's no solution.....aaaaaarrrrrrgggghhhh!
This HAS TO BE something really basic in a trading programme, surely? Please, have any of you guys found the answer yet? I just want Amibroker to keep track of my original buy price. If I haven't had a SELL signal yet, and I get more BUY signals, I don't want my original buy price to change due to the new BUY signal.

Just like AlterEgo, I can't use EXREM(buy,sell) because sell isn't defined yet, and I need the original buy price to help define it.
 
Howard Bandy posted the answer some time ago...


Hi AlterEgo --

There are several ways to treat multiple signals. I did not address any of the options in my earlier posting.

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

Another is to use looping code to handle the conditions exactly as you wish.Thanks,
Howard
 
You'd think there should be a built in AFL function to do it though, rather than having to use looping. Looping is a bit beyond my level of Amibroker ability at this moment in time.
 
You'd think there should be a built in AFL function to do it though, rather than having to use looping. Looping is a bit beyond my level of Amibroker ability at this moment in time.

Yes, it's the same for me unfortunately. I'm not even clever enough to recognise that it was an answer.
I think I actually need a written example to have any chance. I've just spent about 6 hours trying 50 or 60 variations of some looping code that Amibroker support sent me yesterday. Unfortunately, when you don't know what you're doing, trial and error like this becomes very frustrating.
But I shall press on (perhaps for years) until I solve it (it CANNOT be that difficult).

EDIT: rnr, I just went back and re-read HowardBandy's post. After my last couple of days, it made much more sense and I found this in it:
EntryPrice = ValueWhen(Buy,BuyPrice);

I was sure I had tried this before but I just put that line in my code and it worked!! Phew. Thank you, what a relief. There is now just one slight problem - my BuyPrice should be the next day's Open, but this code gives me the Open on the same day as the Buy signal.
I tried SetTradeDelays(1,1,1,1), but that didn't work.
Can anyone here please tell me how to set the BuyPrice as the next day's Open?
 
OK, I just realised that what I wrote above will not work if my sell condition references the BuyPrice because Amibroker will have to update BuyPrice every time there is a new buy signal (even if there hasn't been a 'sell' yet).

This is because I can't tell Amibroker to "ignore new buy signals until there is a sell signal", because that requires ExRem(Buy,Sell), which requires defining "Sell".
And I can't define "Sell" until I know what my BuyPrice was, because I only want to sell if I am greater than BuyPrice.

So I'm back to not knowing the answer, and would really appreciate your help again.

Anybody? Howard? Please?
 
I've just spent about 6 hours trying 50 or 60 variations of some looping code that Amibroker support sent me yesterday. Unfortunately, when you don't know what you're doing, trial and error like this becomes very frustrating.

And why don't you post the whole reply of AB support?
 
OK, I just realised that what I wrote above will not work if my sell condition references the BuyPrice because Amibroker will have to update BuyPrice every time there is a new buy signal (even if there hasn't been a 'sell' yet).

This is because I can't tell Amibroker to "ignore new buy signals until there is a sell signal", because that requires ExRem(Buy,Sell), which requires defining "Sell".
And I can't define "Sell" until I know what my BuyPrice was, because I only want to sell if I am greater than BuyPrice.

So I'm back to not knowing the answer, and would really appreciate your help again.

Anybody? Howard? Please?

Perhaps submit a suggestion to the Amibroker feedback centre to include an "EntryPrice" AFL function in a future Amibroker release? Or I see that Amibroker Support offer a programming service for a fee. Maybe they could write a function to do this for not too much money.
 
Dear all,

I've been backtesting with amibroker for the past few days and I just hit a wall in my simple coding.

I am using 15 days of intraday data (15min).

Buy = Cross(RSI(C,6),15);
Sell = Cross(85,RSI(C,6) AND Ref(Close,-1)>BuyPrice*1.01;

1. One of my two sell condition is to have the exit price > entry price*1.01. In my results some exit prices are still below the entry prices and I've no idea why For example it bought at 5.03 and sold at 4.25!

I am starting to guess that the BuyPrice is not the entry price of my actual trade (5.03)

2. It is possible to test 5min intraday? In the settings periodicity I see only 15min and 20 min?

Any help will be greatly appreciated,
Cheers,

Chris

I did not test this

Code:
Buy = Cross( RSIa( C, 6 ), 15 );
Sellcond = Cross( 85, RSIa( C, 6 ) );
Sell = IIF( Ref( Close, -1 ) > BuyPrice * 1.01, Sellcond, 0 );
 
Hi folks,

I too am experiecing problems with this. I found a "possible solution", although haven't read the finer details/tested it yet see -

http://www.mail-archive.com/amibroker@yahoogroups.com/msg28986.html


Regarding the problem with Buystop I have found a way to fix it. I need
to place the Buyprice sentence:

BuyPrice=ValueWhen(Buy,Max(O,Ref(Entrypt,-1)));

below the Exrem() sentence, thus I remove the other ocurrences of Buy
that move the Buyprice. Try it and you will see that the Buyprice is now
a constant while long (the blue line). I guess this is a typical error
when coding since we always want Buyprice below Buy signal. Buyprice
needs to be static.
 
OK, with a lot of help from Tomasz at Amibroker, I got it working. Here's the template:

Buy = 0;
Sell = 0;

priceatbuy = Null;

patba = Null; // Price At Buy Array (for plotting only)

for( i = 0; i < BarCount; i++ )//
{
SellSignal = YourSellConditions[ i ] AND Close[ i ] >= priceatbuy );

if( SellSignal AND NOT IsNull( priceatbuy ) )
{
Sell[ i ] = 1;
priceatbuy = Null ;
}

BuySignal = YourBuyConditions;

if( IsNull( priceatbuy ) AND BuySignal )
{
Buy[ i ] = 1;
priceatbuy = BuyPrice[ i ];
}

patba[ i ] = priceatbuy; // to plot you need an ARRAY, not scalar
}
 
Hi goog64,

Good work on chasing this up. A couple of minor edits that I needed to get it to compile -

1. SellSignal = YourSellConditions[ i ] AND Close[ i ] >= priceatbuy ); (Need to remove this bracket)
2. BuySignal = YourBuyConditions; (Should also use )


I have also found another way that seems to resolve this issue, although I am still working through to ensure is doesn't cause other issues - see http://www.mail-archive.com/amibroker@yahoogroups.com/msg03974.html

Buy = Buy conditions
Sell = Sell conditions (excluding any that reference BuyPrice)

Buy = Exrem(Buy, Sell) (this removes additional buy signals)

Sell = Sell conditions (including any buyprice references - the Sell array will now include your BuyPrice Sell signals)

Cheers
 
Thanks for posting that, goog64.

No worries.
Thanks for the clean up superboot. (My Buy signals are outside the looping section, so I missed that.)

Superboot, regarding your alternative method: I can't see how it works if all one's signals require a reference to BuyPrice? How do you discard the sell signals generated by this line:
Sell = Sell conditions (excluding any that reference BuyPrice)?
 
Hi,
Am an AmiBroker newbie and am hoping someone can help me. Am trying to code a crossover involving the XAO and a stock. Basically what I am trying to do is put in a condition where a condition is the close of a stock must be above the XAO 15. Am trying to use the foreign function which I read about on here and in the help files (also Bandy's book) but it is not working. The code I am using is:

XAO = Foreign("XAO","Close");
XAO_Long = EMA(XAO,15);

Cond1 = XAO > XAO_Long;

FastMA = EMA( C, 4 );
SlowMA = EMA( C, 12 );


Buy = Cross (FastMA, SlowMA)
AND Cond1

For some reason the backtester does not detect when the stock close is above the EMA of the XAO 15.

Not sure where I am stuffing up but stuffing up I am

Regards
Bailey55
 
For some reason the backtester does not detect when the stock close is above the EMA of the XAO 15.

If you average the XAO for the last 3 weeks it will give you a value of something around 4300-4400. I can't think of too many (actually none on the ASX) stocks that trade at $4400, so your stock is never going to cross 4400.

I wonder if you really mean
-select buys from a basket of stocks, but only buy the XAO is above its own EMA?
 
No worries.
Thanks for the clean up superboot. (My Buy signals are outside the looping section, so I missed that.)

Superboot, regarding your alternative method: I can't see how it works if all one's signals require a reference to BuyPrice? How do you discard the sell signals generated by this line:
Sell = Sell conditions (excluding any that reference BuyPrice)?

Hi googs,

You are correct - it worked for me as I had other Sell conditions - if you only have conditions using BuyPrice, then I believe this option won't work for you :(

I tested a theory with the BuyPrice, however it didn't improve my results so I haven't played with it any further. Who would have thought this could prove so challenging!
 
Top