Advertisement
Kyle772

Investment data

Apr 22nd, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.99 KB | None | 0 0
  1. import urllib2, os
  2. import gspread, datetime, time
  3.  
  4. #----------------------------------------
  5. #------------Get Doge--------------------
  6. #----------------------------------------
  7. gc = gspread.login('XXXXXXXXXXXXX','XXXXXXXXXXXXXX')
  8. sh = gc.open("Investments")
  9. worksheet = sh.get_worksheet(0)
  10.  
  11. values_list = worksheet.col_values(7)
  12.  
  13. doge = values_list[len(values_list) - 1]
  14.  
  15. worksheet.update_acell('C1', doge)
  16.  
  17. print('You currently have ' + doge + ' DOGE \n')
  18.  
  19. spentvalue = worksheet.acell('B6').value
  20.  
  21. print('You currently have spent ' + spentvalue + '\n')
  22.  
  23. spent = float(spentvalue[1:len(spentvalue)])
  24.  
  25. #---------------------------------------
  26. #-------------DOGE to USD---------------
  27. #---------------------------------------
  28. print 'Current Market Values'
  29. print '--------------------------'
  30. print 'DOGE'
  31.  
  32. dogecode = urllib2.urlopen("http://coinmarketcap.com")
  33. page_source = dogecode.read()
  34.  
  35. dogecoin_section = page_source.find("img/DogeCoin.png")
  36. dogecoin_price_section = page_source.find("price", dogecoin_section)
  37. dogecoin_price_start = page_source.find("$", dogecoin_price_section)
  38. dogecoin_price_end = page_source.find("</a>", dogecoin_price_start)
  39.  
  40. value = float(page_source[(dogecoin_price_start)+2:dogecoin_price_end])
  41. dogeUSDValue = value
  42. print("$" + str(value) + " USD")
  43.  
  44.  
  45. #---------------------------------------
  46. #------Doge current satoshi value-------
  47. #---------------------------------------
  48.  
  49. dogecryptsy = urllib2.urlopen("https://www.cryptsy.com/")
  50. page_source = dogecryptsy.read()
  51.  
  52. dogecryptsy_section = page_source.find("DOGE/BTC <span")
  53. dogecryptsy_price_start = page_source.find("market_price_132", dogecryptsy_section)
  54. dogecryptsy_price_starts1 = page_source.find(">", dogecryptsy_price_start)
  55. dogecryptsy_price_end = page_source.find("</span>", dogecryptsy_price_starts1)
  56.  
  57. satoshi = float(page_source[(dogecryptsy_price_starts1) + 1:dogecryptsy_price_end])
  58. dogeBTCValue = satoshi
  59. satoshi = int(satoshi * 100000000)
  60. print(str(satoshi) + " SAT")
  61.  
  62. worksheet.update_acell('E10', dogeBTCValue)
  63.  
  64.  
  65. #---------------------------------------
  66. #---------BTC current value-------------
  67. #---------------------------------------
  68.  
  69. print '\n' + "BTC - Coinbase"
  70.  
  71. btccoinbase = urllib2.urlopen("https://www.coinbase.com/")
  72. page_source = btccoinbase.read()
  73.  
  74. btccoinbase_section = page_source.find("Current Buy Price")
  75. btccoinbase_price_start = page_source.find("$", btccoinbase_section)
  76. btccoinbase_price_end = page_source.find("</a>", btccoinbase_price_start)
  77.  
  78. value = float(page_source[(btccoinbase_price_start) + 1:btccoinbase_price_end])
  79. btcValue = value
  80.  
  81. worksheet.update_acell('J10', btcValue)
  82.  
  83. print("$" + str(value) + " USD")
  84. print '--------------------------'
  85.  
  86. #---------------------------------------
  87. #---------Your current earnings---------
  88. #---------------------------------------
  89.  
  90. dogeUSD = float(doge) * float(dogeUSDValue)
  91. dogeBTC = float(doge) * float(dogeBTCValue)
  92. totalUSD = dogeUSD
  93. totalBTC = dogeBTC
  94.  
  95. print '\n'
  96. print("You currently have:")
  97. print("%.7f" + " BTC or " "$" + "%.2f" + " USD" + " in DogeCoin") % (dogeBTC, dogeUSD)
  98. print '\n'
  99. if(spent > 0):
  100.     print("You have spent $" + '%.2f' + " and ") % spent
  101. print("You currently have $" + '%.2f') % totalUSD
  102. if(spent > 0):
  103.     print("You have earned a whopping $" + '%.2f' % (float(totalUSD) - float(spent)) + " or " + str(float('%.2f' % (totalUSD/spent)) * 100) + "% from your initial investment!")
  104. if(doge > 0):
  105.         print("TO THE MOON!")
  106.  
  107. #--------------------------------------
  108. #-----------Data analysis--------------
  109. #--------------------------------------
  110.  
  111. worksheet = sh.get_worksheet(1)
  112. dcolumn = worksheet.col_values(1)
  113. tcolumn = worksheet.col_values(2)
  114. pcolumn = worksheet.col_values(3)
  115. ccolumn = worksheet.col_values(4)
  116.  
  117. date = datetime.datetime.now().strftime('%m' + "/" +'%d' + "/" + '%y')
  118. time = datetime.datetime.now().strftime("%H:%M")
  119. currentUSD = ('%.2f' % (float(totalUSD) - float(spent)))
  120.  
  121. def formatdate(x):
  122.     date = str(x)
  123.     string = date.replace("/", " ")
  124.     partslist = string.split()
  125.     partslist[2] = str("20" + partslist[2])
  126.     count = 0
  127.     for i in partslist:
  128.         partslist[count] = str(int(i))
  129.         count += 1
  130.     date = str(partslist[0] + "/" + partslist[1] + "/" + partslist[2])
  131.     return date
  132.  
  133. date = formatdate(date)
  134.  
  135.  
  136. def formattime(x):
  137.     #14:12:00
  138.     x = str(x)
  139.     index = x.find(":")
  140.     part1 = x[0:index]
  141.     part2 = str(x[index+1:index+3])
  142.     if(int(part1) > 11):
  143.         part3 = " PM"
  144.     else:
  145.         part3 = " AM"
  146.     if(int(part1) > 12):
  147.         part1 = int(part1) - 12
  148.     if(int(part1) == 0):
  149.         part1 = 12
  150.     return str('%s' + ":" + '%s' + '%s') % (part1, part2, part3)
  151.  
  152. time = formattime(time)
  153.  
  154.  
  155. def checksign(x):
  156.     x = str(x)
  157.     if "-" in x:
  158.         return str(x)
  159.     else:
  160.         return str("+" + x)
  161.  
  162.  
  163. lastUSD = pcolumn[len(pcolumn)-1]
  164. change = str('%.2f' % (float(currentUSD) - float((lastUSD)[1:len(lastUSD)])))
  165. toAppend = [date, time, str(currentUSD), change, "", totalUSD, spent, str(float('%.2f' % (totalUSD/spent))*100)]
  166. if(float(change) == 0.0):
  167.     print("\nDid not append to spreadsheet. No change in price.")
  168.     print("Last change in profit was " + str(checksign(str(ccolumn[len(ccolumn)-1]))) + " from " + pcolumn[len(pcolumn)-2] + " to " + pcolumn[len(pcolumn)-1] + " at " + tcolumn[len(tcolumn)-1])
  169.     print("Done\n")
  170. else:
  171.     print("\n[Appending to Investments > Data]")
  172.     print("\n    Appended " + str(toAppend) +"\n")
  173.     worksheet.append_row(toAppend)
  174.     print("Done\n")
  175.  
  176. #------------------------------------
  177. #---------Average daily data---------
  178. #------------------------------------
  179.  
  180.  
  181. #--- Get Average Profit---
  182. cell_list = worksheet.findall("Day End")
  183. lastdaycell = cell_list[(len(cell_list) -1)]
  184. cellrow = lastdaycell.row+1
  185. plist = pcolumn[cellrow:len(dcolumn)]
  186. vlistofp = worksheet.range(str(('C'+str(cellrow))+":"+('C'+str(len(dcolumn)))))
  187. total = 0
  188. for value in vlistofp:
  189.     total = float(value.value[1:len(value.value)]) + float(total)
  190. avgProfit = total / len(vlistofp)
  191.  
  192. #---Get Average Profit'---
  193. clist = ccolumn[cellrow:len(dcolumn)]
  194. vlistofc = worksheet.range(str(('D'+str(cellrow))+":"+('D'+str(len(dcolumn)))))
  195. total = 0
  196. for value in vlistofc:
  197.     x = value.value
  198.     x = x.replace('$','')
  199.     total = float(x) + float(total)
  200.  
  201. avgCProfit = total / len(vlistofc)
  202.  
  203. #---Get Sum of change for day---
  204. sumCProfit = total
  205.  
  206.  
  207. #--- check for Day End---
  208. if(((dcolumn[len(dcolumn)-1]) != date)):
  209.     print dcolumn[len(dcolumn)-1]
  210.     print date
  211.     print("Appending averages for previous day on [Investments > Data] and [Investments > Averages]")
  212.     toAppend = ["Day End", "Averages follow", avgProfit, avgCProfit, sumCProfit]
  213.     worksheet.append_row(toAppend)
  214.     print("    Appended " + str(toAppend) + '\n')
  215.     #---averages and sums for days---
  216.     worksheet = sh.get_worksheet(2)
  217.     toAppend = [date, time, '%.4f' % (avgProfit), '%.4f' % (avgCProfit), sumCProfit]
  218.     worksheet.append_row(toAppend)
  219.     print("    Appended " + str(toAppend) + '\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement