Advertisement
Guest User

stock2own buy/sell scraper

a guest
Sep 21st, 2018
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. [Console]::BackgroundColor = 'Black'
  2. [Console]::ForegroundColor = 'White'
  3.  
  4. $Username = 'blah@gmail.com'
  5. $Password = 'lol'
  6.  
  7. $S2Ourl = 'https://www.stock2own.com'
  8. $LoginUrl = "$S2Ourl/Investor/Login_AJAX"
  9. $StockUrl = "$S2Ourl/StockAnalysis/Stock"
  10.  
  11. $Stocks = @('US/ACIA'
  12. 'US/AAPL'
  13. 'US/TSLA'
  14. 'US/GILD'
  15. 'US/NFLX'
  16. 'US/GOOGL'
  17. 'US/AMZN'
  18. 'US/BIDU'
  19. 'US/GRMN'
  20. 'US/COH'
  21. 'US/CTSH'
  22. 'US/DLB'
  23. 'US/EBIX'
  24. 'US/FB'
  25. 'US/LULU'
  26. 'CA/WEED'
  27. 'CA/RCH'
  28. )
  29. $Colors = @{
  30. Buy = 'Green'
  31. AlmostBuy = 'Blue'
  32. Mixed = 'Yellow'
  33. Hold = 'Yellow'
  34. AlmostSell = 'DarkYellow'
  35. Sell = 'Red'
  36. Avoid = 'Red'
  37. Bear = 'Red'
  38. Bull = 'Green'
  39. Unknown = 'White'
  40. }
  41. $Results = @()
  42.  
  43. Function Get-IndicatorsLegend {
  44. $Indicators = @()
  45.  
  46. $Indicators += New-Object -TypeName PSObject -Property @{
  47. Indicator = 'AlmostBuy'
  48. Description = 'Some of the charts moved to the "positive" area'
  49. }
  50.  
  51. $Indicators += New-Object -TypeName PSObject -Property @{
  52. Indicator = 'AlmostSell'
  53. Description = 'Some of the charts moved to the "negative" area'
  54. }
  55.  
  56. $Indicators += New-Object -TypeName PSObject -Property @{
  57. Indicator = ' Hold'
  58. Description = 'No indicator changed direction. If you have an open long position, you would better keep it, there is no reason to sell'
  59. }
  60.  
  61. $Indicators += New-Object -TypeName PSObject -Property @{
  62. Indicator = ' Mixed'
  63. Description = 'Alert is unknown: some indicators suggest buy, others suggest sell. So, be ready to act'
  64. }
  65.  
  66. $Indicators += New-Object -TypeName PSObject -Property @{
  67. Indicator = ' Buy'
  68. Description = 'All charts moved to the "positive" area'
  69. }
  70.  
  71. $Indicators += New-Object -TypeName PSObject -Property @{
  72. Indicator = ' Sell'
  73. Description = 'All charts moved to the "negative" area'
  74. }
  75.  
  76. $Indicators += New-Object -TypeName PSObject -Property @{
  77. Indicator = ' Avoid'
  78. Description = 'Pretty obvious what to do with this plague of a stock'
  79. }
  80.  
  81. $Indicators += New-Object -TypeName PSObject -Property @{
  82. Indicator = ' Stay Away'
  83. Description = 'No indicator changed direction. However, technical indicators recommend that you should not have an open long position'
  84. }
  85.  
  86. Return $Indicators
  87. }
  88.  
  89. Write-Host "Logging into '$LoginUrl'"
  90. Try {
  91. $Login = Invoke-WebRequest -Uri $LoginUrl -Method Post -Body @{UserName = $Username; Password = $Password} -SessionVariable S2OSession
  92. $LoginResult = $Login.Content | ConvertFrom-Json
  93. } Catch {
  94. Write-Host "`tFailed to log into '$LoginUrl'" -ForegroundColor Red -BackgroundColor Black
  95.  
  96. Exit
  97. }
  98.  
  99. If ($LoginResult.Error) {
  100. Write-Host "`tFailed to log into '$LoginUrl'" -ForegroundColor Red -BackgroundColor Black
  101.  
  102. Exit
  103. } ElseIf ($LoginResult.success) {
  104. Write-Host "`tSuccessully logged into '$LoginUrl'" -ForegroundColor Green -BackgroundColor Black
  105. }
  106.  
  107. Write-Host 'Looping through $Stocks' -BackgroundColor Black
  108. ForEach ($symbol in $Stocks) {
  109. ## Reset variables each iteration
  110. $trend = $Null
  111. $ti = $Null
  112.  
  113. Write-Host "`tGetting '$StockUrl/$symbol'"
  114. Try {
  115. $getStock = Invoke-WebRequest -Uri "$StockUrl/$symbol" -Method Get -WebSession $S2OSession
  116. $stock = $getStock.Content
  117. } Catch {
  118. Write-Host "`t`tFailed to get '$StockUrl/$symbol'" -ForegroundColor Red -BackgroundColor Black
  119.  
  120. Continue
  121. }
  122.  
  123. If ($stock.IndexOf('TI Alert:') -eq -1) {
  124. Write-Host "`t`tFailed to retrieve the TIs (Technical Indicators) of stock '$symbol', skipping this stock" -ForegroundColor Yellow -BackgroundColor Black
  125.  
  126. Continue
  127. }
  128.  
  129. If ($stock.IndexOf('Relative Strength Index (RSI) is') -eq -1) {
  130. Write-Host "`t`tFailed to retrieve the RSI (Relative Strength Index) of stock '$symbol', ignoring this error" -ForegroundColor Yellow -BackgroundColor Black
  131. }
  132.  
  133. $stock2 = $stock ## Copy var so we can process it for TSI too
  134. $stock = $stock.SubString($stock.IndexOf('TI Alert:'))
  135. $stock = $stock.SubString(0, $stock.IndexOf('</span>')) ## Do this in 2 stages so the initial search point is reset
  136.  
  137. ## Figure out if this overall market is going up (Bull) or going down (Bear)
  138. If ($stock.indexOf('bear') -ne -1) {
  139. $trend = 'Bear'
  140. } ElseIf ($stock.indexOf('bull') -ne -1) {
  141. $trend = 'Bull'
  142. } Else {
  143. $trend = $Null
  144. }
  145.  
  146. $stock = $stock.SubString($stock.IndexOf('" /> ') + $('" /> '.Length))
  147. $ti = $stock.Replace(' ', '') -replace("`r", '') -replace("`n", '')
  148.  
  149. $stock = $stock2 ## Restore variable contents
  150. $stock = $stock.SubString($stock.IndexOf('Relative Strength Index (RSI) is '))
  151. $stock = $stock.SubString(0, $stock.IndexOf('.')) ## Do this in 2 stages so the initial search point is reset
  152. $stock = $stock.SubString($stock.IndexOf('Relative Strength Index (RSI) is ') + $('Relative Strength Index (RSI) is '.Length))
  153. $tsi = $stock.Replace(' ', '') -replace("`r", '') -replace("`n", '')
  154.  
  155. $Results += New-Object -TypeName PSObject -Property @{
  156. Symbol = $symbol
  157. Trend = $trend
  158. TechnicalIndicator = $ti
  159. TechnicalStrength = $tsi
  160. MarketPricePerShare = ''
  161. }
  162. }
  163.  
  164. ## Sort the results
  165. $Results = $Results | Sort-Object TechnicalIndicator, Trend, TechnicalStrength
  166.  
  167. ## Loop through the results and display them to console
  168. Write-Host "`nDisplaying Results" -BackgroundColor Black
  169. ForEach ($stock in $Results) {
  170. Write-Host "'" -BackgroundColor Black -NoNewline -ForegroundColor White
  171. If ($Colors.$($stock.TechnicalIndicator) -eq 'Green' -and $Colors.$($stock.Trend) -eq 'Green') {
  172. Write-Host $stock.Symbol -BackgroundColor Black -NoNewline -ForegroundColor Green
  173. } Else {
  174. Write-Host $stock.Symbol -BackgroundColor Black -NoNewline
  175. }
  176. Write-Host "'`t" -BackgroundColor Black -NoNewline -ForegroundColor White
  177. Write-Host $stock.TechnicalIndicator -BackgroundColor Black -NoNewline -ForegroundColor $Colors.$($stock.TechnicalIndicator)
  178. Write-Host ' (' -BackgroundColor Black -NoNewline -ForegroundColor White
  179. Write-Host $stock.trend -BackgroundColor Black -NoNewline -ForegroundColor $Colors.$($stock.Trend)
  180. Write-Host ')' -BackgroundColor Black -NoNewLine -ForegroundColor White
  181. Write-Host "`t" -BackgroundColor Black -NoNewline -ForegroundColor White
  182. Write-Host "RSI($($stock.TechnicalStrength))" -BackgroundColor Black
  183. }
  184.  
  185. Get-IndicatorsLegend | Format-Table -AutoSize -Wrap Indicator, Description
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement