This kind of conflates, because whether I no longer have the goods because they are stashed in a city across the map, or given to a companion who was then 'lost' from the clan shortly afterwards. I don't possess the goods any longer, so I don't think they should count towards this theoretical average price you're suggesting.
I can't imagine the processing toll taken on the game for tracking the thousands and thousands of grain, flax, or fish I traded at each market. Or how an individual piece of grain may have been moved by me multiple times.
There is no processing toll, only a single value per goods type is tracked and only a single calculation is performed at the time of buying or selling, it is as technically efficient as any possible solution to the problem. The whole point is that it isn't practical to track individual units, and nor is it necessary - my assertion is that you get the same accuracy by using averages properly. I'm looking for scenarios that could prove that assumption wrong, and so far nothing suggested has broken it.
Regarding how to deal with stashes and parties etc, the problem to be solved is one of beneficial control. This is seen in tax/economic/legal systems in the real world and there are principles which provide the basis for solutions. When it is in your stash you still control it, so just as in the real world there would be no gain or loss for tax purposes, in the game there is no gain or loss for skill gain purposes. If it disappears from the stash it is treated the same way as if it disappears from your inventory, the integrity of the system and its tracking remains balanced by following consistent process. The player is rewarded for buying low and selling high as intended, and goods being used up are removed from the system in a fair way.
*** Secondary parties is where things get interesting ***
Thanks you so much, THIS is the sort of thing I was looking for in asking for ways to break the system. Secondary parties have a bunch of conceptual issues which their current design doesn't fully address, how the gains of Trade skill of their leaders is managed is only one of the problems.
Because we want companions to be able to level their Trade skill by use and they presumably make buying and selling decisions autonomously, it makes sense that we draw the line on beneficial control for items they have, so they then become responsible for their value and get gains based on the buy and sell differences. They can operate under the same rules as the player does and everything is fine, with 1 exception: "Free Stuff".
Free Stuff is anything that a responsible entity (the player or a companion's party) acquires without paying for it. Right now we don't have visibility of companion party inventories, but let's assume we will do in future, and even if we can't see them there is evidence that they already have real inventories. When you raised secondary parties it made me realise the need to deal with handling items you might give to a secondary party. What is the value base for the items the companion receives from you? They got them for "free", so does that mean they should be treated as if they bought them for zero denars and whatever they sell it for is 100% profit? That would allow you to power level your companions' trade skill by giving them a ton of stuff which they will subsequently sell. Similar if you take stuff from your companion's inventory - you get it for "free", so you then sell it for 100% profit and power level your own trade skill. Either that or it counts for nothing at all and is ignored and nobody gains skill based on selling it if it was transferred between parties.
In addition to transfering items between parties, the concept of Free Stuff has another unmanaged edge case which is when you receive items in loot. You didn't pay anything for them, does that mean selling them is 100% profit for skill gain purposes or does it mean there should be no skill gain impact when you sell them? The current system drops it on the floor and you get nothing for it, unless you buy some of that goods type after getting it for free and that sets a price for the entire stack. By itself that is a kludge and not a real solution.
Solutions:
For transferring items between parties there is a robust solution: the giver simply loses the items as if they were expended. Their average buy price isn't effected, and since they haven't sold it there is no skill check. The receiver inherits the average buy price of the giver for that item/stack, and thus the potential skill gain is transferred to them without creating any hax skill gain opportunities out of thin air. Integrity of system is preserved and fair skill gain opportunities are tracked. There is potential for optimising skill gain between party members by carefully moving your goods and capital around, but it just that: optimisation and not exploitation.
For getting items in loot the solution I currently have in mind is a little sloppy and there may be a better alternative. My working suggestion is that if you get more of an item you already have, the average doesn't change - it is assumed to have the same value as the stuff you've already bought, the stack just gets bigger. If it will be creating a new stack because you don't have any of that commodity, it starts with the market average value. Even though this isn't perfect, it's still an improvement over the existing kludge.