Australian (ASX) Stock Market Forum

Amibroker FAQ

Also having a lot of trouble getting my buy price formula working.

I want to buy on the day after my signal, 2 price units under the close of the signal bar.

I've tried combinations of BuyPrice and EntryPrice but no luck.
 
Hopefully this will work althought I haven't tested the code (not sure about the IF/IIF statement).

MinTick:=
If(Close <= 0.10,0.001,
If(Close <= 2,00.005,0.01));

Buy=Ref(EntryTrigger,-1);
EntryPrice:=Ref(Close - MinTick*2,-1);
BuyPrice:=IF(Open<EntryPrice,Open,EntryPrice);
 
Hi rnr, I've had a bit (many hours) of a play around with what you posted but without luck.

It's frustrating stuff.

The program doesn't even seem to be consistent with what buy price I'm getting.

Will continue to try.
 
Also having a lot of trouble getting my buy price formula working.

I want to buy on the day after my signal, 2 price units under the close of the signal bar.

I've tried combinations of BuyPrice and EntryPrice but no luck.

Buy price = 2 cents below close price yesterday.

PHP:
SetTradeDelays(1,1,0,0); 

Buy =  Cross(MACD(), Signal());
BuyPrice = Ref(C, -1) - 0.02;
Sell = Cross(Signal(), MACD());
 
Cheers Wysi, easy in theory, but I can't get mine to work! :banghead:
I've tried what you've posted, but the buy can't be ref -1, or it works off the day before the signal. The signal day is ref 0 I assume.
Here is what I'm using:

BuyPrice = Close - (3 * PriceUnit);

IIf (BuyPrice >= Ref(Open, 1), BuyPrice == Ref(Open, 1), BuyPrice);

Cond15 = BuyPrice >= Ref(Low, 1);


Generally it works, but it doesn't work every time. And when it doesn't work I can't see any logical reason why it isn't working. The buy price appears to be random. I know theres something wrong, and I've tried a lot of things. For some reason the == there gives better results than =. I don't know enough yet to know where I'm going wrong.
 
I want to buy on the day after my signal, 2 price units under the close of the signal bar.
Well Synergy if you want to buy the day after the signal then SetTradeDelays(1,1,0,0); does that.

The signal bar is the bar yesterday so Ref(C, -1) - 0.02; uses yesterdays bar close (the signal bar) to define the buy price for today (the day after the signal bar). :)

Note :- Ref(Open, -1); minus 1 is yesterday and Open by itself is today.
 
Cheers Wysi, easy in theory, but I can't get mine to work! :banghead:
I've tried what you've posted, but the buy can't be ref -1, or it works off the day before the signal. The signal day is ref 0 I assume.
Here is what I'm using:

BuyPrice = Close - (3 * PriceUnit);

IIf (BuyPrice >= Ref(Open, 1), BuyPrice == Ref(Open, 1), BuyPrice);

Cond15 = BuyPrice >= Ref(Low, 1);


Generally it works, but it doesn't work every time. And when it doesn't work I can't see any logical reason why it isn't working. The buy price appears to be random. I know theres something wrong, and I've tried a lot of things. For some reason the == there gives better results than =. I don't know enough yet to know where I'm going wrong.

Synergy - hard to understand your code as we dont know what 'priceunit' is.

your IIf statement is wrong

IIf (BuyPrice >= Ref(Open, 1), BuyPrice == Ref(Open, 1), BuyPrice);

in plain english that's "if buyprice is greater than than tomorrow's open, then make the buyprice equal to tomorrow's open, otherwise buyprice is true (equals 1)." That's basically jibberish No wonder you are getting unpredictable results!

Give us a bit more code so we can understand the context of thing's like 'priceunit' then it'll be easier to help you

Use the amibroker manual and read up on IIf statements. Generally you should use it to give some other variable a value eg

buyprice=IIf(...)
 
Here is what I'm using:

BuyPrice = Close - (3 * PriceUnit);

IIf (BuyPrice >= Ref(Open, 1), BuyPrice == Ref(Open, 1), BuyPrice);

Cond15 = BuyPrice >= Ref(Low, 1);

That code doesn't make much sence. Without seeing more of the code it's difficult to give much advice, however I think this may be on the track of what you're trying to do.

SetTradeDelays(0,0,0,0);

EntryPrice = Ref(Close,-1) - (3 * PriceUnit);

Cond15 = EntryPrice >= L;

BuySignal = whatever it is

Buy = Ref(BuySignal,-1) AND Cond15; //plus whatever other buy conditions that you have

BuyPrice = IIf(EntryPrice >= Open, Open, EntryPrice);
 
Hi Synergy,

Does this solve the problem ("PriceUnit" calculation code assumes you are using ASX equities)?

//"SetTradeDelays" function is used for back-testing.
SetTradeDelays(1,1,0,0);

PriceUnit:=
IIf(Close <= 0.10,0.001,
IIf(Close <= 2,00.005,0.01));

EntryTrigger:=
YourEntryConditions;

Buy=EntryTrigger;
EntryPrice:=Ref(Close - MinTick*2,-1);
BuyPrice:=IIF(Open<EntryPrice,Open,EntryPrice);
 
Hi Synergy,

Does this solve the problem ("PriceUnit" calculation code assumes you are using ASX equities)?

//"SetTradeDelays" function is used for back-testing.
SetTradeDelays(1,1,0,0);

PriceUnit:=
IIf(Close <= 0.10,0.001,
IIf(Close <= 2,00.005,0.01));

EntryTrigger:=
YourEntryConditions;

Buy=EntryTrigger;
EntryPrice:=Ref(Close - MinTick*2,-1);
BuyPrice:=IIF(Open<EntryPrice,Open,EntryPrice);

That code is pretty similar to what I wrote above. You only use = in Amibroker though, not := as that is MetaStock language.

Also, I would use SetTradeDelays of 0 instead of 1, checking for entry trigger yesterday and enter today, as in my code above. I prefer not to reference future dates, so use trade delay of 0 and look back for entry trigger rather than trade delay of 1 and reference future date for entry price. But that's just my preference as it's easier for me to follow that way.
 
An update...

I've gone through my code and offset everything back a day, so that my code is now based around the buy day rather than the signal day, and I now have it working!:confused:

I don't mind the idea of working back from the buy day, but it makes the code a real mess. everything based on the signal has to have a REF in front of it!
I'm very keen to find out where the issue is to avoid this.

Anyway, here's the buy code that I have, but I've had many variations of it giving the same dodgy results before I made this change...


SetTradeDelays( 0, 0, 1, 1 );

PriceUnit = IIf(Ref(C,-1) < .1, 0.001, .005);

EntryPrice = Ref(C, -1) - 3 * PriceUnit;

Cond14 = EntryPrice >= Low;

Buy = *buy conditions* AND Cond14;

BuyPrice = IIf (entryprice >= Open, Open, EntryPrice);
 
SetTradeDelays( 0, 0, 1, 1 );

PriceUnit = IIf(Ref(C,-1) < .1, 0.001, .005);

EntryPrice = Ref(C, -1) - 3 * PriceUnit;

Cond14 = EntryPrice >= Low;

Buy = *buy conditions* AND Cond14;

BuyPrice = IIf (entryprice >= Open, Open, EntryPrice);

Looks fine. My only comment would be that PriceUnit will be incorrect for any stocks above $2, as each tick is 0.01 above $2 not 0.005, therefore should and an extra iif statement like rnr did above. Like this:

PriceUnit = iif(Close <= 0.10,0.001, iif(Close <= 2,0.005,0.01));
 
Hi All,

Sorry for the dumb question but how do you add an ASX stock code's to Amibroker?
I have it set up to use IB data and I can find the code in IB but if I type the code into Amibroker it keeps saying the symbol is invalid.

Not to sure if I need to start a new DB or not.

Thanks

lost872
 
OK so I have found the setup guide in this forum (http://www.amibroker.org/3rdparty/ASX_Setup/) and have set it up as per the step by step guide, and now when I try to import the data from the web site http://www.asxhistoricaldata.com/ I get the following error and I'm not too sure why:
Logging started for 'C:\Users\********\Downloads\December-2nd\December 2nd\20111202.TXT' file, using format definition file 'Formats\default.format'
Error in line AAD,20111202,1.065,1.08,1.055,1.065,779563
Invalid date format/value


I have changed the dates around and also change the regional setting on my PC to see if this will help but nothing seems to work. Have also tried both ways to import the data.

Any help would be awesome.
 
I'm trying to set up a manual EMA.

The first bar of data for the EMA is just a simple average of the first x bars.

After that the EMA relies on the EMA for the previous day.

I'd like to know how to identify the first day of data, to calculate the initial average.
And i've got no idea how to reference the previous days EMA.

Cheers

Hi Synergy,

Will this do what you want?

Code:
EMAprd = 10;
ExEMA = EMA(Close,EMAprd);
Count = Cum(1);
SMA = Cum(Close)/Count;
MyEMA = IIf(IsNull(ExEMA),SMA,ExEMA);
Plot(MyEMA,"My EMA",colorRed);
 
Hi Guys,

Is there an easier way to code the below?

and is it possible to define a search criteria for REF or timeframegetprice?
IE i would like to be able to return the last 20 highs when the stochastics above 50?





Code:
//get last 20 highs

wh1 = Ref( H, -1 ); 
wh2 = Ref( H, -2 ); 
wh3 = Ref( H, -3 ); 
wh4 = Ref( H, -4 ); 
wh5 = Ref( H, -5 ); 
wh6 = Ref( H, -6 ); 
wh7 = Ref( H, -7 ); 
wh8 = Ref( H, -8 );
wh9 = Ref( H, -9 ); 
wh10 = Ref( H, -10 );  
wh11 = Ref( H, -11 );
wh12 = Ref( H, -12 );
wh13 = Ref( H, -13 );
wh14 = Ref( H, -14 );
wh15 = Ref( H, -15 );
wh16 = Ref( H, -16 );
wh17 = Ref( H, -17 );
wh18 = Ref( H, -18 );
wh19 = Ref( H, -19 );
wh20 = Ref( H, -20 );
//get last 20 lows

wl1 = Ref( L, -1 ); 
wl2 = Ref( L, -2 ); 
wl3 = Ref( L, -3 ); 
wl4 = Ref( L, -4 ); 
wl5 = Ref( L, -5 ); 
wl6 = Ref( L, -6 ); 
wl7 = Ref( L, -7 ); 
wl8 = Ref( L, -8 ); 
wl9 = Ref( L, -9 ); 
wl10 = Ref( L, -10 );
wl11 = Ref( L, -11 ); 
wl12 = Ref( L, -12 ); 
wl13 = Ref( L, -13 ); 
wl14 = Ref( L, -14 ); 
wl15 = Ref( L, -15 ); 
wl16 = Ref( L, -16 ); 
wl17 = Ref( L, -17 ); 
wl18 = Ref( L, -18 ); 
wl19 = Ref( L, -19 ); 
wl20 = Ref( L, -20 );



adr1 = (wh1+wh2+wh3+wh4+wh5+wh6+wh7+wh8+wh9+wh10+wh11+wh12+wh13+wh14+wh15+wh16+wh17+wh18+wh19+wh20)-(wl1+wl2+wl3+wl4+wl5+wl6+wl7+wl8+wl9+wl10+wl11+wl12+wl13+wl14+wl15+wl16+wl17+wl18+wl19+wl20);
//20 period average 
tadr = ((adr1)/20);
 
Hi Kastin --

The code you posted computes the average of the difference between the high and low for the previous 20 bars. If you just want the average as of the previous bar's close, this should work:

// In two lines to simplify understanding

AveDiff = ma(H,20) - ma(L,20);
PrevAveDiff = Ref(AveDiff,-1);

I don't see any condition testing the stochastic in the code you posted. How does that come into play?

You can refer to the High on bars where the stochastic was above 50. For example:

HighSto = stochd(5,3,3)>=50;
HighStoHigh = ValueWhen(H,HighSto); // Using the most recent bar where the stochastic was greater than 50, assign the value of the High to HighStoHigh.

If you want earlier values, give n a value, then use this:
HighStoHigh = ValueWhen(H,HighSto,n);

Since you have no idea how many previous bars it will take to find 20 bars where the Stochastic is above 50 (or whatever other conditions you plan to include), you might want to code a loop and explicitly include bounds checking.

How will the result be used? There is probably an easier way to get the information you want.


Thanks,
Howard
 
Top