Author Topic: Market algorithm  (Read 857 times)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Market algorithm
« on: November 23, 2011, 09:37:04 AM »
This is partially design and partially coding question.

There is a market, players sell to the market and buy from the market. Market acts as a middleman, players can't sell directly to each other, they have to sell goods to the market. Market also acts as a buffer (market tries to keep a small stockpile of goods so all goods are always/frequently available) and as a price determiner (based on supply and demand).

I thought that the best solution would be to make a "virtual merchant" with some very simple algorithm that runs the market. His objective is to assure stable prices and to keep a small stockpile of goods. Also, he would try to make a small profit (to prevent speculation/abuse by players), but his objective is not to optimize the profit.

Each item type (iron, wood, stone) on the market is treated separately. The base difference between selling and buying price is 10% (I guess it can change under some circumstances if needed). The prices are recalculated each hour, it's called "cycle". The market buy goods at a given price and stockpile it. The selling and buying price depends on the base item price (predefined estimate of the worth of an item) and on the amount stockpiled so far. The market never puts more than 20% owned quantity to sell each cycle (hour), so there is always something saved to put on sale later (also it should prevent the "buy everything from the market and then sell everything when the price rises"). There is max quota how much the market is willing to buy and sell each cycle. The quota is auto increased each time a player sell/buy from/to the market (so everything that was sold to the market in that cycle can be always bought during that cycle regardless of quotas set by the market).

Variables:
- quantity (stockpiled goods)
- inprice (the price for goods sold by player to the market)
- outprice (the price for goods bought by player from the market); it's +10% of inprice
- inmax (how much the market is willing to buy in that cycle)
- outmax (how much the market is willing to sell in that cycle; never more than quantity of course)
* predefinedbaseprice (if needed I can hardcode some estimate how much the item should be worth)

Questions:
* How to determine inprice for the current cycle?
* How to determine inmax and outmax for the current cycle?
(the formula might use "number of active players" to estimate the volume of transactions and recommended stockpile volume)

Offline AltarofScience

  • Level 12
  • *
  • Posts: 90
  • Reputation: +1/-0
    • View Profile
Re: Market algorithm
« Reply #1 on: November 23, 2011, 10:35:54 AM »
well there are many problems with the market currently. firstly, these goods are of the most value to gather:
wood, meat, iron, clay, stone

this is due to the stamina cost of these items, 3, and the sell price 1, and they are further ordered by amount gained per stamina.

so 3 stamina for wood nets you 7 gold, 7 for meat, 6 for iron, 6 for clay, and 4 for stone.

harvesting is essentially 18 stamina plus a massive time cost for minimal gold gain. something like 10-15 gold.
workshops have an incredible base cost, about 120 gold if you value materials and then you need to use basic materials to craft items whose costs are not proportional to the materials used, or the stamina, or the capital cost of the workshop.

the value of a produced good needs to be the entire value of the input, as well as the value of the excess stamina compared to wood wood gained by excess, and also must be worth it to sink the initial 120 gold for the workshop.
the other problem is that the market will almost instantly deflate, inside a week or two because of the limited base resources vs the starting stamina. the market will be flooded.

i presume this will be somewhat alleviated by the introduction of the player shops, however unless you intend to alter the npc buying values based on production, you will run into an issue. i suspect that given the limited number of cities, if the game becomes popular, the buying price of npcs from player shops will also crash massively. and the older players, who made the sales before will be able to have a much larger amount of gold from before the crash.

i suppose you could grow the npc population as a multiplier of the player base, but this will allow players who are in smaller countries language wise to be much richer, which i suspect will cause player migrations, which will cause your goal of having each country run by people from there in the real world to break.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #2 on: November 23, 2011, 11:00:38 AM »
Don't worry about such things. I want only a solution for a market (prices and volume). All other problems (goods sinks, starting prices, etc) are not the scope of this topic. I will solve these later by other means. Just a working solid market model, that's all I need.

Offline AltarofScience

  • Level 12
  • *
  • Posts: 90
  • Reputation: +1/-0
    • View Profile
Re: Market algorithm
« Reply #3 on: November 23, 2011, 11:02:40 AM »
Don't worry about such things. I want only a solution for a market (prices and volume). All other problems (goods sinks, starting prices, etc) are not the scope of this topic. I will solve these later by other means. Just a working solid market model, that's all I need.

the model will differ based on these other aspects.

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
Re: Market algorithm
« Reply #4 on: November 23, 2011, 03:25:53 PM »
the science you are doing around this market is overkill :) overkill for your "resources" like time, happiness, motivation, stamina :D
long time ago I was thinking of such market too...but it is HARD because you can only assume that people will buy things at ~same number each cycle

what is wrong with ordinary market? it brings something more in game and balance out pretty good

EDIT: I have done some calculations and there is some thingy :)
why do you want to limit  outprice = 1.1 x inprice?
lets say you have A,B,C goods and they are the same in terms of volume A=B=C
inprice of A = 100, outprice = 110
I have stockpiled PLENTY of goods A and I sell them all
so then on market there are 100xA and B=C, which means A should loose much value, I buy those goods for lets say 6 cycles 73,78% which means this:
lets say I sold 10.000 of A for 10.000.000, and bought back 7.378 of A for 737.800 to be in positive
which means that you dont have to allow big price drops, BUT if you dont do that market will look strange :D
« Last Edit: November 23, 2011, 04:13:12 PM by saljutin »

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #5 on: November 23, 2011, 04:00:12 PM »
OK, so how to make "ordinary" market? What is the price change formula?

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
Re: Market algorithm
« Reply #6 on: November 23, 2011, 04:20:21 PM »
main problem in your design is that 20% maximum sell
I tried some newPrice = oldPrice * (goodsStart/goodsEnd) stupid system
it didnt work well :)
in cycle 0 I sold 10.000 of items for 100 (on market there were 100 items)
each cycle I bought 20% of items so after 6 cycles I had almost 75% of sold items and 98,6% of money I gained by selling items :)
thing is called: Crash the market with HUGE amount of goods

EDIT:
problem is that you can sell HUGE amount and buy only 20%, so I tried using that formula above with limit newPrice = MIN(0.8 * oldPrice) and it turns out better :) but I dont know if that is solution...should run more tests

EDIT2:
this limit is not good
lets say I have 2.1M and in 1st cycle I buy 10k goods for 1M (price 100), 2nd cycle I buy 10k goods for 1.1M (price 110)
in 3rd cycle I sell 20k goods for 121 / 1,1 = 110 which means in 2 hours I earned 5% of invested money....you cant overkill this system but one can still play smart and within border to outsmart system
« Last Edit: November 23, 2011, 04:33:11 PM by saljutin »

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #7 on: November 23, 2011, 05:06:12 PM »
You can't buy/sell a huge volume of goods, inmax/outmax prevents it :) For example, inmax/outmax could be 10*numberOfPlayers.
Plus, another rule, market will never buy if quantity stockpiled is 50*numberOfPlayer (it will only sell then).
The amount put on sale (outmax) could be (instead of 20%) 50% or 2*numberOfPlayers (the lower number).

I think, the prices should take into account mostly stockpile. If stockpile is low increase the price by 0.05-2%, if stockpile is high decrease the price by 0.05-2%. So, it is purely self centred algorithm. It does not care about players, it only cares about its own stockpile and try to maximise total amount of transactions.

Also, do not forget that the MARKET OUT PRICE IS HIGHER THAN IN PRICE BY 10% :) Which means, if you can't cheat the system above 10% you are still losing money :) A speculation where you earn 6% per transaction won't work, you are -4% then.
« Last Edit: November 23, 2011, 05:41:10 PM by Chris »

Offline AltarofScience

  • Level 12
  • *
  • Posts: 90
  • Reputation: +1/-0
    • View Profile
Re: Market algorithm
« Reply #8 on: November 23, 2011, 08:07:42 PM »
fascist
>.>
<.<

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #9 on: November 25, 2011, 04:38:32 PM »
I made a simple algorithm for now. It's based on average quantity of ALL goods stockpiled by the market. If the particular good's stockpile is below the average value (shortage) the price goes up, if it is above (surplus) the price goes up. It behaves strange when it reach the maximum price but it kind of works; plus it will never be flooded by goods as AltarofScience noticed since it always compare to the avarage stockpile of goods on the market.

Below 24 hours after I implemented it I noticed players attempts to speculate and outcheat the market :D Not a big surprise, but still they are fast considering the game has just 10 players (you can check it on www.europe1300.eu, go to city then marketplace, when you click "analysis" below the market you will see various internal variables).

Offline AltarofScience

  • Level 12
  • *
  • Posts: 90
  • Reputation: +1/-0
    • View Profile
Re: Market algorithm
« Reply #10 on: November 26, 2011, 10:13:42 AM »
I did some work on the market. It is actually not that difficult to use multies in my opinion, although the fact that market prices are massively illogical is a problem. If I overload the market on barrels with one account, its still not that useful for win because you have to buy grapes from the luxury shop so the net value of wine isnt super high even with cheap barrels. Also furniture is worth less than cloth, which is entirely illogical. There are many other bizarre price mismatches market wise also.

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
Re: Market algorithm
« Reply #11 on: November 26, 2011, 04:12:13 PM »
furniture is from Ikea
clothing is from Versace
 ;D

Offline jack13580

  • Level 7
  • *
  • Posts: 35
  • Reputation: +0/-0
    • View Profile
Re: Market algorithm
« Reply #12 on: November 26, 2011, 05:01:23 PM »
right now im just wondering when the battle system will be fixed because its impossible to attack anyone now because all we get are knights and it never goes above 50% win

Offline AltarofScience

  • Level 12
  • *
  • Posts: 90
  • Reputation: +1/-0
    • View Profile
Re: Market algorithm
« Reply #13 on: November 26, 2011, 06:15:19 PM »
I attack people all the time and win boatloads of gold. even been in 2 battles that got ended. had like 400 knights one time.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #14 on: November 27, 2011, 07:06:08 AM »
Prices are strange partially because I'm learning/experimenting and I'm curious if I could make a market system that could "fix" the prices that were wrongly predefined by the designer without opening a vulnerability enough to break the system.
As for other features of the game, these will be done later. First, market :) It absolutely has to work.

I noticed a strange profit on hay. It's "-4.47035e-10". That's an astronomical number... Also I don't see that gold anywhere in the game, which could not disappear if someone managed to speculate the market to that extend... Could it be some problem with float math? Any idea how could it have happened?
Also take a look at hay (due to low values the 10% profit is not taken by the market so it is the most vulnerable commodity). Are you able to speculate on this? If yes, how much gold you earned?

saljutin, how are the test on your market? Have you discovered something interesting?

Offline AltarofScience

  • Level 12
  • *
  • Posts: 90
  • Reputation: +1/-0
    • View Profile
Re: Market algorithm
« Reply #15 on: November 27, 2011, 12:37:57 PM »
its too much of a pain to test the game with half the functions for producing various goods missing and with many goods being of unknown origin. also with only the normal stamina its hard to do really good tests. change the rules for alpha testing to unlimited stamina and battle points. otherwise you would have to play multiple rounds with many many players to test out the market properly. And explain where spices and rare res is produced.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #16 on: November 27, 2011, 01:42:45 PM »
its too much of a pain to test the game with half the functions for producing various goods missing and with many goods being of unknown origin. also with only the normal stamina its hard to do really good tests. change the rules for alpha testing to unlimited stamina and battle points. otherwise you would have to play multiple rounds with many many players to test out the market properly. And explain where spices and rare res is produced.
I will enable the miracle fountain on Monday (stamina). As for spices you can't get these yet, same for grapes and dye (except luxury shop). Everything else can be obtained via normal ways.

Offline Monatar

  • Level 4
  • *
  • Posts: 13
  • Reputation: +2/-0
    • View Profile
Re: Market algorithm
« Reply #17 on: November 27, 2011, 03:25:35 PM »
Quote from: Chris
It's "-4.47035e-10".

This actually is a very small number (there is a minus-sign after the e). Might be a calculation or rounding difference.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #18 on: November 27, 2011, 03:34:46 PM »
Quote from: Chris
It's "-4.47035e-10".

This actually is a very small number (there is a minus-sign after the e). Might be a calculation or rounding difference.
Oh! That explains it :) Hay has very similar prices so that's why the profit is so strange. I hate the small quirks of float, but it is sooo convenient to use for all other reasons.

Offline Murzim

  • Game Owner
  • Level 14
  • *
  • Posts: 107
  • Reputation: +1/-0
    • View Profile
Re: Market algorithm
« Reply #19 on: November 30, 2011, 05:35:10 AM »
I admit I have not read the full thread on it, but I will reply as I had this in mind as well.

Here is my idea, which amazingly is very simple and you may like it Chris:
-You can buy and sell with gold
-You can put any resources you want in this market except gold of course (You as the coder, not the players)
-Set a default price, same for all resources when you first launch the market let's say at 100 gold
-Set the default quantity for each buy/sell (The most important to set)

Thereafter, you never set a price or worry about the price again. It is the players job to determine the price:
-Each resource buy rises the price 5%
-Each resource sell drops the price 5%
-Resources never end
-Every minute, update prices: If someone bought, rise the price by 1. If someone sold, drop the price by 1.

Voila! Here's your algorithm :)
« Last Edit: November 30, 2011, 05:37:36 AM by Murzim »

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
Re: Market algorithm
« Reply #20 on: November 30, 2011, 09:44:58 AM »
this could work
so at each resource you have buy and sell button so you cannot set quantity
but this system has some problems
if I buy it 10 times that is 1.05^10 of price which is 63% bigger then it was
meaning prices can go skyhigh :) but Im not sure if chris wanted such system :)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #21 on: December 01, 2011, 03:06:14 PM »
The price has to be within a certain bracket. Like if wood base cost is $100 it can reach min 1/3 and max x3 of the base price ($33-$300). The assumption is that if the value exceeds these brackets then the market failed with determining the proper price of the good. The goods are not abstact, these are usable by players and made by players, so these have some inherited real value and real cost (if sending an expedition to China to import silk costs $1000 + effort/time then the market can't set the price of silk from that expedition at $1, on average and except situation when way too many players try to do it the price should be above $1000). The market is only to adjust the value in case the designer was wrong and to make players readjust their strategy based on supply and demand (too many carpenters = the price for wooden products falls, some should switch to another profession).

Offline Murzim

  • Game Owner
  • Level 14
  • *
  • Posts: 107
  • Reputation: +1/-0
    • View Profile
Re: Market algorithm
« Reply #22 on: December 01, 2011, 03:59:43 PM »
Why worry about the price ? You won't be inflating anything after all. The players will continue to produce these goods. If a price skyrockets, then the players might see this as an opportunity to sell their stocks at that exact point. This will bring back the price to where EXACTLY it should be.

Ocassionally, some players may earn big sums through the market but always in the expense of other players who managed it more poorly than them. If you want to avoid this, then it is better to drop the auto market idea.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Market algorithm
« Reply #23 on: December 04, 2011, 08:38:20 AM »
Why worry about the price ? You won't be inflating anything after all. The players will continue to produce these goods. If a price skyrockets, then the players might see this as an opportunity to sell their stocks at that exact point. This will bring back the price to where EXACTLY it should be.
As a player, I find skyrocketing prices lame. I would not enjoy a game where the income depends if I logged in at a proper moment (maybe with exception to stock exchange simulators, but I don't play such games anyway). It simply means boring grinding (login as frequent as possible to check the prices and act if these are very big/low). Some income from speculation is OK, and even desired, but if you can double/tripple your standard income from producing/finding things via speculation on a market then such game is broken to me, as a player.

Quote
may earn big sums through the market but always in the expense of other players who managed it more poorly than them
No, not at expense of players but at expense of the system. The market we are talking here is not an auction but a game controlled&owned market. You buy from the market and sell to the market (not directly from other players), so all loses are inflicted to an abstract market entity not to players (in short you can produce money from air via it, if you manage to abuse it).

The market I implemented so far works this way:
* the market algorithm's objective is to make profit via sell/buy prices difference and to keep a decent stockpile of goods of all kinds
- the market buy goods from players and stockpile them
- the market sell goods to players at 10% higher then the current buy price (only if there are goods stockpiled, can't sell if nothing stockpiled)
- the market try to put no more than 20% of goods (but no less than 100 units of good) on sale and keep the rest as a reserve for later (to minimize price fluctuations from temport shortage of goods)
- the price (again, market sell at +10%, so there are actually two prices) depends on the stockile (if stockpile is estimate high comared to otherd goods then the price go down, and it go up if there is shortage of stockpile).
- there is a predefined base price for each good, the market price can never tripple it.
- there is a limit how much the market is willing to buy/sell each hour (2 separate values of course). The limit depends on number of players, stockpile and current price (where current price is opposite to expectation, if there is shortage and the price is very high the market is willing to buy less of such goods, purely to avoid losing money via players selling a high volume of goods at a high price at once).
- each time a player sell a good the limit of buy increases by the same amount and when buy the sell limit increases by the same amount (so in theory players overall can make an unlimited number of transactions as long as some players are selling and some are buying the same good within the same hour).

It works OK, buy I have doubts about the buy/sell limits on the market. Not sure how it will work with more players... Plus, is means that sometimes you can't sell your goods because the hourly global quota of transaction has been reached.
On the other, I don't want to remove it because it already saved the marked a few times (it means that any bug/glitch/whatever with market can be abused only to a relatively small extend, which is very nice).

I'm wondering if I should not introduce indivudual limits instead. Like: each player can have 1000 carts max, +100 per hour. Each cart = 1 units of good bought/sold. If your profession is merchant you have doubled carts.

Or keep them both... Global market limit (but more generous) and individual carts limit.

« Last Edit: December 04, 2011, 08:40:39 AM by Chris »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal