PowerShell: Vahur Laas, stock.ps1

From ICO wiki

<source lang="powershell">

<# .SYNOPSIS

   Leiab 200 kallima NASDAQi tehnoloogiafirma aktsiate andmed ning teeb nende baasil naiivseid soovitusi osta, müüa või hoida.

.NOTES

   Autor: Vahur Laas
  1. >


  1. Laeme sümbolite listi

write-host "Download symbol list" $symbolstxt = (new-object net.webclient).DownloadString("http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download")

  1. Konverdime CSV'st objektiks, päise paneme ise, sest olemasolev ei ole hea

write-host "Convert" $symbols = convertfrom-csv -header Symbol,Name,LastSale,MarketCap,ADR_TSO,IPOyear,Sector,Industry,Summary_Quote $symbolstxt | select-object Symbol,LastSale,Sector

  1. Loome saja kalleima firma listi stringikujul eraldajaks + nagu seda vajab järgmisena kasutatav keskkond

$symlist = "" $symbols | where-object {$_.Sector -eq "technology"} | sort-object {[decimal] $_.lastsale} | select-object -last 200 | ForEach-Object {

   if ($_.sector = "Technology"){    
       $symlist = $symlist + $_.symbol.trim() + "+"
   }

}

  1. Sikutame sümbolite detailsed andmed

write-host "Download detailed data" $wdata = (new-object net.webclient).DownloadString("http://finance.yahoo.com/d/quotes.csv?s=" + $symlist.trim("+") + "&f=snjkl1") $data = convertfrom-csv -header Symbol,Name,Low,High,Price $wdata

  1. Loome väljundi jaoks tabeli struktuuri

$suggestions = New-Object system.Data.DataTable “Suggestions” $col1 = New-Object system.Data.DataColumn Symbol,([string]) $col2 = New-Object system.Data.DataColumn Name,([string]) $col3 = New-Object system.Data.DataColumn Low,([decimal]) $col4 = New-Object system.Data.DataColumn High,([decimal]) $col5 = New-Object system.Data.DataColumn Price,([decimal]) $col6 = New-Object system.Data.DataColumn Suggestion,([string])

$suggestions.columns.add($col1) $suggestions.columns.add($col2) $suggestions.columns.add($col3) $suggestions.columns.add($col4) $suggestions.columns.add($col5) $suggestions.columns.add($col6)

  1. Käime saadud objektid läbi ja teeme otsuse ning täidame tabeli

$data | ForEach-Object {

   $suggestion = "Hold"
   [decimal] $perc10 = [decimal] $_.price * 0.1
   if ([decimal] $_.price -gt [decimal] $_.high - $perc10){
       $Suggestion = "Sell"
   }
   if ([decimal] $_.price -lt [decimal] $_.low + $perc10){
       $suggestion = "Buy"
   }    
   $row = $suggestions.newrow()
   $row.symbol = $_.symbol
   $row.name = $_.name
   $row.low = [decimal] $_.low
   $row.high = [decimal] $_.high
   $row.price = [decimal] $_.price
   $row.suggestion = $suggestion
   $suggestions.rows.add($row)

}

  1. Väljastame tulemuse

$suggestions| format-table -auto