Saturday, January 30, 2010

Indian Stock Quotes through Selenium

I thought to get the current indian (NSE) stock quotes by using Selenium tool. I chose Selenium RC and Java language. I have developed a java class for all file related tasks. Another java program used to get the stock values from set of given scrips.

Selenium RC required set of configuration to run the tests. We need to give the proxy details (with or without proxy) for internet connection. I'm planning to cover the configuration in upcoming posts. Below I have given the java code to get current stock values.

Current Stock quotes - Selenium RC with Java
Note: FileHandlerLib class - file related tasks.

/* To get all stockquotes * To Compile: javac GetAllStockQuotes.java -Xlint:unchecked * To Run: java junit.textui.TestRunner com.palani.tests.GetAllStockQuotes * To Run: java GetAllStockQuotes * Note: Recompile with -Xlint:unchecked for details. * javac FileHandlerLib.java -Xlint:unchecked */ package com.palani.tests; // import FileHandlerLib; import com.palani.utils.*; import com.thoughtworks.selenium.*; import junit.framework.*; import java.util.regex.Pattern; import java.io.*; import java.util.*; import java.text.DateFormat; import java.text.SimpleDateFormat; public class GetAllStockQuotes extends SeleneseTestCase { private Selenium selenium; private FileHandlerLib fhNew = new FileHandlerLib(); // @Before public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.nseindia.com/"); selenium.start(); } // @Test public void testGetAllStockQuotes() throws Exception { // Variables String sScripsFile; String sQuotesFile; String sUrlPart; String sCurrentDate; String sQuote; String sQuoteValue; String sOutput; ArrayList aData = new ArrayList(); // Initialization // sCurrentDate = "20100101" ; sCurrentDate = fhNew.getDateTime(); sQuotesFile = "D:\\Stocks\\"+sCurrentDate+"_stocks.txt"; fhNew.DeleteFile (sQuotesFile); sScripsFile = "D:\\Stocks\\StockScrips.txt"; sUrlPart="/marketinfo/companyinfo/companysearch.jsp?cons="; aData = fhNew.ReadFileToArray (sScripsFile); Object[] elements = aData.toArray(); System.out.println ("SCRIPs File: " + sScripsFile); System.out.println ("Quotes File: " + sQuotesFile); selenium.open("/content/equities/cmquote.htm"); Thread.sleep(2000); for(int iIndex=0; iIndex < elements.length ; iIndex++) { sQuote=(String)elements[iIndex]; sQuote=sQuote.trim(); sQuoteValue = "0"; System.out.println("Quote:"+elements[iIndex]+ " ,index: " + iIndex); if (!(sQuote.equals("") ) ) { if (selenium.isElementPresent("companyname")) { selenium.type("companyname", sQuote); } else { selenium.type("company", sQuote); } selenium.click("submit1"); Thread.sleep(2000); for (int second = 0;; second++) { if (second >= 150) System.out.println("timeout"); try { if (selenium.isElementPresent("//div[@id='PI4']/b")) { sQuoteValue = selenium.getText("//div[@id='PI4']/b"); System.out.println ("Quote Value: " + sQuoteValue); sOutput=sQuote.trim()+","+sQuoteValue.trim(); fhNew.FileWrite (sQuotesFile,sOutput); break; } // end if } catch (Exception e) { System.out.println("Selenium Run: " + e.getMessage() + "\n"); e.printStackTrace(); } Thread.sleep(1000); } } // End If } // end for loop } // @After public void tearDown() { selenium.stop(); } // To run Selenium Testcase using JUnit public static Test suite() { return new TestSuite(GetAllStockQuotes.class); } // To run Selenium Testcase public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } }

Saturday, January 16, 2010

MSN Money Quotes

Few months back, I was searching a solution, which can get the current stock value for the given quote. I did not find proper solution to get current quote. I have done few VBA procedures in Excel, to do few more calculations after getting current stock quote.

I found Microsoft's Excel 2003/2002 Add-in: MSN Money Stock Quotes. It can be installed with Excel 2007 version also. This Add-in is very useful to the people, who wish to maintain or track their stock investments. It is used only for US stock markets. Microsoft provides better online help to this addin as MSN MoneyCentral Stock Quotes help.

However I cannot use this add-in for NSE or BSE markets. I used selenium to retrieve the quote from net.

Saturday, January 2, 2010

Strange Internet connection Problem

Recently I saw a strange problem in a laptop, which has Vista OS. I have broadband connection. Router was showing as internet connection successful. But none of the web pages were not loaded. I checked all the internet connection settings. All are correct. I checked in Safari, Internet Explorer and Firefox.

I tried with a startup mode SafeMode with Networking . To know more about these startup modes, see my post Strange behavior with Service Pack. In SafeMode with Networking, I was able to do internet browsing. Then I realized the problem might be due to setting changes or some configuration would have changed.

Solution

  1. Open a command prompt (cmd.exe) with 'Run as administrator' privilege
  2. Type Ipconfig /flushdns and press ENTER key
  3. Type Netsh int ip reset and press ENTER key
  4. Reboot (Re-start) the system
  5. Again Open a command prompt (cmd.exe) with 'Run as administrator' privilege
  6. Type Netsh winsock reset and press ENTER key
  7. Reboot (Re-start) the system again.

After doing above things, I were able to do net surfing. I hope that, Vista is removing the settings or resetting the configuration, if any of devices or drivers went wrong. Microsoft Knowledge-base article has different solution. That solution is You receive an error message in Internet Explorer: "Internet Explorer cannot display the webpage" .

Removing Malware
Elphantboycomputers has prepared wonderful article to Remove Malware. See the section Recap of what you will need to have on-hand before you start the cleanup process

1. To repair or reset Winsock in Vista/Win7
a. Start>Run>cmd [enter]
b. netsh winsock reset catalog [enter]
c. Reboot the system.
2. Sysclean or Multi-AV
3. Full-featured antivirus with updates downloaded separately for manual update
4. MBAM
5. SuperAntiSpyware
6. HijackThis
7. Possibly Process Explorer and Killbox.