我如何获得多重股票利润? [英] How do i get multiple stock profits?

查看:71
本文介绍了我如何获得多重股票利润?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我寻找可以从股票中获得最大利润的解决方案。

This is my solution to find the maximum profit i can make from stocks.

int [] aktiePris = new int [] {10,7,5,8,11,11};是一个数组,其中的索引是股市开盘后的分钟,而值是股票的价格。

int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9}; is an array where the index is minutes after the stock market opens, and the values are the price of the stock.

因此,例如aktiePris [60] = 300意味着股票的价值是300,在股市开盘后一小时。

So for example aktiePris[60] = 300 means that the value of the stock is 300, one hour after the stock market opens.

现在,我的代码返回我可以通过买卖一只股票而获得的最大可能利润。 。我希望能够持有不止一只股票。我如何找到所有可能的利润并打印出来?

Right now my code returns the maximum possible profit i can make from buying and selling one single stock. I want to be able to se more than one single stock. How can i find all the possible profits and print them?

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class ProfitCalculator {
    static int minValue, maxValue, maxDiff;
    static Calendar timeMin, timeMax;
    static int indeksMinMinut, indeksMaxMinut;

    public static void main(String args[]) {

        int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9};
        int profit = findProfit(aktiePris);

        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

        timeMin = findTime();
        timeMin.add(timeMin.MINUTE, indeksMinMinut);
        timeMax = findTime();
        timeMax.add(timeMax.MINUTE, indeksMaxMinut);
        System.out.println("Best time & price for buying is " + timeFormat.format(timeMin.getTime()) + " for " + minValue + " EUR." + "\n"
                + "Best time & price for selling is " + timeFormat.format(timeMax.getTime()) + " for " + maxValue + " EUR." + "\n"
                + "Profit: " + profit);
    }

    public static int findProfit(int[] inputArray) {

        if (inputArray.length < 1)
            return 0;

        maxDiff = 0;
        minValue = inputArray[0];
        maxValue = minValue;

        for (int i = 1; i < inputArray.length; i++) {
            if (inputArray[i] > maxValue) {
                maxValue = inputArray[i];
                indeksMaxMinut = i;
                int priceDiff = maxValue - minValue;
                if (priceDiff > maxDiff) {
                    maxDiff = priceDiff;
                }
            } else if (inputArray[i] < minValue) {
                minValue = maxValue = inputArray[i];
                indeksMinMinut = i;
            }
        }

        return maxDiff;
    }

    public static Calendar findTime() {

        Calendar calendar = Calendar.getInstance();
        calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 30);

        return calendar;
    }
}


推荐答案

制作共享分析器类,其中封装了您想要的功能:

Make a share analyzer class, which encapsulates the functionality you want:

class ShareAnalyzer {

    private final int[] aktiePris;
    private int sellMinut = 0, buyMinut = 0, buyPrice,  sellPrice;

    ShareAnalyzer(int[] aktiePris) {

        this.aktiePris = aktiePris;
        findProfit();
    }

    private void findProfit() {

        if (aktiePris.length < 1) return ;
        int minValue = aktiePris[0];
        int maxValue = minValue;
        int indeksMaxMinut = 0, indeksMinMinut = 0;

        for (int i = 1; i < aktiePris.length; i++) {
            if (aktiePris[i] > maxValue) {
                maxValue = aktiePris[i];
                indeksMaxMinut = i;
                int priceDiff = maxValue - minValue;
                if (priceDiff > getProfit()) {
                    sellPrice = maxValue;
                    buyPrice  = minValue;
                    sellMinut = indeksMaxMinut;
                    buyMinut = indeksMinMinut;
                }
            } else if (aktiePris[i] < minValue) {
                minValue = maxValue = aktiePris[i];
                indeksMinMinut = i;
            }
        }
    }

    //time in minutes from opening
    int getSellTime() {return sellMinut;}

    int getSellPrice() {return sellPrice;}

    //time in minutes from opening
    int getBuyTime() {return buyMinut;  }

    int getBuyPrice() {return buyPrice;}

    int getProfit() {   return sellPrice - buyPrice;    }
}

您还可以添加打印共享分析器信息的方法:

You could also add a method to print out share analyzer info:

    //this could be implemented as `toString` of ShareAnalyzer
    private static void printShareInfo(ShareAnalyzer shareAnalyzer){
        System.out.println("Best time & price for buying is " + shareAnalyzer.getBuyTime() + " minutes after opening for " + shareAnalyzer.getBuyPrice() + " EUR." + "\n"
                + "Best time & price for selling is " + shareAnalyzer.getSellTime() + " minutes after opening for " + shareAnalyzer.getSellPrice() + " EUR." + "\n"
                + "Profit: " + shareAnalyzer.getProfit());
    }

构造一个新的 ShareAnalyzer 您要分析的每个共享的实例。

Construct a new ShareAnalyzer instance for each share you want to analyze.

public static void main(String args[]) {

    int[] aktiePris1 = new int[]{10, 7, 5, 8, 11, 9};
    ShareAnalyzer shareAnalyzer1 = new ShareAnalyzer(aktiePris1);
    printShareInfo(shareAnalyzer1);

    int[] aktiePris2 = new int[]{2, 12, 4, 7 , 11, 9, 1 , 8};
    ShareAnalyzer shareAnalyzer2 = new ShareAnalyzer(aktiePris2);
    printShareInfo(shareAnalyzer2); 
}

您可以运行代码并使用此链接

You can run the code and test it using this link

这篇关于我如何获得多重股票利润?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆