Fund UCITS constraints: 5/10/40 rule

Hi,
I really enjoy the platform. Though I'd like to know if it's possible to implement the UCITS rule of 5/10/40 rule. A rule that should be enforced during the backtest and the live trading of the portfolio. The rule is that every stock in the portfolio should not exceed 5% of the NAV and for those that are bigger than 5% the sum of those stock should not exceed 40% of the total portfolio. Can you please help with ideas to implement this constraint or better provide a code for that.
Thank you

At every
next()
you need to: get broker
value
 loop thru all positions currently held and for every position:
 get position
price
 if position
price
larger than 5% of brokervalue
then save it to dictionarybad_positions
 get position
 calculate value of all positions in the
bad_positions
dictionary  if this
value
larger then 40% of brokervalue
then do what need to be done and what was skipped in your post
 get broker

@ab_trader Perfect thank you for this insight. I'm new to this platform!!

Is it possible to imagine a seperate function that would do that for the portfolio? so we can activate it or copy paste it to every algorythm easily?

@spyamine said in Fund UCITS constraints: 5/10/40 rule:
Is it possible to imagine a seperate function that would do that for the portfolio?
Of course you can imagine such a function: you can define an additional method in your strategy and invoke it from
next

@backtrader said in Fund UCITS constraints: 5/10/40 rule:
Of course you can imagine such a function: you can define an additional method in your strategy and invoke it from next
will try to do this. just a normal function ??
thanks