-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merging changes, defined price functions
- Loading branch information
martin
committed
May 25, 2026
1 parent
f4b97df
commit 69f7abe
Showing
5 changed files
with
50 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 31 additions & 23 deletions
54
src/main/java/millions/model/calculators/PriceChangeCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters