Pos. Value and Shares Calculation in Backtester Report (Trades tab)

Hello all,

Very newbie and beginner question so hopefully this is a simple guidance on my likely mistake.

I am trying to verify how "Pos.value" and "Shares" are calculated in the Trades Tab in Backtest report.
I interpret "Pos. Value" as the Exposure in the trade (i.e. Position Size x buy price) and "Shares" as the number of shares in the trade. I searched and could not find anywhere in the help files explanation for these.

My trade risk is $2,000 (see code below) and going long at $0.45116 with initial stop at $0.405573.
according to this my Position Size should be: Risk/(buy-initial stop) = 2000/(0.45-0.40) = 43,885
and Exposure should be $19,798. This is what I get on my Excel sheet, Van tharp's book, chat GPT and using Explore in Amibroker.
However, in the trade list in the report I get is attached. Just look at the 1st trade please.
Here I read my Pos. Value (exposure) as 43885 which is (incorrectly) same as my position size. and Exposure as $97275 which is also wrong.

My code is below:

// Version Control: Version 9
// Position sizing and risk management parameters
InitialCapital = 100000;
RiskPercent = 2;

// Calculate risk per trade
RiskPerTrade = (RiskPercent / 100) * InitialCapital;

// Define the lookback period for highest high and lowest low
LookbackPeriod = 20;

// Calculate highest high and lowest low values based on High and Low
HighestHigh = HHV(High, LookbackPeriod);
LowestLow = LLV(Low, LookbackPeriod);

// Buy condition: Close is higher than the highest high of the last 20 days
BuyCondition = Close > Ref(HighestHigh, -1);

// Sell condition: Close is lower than the lowest low of the last 20 days
SellCondition = Close < Ref(LowestLow, -1);

// Position sizing based on risk management
PositionSize = IIf(Close != LowestLow, RiskPerTrade / (Close - LowestLow), 0);

// Apply trading signals at the next day's open
Buy = BuyCondition;
Sell = SellCondition;

// Remove conflicting signals
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

// show all bars
Filter = 1; 
Exposure = PositionSize*Close;

AddColumn(C,"Close" );
AddColumn(Ref(HighestHigh,-1),"H-1");
AddColumn(Buy,"BuyCond.");
AddColumn(ValueWhen(Buy,Close ), "ValueWhen(Buy,Close)");
AddColumn(LowestLow,"Stoploss");
AddColumn(RiskPerTrade,"$Risk");
AddColumn(PositionSize,"CountofShares");
AddColumn(Exposure,"Exposure");
AddColumn(Ref(LowestLow,-1),"L-1");
AddColumn(Sell,"SellCond.");
AddColumn( ValueWhen(Sell, Close), "ValueWhen(Sell,Close )" );
type or paste code here

Thanks,
Arpad

1 Like

Thank you for the response. I am fully familiar with this position sizing approach. My question was how come the code I back tested gave me a different response for exposure and position size compared to the exploration results? I'd like to understand how was "Pos.Value" and "Shares" were calculated in the trades report. Appreciate your patience withe me on this one.
Thanks,
Arpad

I cant see setpositionsize code in your provided code so you are trading full equity.

https://www.amibroker.com/guide/afl/setpositionsize.html

Try the code in the knowledgebase article already provided to you.

In your code you are assigning PositionSize directly, rather than using the SetPositionSize function as @RichardScanlan suggested. That means you are responsible for the encoding. PositionSize values greater than 0 indicate the value of the trade, not the number of shares. So your Exploration is not "correct", becauae you output the PositionSize variable with a label that indicates how you think that variable is interpreted, but not actually how it works.

See the SetPositionSize documentation for details on PositionSize encoding.

Thank you RichardScanlan and mradtke for the clear explanation. Now I understand the logic.

regards,
Arpad

1 Like

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