-
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 pull request #125 from einaskoi/per/NewsEvent
implimented events with stocks and mail app
- Loading branch information
Showing
12 changed files
with
308 additions
and
86 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
114 changes: 87 additions & 27 deletions
114
src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/StockController.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,58 +1,118 @@ | ||
| package edu.ntnu.idi.idatt2003.gruppe42.Controller; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.MailController; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.EventBus; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.StockEvent; | ||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| public class StockController { | ||
| private Stock stock; | ||
| private int timeframe; | ||
|
|
||
| private static final int STEEP_THRESHOLD = 46; | ||
| private static final int EVENT_MAGNITUDE = 90; | ||
| private static final double NOISE_RANGE = 8.0; | ||
| private static final int MIN_TREND_FRAMES = 10; | ||
|
|
||
| private final Stock stock; | ||
|
|
||
| private int trendTimeframe; | ||
| private int trendFramesRemaining; | ||
| private int trend; | ||
| private int delay; | ||
| private int time; | ||
| private Random random; | ||
| private double timeMultiplier; | ||
| private BigDecimal price; | ||
| private BigDecimal startPrice; | ||
| private BigDecimal offset; | ||
| private BigDecimal priceChange; | ||
|
|
||
| private BigDecimal pendingShock; | ||
|
|
||
| public StockController(Stock stock) { | ||
| public StockController(final Stock stock) { | ||
| this.stock = stock; | ||
| time = 0; | ||
| random = new Random(); | ||
| timeMultiplier = (random.nextDouble() + 0.1) / 20; | ||
| startPrice = stock.getSalesPrice(); | ||
|
|
||
| price = startPrice; | ||
| priceChange = new BigDecimal(random.nextInt(startPrice.intValue()/2)); | ||
| this.time = 0; | ||
| this.delay = 0; | ||
| this.random = new Random(); | ||
| this.startPrice = stock.getSalesPrice(); | ||
| this.price = startPrice; | ||
| nextTrend(); | ||
| } | ||
|
|
||
| public BigDecimal updatePrice() { | ||
| BigDecimal marketFluctuation = BigDecimal.valueOf(Math.sin(timeMultiplier * time)) | ||
| .multiply(priceChange) | ||
| .add(startPrice); | ||
| BigDecimal noise = BigDecimal.valueOf((random.nextDouble() * 2 - 1) * NOISE_RANGE); | ||
|
|
||
| if (isDelay()) { | ||
| delay--; | ||
| if (delay == 0 && pendingShock != null) { | ||
| price = price.add(pendingShock); | ||
| pendingShock = null; | ||
| } | ||
|
|
||
| if (price.compareTo(BigDecimal.ZERO) > 0) { | ||
| price = price.add(noise); | ||
| } | ||
|
|
||
| if (price.compareTo(BigDecimal.ZERO) < 0) { | ||
| price = BigDecimal.ZERO; | ||
| } | ||
| return price.setScale(2, RoundingMode.HALF_UP); | ||
| } | ||
|
|
||
| if (price.compareTo(BigDecimal.ZERO) > 0 || (isTrajectorySteep() && trend > 0)) { | ||
| BigDecimal trendStep = BigDecimal.valueOf(trend) | ||
| .divide(BigDecimal.valueOf(trendTimeframe), 6, RoundingMode.HALF_UP); | ||
|
|
||
| double speculatorFluctuation = random.nextDouble(2) - 1; | ||
| offset = BigDecimal.valueOf(speculatorFluctuation*30); | ||
| price = price.add(trendStep).add(noise); | ||
| } | ||
|
|
||
| time += 1; | ||
| price = marketFluctuation; | ||
| if (price.compareTo(BigDecimal.ZERO) < 0) { | ||
| price = BigDecimal.ZERO; | ||
| } | ||
|
|
||
| return price.add(offset); | ||
| trendFramesRemaining--; | ||
| time++; | ||
|
|
||
| if (trendFramesRemaining <= 0) { | ||
| nextTrend(); | ||
| } | ||
|
|
||
| return price.setScale(2, RoundingMode.HALF_UP); | ||
| } | ||
|
|
||
| public void handleEvent() { | ||
| private void nextTrend() { | ||
| trend = random.nextInt(101) - 50; | ||
| trendTimeframe = random.nextInt(120) + MIN_TREND_FRAMES; | ||
| trendFramesRemaining = trendTimeframe; | ||
|
|
||
| if (isTrajectorySteep()) { | ||
| prepareEvent(); | ||
| delay = 20; | ||
| } else { | ||
| delay = 0; | ||
| pendingShock = null; | ||
| } | ||
| } | ||
|
|
||
| private void prepareEvent() { | ||
| int direction = trend > 0 ? 1 : -1; | ||
| double scaledMagnitude = EVENT_MAGNITUDE * ((double) Math.abs(trend) / 50.0); | ||
| pendingShock = BigDecimal.valueOf(direction * scaledMagnitude); | ||
| EventBus.get() | ||
| .publish(new StockEvent(stock.getCompany(), trend)); | ||
| } | ||
|
|
||
| public boolean isTrajectorySteep() { | ||
| return true; | ||
| private boolean isTrajectorySteep() { | ||
| return Math.abs(trend) > STEEP_THRESHOLD; | ||
| } | ||
|
|
||
| public boolean isDelay() { | ||
| private boolean isDelay() { | ||
| return delay > 0; | ||
| } | ||
| } | ||
|
|
||
| public BigDecimal getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.