Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. from datetime import date, timedelta, datetime
  2. import yfinance as yf
  3. from pynput.mouse import Button, Controller as mo
  4. from pynput.keyboard import Key, Controller as ke
  5. import time
  6. from yahoo_fin import stock_info as si
  7. import numpy as np
  8.  
  9. stockName = "^DJI"
  10.  
  11. mouse = mo()
  12. keyboard = ke()
  13.  
  14. running = False
  15.  
  16. delay = 0.2
  17.  
  18. #state: Long = True, short = False.
  19. def buyStock(state):
  20.  
  21. #Universal
  22. mouse.position = (1356,275)
  23. time.sleep(delay)
  24. mouse.click(Button.left)
  25. time.sleep(delay)
  26. mouse.click(Button.left)
  27. time.sleep(delay)
  28.  
  29.  
  30. if(state == True):
  31. #Buy
  32. mouse.position = (590, 350)
  33. time.sleep(delay)
  34. mouse.click(Button.left)
  35. time.sleep(delay)
  36.  
  37. elif (state == False):
  38. #Sell
  39. mouse.position = (432, 348)
  40. time.sleep(delay)
  41. mouse.click(Button.left)
  42. time.sleep(delay)
  43.  
  44. #Click stop / limit
  45. mouse.position = (601, 404)
  46. time.sleep(delay)
  47. mouse.click(Button.left)
  48. time.sleep(delay)
  49.  
  50. #Ready to type stop loss points.
  51. mouse.position = (430, 509)
  52. time.sleep(delay)
  53. mouse.click(Button.left)
  54. time.sleep(delay)
  55. mouse.click(Button.left)
  56. time.sleep(delay)
  57.  
  58. #Type 131
  59. keyboard.type("131")
  60. time.sleep(delay)
  61.  
  62. #Submit
  63. mouse.position = (416, 727)
  64. time.sleep(delay)
  65. mouse.click(Button.left)
  66. time.sleep(delay)
  67.  
  68. #Def done
  69.  
  70. def getYesterdayStockPrice():
  71. if(datetime.now().weekday() == 0):
  72. yesterday = date.today() - timedelta(days=2) # This is indirectly getting fridays stockprice
  73. temp = yf.download(stockName, yesterday, yesterday)
  74. return np.array(temp["Close"])[0]
  75. elif (datetime.now().weekday() > 0 & datetime.now().weekday() <= 4):
  76. yesterday = date.today() # This is indirectly getting yesterdays stockprice
  77. temp = yf.download(stockName, yesterday, yesterday)
  78. return np.array(temp["Close"])[0]
  79.  
  80. #Run script
  81. def run():
  82. tstart = si.get_live_price(stockName)
  83. print("Current stock price: " + str(tstart))
  84.  
  85. yesterdayStockPrice = getYesterdayStockPrice()
  86. print("Yesterdays close price: " + str(yesterdayStockPrice))
  87.  
  88. time.sleep(0.5)
  89.  
  90. #If todays price is bigger than yesterdays, then buy.
  91. if(tstart > yesterdayStockPrice):
  92. buyStock(True)
  93. elif (tstart < yesterdayStockPrice):
  94. buyStock(False)
  95. else:
  96. print("ERROR, same price yesterday as today.")
  97.  
  98.  
  99. def closePosition():
  100. #Close X
  101. mouse.position = (1578,665)
  102. time.sleep(delay)
  103. mouse.click(Button.left)
  104. time.sleep(0.1)
  105.  
  106. #Close Submit
  107. mouse.position = (417,560)
  108. time.sleep(delay)
  109. mouse.click(Button.left)
  110. time.sleep(delay)
  111.  
  112. #THE LOOP, which checks if it is weekend, and if not, then gets all the data.
  113. while True:
  114. if(datetime.now().weekday() >= 0 and datetime.now().weekday() <= 4):
  115.  
  116. currentTime = datetime.now().strftime("%H:%M:%S")
  117. print(currentTime)
  118. if(running == False):
  119. if (currentTime == "15:29:58"):
  120. run()
  121. running = True
  122. elif(running == True):
  123. if(currentTime == "21:59:58"):
  124. closePosition()
  125. running = False
  126.  
  127. else:
  128. print("WEEKEND")
  129. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement