Skip to content

Commit

Permalink
Merging changes, defined price functions
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed May 25, 2026
1 parent f4b97df commit 69f7abe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public CSVStockFileParser() {}
public boolean verifyCSV(List<String> lines) {
return lines.stream()
.filter(l -> !(l.startsWith("#") || l.isBlank()))
.noneMatch(l -> l.split(",").length != 4);
.noneMatch(l -> l.split(",").length != 4 || l.split(",")[3].split(";").length != 6);
}

/**
* Parses the supplied lines if they satisfy the correct format expectations
*
* @param lines <p>lines to be parsed. <br>
* Each line must contain three data fields: String,String,BigDecimal <br>
* Each line must contain four data fields: String,String,BigDecimal,String <br>
* (Fields cannot be blank) <br>
* blank lines or lines beginning with '#' are ignored
* </p>
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/millions/model/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import millions.model.calculators.PriceChangeCalculator;
import millions.model.factories.PurchaseFactory;
import millions.model.factories.SaleFactory;
import millions.model.factories.TransactionFactory;

/**
* The stock exchange where players buy and sell shares. Manages stocks and simulates weekly price changes.
* The stock exchange where players buy and sell shares. Manages stocks and simulates weekly price
* changes.
*/
public class Exchange {
private String name;
Expand Down Expand Up @@ -141,23 +141,22 @@ public List<Stock> getLosers(int limit) {
.collect(Collectors.toList());
}

/**
* Advances the current game week by performing new price calculations for all stocks.
*/
/** Advances the current game week by performing new price calculations for all stocks. */
public void advance() {
PriceChangeCalculator priceChangeCalculator = new PriceChangeCalculator();
PriceChangeCalculator priceChangeCalculator = new PriceChangeCalculator();
this.weekNumber++;
for (Stock stock : this.stocks.values()) {
BigDecimal change = priceChangeCalculator.calculateChange(stock);
stock.addNewSalesPrice(stock.getSalesPrice().add(change));

// double change = 0.9 + random.nextDouble() * 0.2;
// stock.addNewSalesPrice(
// stock
// .getSalesPrice()
// .multiply(BigDecimal.valueOf(change))
// .setScale(2, RoundingMode.HALF_UP));
// // RoundingMode from AI suggestion
stock.addNewSalesPrice(stock.getSalesPrice().add(change).setScale(2, RoundingMode.HALF_UP));
// Round to stop crazy values

// double change = 0.9 + random.nextDouble() * 0.2;
// stock.addNewSalesPrice(
// stock
// .getSalesPrice()
// .multiply(BigDecimal.valueOf(change))
// .setScale(2, RoundingMode.HALF_UP));
// // RoundingMode from AI suggestion
}
notifyWeekAdvanced();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/millions/model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Stock(String symbol, String company, List<BigDecimal> prices, List<BigDec

this.volatility = volatilityParameters;

if (volatilityParameters.size() != 5) {
if (volatilityParameters.size() != 6) {
throw new IllegalArgumentException("Invalid volatility function count");
}

Expand Down
54 changes: 31 additions & 23 deletions src/main/java/millions/model/calculators/PriceChangeCalculator.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
package millions.model.calculators;

import millions.model.Stock;

import java.math.BigDecimal;
import java.util.List;
import millions.model.Stock;

public class PriceChangeCalculator {

public PriceChangeCalculator() {}

public BigDecimal calculateChange(Stock stock) {
List<BigDecimal> values = stock.getVolatilityParameters();
int week = stock.getHistoricalPrices().size();
BigDecimal change = BigDecimal.ZERO;
List<BigDecimal> volatilityParameters = stock.getVolatilityParameters();
change = change.add(upChange(volatilityParameters.get(0)));
change = change.add(downChange(volatilityParameters.get(1)));
change = change.add(randomChange(volatilityParameters.get(2)));
change = change.add(sinChange(volatilityParameters.get(3)));
change = change.add(cosChange(volatilityParameters.get(4)));
return change;
change = change.add(drift(values.get(0)));
change = change.add(volatility(values.get(1)));
change = change.add(cycle(values.get(2), values.get(3), week));
change = change.add(explosion(values.get(4), values.get(5)));
return stock.getSalesPrice().multiply(change);
}

private BigDecimal upChange(BigDecimal input) {
/*
* Flat slope change, eg upwards/downwards slope
*/
private BigDecimal drift(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return input;

}
private BigDecimal downChange(BigDecimal input) {

/*
* Random noise of size x
*/
private BigDecimal volatility(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return input.negate();
return input.multiply(BigDecimal.valueOf(Math.random() * 2 - 1));
}

private BigDecimal randomChange(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
/*
* Sinus curve based on week, times size of the curve
*/
private BigDecimal cycle(BigDecimal speed, BigDecimal size, int week) {
if (speed.equals(BigDecimal.ZERO) || size.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return new BigDecimal(Math.random()*10).multiply(input);
return size.multiply(BigDecimal.valueOf(Math.sin(week * speed.doubleValue())));
}

private BigDecimal sinChange(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
/*
* probability% change of an explosion of size% positive or negative
*/
private BigDecimal explosion(BigDecimal probability, BigDecimal size) {
if (probability.equals(BigDecimal.ZERO) || size.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return new BigDecimal(Math.sin(input.doubleValue()));
}
private BigDecimal cosChange(BigDecimal input) {
if (input.equals(BigDecimal.ZERO)) {
if (Math.random() >= probability.doubleValue()) {
return BigDecimal.ZERO;
}
return new BigDecimal(Math.cos(input.doubleValue()));
return Math.random() < 0.5 ? size : size.negate();
}
}
14 changes: 2 additions & 12 deletions src/main/java/millions/view/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,7 @@ private void refreshQuantityControls(Stock stock) {
private void refreshPortfolio() {
Player player = controller.getPlayer();

portfolioList
.getItems()
.setAll(
player.getPortfolio().getShares().stream()
.map(ViewUtils::formatPortfolioShare)
.toList());
portfolioTable.getItems().setAll(player.getPortfolio().getShares());
}

private void refreshTransactions() {
Expand All @@ -352,12 +347,7 @@ private void refreshTransactions() {
return;
}

transactionsList
.getItems()
.setAll(
player.getTransactionArchive().getTransactions().stream()
.map(ViewUtils::formatTransaction)
.toList());
transactionsTable.getItems().setAll(player.getTransactionArchive().getTransactions());
}

private void buySelectedStock() {
Expand Down

0 comments on commit 69f7abe

Please sign in to comment.