StockMarket is a Minecraft mod that brings a better way for trading to the game. It enables players to trade items on a virtual stock market, making it ideal for multiplayer servers with active economies. For Server that have not enough players or even for single player, a market bot can be added that simulates a large player base and provides liquidity to the market.
- Features
- Blocks
- Items
- Matching Engine
- How to use
- Commands
- Stock Market Bot
- Discord to help me improve the mod
- Adds a banking system to the game for money and items.
- Adds new blocks to interact with the market or bank account.
- Implementation of a matching engine inspired by the real market.
- Configurable bot which provides the market with liquidity and volatility
- Bank System Mod
- Architectury
- Quilted Fabric API (Only needed for Quilt)
- Mod Utilities (Only needed for Quilt)
- Fabric API (Only needed for Fabric)
| Minecraft | Fabric | Forge | Quilt | Neoforge |
|---|---|---|---|---|
|
Stock Market Block Used to get access to the stock market. Interaction using right click. Right click on a Terminal Block using a Trading Software to create this block. |
|
Trading Software Used to programm the Terminal Block to be a Stock Market Terminal Block 1 Empty Software 1 Emerald |
A matching engine is the core component of a trading system that facilitates the buying and selling of items, stocks, or other assets. Its primary function is to match buy orders and sell orders based on specific rules to execute trades efficiently and fairly.
For simplicity are no special roles implemented. The type of order and the price is all what the matching engine needs to match the orders.
A market-order requires only the amount to buy/sell. The trade is executed on the best available price.
Benefits
- The trade is executed immediately
Disadvantages
- When buing/selling, the average buy/sell price is mostly higher/lower than the market price before the trade.
- Creates high volatility for markets with low liquidity, see: Order Book Swipe
- A market-order can only be executed when there are enough volume present on the opposite side to fill the order. Otherwise the order may only be filled partially.
A limit order requires an amount and price at which the trade gets executed. A buy order gets mostly placed below the current market price. The order is executed when the market price moves towards the order price.
Benefits
- For a buy order, the price a trader has to pay is at the price level or lower.
- For a sell order, the received amount is at least the price or higher.
- Limit orders are important for a market because it provides the required liquidity for the market to be able to process market-orders.
Disadvantages
- The order needs time to be filled by the market.
- When the price never reaches the desired buy/sell price, the order never gets executed and stays pending.
To sell items the item must be available on the market. This is managed by the server admin.
Assuming the item can be traded on the market, you first have to deposit the item to your bank account. This can be done using the Bank Terminal Block.
See Bank System Mod to learn how.
Now open the Stock Market Terminal Block using right click.
This opens the trading window.
On the left side you can see the Candle stick chart
On the left edge of the chart is the price axis.
On the right edge of the chart is the Order Book volume displayed.
On the right side of the window is the interaction panel. There you can select the desired item to be traded. Just click on the button and a list of all tradable items will appear.
Type the amount of items you want to buy/sell.
Decide whether you want to execute a market-order or a limit order.
If you want to create a limt order, you have to type in the desired price.
Click on the sell button for market or limit order to execute the trade.
A market-order is executet directly but the limit order will be listed on the left side until it is filled or gets canceled.
To buy a item you have to get some money first. You can do that by selling other items or receiving money from other players.
A server admin can also provide you money...
Assuming you have money, open the Stock Market Terminal Block
Select the item you want to trade by pressing the button on the top right with the Label "Price: XX".
Type the amount you want to buy, type a limit price if you want to execute a limit order.
Click on the buy button.
A market-order gets executed directly but a limit order may take some time until it gets filled by the market.
Once you have received the items, you can go to the Bank Terminal Block receive the bought items.
A limit order can be changed by clicking on the marker on the position of the order. There is a small botton on the left sinde of
the market. Drag and drop the order to the new price level.
If you do not have enough money to move a buy order higher, the order will not be moved to that location.
When you first join a server/save using this mod, some default markets are getting created by the mod. Each player receives its bank account with 0 balance.
Quickstart
Setting up the market botsThe simplest way to setup the bots is by calling the command bellow.
This command creats a bot for each market which was added by default from the mod.
You may want to change the settings for the bots later.
/StockMarket createDefaultBots
Create a new market
Replace the [itemID] with the minecraft item ID for which you want to create a market.Check out the website https://minecraftitemids.com/ to find the item ID you need.
/StockMarket [itemID] createYou may need to type the " in order to type a string that contains :.
/StockMarket "minecraft:copper_ingot" create
Remove a market
Replace the [itemID] with the minecraft item ID for which you want to remove the market.Check out the website https://minecraftitemids.com/ to find the item ID you need.
/StockMarket [itemID] removeYou may need to type the " in order to type a string that contains :.
/StockMarket "minecraft:copper_ingot" removeOpen positions from players will be automaticly closed.
Creating a custom bot
There is now a GUI that lets you create and manage the bots. No complicate command line usage is needed any more.
Go to the next expandable section "Change bot settings" to learn about that
To create a bot from scratch make sure the market, for which you want to create the bot, already exists.
Type the following command, replace the item ID with your markets item ID.
/StockMarket "minecraft:copper_ingot" bot createThe bot gets created but is not active, you have to configure it first.
Type the following command to read the settings the bot has by default:
/StockMarket "minecraft:copper_ingot" bot settings getTo set a setting you can type the following and use the auto complete to select your desired parameter.
(**[parameter]** is a placeholder for your chosen parameter you want to set)
/StockMarket "minecraft:copper_ingot" bot settings set [parameter]If you are not sure about what you have to set to which value, just let the values on default.
The only values you have to change because they are unique for each item are the following:
- Target Price Range
Defines the range the bot will try to move the price around. The value you have to set is the upper bound of the range. The lower bound is automaticly 0.
- Target Item Balance
Defines the amount of items the bot try's to hold in its bank account. If player buy to many items from a bot, the bot will rise the price. When players sell to many items,
the bot will lower the price. When the bot holds exactly the target amount balance, the price will move to the target_price_range_value/2.
Lets say you want the bot to hold 1000 copper ingots and the price should than be at around 100$, you have to type the following:
/StockMarket "minecraft:copper_ingot" bot settings set targetItemBalance 1000/StockMarket "minecraft:copper_ingot" bot settings set targetPriceRange 200Before the bot can be enabled, you have to overwrite the item balance of the bot.
/bank "StockMarketBot" setBalance "minecraft:copper_ingot" 1000If you want the bot to start the market at the target_price_range_value/2 you must give the bot the same amount of items as you have set the Target Item Balance.
If you want to create an artificial demand from the bot at the start, you can give less to the bot.
The bot must always have some items and money, otherwise it can't place orders.
/bank "StockMarketBot" show/bank "StockMarketBot" setBalance "banksystem:money" 1000000000Change bot settings
Just type the command/StockMarket BotSettingsGUIThis opens a gui that lets you change all settings from a bot.
- Change Item to select a market.
- Save Settings to save the current settings which are listed below.
- Market Open Open/Close the market for players to trade in order to safely change settings without players exploiting the market movements.
Players can not place new orders when a market is closed. Already placed orders will still get processed when the market is closed.
- Destroy Bot/Create Bot to delete or create a bot for that item.
- Bot Setup Opens a setup guide, that asks questions about desired market behavior in order to find the correct bot settings.
- Bot Bank to access the bank account of the bot to set its balance.
1) Close the market so players can't exploit the market when you change settings. Click Save
2) Click on Bot Setup and follow the instructions.
3) You can now ajust the settings from the setup guide to make more individual changes if you want.
4) Click on Save Settings to apply the settings to the bot and watch the market ajusts for the new settigns.
5) Once you are happy with the result, Open the market again to enable trading for players.
Some Informations about ajusting settings manually:
The bot must always have some items and money, otherwise it can't place orders.
I reccoment to try it on a single player to get a feeling for it. There are so many differend possibilities the bot can react to it.
🚧A detaild explanation for each setting will come soon.
| Command | Description | Admin only |
|---|---|---|
| /StockMarket setPriceCandleTimeInterval [seconds] | Set the interval for the price candles. (Each candle will represent this amount of time) | ✔️ |
| /StockMarket createDefaultBots | Create default bots | ✔️ |
| /StockMarket createDefaultBot [itemID] | Create a default bot for that item if presets are available | ✔️ |
| /StockMarket order cancelAll | Cancel all orders | |
| /StockMarket order cancelAll [itemID] | Cancel all orders of an item | |
| /StockMarket order [username] cancelAll | Cancel all orders of a player | ✔️ |
| /StockMarket order [username] cancelAll [itemID] | Cancel all orders of a player for an item | ✔️ |
| /StockMarket BotSettingsGUI | Open the settings GUI for the market bots | ✔️ |
| /StockMarket ManagementGUI | Open the management GUI to create and remove trading items | ✔️ |
| /StockMarket save | Save the Stockmarket data | ✔️ |
| /StockMarket load | Load the Stockamrket data | ✔️ |
| /StockMarket [itemID] bot settings get | Get bot settings | ✔️ |
| /StockMarket [itemID] bot settings set enabled | Enable bot | ✔️ |
| /StockMarket [itemID] bot settings set disabled | Disable bot | ✔️ |
| /StockMarket [itemID] bot settings set volatility [volatility] | Market volatility strength | ✔️ |
| /StockMarket [itemID] bot settings set orderRandomness [orderRandomness] | Additional random sell/buy orders | ✔️ |
| /StockMarket [itemID] bot settings set imbalancePriceChangeFactorLinear [factor] | Linear scale how much a price changes when the item stock moves away from the target item stock. | ✔️ |
| /StockMarket [itemID] bot settings set imbalancePriceChangeFactorQuadratic [factor] | Quadratic scale how much a price changes when the item stock moves away from the target item stock. (only active when current balance [ desired balance) | ✔️ |
| /StockMarket [itemID] bot settings set imbalancePriceRange [priceRange] | Set imbalance price range | ✔️ |
| /StockMarket [itemID] bot settings set targetItemBalance [balance] | Set the amount the bot trys to hold in its bank | ✔️ |
| /StockMarket [itemID] bot settings set targetPriceRange [priceRange] | Defines a price X. The bot will try to move the price up to X if its stock is faar below its targetItemBalance. For example: If the bot try's to hold 1000 Iron ingots, the price range is set to 100 and the bots current balance is 1000 Iron Ingots. The bot will move the price to 500 which is half of the TargetPriceRange. | ✔️ |
| /StockMarket [itemID] bot settings set maxOrderCount [orderCount] | Set max order count a bot can create | ✔️ |
| /StockMarket [itemID] bot settings set volumeScale [volumeScale] | Set volume scale factor to scale the liquidity in the orders | ✔️ |
| /StockMarket [itemID] bot settings set volumeSpread [volumeSpread] | Set the price range the bot places some orders | ✔️ |
| /StockMarket [itemID] bot settings set volumeRandomness [volumeRandomness] | Set volume randomness added to each limit order | ✔️ |
| /StockMarket [itemID] bot settings set timer [timer] | Set the current timer millis, used to do a new random walk step | ✔️ |
| /StockMarket [itemID] bot settings set minTimer [timer] | Set random min timer value for random walk timer | ✔️ |
| /StockMarket [itemID] bot settings set maxTimer [timer] | Set random max timer value for random walk timer | ✔️ |
| /StockMarket [itemID] bot settings set updateInterval [timeMS] | Set the bot update interval | ✔️ |
| /StockMarket [itemID] bot settings set pidP [pidP] | Set PID P | ✔️ |
| /StockMarket [itemID] bot settings set pidI [pidI] | Set PID I | ✔️ |
| /StockMarket [itemID] bot settings set pidD [pidD] | Set PID D | ✔️ |
| /StockMarket [itemID] bot settings set pidIBounds [bounds] | Set PID I bounds, abs(I) will be [pidIBounds] | ✔️ |
| /StockMarket [itemID] bot settings set pidIntegratedError [pidIntegratedError] | Set the current integrated error of the PID controller | ✔️ |
| /StockMarket [itemID] bot create | Create bot | ✔️ |
| /StockMarket [itemID] bot remove | Remove bot | ✔️ |
| /StockMarket [itemID] create | Create marketplace | ✔️ |
| /StockMarket [itemID] remove | Remove marketplace | ✔️ |
| /StockMarket [itemID] open | Open the marketplace for trading | ✔️ |
| /StockMarket [itemID] close | Close the marketplace for trading | ✔️ |
| /StockMarket [itemID] currentPrice | Get current price | ✔️ |
The StockMarketBot has multiple purposes.
- Providing the market with liquidity
- Changing price depending on supply and demand
- Source for capital for players (if they start with 0 money)
Liquidity is a very important part of every market. For example if player A wants to buy wood, someone has to sell the same amount. Without the counterpart, a trade can't be made. Since it is unlikely that a server creates enough sell/buy orders to create this liquidity, the bot is used to provide both buy and sell orders. The bot will buy if a player wants to sell, and he will sell if a player wants to buy. The bot can only sell so many items it has and he also can only buy as long as he has money. The bot always needs some balance of items and money. Without that its function will break.
The bot is responsible for the most price movements you see on the chart.
The bot is always buying and selling to it self.
Using a combination of limit and market -orders will create
movements in price.
To create a random price chart that also stays in its realistic bounds, multiple stages are needed.
-
Defining parameters
- target stock balance
- target price range
- Others...
-
Defining a target price the market should move to Depending on how many items the bot holds and what its target item balance is, it will try to rise or lower the price. For example if the bot holds 1000 items, wants to hold 500, the bot will lower the target price, making it attractive to players to buy. In the case the bot has more items than the target stock balance, the target price fall linear and also can reach a price of 0. In the case the bot holds less than he wants, the target price will rise quadraticly, producing a even larger price increase. The reason for that is, that the bot is dependend on enough supplies to work correct, a larger price increase makes it lucrative for players to sell to the bot.
Additionaly some random noise is used to create the volatility. This noise is added to the target price and is generated using a Random Walk generator.
-
Move the market to the target price To change the price the bot creates market- sell/buy -orders. The market oder will fill the previously placed limit orders from the bot or a player. Click here to learn why a price moves. The size of the market-order defines how fast the price will change. A PID-Controller is used to generate the buy/sell market-orders. The PID-Controller will make sure to generate the right amount of orders until the target price is reached.
If you don't know what a Order Book is, learn the basics first and come back later.
A price can move in two ways:
- market-order
- Limit on oposite price A market-order is always executed at the current price. Example with a buy market-order:
- A player places a market buy order with a volume of 8.
- The matching engine will search a sell limit order with the lowest price.
- The market-order gets filled by the sell order.
- If the buy order is not filled completly, the matching engine searches again for the best sell order
- The fill-search process gets repeated until the complete market-order is filled.
- If no more sell orders can be found to fill the buy order, the buy order can not be filled completly. No one is selling, so the remainings of the order will get canceled. To avoid this problem, enough liquidity is needed for the market.
Example with a buy limit order:
- A player places a limit order with a volume of 8 above the current market price.
- The matching engine will search a sell limit order with the lowest price to fill the buy order, just like the market-order. But the engine will not search for higher limit orders than the limit price of the buy order.
- If the order was not filled completly, the remaining amount will stay at that position as a normal limit buy order.
As you can see in the pictures below, the current price is always that price, where the matching engine has last processed 2 orders.
The Order Book is a list with all limit orders waiting to be processed. A buy order is always on a lower or equal price as the current market price and sell orders above or at the same price. When a buy order is at the same price like a sell order, they will get matched by the matching engine. The spread is the price difference from the last executed sell to the last executed buy price. The smaller the spread the better. Buy/Sell orders which are placed nearer to the current market price will get processed first.
A order book swipe can occure when a market does not provide enough liquidity to fill large market-orders. The image above shows that in an example for a large buy order and to less sell limit orders. Since the matching engine try's to fill the market-order, the market price can rise extremly high when the market-order is large enough and the sell volume is low enough. For the player who executed the buy order, this is bad since all sell orders are on different prices, the average buy price can rise much higher than expected. The same can happen for sell orders, in that case the player will not receive as much money for the selled goods as expected.
The candle stick chart is a common used representation of price history. Each candle has a fixed time width. Candles are available for different time scales. The timescale can't be changed in the mod. A admin can change the time for each candle, default is 1 min. A cancle can be defined with 4 values:
- Open Price The current price the market had when the new candle was created.
- High Price The highest price the market had since the candle was created.
- Low Price The lowest price the market had since the candle was created.
- Close Price The current price the market had when the candle was finished.
Below is a short animation to show how a price movement can be visualized using candle sticks.
A random walk is a way to generate pseudorandom values that depend on the previous values. This value is a great source to create random market prive movements.
Since this is a complex field from control theory, I will not cover this here. PID Controller Wikipedia








