Function that update stock by date

I have a query that insert only data in table stock by date, now i want to do a function that also have update.

See fiddle : https://dbfiddle.uk/?rdbms=postgres_12&fiddle=f380003bb7481b37928da5cea669ea0a