Postgres window function to calculate summary position of orders
Not sure if I have got all the logic right but you'll at least have a starting point:

WITH RECURSIVE pos AS
  ( SELECT *
    FROM ( SELECT DISTINCT ON (symbol)
               t.*,  0::numeric  AS order_profit_loss, 
                     qua         AS position_qua, 
                     price       AS position_price 
           FROM public.trades AS t
           ORDER BY symbol, dt 
         ) AS starting
    UNION ALL
    SELECT  
        n.id, n.symbol, n.price, n.qua, n.dt, 
        CASE WHEN n.qua &lt; 0
            THEN n.qua * (p.position_price - n.price)
            ELSE 0
        END                     AS order_profit_loss,
        p.position_qua + n.qua  AS position_qua, 
        CASE WHEN n.qua &lt; 0
            THEN p.position_price
            ELSE (p.position_qua * p.position_price + n.price * n.qua)
                 / (p.position_qua + n.qua)
        END                     AS position_price 
    FROM 
        pos AS p,
      LATERAL
        ( SELECT t.*
          FROM public.trades AS t
          WHERE t.symbol = p.symbol
            AND t.dt &gt; p.dt
          ORDER BY t.dt    LIMIT 1
        ) AS n
  )
SELECT * 
FROM pos
ORDER BY symbol, dt ;