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
- stockName = "^DJI"
- mouse = mo()
- keyboard = ke()
- running = False
- delay = 0.2
- #state: Long = True, short = False.
- def buyStock(state):
- #Universal
- mouse.position = (1356,275)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- if(state == True):
- #Buy
- mouse.position = (590, 350)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- elif (state == False):
- #Sell
- mouse.position = (432, 348)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- #Click stop / limit
- mouse.position = (601, 404)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- #Ready to type stop loss points.
- mouse.position = (430, 509)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(delay)
- #Type 131
- keyboard.type("131")
- time.sleep(delay)
- #Submit
- mouse.position = (416, 727)
- 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 = (1578,665)
- time.sleep(delay)
- mouse.click(Button.left)
- time.sleep(0.1)
- #Close Submit
- mouse.position = (417,560)
- 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