# How to create / program a fluctuating market

Hello.
I am creating a 2d RPG akin to Uncharted Waters: New Horizons on the SNES. I need to find out how to create a fluctuating market (buying an item in one port and selling it in a different port for a profit/loss) and I cant find any guidance on the subject.

Not really as simple as just a few lines of code. Youâ€™ll need to set up classes to sell stuff in your shops first. If you have your shops and all the buy and sell code setup, then you have to determine what changes the price of products. Then you just use those factors to adjust the price of the product.

1 Like

But weâ€™d all get rich if if were .

Countless studies have established that the American stock market is effectively random, so you could simulate a market by simply choosing some random percentage of a commodityâ€™s current price and adding (or subtracting) that percentage. The size (standard deviation) of the change could vary by commodity (stock market people call this â€śvolatilityâ€ť).

Now, if you want to simulate arbitrage (attempting to profit from the difference in price for the same commodity in two different markets), you probably want the prices for something in two markets to at least correlate with each other. You could do that by using one random price history as created by the above method, then add/subtract small fluctuations that were specific to each market.

As @Brathnann points out, it can get a lot more complicated. If, for example, the item you bought is wheat, and you bought it in a place where there had been a record crop year, while elsewhere the same type of wheat had suffered from a drought and very low production, you could sell your wheat at a profit if you can move it to the drought-stricken area for less than the price difference per unit. That is, if you buy a bushel of wheat for a dollar where it is cheap, and it costs fifty cents to ship that bushel to where they have had a drought, and can sell it there for two dollars, you have made a profit of fifty cents on the full transaction.

But the laws of economics get into the game at this point. The result will be that others will also ship wheat and offer it for sale at \$1.90, to undercut you while still making forty cents of profit. Yet others will then sell their wheat at \$1.80, then \$1.70, and so on, until someone is offering wheat at \$1.51, making barely any profit, but still offering the best deal.

Now, if you have a means of shipping wheat that is cheaper than others have, you can still make a profit that others cannot make by undercutting you. For example, if you are the only owner of a big wheat-carrying ship, maybe you can ship huge amounts of wheat at forty-five cents per bushel. Now, you can sell your wheat at \$1.50, make five cents per bushel and, because no one else can ship as cheaply as you can, you have the market to yourself.

In a game, you can decide which factors will influence prices. Basic economics of supply and demand are the place to start, those two factors being driven by whatever environmental concerns you decide to include. Then, when you want to add arbitrage, isolate the supply and demand of the two markets (along with their environmental concerns), add in the shipping costs, and you have a decent starting model. To make it even more interesting, you could add a time factor, where demand varies between the markets over time, and not the same way. For example, when it is warm in the southern hemisphere and the weather makes it easy to grow bananas, it is cold in the north. So, cheap bananas from the south would sell for higher prices in the north. But you have to ship them, which cuts into the difference and your profit. Instead, however, if you could store your bananas when they are cheap to buy, then wait for cold weather when bananas are more expensive, you could sell them without shipping them and get the increased price for a profit.

Problem there is that bananas donâ€™t keep. Your challenge, then is to find a commodity that you can store, and that you can store more cheaply than anyone else can store them. This time, instead of shipping, you have to pay for storage (market people call this â€śthe cost of carryâ€ť). The math ends up being about the same, itâ€™s just another way to look at arbitrage: time instead of space.

Hope that helps.

3 Likes

Great post, very interesting!

1 Like

Iâ€™d love to hear how this goes. I explored a similar concept a couple years ago and never was able to build anything convincing and fun for the market.

Actually I found a script by Callum2409 here called TRADE SYS which seems to be exactly what Iâ€™m looking for. I just donâ€™t know how to implement it into my project. I messaged him so hopefully Iâ€™ll hear back soon.

Did you ever hear back?