Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import date, timedelta, datetime
- import yfinance as yf
- from pynput.mouse import Button, Controller as mo
- from pynput.keyboard import Key, Controller as ke
- import time
- from yahoo_fin import stock_info as si
- import numpy as np
- """
- User-setup
- """
- #Trade button
- tradeB = (1356,275)
- #Sell button
- sellB = (432, 348)
- #Buy button
- buyB = (590, 350)
- #Stop Loss/Limit button
- stopLossB = (601, 404)
- #Point Away field
- pointsAwayF = (430, 509)
- #Submit button
- sumbitB = (416, 727)
- #Close X
- closeB = (1578,665)
- #Sumbit Close
- submitCloseB = (417,560)
- stockName = "^DJI"
- delay = 0.2
- stopLoss = 131
- """
- User-setup complete
- """
- mouse = mo()
- keyboard = ke()
- running = False
- #state: Long = True, short = False.
- def buyStock(state):
- #Universal
- mouse.position = (tradeB[0],tradeB[1])
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- if(state == True):
- #Buy
- mouse.position = (buyB[0], buyB[1])
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- elif (state == False):
- #Sell
- mouse.position = (sellB[0], sellB[1])
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- #Click stop / limit
- mouse.position = (stopLossB[0], stopLossB[1])
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- #Ready to type stop loss points.
- mouse.position = (pointsAwayF[0], pointsAwayF[1])
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- #Type 131
- keyboard.type(str(stopLoss))
- time.sleep(delay)
- #Submit
- mouse.position = (sumbitB[0], sumbitB[1])
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- #Def done
- def getYesterdayStockPrice():
- if(datetime.now().weekday() == 0):
- yesterday = date.today() - timedelta(days=2) # This is indirectly getting fridays stockprice
- temp = yf.download(stockName, yesterday, yesterday)
- return np.array(temp["Close"])[0]
- elif (datetime.now().weekday() > 0 & datetime.now().weekday() <= 4):
- yesterday = date.today() # This is indirectly getting yesterdays stockprice
- temp = yf.download(stockName, yesterday, yesterday)
- return np.array(temp["Close"])[0]
- #Run script
- def run():
- tstart = si.get_live_price(stockName)
- print("Current stock price: " + str(tstart))
- yesterdayStockPrice = getYesterdayStockPrice()
- print("Yesterdays close price: " + str(yesterdayStockPrice))
- time.sleep(0.5)
- #If todays price is bigger than yesterdays, then buy.
- if(tstart > yesterdayStockPrice):
- buyStock(True)
- elif (tstart < yesterdayStockPrice):
- buyStock(False)
- else:
- print("ERROR, same price yesterday as today.")
- def closePosition():
- #Close X
- mouse.position = (closeB[0], closeB[1])
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(0.1)
- #Close Submit
- mouse.position = (submitCloseB[0], submitCloseB[1])
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- #THE LOOP, which checks if it is weekend, and if not, then gets all the data.
- while True:
- if(datetime.now().weekday() >= 0 and datetime.now().weekday() <= 4):
- currentTime = datetime.now().strftime("%H:%M:%S")
- print(currentTime)
- if(running == False):
- if (currentTime == "15:29:58"):
- run()
- running = True
- elif(running == True):
- if(currentTime == "21:59:58"):
- closePosition()
- running = False
- else:
- print("WEEKEND")
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement