Leverage trading at AFL for stock trading (Duplicate)

Hello everyone.
How can I setting leverage trading at AFL for stock trading?

for example,
use $100 to buy $400 stock A;
use $100 to buy $400 stock B,
...
...

I searched and tried SetOption("accountmargin",4), but not works.

Could you please give me a complete example?

I appreciate your help.

That's not the way AccountMargin works. Read this: https://www.amibroker.com/guide/w_settings.html

If you want 4-to-1 leverage, you should set AccountMargin to 25. That means for each trade entry, 25% of the entry value will come from the cash in your account, and 75% will use margin (i.e. will be borrowed from your broker).

1 Like

How can set that at AFL?

You should be able to use the SetOption call that you referenced in your previous message.

Hi @Tomasz, I set SetOption("accountmargin",25);
However, my backtester still buy without leverage.

I attached my code setting and screenshot of some result:

// ##################  general #################  
SetBacktestMode( backtestRegular ); 
SetOption("InitialEquity", 10000 ); 

// ##################  commission  #################  
SetOption( "CommissionMode", 3 ); 
SetOption( "CommissionAmount", 0.0035 );
 
SetOption("AllowSameBarExit",true); 

// ##################  BACKTESTER #####################  
RoundLotSize	= 1;  		// 0 for Funds, 100 for Stocks 
TickSize		= 1;  		// 0 for no min. size
SetOption("accountmargin",25);


// ##################  position size ################# 
PosQTY = 1;// Optimize("PosQTY",12,1,30,1); 
SetOption("MaxOpenPositions", PosQTY ); 
SetPositionSize( 100, spsPercentOfEquity  );



// ################## price ################# 
BuyPrice=O; 
SellPrice=O; 
ShortPrice=O; 
CoverPrice=O;
SetTradeDelays(1,1,1,1); 

Screen Shot 2020-09-15 at 18.11.46

Screen Shot 2020-09-15 at 18.12.23

What leads you to believe that the backtester is not using leverage?

update setting

// ##################  general #################  
SetBacktestMode( backtestRegular ); 
SetOption("InitialEquity", 10000 ); 

// ##################  commission  #################  
SetOption( "CommissionMode", 3 ); 
SetOption( "CommissionAmount", 0.0035 );
 
SetOption("AllowSameBarExit",true); 

// ##################  BACKTESTER #####################  
RoundLotSize	= 100;  		// 0 for Funds, 100 for Stocks 
TickSize		= 1;  		// 0 for no min. size

leverage = 4;
SetOption("accountmargin",100/leverage);
SetOption("FuturesMode", False); 

 

// ##################  position size ################# 
PosQTY = 1;
SetOption("MaxOpenPositions", PosQTY ); 
SetPositionSize( leverage*100/PosQTY, spsPercentOfEquity  );

Screen Shot 2020-09-15 at 20.07.33

position value is very small and only 100 shares bought. please see the update setting and screenshot.

Go into your Analysis Settings, then the Portfolio tab. Make sure you have set "Limit trade size as % of entry bar volume" to 0.

1 Like

that works! Thank you so much.
Amibroker has so much "secret" to discover.

That is not the solution, see:

2 Likes

The position sizes shown in the trade list are quite small, so this isn't an issue with unlimited compounding. Based on the entry dates and times, it appears that the OP is testing against 1-minute bars. I don't think it's reasonable to assume that the trading volume over a one minute time span is indicative of one's ability to trade in a live market without slippage. I'm sure others would make different assumptions than I do, but we probably all have the same goal: to generate backtest results that are as representative as possible of what would have happened if we'd actually been trading our strategy live over a particular time period.

Than you so much! That makes sense.

For 1 minute interval it would be useful to set it to say 50, not zero.

1 Like

I believe that's a very conservative assumption. MYL traded over $114M worth of shares on March 12, 2020. I think it's quite likely that a trader could enter a $10,000 position with no slippage. Clearly we could find other examples where the volume limit is much more relevant. My point is simply that I think it's hard to gauge an entire trading session based on a single 1min bar.

The good news is that you've written AmiBroker in such a way that we can both implement our own assumptions, so thank you for that!

2 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.