Fund UCITS constraints: 5/10/40 rule

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.