> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dietrich-development.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Developer reference

> Stockmarket database schema, stock type configuration, and custom data sources

## Custom data sources

You can influence stock prices from your own scripts by returning a numeric factor from a function per stock id:

```lua config.lua theme={null}
-- Custom Influence your Stocks (Only use if you know what you are doing)
Config.CustomDataSources = {
    --[[ ["VCC"] = function()
        local cash = exports.myeconomy:getCompanyBalance("valentine_coal")
        return cash / 1000
    end,

    ["SBF"] = function()
        local reserves = exports.bank_system:getReserves("saint_denis")
        return reserves / 500
    end, ]]
}
```

<Warning>
  Custom data sources should only be used if you understand how they affect stock pricing. Incorrect implementation can lead to unstable market behavior.
</Warning>

## Database table structure

The stock market system uses a database table called `stocks` to store all stock configurations. Each stock is defined by a single row in this table with the following fields:

### Stock table fields

<AccordionGroup>
  <Accordion title="Basic information">
    * **id** (string): Unique stock identifier (e.g., "VCC", "AMCO")
    * **label** (string): Display name shown to players (e.g., "Valentine Coal Co.")
    * **description** (string): Detailed description of the company/stock
    * **base\_price** (integer): Initial stock price when first created
    * **volatility** (float): Price volatility multiplier (1.0 = normal, 2.0 = double volatility)
    * **enabled** (boolean): Whether the stock is active and tradeable
  </Accordion>

  <Accordion title="Stock type configuration">
    * **type** (string): Stock calculation method - "realtime", "random", "custom", or "mixed"
    * **realtime\_source** (string): API provider name for real-time data (e.g., "api\_provider")
    * **realtime\_symbol** (string): Stock symbol for real-time data (e.g., "AAPL", "TSLA")
    * **realtime\_weight** (float): Influence of real-time data on price (0.0 to 1.0)
    * **random\_enabled** (boolean): Enable random price fluctuations
    * **random\_weight** (float): Influence of random fluctuations on price (0.0 to 1.0)
    * **random\_max\_change** (float): Maximum percentage change per update cycle
    * **custom\_weight** (float): Influence of custom data sources on price (0.0 to 1.0)
  </Accordion>

  <Accordion title="Item integration">
    * Currently disabled
  </Accordion>

  <Accordion title="Dividend settings">
    * **dividends\_enabled** (boolean): Enable dividend payouts for this stock
    * **dividends\_mode** (string): "fixed" for set amount or "percent" for percentage
    * **dividends\_amount** (float): Fixed dividend amount (used when mode is "fixed")
    * **dividends\_percent** (float): Dividend percentage (used when mode is "percent")
  </Accordion>

  <Accordion title="RP events">
    * **rp\_events\_enabled** (boolean): Enable RP events to affect this stock's price
  </Accordion>
</AccordionGroup>

### Stock type examples

<AccordionGroup>
  <Accordion title="Real-time stock">
    ```sql theme={null}
    -- Stock that follows real-world market data
    type = "realtime"
    realtime_source = "api_provider"
    realtime_symbol = "AAPL"
    realtime_weight = 1.0
    random_enabled = 0
    random_weight = 0
    custom_weight = 0
    ```
  </Accordion>

  <Accordion title="Random stock">
    ```sql theme={null}
    -- Stock with purely random price movements
    type = "random"
    realtime_source = NULL
    realtime_symbol = NULL
    realtime_weight = 0
    random_enabled = 1
    random_weight = 1.0
    random_max_change = 5.0
    custom_weight = 0
    ```
  </Accordion>

  <Accordion title="Mixed stock">
    ```sql theme={null}
    -- Stock combining multiple price influences
    type = "mixed"
    realtime_source = "api_provider"
    realtime_symbol = "TSLA"
    realtime_weight = 0.6
    random_enabled = 1
    random_weight = 0.3
    random_max_change = 3.0
    custom_weight = 0.1
    ```
  </Accordion>

  <Accordion title="Custom stock">
    ```sql theme={null}
    -- Stock influenced by custom data sources
    type = "custom"
    realtime_source = NULL
    realtime_symbol = NULL
    realtime_weight = 0
    random_enabled = 0
    random_weight = 0
    custom_weight = 1.0
    ```
  </Accordion>
</AccordionGroup>

## Database usage examples

### Creating a real-time stock

```sql theme={null}
-- Example: Apple-inspired technology stock
INSERT INTO `stocks` (
    id, label, description, base_price, volatility, type, 
    item_name, item_enabled, realtime_source, realtime_symbol, realtime_weight,
    random_enabled, random_weight, random_max_change, custom_weight,
    rp_events_enabled, dividends_enabled, dividends_mode, dividends_amount, dividends_percent, enabled
) VALUES (
    'TECH', 'Valentine Tech Co.', 'Innovative technology solutions', 
    150, 1.2, 'realtime', 'stock_tech', 1, 
    'api_provider', 'AAPL', 1.0, 
    0, 0, 0, 0, 
    1, 1, 'percent', 0, 2.0, 1
);
```

### Creating a randomized stock

```sql theme={null}
-- Example: Local business with random fluctuations
INSERT INTO `stocks` (
    id, label, description, base_price, volatility, type, 
    item_name, item_enabled, realtime_source, realtime_symbol, realtime_weight,
    random_enabled, random_weight, random_max_change, custom_weight,
    rp_events_enabled, dividends_enabled, dividends_mode, dividends_amount, dividends_percent, enabled
) VALUES (
    'LOCAL', 'Local Business', 'Small local enterprise', 
    25, 0.8, 'random', 'stock_local', 1, 
    NULL, NULL, 0, 
    1, 1.0, 5.0, 0, 
    1, 1, 'fixed', 1, 0, 1
);
```

### Creating a mixed mode stock

```sql theme={null}
-- Example: Mining company with mixed price calculation
INSERT INTO `stocks` (
    id, label, description, base_price, volatility, type, 
    item_name, item_enabled, realtime_source, realtime_symbol, realtime_weight,
    random_enabled, random_weight, random_max_change, custom_weight,
    rp_events_enabled, dividends_enabled, dividends_mode, dividends_amount, dividends_percent, enabled
) VALUES (
    'MINE', 'Heartland Mining', 'Mining operations in the Heartlands', 
    75, 1.0, 'mixed', 'stock_mine', 1, 
    'api_provider', 'FCX', 0.6, 
    1, 0.3, 4.0, 0.1, 
    1, 1, 'percent', 0, 1.5, 1
);
```

### Updating stock settings

```sql theme={null}
-- Change stock price manually
UPDATE `stocks` SET base_price = 200 WHERE id = 'VCC';

-- Enable/disable a stock
UPDATE `stocks` SET enabled = 0 WHERE id = 'AMCO';

-- Modify dividend settings
UPDATE `stocks` SET dividends_percent = 3.0, dividends_mode = 'percent' WHERE id = 'SDBT';

-- Adjust volatility
UPDATE `stocks` SET volatility = 1.5 WHERE id = 'BWTC';
```
