Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.math.RoundingMode;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JScrollPane;
- import javax.swing.JSlider;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- public class TestYahooFinance extends JFrame implements ActionListener,
- ChangeListener
- {
- private JButton bRunSimulation, bExit, bChart;
- private JLabel lInvestmentAmount, lSectorSelect, lEntitiesNumber,
- lStrdDays;
- private JSlider slGetHowManyDaysStrdv, slNumberOfEntities;
- private JTextField tGetInvestmentAmount;
- private JComboBox cIndexchoice;
- private JTextArea taResultArea;
- private int HowManyDaysStrdv, NumberOfEntities;
- private int investmentAmount;
- public TestYahooFinance()
- {
- // creating main window
- setSize(700, 700);
- setTitle("Day-trading simulation");
- bChart = new JButton("Create chart");
- bChart.setBounds(20, 430, 150, 50);
- bChart.setEnabled(false);
- add(bChart);
- // creating buttons
- bRunSimulation = new JButton("Run Simulation");
- setLayout(null);
- bRunSimulation.setBounds(20, 500, 150, 50);
- // adding buttons to the main window
- add(bRunSimulation);
- bRunSimulation.addActionListener(this);
- bExit = new JButton("Exit");
- bExit.setBounds(20, 570, 150, 50);
- add(bExit);
- bExit.addActionListener(this);
- lSectorSelect = new JLabel("Select sector for simulation:");
- lSectorSelect.setBounds(20, 20, 190, 25);
- add(lSectorSelect);
- cIndexchoice = new JComboBox();
- cIndexchoice.setBounds(20, 50, 100, 20);
- cIndexchoice.addItem("XLU");
- cIndexchoice.addItem("XLK");
- cIndexchoice.addItem("XLB");
- cIndexchoice.addItem("XLP");
- cIndexchoice.addItem("XLY");
- cIndexchoice.addItem("XLI");
- cIndexchoice.addItem("XLV");
- cIndexchoice.addItem("XLF");
- cIndexchoice.addItem("XLE");
- add(cIndexchoice);
- cIndexchoice.addActionListener(this);
- lStrdDays = new JLabel("Days of Standard Deviation: 2");
- lStrdDays.setBounds(20, 80, 300, 20);
- add(lStrdDays);
- slGetHowManyDaysStrdv = new JSlider(2, 30, 2);
- slGetHowManyDaysStrdv.setBounds(10, 100, 180, 45);
- slGetHowManyDaysStrdv
- .setToolTipText("Enter number days of Standard Deviation");
- slGetHowManyDaysStrdv.setMajorTickSpacing(5);
- slGetHowManyDaysStrdv.setMinorTickSpacing(1);
- slGetHowManyDaysStrdv.setPaintLabels(true);
- slGetHowManyDaysStrdv.setPaintTicks(true);
- slGetHowManyDaysStrdv.addChangeListener(this);
- add(slGetHowManyDaysStrdv);
- lEntitiesNumber = new JLabel("Number of entities: 1");
- lEntitiesNumber.setBounds(20, 145, 200, 35);
- add(lEntitiesNumber);
- slNumberOfEntities = new JSlider(1, 20, 1);
- slNumberOfEntities.setBounds(10, 180, 180, 45);
- slNumberOfEntities.setToolTipText("Set number of entities");
- slNumberOfEntities.setMajorTickSpacing(5);
- slNumberOfEntities.setMinorTickSpacing(1);
- slNumberOfEntities.setPaintLabels(true);
- slNumberOfEntities.setPaintTicks(true);
- slNumberOfEntities.addChangeListener(this);
- add(slNumberOfEntities);
- tGetInvestmentAmount = new JTextField();
- tGetInvestmentAmount.setBounds(20, 260, 100, 25);
- tGetInvestmentAmount.setToolTipText("Enter amount of investments");
- add(tGetInvestmentAmount);
- lInvestmentAmount = new JLabel("Amount of investment:");
- lInvestmentAmount.setBounds(20, 235, 300, 20);
- add(lInvestmentAmount);
- taResultArea = new JTextArea();
- taResultArea.setEditable(false);
- JScrollPane scrollPane = new JScrollPane(taResultArea);
- scrollPane.setBounds(300, 15, 350, 600);
- add(scrollPane);
- }
- public static void main(String[] args)
- {
- // tworzenie nowego obiektu
- TestYahooFinance simulation = new TestYahooFinance();
- // jak nacisniemy X to sie ramka wylacza a nie ukrywa
- simulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // setVisible ramka sie pojawia
- simulation.setVisible(true);
- }
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource() == bRunSimulation)
- {
- long t1 = System.currentTimeMillis();
- HowManyDaysStrdv = slGetHowManyDaysStrdv.getValue();
- investmentAmount = Integer.parseInt(tGetInvestmentAmount.getText());
- NumberOfEntities = slGetHowManyDaysStrdv.getValue();
- // Start Date
- Calendar startDate = Calendar.getInstance();
- // Set startDate
- startDate.set(Calendar.YEAR, 2013);
- startDate.set(Calendar.MONTH, 0); // Month Jan = 0, Feb = 1...Dec
- // =// 11
- startDate.set(Calendar.DATE, 1);
- // End Date
- Calendar endDate = Calendar.getInstance();
- // Set endtDate
- endDate.set(Calendar.YEAR, 2014);
- endDate.set(Calendar.MONTH, 11);
- endDate.set(Calendar.DATE, 15);
- // geting sector from combobox and change it to string
- String sectorSelected = cIndexchoice.getSelectedItem().toString();
- // sector (xlu.[0]) + entities belong to the sector XLU
- String[] XLU = { "XLU", "DUK", "SO", "D", "NEE", "EXC", "AEP",
- "PCG", "SRE", "PPL", "ED", "PEG", "FE", "EIX", "XEL", "NU",
- "ETR", "DTE", "CNP", "WEC", "OKE", "NI", "AEE", "NRG",
- "AES", "CMS", "SCG", "PNW", "POM", "GAS", "TEG", "TE" };
- String[] XLK = { "XLK", "AAPL", "GOOG", "MSFT", "VZ", "IBM", "T",
- "ORCL", "QCOM", "CSCO", "FB", "V", "INTC", "MA", "EBAY",
- "EMC", "ACN", "TXN", "ADP", "YHOO", "CRM", "ADBE", "CTSH",
- "GLW", "MU", "INTU", "AMAT" };
- String[] XLB = { "XLB", "DD", "DOW", "MON", "LYB", "PX", "FCX",
- "ECL", "PPG", "APD", "IP", "SHW", "MOS", "NUE", "CF",
- "EMN", "AA", "SIAL", "FMC", "VMC", "BLL", "IFF", "ARG",
- "MWV", "SEE", "OI", "AVY", "BMS", "ATI", "X", "CLF" };
- String[] XLP = { "XLP", "PG", "PM", "CVS", "MO", "WAG", "CL",
- "MDLZ", "COST", "KMB", "GIS", "KRFT", "ADM", "KR", "SYY",
- "WFM", "STZ", "LO", "EL", "RAI", "HSY", "K", "MJN", "BEAM",
- "CAG", "TSN", "CCE", "CLX", "DPS", "SJM", "SWY", "TAP",
- "MNST", "MKC", "CPB", "HRL", "AVP" };
- String[] XLY = { "XLY", "AMZN", "CMCSA", "DIS", "HD", "MCD",
- "PCLN", "FOXA", "TWX", "F", "NKE", "SBUX", "LOW", "TJX",
- "TWC", "DTV", "TGT", "CBS", "VIAB", "YUM", "JCI", "NFLX",
- "M", "VFC", "DISCA", "OMC", "WYNN", "AZO", "DLPH", "CMG",
- "KORS", "CCL", "DG", "ORLY", "ROST", "HOG", "HOT", "BBBY",
- "BWA", "LB" };
- String[] XLE = { "XLE", "XOM", "CVX", "SLB", "OXY", "EOG", "COP",
- "PXD", "HAL", "APC", "PSX", "VLO", "NOV", "APA", "BHI",
- "SE", "NBL", "COG", "HES", "MPC", "DVN", "MRO", "RRC",
- "TSO", "FTI", "CAM", "KMI", "CHK", "EQT", "CNX", "RIG" };
- String[] XLF = { "XLF", "WFC", "JPM", "C", "AXP", "USB", "GS",
- "MET", "SPG", "PNC", "MS", "COF", "PRU", "BK", "BLK",
- "ACE", "AMT", "SCHW", "TRV", "AFL", "STT", "DFS", "BBT",
- "MMC", "ALL", "AON", "CCI", "PSA", "CME", "ICE", "CB",
- "MHFI", "AMP" };
- String[] XLI = { "XLI", "GE", "UTX", "UNP", "BA", "MMM", "HON",
- "UPS", "CAT", "DHR", "EMR", "LMT", "CMI", "FDX", "ETN",
- "GD", "DE", "PCP", "RTN", "ITW", "CSX", "NSC", "DAL",
- "NOC", "PCAR", "PH", "ROK", "GWW", "TYC", "WM", "LUV",
- "FLR", "DOV", "PNR", "IR" };
- String[] XLV = { "XLV", "JNJ", "PFE", "MRK", "GILD", "AMGN", "BMY",
- "ABBV", "BIIB", "UNH", "ESRX", "CELG", "MDT", "LLY", "ABT",
- "TMO", "MCK", "ACT", "AGN", "BAX", "ALXN", "COV", "WLP",
- "AET", "REGN", "CAH", "SYK", "BDX", "FRX", "PRGO", "CI",
- "MYL", "STJ", "A", "HUM" };
- // creating new arrays based on stocks in index selected by user
- // (comobobox)
- DataGeting[] listEntities = new DataGeting[XLK.length];
- for (int i = 0; i < XLK.length; i++)
- {
- ConstructorURL spolka = new ConstructorURL(
- startDate, endDate, XLK[i]);
- DataGeting new1 = new DataGeting(
- spolka.constructURL(), HowManyDaysStrdv, i);
- listEntities[i] = new1;
- }
- // creating new arrays with pseudo ID and standard deviation for
- // definite day (day 0 = end date)
- // rows = days
- // columns = stocks
- StockStandardDeviation[][] listStrd = new StockStandardDeviation[listEntities[0]
- .getStandardDeviationList().size()][XLK.length - 1];
- // filling arrays with ID's and standard deviation's stock
- for (int i = 0; i < listEntities[0].getStandardDeviationList()
- .size(); i++)
- {
- for (int j = 0; j < XLK.length - 1; j++)
- {
- StockStandardDeviation newStock = new StockStandardDeviation(
- j + 1, listEntities[j + 1].getStandardDeviation(i));
- listStrd[i][j] = newStock;
- }
- }
- // sorting every day (day 0 = end date) from stock with the lowest
- // standard deviation to the highest
- Sorter sortMachine = new Sorter();
- for (int j = 0; j < listStrd[0].length; j++)
- {
- for (int i = 0; i < listStrd.length; i++)
- {
- Arrays.sort(listStrd[i], sortMachine);
- }
- }
- // creating list of result from opening short position every day on
- // index ETF
- // number of shares is calculating using the invest amount/2
- // (because the second half
- // of amount will be invest in stock belongs to index) / adjustment
- // close price
- ArrayList<Double> indexResult = new ArrayList<Double>();
- for (int i = 0; i < listStrd.length; i++)
- {
- int numberOfShares = (int) (Math.round((investmentAmount / 2)
- / listEntities[0].getAdjClosePrice(i + 1)));
- double result = (listEntities[0].getOpenPrice(i) - listEntities[0]
- .getClosePrice(i)) * numberOfShares;
- indexResult.add(result);
- }
- // same as the above but creating list for stocks in xlu index
- // with the smallest standard deviation
- // param j(5 in this case) is a number of stocks to buy, so
- // simulation buy 5 stocks
- // with the lowest standard deviation
- ArrayList<Double> stocksResult = new ArrayList<Double>();
- for (int i = 0; i < listStrd.length; i++)
- {
- double result = 0;
- for (int j = 0; j < NumberOfEntities; j++)
- {
- int numberOfShares = (int) ((Math
- .round((investmentAmount / 2)) / NumberOfEntities / listEntities[listStrd[i][j]
- .getID()].getAdjClosePrice(i + 1)));
- result += ((listEntities[listStrd[i][j].getID()]
- .getClosePrice(i) - listEntities[listStrd[i][j]
- .getID()].getOpenPrice(i)) * numberOfShares);
- }
- stocksResult.add(result);
- }
- // showing results in AreaText
- taResultArea.append("Results of invested " + investmentAmount
- + "$ and Standard Deviation of: " + HowManyDaysStrdv
- + " Days are: \n");
- taResultArea.append("Date" + " Result \n");
- // creating loop which sum the result from stocks and index and then
- // show the result in the textarea
- // and calculating cumulative result
- double cumulativeResult = 0;
- for (int i = 0; i < stocksResult.size(); i++)
- {
- taResultArea.append("Result for day "
- + listEntities[0].getDefineDate(i));
- taResultArea.append(String.format(" is: %.2f",
- indexResult.get(i) + stocksResult.get(i))
- + " $ \n");
- cumulativeResult += indexResult.get(i) + stocksResult.get(i);
- }
- taResultArea.append(String.format("Cumulative result: %.2f ",
- cumulativeResult) + "$ \n");
- taResultArea.append(String.format("Average result per day: %.2f ",
- cumulativeResult / stocksResult.size()) + "$ \n");
- long t2 = System.currentTimeMillis();
- taResultArea.append("Time needed to created the simulation: "
- + String.valueOf((t2 - t1) / 1000.0) + "sec");
- bChart.setEnabled(true);
- }
- else if(e.getSource() == bChart){
- // here i want creat chart
- }
- else if (e.getSource() == bExit)
- {
- dispose();
- }
- }
- @Override
- public void stateChanged(ChangeEvent arg0)
- {
- lEntitiesNumber.setText("Number of entities: "
- + slNumberOfEntities.getValue());
- lStrdDays.setText("Days of Standard Deviation: "
- + slGetHowManyDaysStrdv.getValue());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement