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.util.Arrays;
- import java.util.Calendar;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class TestYahooFinance extends JFrame implements ActionListener
- {
- private JButton bRunSimulation, bExit;
- private JLabel lShowResults;
- private JTextField tGetHowManyDaysStrdv;
- private JTextField tGetInvestmentAmount;
- private int HowManyDaysStrdv;
- private int investmentAmount;
- private JTextArea taResultArea;
- public TestYahooFinance()
- {
- // tworzenie ramki o rozmiarach i nazwie
- setSize(600, 700);
- setTitle("Day-trading simulation");
- // tworzymy przyciski o nazwie w nawiasie
- bRunSimulation = new JButton("Run Simulation");
- setLayout(null);
- bRunSimulation.setBounds(20, 500, 150, 50);
- // dodajemy do ramki przycisk
- add(bRunSimulation);
- // "sluchacz" odbiera sygnal/ruch z przycisku this oznacza cala ramka
- bRunSimulation.addActionListener(this);
- bExit = new JButton("Exit");
- bExit.setBounds(20, 570, 150, 50);
- add(bExit);
- bExit.addActionListener(this);
- lShowResults = new JLabel("Days of Standard Deviation:");
- lShowResults.setBounds(20, 20, 300, 20);
- add(lShowResults);
- tGetHowManyDaysStrdv = new JTextField();
- tGetHowManyDaysStrdv.setBounds(20, 50, 100, 25);
- tGetHowManyDaysStrdv.setToolTipText("Enter number days of Standard Deviation");
- add(tGetHowManyDaysStrdv);
- JTextArea taResultArea = new JTextArea();
- // taResultArea.setEditable(false);
- JScrollPane scrollPane = new JScrollPane(taResultArea);
- scrollPane.setBounds(200, 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)
- {
- HowManyDaysStrdv = Integer.parseInt(tGetHowManyDaysStrdv.getText());
- // 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, 2);
- endDate.set(Calendar.DATE, 15);
- // sector + 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" };
- // creating new arrays based on stocks in XLU arrays above
- YahooFinanceHttp[] listEntities = new YahooFinanceHttp[xlu.length];
- for (int i = 0; i < xlu.length; i++)
- {
- YahooFinanceURLConstruct spolka = new YahooFinanceURLConstruct(
- startDate, endDate, xlu[i]);
- YahooFinanceHttp new1 = new YahooFinanceHttp(
- spolka.constructURL(), HowManyDaysStrdv, i);
- listEntities[i] = new1;
- }
- // creating new arrays with pseudo ID and standard deviation for
- // definite day (day 0 = end date)
- StockStandardDeviation[][] listStrd = new StockStandardDeviation[listEntities[0]
- .getSize() - listEntities[0].getHowManyDays()][xlu.length - 1];
- // filling arrays with ID's and standard deviation's stock
- for (int i = 0; i < listEntities[0].getSize()
- - listEntities[0].getHowManyDays() - 1; i++)
- {
- for (int j = 0; j < xlu.length - 1; j++)
- {
- StockStandardDeviation newStock = new StockStandardDeviation(
- j + 1, listEntities[j + 1].getStandardDeviation(i),
- listEntities[j + 1].getDefineDate(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);
- }
- }
- // for (int i = 0; i < 25; i++)
- // {
- // System.out.println(listStrd[i][i + 5]);
- // }
- // Tutaj bedzie petla ktora bedzie zliczac wyniki nastepnie chcialem je wyswietlac w textArea ale poki co
- // nawet jednej linii nie potrafie wyswietlic
- // dla Jlabel dziala poprawnie
- double result = ((listEntities[listStrd[0][0].getID()]
- .getClosePrice(0)) - (listEntities[listStrd[0][0].getID()]
- .getOpenPrice(0))) * 1000;
- lShowResults.setText(String.format("Result: %.2f", result) + "$");
- taResultArea.setText(String.format("Result: %.2f", result) + "$");
- long t2 = System.currentTimeMillis();
- }
- else if (e.getSource() == bExit)
- {
- dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment