Australian (ASX) Stock Market Forum

Amibroker FAQ

Actually Im not sure I said that right. I want the candle that is "lpds" back from the current price to appear blue.


Example:

DonchianLower = LLV(Ref(L,-1),Lpds)

If Lpds =12, then I want the candle that is 12 days back to be blue.
 
I use separate databases for each exchange, only takes 2 clicks to load each database and keeps everything simple. Others may have different experiences with organising data into different markets using the same database but for the sake of simplicity I've found maintaining data in individual databases works for me.

Ah, yes that makes sense. Thanks :)
 
I’m trying to set up fixed fractional position sizing in Amibroker and am unsure how to set a maximum limit on the calculated position size. Can Anyone help?

This is what I’ve got so far:

PositionSize = -1 / (BuyStop - StopLoss) * BuyStop;

BuyStop and StopLoss are obviously defined elsewhere in the code. This sets the maximum risk per trade to 1%. This seems to work, but I also want to limit the position size to a maximum of 20% of the portfolio equity (some trades in the report appear to be putting the entire equity on a single trade!). ie. Position size should be the MINIMUM of the above calculation OR 20% of portfolio equity. How would I do that?

thanks
 
I’m trying to set up fixed fractional position sizing in Amibroker and am unsure how to set a maximum limit on the calculated position size. Can Anyone help?

This is what I’ve got so far:

PositionSize = -1 / (BuyStop - StopLoss) * BuyStop;

BuyStop and StopLoss are obviously defined elsewhere in the code. This sets the maximum risk per trade to 1%. This seems to work, but I also want to limit the position size to a maximum of 20% of the portfolio equity (some trades in the report appear to be putting the entire equity on a single trade!). ie. Position size should be the MINIMUM of the above calculation OR 20% of portfolio equity. How would I do that?

thanks

Does adding

PositionSize = -20 OR -1 / (BuyStop - StopLoss) * BuyStop;

do it?
 
I’m trying to set up fixed fractional position sizing in Amibroker and am unsure how to set a maximum limit on the calculated position size. Can Anyone help?

This is what I’ve got so far:

PositionSize = -1 / (BuyStop - StopLoss) * BuyStop;

BuyStop and StopLoss are obviously defined elsewhere in the code. This sets the maximum risk per trade to 1%. This seems to work, but I also want to limit the position size to a maximum of 20% of the portfolio equity (some trades in the report appear to be putting the entire equity on a single trade!). ie. Position size should be the MINIMUM of the above calculation OR 20% of portfolio equity. How would I do that?

thanks

Does this work?

Code:
PositionSize = max( -20 , -1 / (BuyStop - StopLoss) * BuyStop);
Max returns the largest number, so if your stop loss calcs return a number less than -20 (ie. position size greater than 20% of equity) then the position size defaults to 20% of equity. Position sizing uses negative numbers so max should give the correct solution as you want the number to be greater than -20.
 
Hi Cap. Does PositionSize not take the lowest of two choices for an OR situation?

No, it will take the first number, whether it's higher or lower. Try these two in a backtest formula.

PositionSize = -50 OR -20;

PositionSize = -20 OR -50;

The first one will open 2 postions at a time. The second will open 5. Position score formula needs to return a single negative number, using OR doesn't return a single number.
 
Does this work?

Code:
PositionSize = max( -20 , -1 / (BuyStop - StopLoss) * BuyStop);
Max returns the largest number, so if your stop loss calcs return a number less than -20 (ie. position size greater than 20% of equity) then the position size defaults to 20% of equity. Position sizing uses negative numbers so max should give the correct solution as you want the number to be greater than -20.

That code looks good. I like it! :)


thanks very much,
AlterEgo
 
No, it will take the first number, whether it's higher or lower. Try these two in a backtest formula.

PositionSize = -50 OR -20;

PositionSize = -20 OR -50;

The first one will open 2 postions at a time. The second will open 5. Position score formula needs to return a single negative number, using OR doesn't return a single number.

Okay thanks. Here is one that baffles me. I would like to switch index via Parameters for backtesting/scan/exploration rather than manually changing the code. Is this possible Captain (or anyone)?

I thought this would work because the Index can be changed in Parameters but the Foreign Function does not read the change.

Code:
Index = ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS",0) ; 

X = Foreign("Index","Close",1);
X1 = DEMA(X, 250);
X2 = X > X1;
 
Okay thanks. Here is one that baffles me. I would like to switch index via Parameters for backtesting/scan/exploration rather than manually changing the code. Is this possible Captain (or anyone)?

I thought this would work because the Index can be changed in Parameters but the Foreign Function does not read the change.

Code:
Index = ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS",0) ; 

X = Foreign("Index","Close",1);
X1 = DEMA(X, 250);
X2 = X > X1;

You need to create a condition for each "constituent" of index.

Example...

If (Index == "^AORD")
{
...some condition....
}
Else.... etc.
 
You need to create a condition for each "constituent" of index.

Example...

If (Index == "^AORD")
{
...some condition....
}
Else.... etc.

Okay I have had a good couple of hours with this and using the tutorial on if/else. Also similar example in library. E.g. ...

if(Market == "NASDAQ 100")
{
dnvol = Foreign("VI5D.Z", "C");
upvol = Foreign("VI5A.Z", "C");
advn = Foreign("II5A.Z", "C" );
decl = Foreign("II5D.Z", "C");
}

if(Market == "NASDAQ COMP")
{
dnvol = Foreign("DIQD.Z", "C");
upvol = Foreign("DIQA.Z", "C");
advn = Foreign("IIQA.Z", "C" );
decl = Foreign("IIQD.Z", "C");
}
However my statement does not execute correctly. That is only open trades when ^AORD is above DEMA 250. Is this the idea you suggested Captain?

PHP:
Index = ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS" );

if (Index == "^AORD")
{
X = Foreign("^AORD", "Close");
X1 = DEMA(X, 250); 
X2 = X > X1;
Buy = X2;
}
else
{
X = Foreign("^GSPC", "Close");
X1 = DEMA(X, 250); 
X2 = X > X1;
Buy = X2;
}

if (Index == "^GSPTSE")
{
X = Foreign("^GSPTSE", "Close");
X1 = DEMA(X, 250); 
X2 = X > X1;
Buy = X2;
}
else
{
X = Foreign("^FTAS", "Close");
X1 = DEMA(X, 250); 
X2 = X > X1;
Buy = X2
}
 
Sorry, I'm not 100% sure what you're trying to do. If you're using the result from each index ticker in the same formula why not simply setup a different AFL for each index ticker? If you want them to run automatically then use a utility like Batman.

At the moment only ^AORD is being used because the each index ticker is assigned to the same variable X. It needs to be in a form of If index is AORD perform calculation X else if index is GSPC perform calculation Y etc.
 
That's okay. I simply want to change the foreign function in Parameters instead of having to change the foreign function in the code every time.

In AA Parameters with this statement ...

Index = ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS",0) ;

I can change the "Index".

But what then needs to happen is the foreign function needs to read the change.

So the change in AA Parameters here.
PHP:
Index = ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS",0) ;

creates a change in foreign function here.
PHP:
X = Foreign("^AORD", "Close");

I can manually change from "^AORD" to "^FTAS" etc. but would like to simply switch through AA Parameters.

:)
 
That's okay. I simply want to change the foreign function in Parameters instead of having to change the foreign function in the code every time.

In AA Parameters with this statement ...

Index = ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS",0) ;

I can change the "Index".

But what then needs to happen is the foreign function needs to read the change.

So the change in AA Parameters here.
PHP:
Index = ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS",0) ;
creates a change in foreign function here.
PHP:
X = Foreign("^AORD", "Close");
I can manually change from "^AORD" to "^FTAS" etc. but would like to simply switch through AA Parameters.

:)

I think I get what you're trying to do now.

Does this work?

Code:
X = Foreign(ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS",0) , "Close");
Click on the Parameters button in AA and the Index constituents are listed.
 
I think I get what you're trying to do now.

Does this work?

Code:
X = Foreign(ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS",0) , "Close");
Click on the Parameters button in AA and the Index constituents are listed.
This should work but it doesn't. This works exactly as stated
PHP:
X = Foreign("^AORD", "Close");
so why Paramlist won't connect is a hurdle.
 
I've run a backtest using either XAO or ANZ as an index filter with the code below and it works as expected. I simply set it to exit all positions when the close of the index is lower than the 40 day moving average. After selecting XAO or ANZ in the parameters box I get the correct exit for each filter. So there must be an error in some other part of your code that isn't behaving as expected.

Code:
SetForeign( ParamList("Index","XAO|ANZ"), True, True );
Buyfilter = Close >MA( C, 40 );
Sellfilter = Close < MA( C, 40 );
RestorePriceArrays( True );
 
Thanks for your patience here Captain.

This is the filter I want to experiment with and using the setforeign function. This is the code (except for the buy/sell/short/cover variables) that I want to use. You will need to have two charts open. One with the foreign price (^AORD etc.) and one chart for the traded stock price. A DEMA 250 line will need plotting on the foreign index chart.

This code doesn't recognise the X array. :confused:

PHP:
X = SetForeign( ParamList("Index","^AORD | ^GSPC | ^GSPTSE | ^FTAS"), True, True );

X1 = DEMA(C, 250); 
X2 = X > X1;  //Open long stock positions       
X3 = X < X1;  //Open short stock positions

Buy = X2 && Close >MA( C, 40 );
Sell = Close < MA( C, 40 );
Short = X3 && Cross(0, MACD());
Cover = Cross(MACD(), 0);

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

RestorePriceArrays( True );
 
Hi Wysiwyg, I'll have a better look tonight or over the weekend when things are quieter but on a quick glance, you should have your buy/sell statements after the restorepricearrays function. I'll be back in a bit.
 
Had a quick play around and here's something to try. I'm using XAO and ANZ again as I've got my ASX database loaded atm but change it to suit what you're after.

I haven't run through the trade list but I'm getting different results using XAO and ANZ as filters.

Code:
SetForeign( ParamList("Index","XAO|ANZ"), True, True );

X1 = DEMA(C, 250); 
X2 = C > X1;  //Open long stock positions       
X3 = C < X1;  //Open short stock positions

RestorePriceArrays( True );  

Buy = X2 && Close >MA( C, 40 );
Sell = Close < MA( C, 40 );
Short = X3 && Cross(0, MACD());
Cover = Cross(MACD(), 0);

SetFormulaName( "wysiwyg_test" ); 
SetOption( "PriceBoundChecking", 1 ); 
SetOption( "CommissionMode", 2 ); 
SetOption( "CommissionAmount", 6 ); 
SetOption( "UsePrevBarEquityForPosSizing", 1 );

pq = 10;
PosQty = pq;
PositionSize = -100 / posqty;
SetOption( "MaxOpenPositions", PosQty );

PositionScore = 100/ Close;
SetTradeDelays( 1, 1, 1, 1 );

BuyPrice = Coverprice = Open;
SellPrice = Shortprice = Open;
 
Had a quick play around and here's something to try. I'm using XAO and ANZ again as I've got my ASX database loaded atm but change it to suit what you're after.

I haven't run through the trade list but I'm getting different results using XAO and ANZ as filters.

:)

Using close price in relation to DEMA 250 this way references close price of stock and not foreign index.

SetForeign( ParamList("Index","^AORD | ^GSPC"), True, True );

X1 = DEMA(C, 250);
X2 = C > X1; //Open long stock positions
X3 = C < X1; //Open short stock positions

You see the short taken when the stock price is below the DEMA 250 while the Index price is above the DEMA 250.

example WOW stock price chart top, AORD below...
 

Attachments

  • untitled.jpg
    untitled.jpg
    171.3 KB · Views: 1
Top