Amibroker setup for FOREX

Hello Amibroker team,

can you please help me witn an AB setup for intraday forex setup?

I have created a new database, where 1min data for US30 index were imported.
For this imported ticker I have setup up a contract specification:


I have also created a simple strategy, howevere it returns no rows in backtest. Displayed entries/exits on chart displays fine.

A simple strategy, just to display anything in backtest is:

// Backtest Setup
SetOption("InitialEquity", 1000);     
SetOption("MaxOpenPositions", 1);       
SetOption("AccountMargin", 100);  

BuyPrice  = Open;                        
SellPrice = Close;                     

ShortPrice = Open;
CoverPrice = Close;

// parameters
period_MA_SMA_Entry = 139;
period_MA_EMA_High_Low_Entry = 5;
period_MA_EMA_Close_Entry = 55;
period_MA_SMA_Exit = 35;

// Trading Rules 
SMA_C_ENTRY = MA(C, period_MA_SMA_Entry);
EMA_C   	= EMA(C, period_MA_EMA_Close_Entry);
EMA_H		= EMA(H, period_MA_EMA_High_Low_Entry);
EMA_L		= EMA(L, period_MA_EMA_High_Low_Entry);

SMA_C_EXIT  = MA(C, period_MA_SMA_Exit);

BuyRule1		= SMA_C_EXIT > SMA_C_ENTRY;    		
Buy 			= BuyRule1; 

Sell 			= Cross(C, EMA_H) OR Cross(SMA_C_EXIT, C);

Short = False;
Cover = False;

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

// Position Sizing & Risk Management
//PercentSize = 0.1;
//SetPositionSize( PercentSize, spsPercentOfEquity );
SetPositionSize( 1, spsShares ); 

// Plot indicators
Plot(SMA_C_ENTRY,"SMA_C_ENTRY", colorGreen, styleLine,width = 2);
Plot(EMA_C,"EMA_C_ENTRY", colorBlue, styleLine,width = 2);
Plot(EMA_H,"EMA_H", colorWhite, styleLine, width = 3);
Plot(EMA_L,"EMA_L", colorWhite, styleLine, width = 5);

Plot(SMA_C_EXIT,"SMA_C_EXIT",colorRed, styleLine, width = 2);

PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,layer = 0, yposition = graph0, offset = -12, XShift = 0 );
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,layer = 0, yposition = graph0, offset = -12, XShift = 0 );

And when I run detailed report, I receive 0 trades.

So, there is surely something wrongly set with position size.
I would like to buy just 1 lot for every entry, resp. 0.5 lot, etc...

Can you please suggest what is wrong?

I run backtest o 5 min data.

Thank you,

See Knowledge Base:

Hi Tomasz,

thanks for url, it helped me to move forward.
I have managed to set it up for fixed 1 lot size. However, if I test with just 0.1 lot size, it fails.

Please, how should be Round Lot Size, Min Shares and Min. position values set to allow also a partial lot sizes?

In code when I use:

SetPositionSize( 0.1, spsShares ); // number of shares/contracts per trade

I receive an error message:

01.10.2020 00:05:00
	Entry signals(score):USA30IDXUSD=Buy(1), 
	Exit signals:
	 USA30IDXUSD not entered because of insufficient funds or wrong position size/value (reqEntryPrice: 260, reqEntryPosSize: 0.09997559 shares (value = 25.99365), reqLotSize: 1)
	0 Open Positions: , Market Value: 0.00, Equity: 1000.00, Cash: 1000.00, Margin: 0.00, Net Cash Balance: 1000.00, 

When using exactly 1 stare (1 lot), then some results are displayed.

SetPositionSize(1, spsShares ); // number of shares/contracts per trade

Thanks a lot,


Re-read the Knowledge Base and apply it precisely, each sentence and each number is important. Your settings are simply wrong. If you have round lots size set to 1, then OBVIOUSLY you can't enter 0.1 position size.
So AmiBroker tells you in RED LETTERS that your position size is wrong.

@tomeo, in addition to the above linked official information, here you can find an FX tutorial (mainly addressing how to use dynamic rates in AmiBroker ) that was kindly shared by @fxshrat.

1 Like

Hi Tomasz,

yes, I see what's wrong based on red comments, however I am not able to fix it.
I have of course tried a multiple options, before posting a question here.

Thanks for shared documents, it helped for sure.

Currently I am able to set up important setting within code directly and this is a high advantage.

However, still doing something wrong.

I have added below settings into code:

// FX Margin account settings ::::::::::::::::::::::::::::::::
// these settings override settings of "Information" window
TickSize = IIf( StrFind( Name(), "JPY" ), 0.01, 0.0001 );
RoundLotSize = 0.1;
PointValue = 1;
Leverage = 500; // change to different leverage if using other broker i.e. IAB -> 40
MarginRate = 1 / Leverage;
// dynamic calculation of margin
MarginDeposit = -100 * MarginRate;

SetPositionSize( 0.1, spsShares ); // number of shares/contracts per trade

And RED ERROR message is:


So, now I have a RoundLotSize set to 0.1
and trying to buy just a 0.1 lot

I am also using the same Leverage as broker.

Thanks and sorry if my questions are so dummy for you.


There is no such thing as 0.1 round lot and 0.1 contract. You just have ONE contract that is worth 1000 (micro lot).
Contracts and round lots are INTEGERS. "Round lot" means rounded to integer. That's the purpose of rounding.
Fractional sizes are for mutual funds only (where you can actually buy fractional units).
You should use :

SetPositionSize( 1, spsShares ); // ONE contract (you can't buy fractional contracts )!
RoundLotSize = 1; 
PointValue = 0.1; // just POINT VALUE is 10 x less.

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