-
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.
Merge branch 'dev' into test-development
- Loading branch information
Showing
8 changed files
with
193 additions
and
42 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
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
64 changes: 64 additions & 0 deletions
64
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package millions.model.calculators; | ||
|
|
||
| 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; | ||
| 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); | ||
| } | ||
|
|
||
| /* | ||
| * Flat slope change, eg upwards/downwards slope | ||
| */ | ||
| private BigDecimal drift(BigDecimal input) { | ||
| if (input.equals(BigDecimal.ZERO)) { | ||
| return BigDecimal.ZERO; | ||
| } | ||
| return input; | ||
| } | ||
|
|
||
| /* | ||
| * Random noise of size x | ||
| */ | ||
| private BigDecimal volatility(BigDecimal input) { | ||
| if (input.equals(BigDecimal.ZERO)) { | ||
| return BigDecimal.ZERO; | ||
| } | ||
| return input.multiply(BigDecimal.valueOf(Math.random() * 2 - 1)); | ||
| } | ||
|
|
||
| /* | ||
| * 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 size.multiply(BigDecimal.valueOf(Math.sin(week * speed.doubleValue()))); | ||
| } | ||
|
|
||
| /* | ||
| * 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; | ||
| } | ||
| if (Math.random() >= probability.doubleValue()) { | ||
| return BigDecimal.ZERO; | ||
| } | ||
| return Math.random() < 0.5 ? size : size.negate(); | ||
| } | ||
| } |
Oops, something went wrong.