Advertisement
Guest User

Untitled

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