Profit calculation per contract - futures mode

Hello guys,

I am struggling several days to set proper backtesting for futures and I would appreciate if you could help me.

The thing is that for some reason (may be I am a bit dumb but please don't judge me :slight_smile: ) I can't make Amibroker calculate the correct profit/loss.

Here is the example with simple system of course:

SetOption("MaxOpenPositions", 100);
SetOption("AllowSameBarExit", False);
SetTradeDelays(0,0,0,0);
MarginDeposit =  -2;

BuyPrice = Close;
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;

Buy = C >= HHV(C, 50) AND MA(C, 50) > MA(C, 100);
Sell = 0;

Short = C <= HHV(C, 50) AND MA(C, 50) < MA(C, 100);
Cover = 0;

RiskPerContract = 3 * ATR( 30 );
ApplyStop( stopTypeTrailing, stopModePoint, RiskPerContract, True );
//
// risk 1% of entire equity on single trade
PositionRisk = 2;
PctSize =  PositionRisk * (MarginDeposit*(-1))  / ( RiskPerContract * PointValue );
SetPositionSize( PctSize, spsPercentOfEquity );

I would like to trade CL1! (Crude Oil futures contracts).
tick size = 0.01
Point value = 1
Margin deposit = 2%

So the problem:

By my understanding the profit should be like:
28.02 - 27.91 = 0.11
0.11 = 11 points
1 point = 1 USD
Amibroker gets 279.7 Contracts
So total loss should be = 279.7*11 = 3076.7 USD
Amibroker loss result = 30.77 USD

Capture

So obviously something I don't understand at setting up futures mode in Amibroker.
Little help will be greatly appreciated.

i did not test your code but for CL futures you need

Round Lot Size = 1
margin deposit = 5000 (depends on broker)
Tick Size = 0.01
Point Value = 1000.

Round lot size means the size of the smallest position. That is 1 future, not 0,1. Point Value means the that when CL moves 1 dollar then it moves 1000$ in your account

see also: Back testing systems for futures contracts

2 Likes

OK, thanks a lot for your feedback.
Could you please also help me understand the following formula:

I am using 2% margin deposit (with MarginDeposit = -2).
Should I multiply bellow with (-1)?

RiskPerContract = 3 * ATR( 30 );
ApplyStop( stopTypeTrailing, stopModePoint, RiskPerContract, True );
//
// risk 1% of entire equity on single trade
PositionRisk = 2;
testVar = 
PctSize =  ((PositionRisk * (MarginDeposit)*(-1))  / ( RiskPerContract * PointValue ));

I am getting strange results like:

my full code again is the same:

SetOption("MaxOpenPositions", 100);
SetOption("AllowSameBarExit", False);
SetTradeDelays(0,0,0,0);

MarginDeposit =  -10;

BuyPrice = Close;
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;

Buy = C >= HHV(C, 50) AND MA(C, 50) > MA(C, 100);
Sell = 0;

Short = C <= HHV(C, 50) AND MA(C, 50) < MA(C, 100);
Cover = 0;

RiskPerContract = 3 * ATR( 30 );
ApplyStop( stopTypeTrailing, stopModePoint, RiskPerContract, True );
//
// risk 1% of entire equity on single trade
PositionRisk = 2;
testVar = 
PctSize =  ((PositionRisk * (MarginDeposit)*(-1))  / ( RiskPerContract * PointValue ));
SetPositionSize( PctSize, spsPercentOfEquity   );

if you are just beginning to develop your systems in Amibroker I would start with a very simple system. Maybe I will post 1 a bit later else tomorrow (since I am drinking beer at the moment ). I do not use ApplyStop myself because I want to see in the chart what is going on. So I can not help you with that because I do not use it.

I will show a very simple example later.

so here is a simple system. Try to backtest this and see if your results correspond to what you see in the chart

// back test for OIL (CL) futures
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "InitialEquity", 100000 );
SetOption( "FuturesMode", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 3 );
SetPositionSize( 1, spsShares );

// these 4 lines can also be put inside the information window
MarginDeposit = 5000;
PointValue = 1000;
TickSize = 0.01;
RoundLotSize = 1;

ma1 = MA( C, 20 );
ma2 = MA( C, 50 );
Buy = Cross( ma1, ma2 );
Sell = Cross( ma2, ma1 );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Short = Sell;
Cover = Buy;
BuyPrice = SellPrice = ShortPrice = CoverPrice = C;

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "", colorWhite, styleCandle, Null, Null, 0, 0, 1 );

Plot( ma1, "ma1", colorAqua, styleLine, Null, Null, 0, 0, 1 );
Plot( ma2, "ma2", colorViolet, styleLine, Null, Null, 0, 0, 1 );

PlotShapes( IIf( Buy, shapeUpTriangle, shapeNone ), colorGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownTriangle, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSquare, shapeNone ), colorWhite, 0, SellPrice, 0 );
PlotShapes( IIf( Short, shapeDownTriangle, shapeNone ), colorOrange, 0, H, -30 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorWhite, 0, ShortPrice, 0 );
PlotShapes( IIf( Cover, shapeUpTriangle, shapeNone ), colorAqua, 0, L, -30 );
PlotShapes( IIf( Cover, shapeSquare, shapeNone ), colorWhite, 0, BuyPrice, 0 );

Hello and thanks for your help and support once again!

Yes, indeed your system gives me reasonable results.

I also set my test system to calculate futures correctly but I don't understand the mechanics of Profit/Loss percentage. I am using position size based on risk and percentage calculating the position size SetPositionSize( PctSize, spsPercentOfEquity );

So the thing is %, Profit is calculated based on the position value which gives me not a good estimate of % profit.

I'd like to calculate % Profit based on Position value * Point value.
So the %, Profit for the first row should be: 2970 / (15.13*1000) = 19.628% (currently the % Profit is 19 628 %)

Do you have any idea how to fix this?

Just for your information system is exactly the same as in the first post just I changed the point value to 1000, tick size the same - 0.01, Round lot size - 1.

something is going wrong here: SetPositionSize( PctSize, spsPercentOfEquity );

look at the number of contracts it takes. First trade 27 contracts. You know what the underlying value of this is? 1 futures contract has as underlying value 1000 barrels of oil. So at 86$ per barrel this is 86.000$ per contract. If you buy 27 contracts that would give an underlying value of 2.322.000$. Your initial equity is only 10000. So that can not be. You need to set the Margin requirement to 5000. Then you can only buy 2 contracts maximum.

This is why I gave you the example. Forget about setting your "Positions Size" to "percent of equity". Just test it with 1 contract. That is the max you can trade with 10000$ as initial equity.

In futures mode You have to set MarginDeposit and PointValue correctly. This is absolute minimum, Back testing systems for futures contracts

@empottasch wrote you values you should use. Follow his code, not yours.

It should be correct. I got it from here:
Position sizing based on risk

Just a small correction for you from my side.
if you take a look at the screenshot you will notice that the price in year 2000 is 28.02 USD which means 28.02*1000= 28 020 for contract. My initial equity was changed to 1000000 for test purposes in this situation.

As I said above I am using CL futures just for example because I don't have data from my broker but using Tradingview data and I set the properties as per my broker instructions where:
Margin deposit is 1% for professionals and 10% for guys like me.
Point value = 1
tick size = 0.01
This translates as you discribed as for 1 $ difference in contract = 100 $

As I said everything seems to work perfectly with your example and my system just I want to calculate somehow correct % Profit.

Here again my settings:

My code again the same:

SetOption("MaxOpenPositions", 100);
SetOption("AllowSameBarExit", False);
SetTradeDelays(0,0,0,0);

MarginDeposit =  -2;

BuyPrice = Close;
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;

Buy = C >= HHV(C, 50) AND MA(C, 50) > MA(C, 100);
Sell = 0;

Short = C <= HHV(C, 50) AND MA(C, 50) < MA(C, 100);
Cover = 0;

RiskPerContract = 3 * ATR( 30 );
ApplyStop( stopTypeTrailing, stopModePoint, RiskPerContract, True );
//
// risk 1% of entire equity on single trade
PositionRisk = 2;
testVar = PositionRisk*MarginDeposit*(-1);
PctSize =  (((PositionRisk * (MarginDeposit)*(-1))  / ( RiskPerContract * PointValue )));
SetPositionSize( PctSize, spsPercentOfEquity   );

if the Point Value is 100 then that must be a Micro Contract. Anyways, why do you use Margin Deposit = -2? In the information window you use 0. If you use -2 that mean that the backtester thinks you do not need any money in your account.

As I wrote original poster uses wrong MarginDeposit. Negative MarginDeposit means percent of price. With -2 it means that deposit is 2% of price, which obviously is wrong, because with price like $25 you would need 2% of that i.e. $0.5 deposit which is absurd. Do as @empottasch told you, specify correct deposit in dollars as actually charged by the broker.

Also use detailed report mode it will tell you everything you are missing.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

Hello Tomasz,
Thanks a lot for your clarification.
Just for your information as I wrote above there is such a MarginDeposit with my broker.
Check here: https://admiralmarkets.com/instrument/wti-crude-oil-100-barrels-futures-cfd-usd-crudeoilus
It says 1% for professionals and 10% margindeposit for retail (like me).
Also, please note calculated margin from broker platform directly:

I still want to use position based on risk using margindeposit as % (because my broker is calculating this way), however, this formula seems to to work incorrectly in some cases:

MarginDeposit =  -10;
RiskPerContract = 2 * ATR( 30 );
ApplyStop( stopTypeTrailing, stopModePoint, RiskPerContract, True );
//
// risk 1% of entire equity on single trade
PositionRisk = 1;
PctSize =  (PositionRisk * (MarginDeposit) / ( RiskPerContract * PointValue ));

PositionSize = PctSize;

Let's look an example:
ES_: broker_specifications
Minimum margin = 10%
Tick size = 0.01
Pip Value Per Minimum Contract, USD = 0.1 (this sounds misleading...)
So I calculated that Point Value = 1

Result:
There is no position to open because the calculated positionsize is too small:

I assume the above code / formula is tested for many years and it should be correct just really so far I couldn't find why does not work for me. I invested considerable amount of time for the past week and seems I need a bit more but help from your side is really appreciated!

It might be 10% of contract value, not 10% of the price. These things are different for any contract with PointValue NOT equal 1.

1 Like

Assuming Point Value = 1 and margin equal to 10% of total exposure, this should work (I've checked the results and it seems it is what you're trying to achieve)

MarginDeposit =  -10; // 10% of total value
RiskPerContract = 2 * ATR( 30 );
ApplyStop( stopTypeTrailing, stopModePoint, RiskPerContract, True );
PositionRisk = 1; // risk 1% of entire equity on single trade
PointValue=1; 
PctSize =  (PositionRisk * -MarginDeposit*C/100) / ( RiskPerContract * PointValue );

SetPositionSize( PctSize,spsPercentOfEquity); // futures
1 Like

Yes, thanks a lot!
Your formula work as I wanted and it is the exact representation of percentage margin deposit from my broker.

Just to mention for those who may searching for similar approach in the future the original formula Position sizing based on risk was also suitable for my needs just I calculated some worst case dollar deposit value per symbol (according to requirements of my broker).

Have a lovely day pmxgs :slight_smile:

1 Like

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