Last active
September 23, 2021 08:12
-
-
Save mohamed-taman/0f89d0a6699d3524657b3f0df71405b1 to your computer and use it in GitHub Desktop.
Class to calculate the best days to buy and sell trade stock options.
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
package org.tm.siriusxi.lm; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Objects; | |
/** | |
* This class contains methods to help best trade stock options, given an array of stock price | |
* predictions for one month. | |
* | |
* <p>We can use Local Maxima and Minima to calculate the days to buy and sell respectively. | |
* Check <code>treadOptions(data)</code> method. | |
* | |
* @author Mohamed Taman | |
* @since 28.12.2020 | |
*/ | |
public class StockOptionsTrader { | |
public static void main(String[] args) { | |
// Consider day start from 1 instead of zero, so given array is 9 days | |
// days = {1, 2, 3, 4, 5, 6, 7, 8, 9} | |
int[] data = {10, 20, 15, 14, 13, 25, 5, 4, 3}; | |
/* According the the given array above: | |
1. The best days to buy stocks are: [1, 5, 9] | |
2. The best days to sell stocks are: [2, 6] | |
*/ | |
treadOptions(data); | |
} | |
/** | |
* This Method will help you to determine exactly when is it the best days to buy stock options, | |
* and when to sell it to achieve the highest profit. | |
* | |
* @param data of the Stock price predictions. | |
* @throws NullPointerException if data is null. | |
*/ | |
private static void treadOptions(int... data) { | |
Objects.requireNonNull(data, "Data can't be null"); | |
// Empty lists to store days of buying and selling | |
var buyingDays = new ArrayList<Integer>(); | |
var sellingDays = new ArrayList<Integer>(); | |
int size = data.length; | |
// Checking whether the first day is valid for buy or sell or none | |
if (data[0] > data[1]) sellingDays.add(1); | |
else if (data[0] < data[1]) buyingDays.add(1); | |
// Iterating over all days to check when to to buy (LM) and when to sell (LM) | |
for (int i = 1; i < size - 1; i++) { | |
// Condition to buy | |
if ((data[i - 1] > data[i]) && (data[i] < data[i + 1])) buyingDays.add(i + 1); | |
// Condition to sell | |
else if ((data[i - 1] < data[i]) && (data[i] > data[i + 1])) sellingDays.add(i + 1); | |
} | |
// Checking whether the last day is valid for buy or sell or none | |
if (data[size - 1] > data[size - 2]) sellingDays.add(size); | |
else if (data[size - 1] < data[size - 2]) buyingDays.add(size); | |
// Print all the buying and selling days | |
if (!buyingDays.isEmpty()) { | |
print(buyingDays, "The best days of buying stocks are: "); | |
} else System.out.println("There are no good days to buy options."); | |
if (!sellingDays.isEmpty()) { | |
print(sellingDays, "The best days to sell stocks are: "); | |
} else System.out.println("There are no days to sell options with profit."); | |
} | |
private static void print(List<Integer> days, String message) { | |
System.out.printf("%s %s %n", message, days.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
as the original task is to find the highest profit you should not buy stock if you can't sell it. also, you can't sell it if you don't have it (haven't bought it).
so the first day can't be selling day and the last day can't be buying day.