├── .DS_Store ├── .gitignore ├── README.md ├── main.py ├── testCode.py └── tradingWindow.png /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/md-raghib/tradingBotPython/47cdd8911192bb3b538db40c90bca6285c114136/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tradingBotPython 2 | Trading bot to backtest on tradeview 3 | 4 | prerequisite : 5 | 1. Python3.* 6 | 2. pip3 install nsepy 7 | 3. pip3 install selenium 8 | 4. pip3 install webdriver_manager 9 | 10 | 11 | Reference Document - 12 | 13 | 1. chromedriver - https://sites.google.com/a/chromium.org/chromedriver/getting-started 14 | 2. Tradingview sample code - https://python-tradingview-ta.readthedocs.io/en/latest/overview.html 15 | 16 | Working - 17 | 1. Run python3 main.py at 9:00 AM on open market days 18 | 2. bot tries to read the nifty and banknifty current value 19 | 3. bot runs forever, starting from 9:00AM till 03:00 PM in the interval of 5 mins. 20 | 4. bot used strategies based on RSI and EMA ( please refer to google to understand more about these ) 21 | 22 | Steps to run - 23 | 1. execute - python3 main.py - this will open a browser, login to your tradeview account and connect to paper trading. 24 | 2. Open the trading box by clicking on the icon shown in the image below - 25 | ![alt text](https://github.com/md-raghib/tradingBotPython/blob/access/tradingWindow.png) 26 | 3. Let the code run. It will close at 3:00PM and sums up your profit for the day. 27 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | ################################################## 2 | ## Description 3 | # This is the code for the trading bot. 4 | # You should execute this code on an open market day @8:58AM. 5 | # this bot places order based on conditions acquired from the indicators RSI and EMA 6 | # Python code is used to create the condition and selenium is used to place the order from the browser 7 | # to get a web interface feel. 8 | # steps to execute and pre-requisites are mentioned in the readme file in the repo. 9 | ################################################## 10 | ################################################## 11 | ## License_info 12 | ################################################## 13 | ## Author: Mohammad Raghib 14 | ## Credits: [Manisha Sharma] 15 | ## Email: happyengineer0101@gmail.com 16 | ## Status: DEV 17 | ################################################## 18 | 19 | from nsepy import get_history 20 | from datetime import date, datetime 21 | import os 22 | from tradingview_ta import TA_Handler, Interval, Exchange 23 | import time 24 | from selenium import webdriver 25 | from selenium.webdriver.chrome.service import Service 26 | from webdriver_manager.chrome import ChromeDriverManager 27 | from selenium.webdriver.common.by import By 28 | 29 | os.system("figlet -c Python Trading Bot ") 30 | Today = date.today() 31 | y = Today.strftime("%Y") 32 | m = Today.strftime("%m") 33 | d = Today.strftime("%d") 34 | # d = "30" 35 | 36 | 37 | #last order 38 | last_order="sell" 39 | sold_before = False 40 | bought_before = False 41 | current_price = 0 42 | take_profit = 0.0 43 | take_loss = 0.0 44 | 45 | 46 | #load chrome driver 47 | # driver = webdriver.Chrome(executable_path="/Users/mrm/Downloads/chromedriver") 48 | s=Service(ChromeDriverManager().install()) 49 | driver = webdriver.Chrome(service=s) 50 | driver.maximize_window() 51 | driver.get("https://in.tradingview.com/") 52 | time.sleep(60) 53 | 54 | 55 | #initiating tradingview handler to get the recomendation for sonata software for 15 min interval 56 | ssw = TA_Handler( 57 | symbol="SONATSOFTW", 58 | screener="india", 59 | exchange="NSE", 60 | interval=Interval.INTERVAL_5_MINUTES 61 | ) 62 | def countdown(t): 63 | 64 | while t: 65 | mins, secs = divmod(t, 60) 66 | timer = '{:02d}:{:02d}'.format(mins, secs) 67 | print(timer, end="\r") 68 | time.sleep(1) 69 | t -= 1 70 | 71 | 72 | while True: 73 | now = datetime.now() 74 | current_time = now.strftime("%H:%M:%S") 75 | if(current_time >= "09:30:00" and current_time < "15:10:00"): 76 | 77 | rec = ssw.get_analysis() 78 | RSI = rec.indicators["RSI"] 79 | # MACD = rec.indicators["MACD.macd"] 80 | EMA = rec.moving_averages["COMPUTE"]["EMA10"] 81 | print("RSI:", RSI, "EMA:", EMA) 82 | 83 | 84 | if ( RSI >= 30 and RSI <= 70 and EMA == "BUY" ): 85 | if (last_order=="sell"): 86 | print("Buying 1 stock of SONATSOFTW") 87 | last_order="buy" 88 | print(last_order) 89 | print(sold_before) 90 | #buy 1 stock of SONATSOFTW 91 | driver.find_element(By.XPATH,"//div[8]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[2]").click() 92 | driver.find_element(By.XPATH,"//div[1]/div[1]/div[6]/button[1]/div[1]/span[2]").click() 93 | current_price = driver.find_element(By.XPATH,"//div[2]/div[8]/div[1]/div/div/div[1]/div[2]/div/div[2]/div[2]/div").text 94 | print(current_price) 95 | take_profit = float(current_price) + 8 96 | take_loss = float(current_price) - 5 97 | while True: 98 | print("Time left till next call - ") 99 | countdown(int(5)) 100 | rec = ssw.get_analysis() 101 | RSI = rec.indicators["RSI"] 102 | # MACD = rec.indicators["MACD.macd"] 103 | EMA = rec.moving_averages["COMPUTE"]["EMA10"] 104 | print("RSI:", RSI, "EMA:", EMA) 105 | current_price = driver.find_element(By.XPATH,"//div[2]/div[8]/div[1]/div/div/div[1]/div[2]/div/div[2]/div[2]/div").text 106 | if((RSI >= 30 and EMA == "SELL") or (float(current_price) >= take_profit) or (float(current_price) <= take_loss)): 107 | #sell the stock 108 | print("Selling 1 stock of SONATSOFTW") 109 | last_order="sell" 110 | print(last_order) 111 | #sell 1 stock of SONATSOFTW 112 | driver.find_element(By.XPATH,"//body[1]/div[2]/div[8]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]").click() 113 | time.sleep(2) 114 | driver.find_element(By.XPATH,"//button[1]/div[1]/span[2]").click () 115 | break 116 | else: 117 | print("no adjustment required") 118 | else: 119 | print("last order not sold") 120 | elif( RSI >= 50 and EMA == "SELL" ): 121 | if ( last_order == "sell"): 122 | print("selling stock of SONATSOFTW") 123 | driver.find_element(By.XPATH,"//body[1]/div[2]/div[8]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]").click() 124 | time.sleep(2) 125 | driver.find_element(By.XPATH,"//button[1]/div[1]/span[2]").click () 126 | current_price = driver.find_element(By.XPATH,"//div[2]/div[8]/div[1]/div/div/div[1]/div[2]/div/div[2]/div[2]/div").text 127 | print(current_price) 128 | take_profit = float(current_price) - 8 129 | take_loss = float(current_price) + 5 130 | while True: 131 | print("Time left till next call - ") 132 | countdown(int(5)) 133 | rec = ssw.get_analysis() 134 | RSI = rec.indicators["RSI"] 135 | # MACD = rec.indicators["MACD.macd"] 136 | EMA = rec.moving_averages["COMPUTE"]["EMA10"] 137 | print("RSI:", RSI, "EMA:", EMA) 138 | current_price = driver.find_element(By.XPATH,"//div[2]/div[8]/div[1]/div/div/div[1]/div[2]/div/div[2]/div[2]/div").text 139 | if((RSI <= 30 and EMA == "BUY") or ( float(current_price) <= take_profit) or (float(current_price) >= take_loss)): 140 | #buy the stock 141 | print("Buying the stock") 142 | driver.find_element(By.XPATH,"//div[8]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[2]").click() 143 | driver.find_element(By.XPATH,"//div[1]/div[1]/div[6]/button[1]/div[1]/span[2]").click() 144 | break 145 | else: 146 | print("no adjustment required") 147 | 148 | else: 149 | print("condition not favourable..waiting") 150 | 151 | elif(current_time >= "15:10:00"): 152 | print("Time to close for the day") 153 | # #fetch open profit 154 | open_profit = driver.find_element(By.XPATH,"//div[4]/div[1]/div[1]/div[1]/div[2]/div[3]/div[1]").text 155 | # print(open_profit) 156 | # P = "1000" 157 | print("Calculating profit :",open_profit) 158 | break 159 | else: 160 | if(current_time >= "09:15:00" and current_time < "09:30:00"): 161 | print("Analysing market","\n\n") 162 | elif(current_time < "9:15:00"): 163 | print("Waiting for market to open") 164 | else: 165 | print("No action required") 166 | 167 | 168 | 169 | 170 | 171 | -------------------------------------------------------------------------------- /testCode.py: -------------------------------------------------------------------------------- 1 | 2 | ################################################## 3 | ## Description 4 | # This is a test code for the trading bot. 5 | # You should execute this code before executing the main code. 6 | # This code is required to check all the functionality and proper working of the tradin bot 7 | # before executing the main code on an open market day. 8 | # This code fetches the data of the provided stock every five minutes. 9 | # The time to login to the tradingview is reduced to 60 sec 10 | # and sleep after every hit to the website is refreshed every 60 sec for a quick test 11 | ################################################## 12 | ## License_info 13 | ################################################## 14 | ## Author: Mohammad Raghib 15 | ## Credits: [Manisha Sharma] 16 | ## Email: happyengineer0101@gmail.com 17 | ## Status: DEV 18 | ################################################## 19 | from nsepy import get_history 20 | from datetime import date, datetime 21 | import os 22 | from tradingview_ta import TA_Handler, Interval, Exchange 23 | import time 24 | from selenium import webdriver 25 | 26 | os.system("figlet -c Python Trading Bot ") 27 | Today = date.today() 28 | y = Today.strftime("%Y") 29 | m = Today.strftime("%m") 30 | # d = Today.strftime("%d") 31 | d = "30" 32 | now = datetime.now() 33 | current_time = now.strftime("%H:%M:%S") 34 | 35 | #last order 36 | last_order="sell" 37 | 38 | #load chrome driver 39 | driver = webdriver.Chrome(executable_path="/Users/mrm/Downloads/chromedriver") 40 | driver.maximize_window() 41 | driver.get("https://in.tradingview.com/") 42 | time.sleep(60) 43 | 44 | #initiating tradingview handler to get the recomendation for sonata software for 15 min interval 45 | ssw = TA_Handler( 46 | symbol="SONATSOFTW", 47 | screener="india", 48 | exchange="NSE", 49 | interval=Interval.INTERVAL_5_MINUTES 50 | ) 51 | 52 | 53 | while True: 54 | # if(current_time >= "09:30:00" and current_time <= "15:00:00"): 55 | 56 | rec = ssw.get_analysis() 57 | RSI = rec.indicators["RSI"] 58 | MACD = rec.indicators["MACD.macd"] 59 | EMA = rec.moving_averages["COMPUTE"]["EMA10"] 60 | 61 | if ( RSI >= 30 and last_order=="sell" and MACD >= -3 and EMA == "BUY" and RSI <=70): 62 | print("Buying 1 stock of SONATSOFTW") 63 | last_order="buy" 64 | #buy 1 stock of SONATSOFTW 65 | driver.find_element_by_xpath("//div[8]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[2]").click() 66 | #click on buy 1 NES: 67 | driver.find_element_by_xpath("//div[1]/div[1]/div[6]/button[1]/ div[1]/span[2]").click() 68 | elif( RSI >= 30 and last_order=="buy" and EMA == "SELL"): 69 | print("Selling 1 stock of SONATSOFTW") 70 | last_order="sell" 71 | #sell 1 stock of SONATSOFTW 72 | driver.find_element_by_xpath("//body[1]/div[2]/div[8]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]").click() 73 | time.sleep(2) 74 | driver.find_element_by_xpath("//button[1]/div[1]/span[2]").click () 75 | elif( RSI >= 70 and last_order=="sell" and EMA == "SELL"): 76 | print("Selling 1 stock of SONATSOFTW") 77 | last_order="sell" 78 | #sell 1 stock of SONATSOFTW 79 | driver.find_element_by_xpath("//body[1]/div[2]/div[8]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]").click() 80 | time.sleep(2) 81 | driver.find_element_by_xpath("//button[1]/div[1]/span[2]").click 82 | else: 83 | print("No adjustment required") 84 | # elif(current_time >= "15:00:00"): 85 | # print("Time to close for the day") 86 | # # #fetch open profit 87 | # open_profit = driver.find_element_by_xpath("//div[4]/div[1]/div [1]/div[1]/div[2]/div[3]/div[1]").text 88 | # # print(open_profit) 89 | # # P = "1000" 90 | # print("Calculating profit :",open_profit) 91 | # break 92 | # else: 93 | # if(current_time >= "09:05:00"): 94 | # print("==========Getting Nifty and Bank Nifty opening price==========","\n\n") 95 | # nifty = get_history(symbol='NIFTY 50', 96 | # start=date(int(y), int(m), int(d)), 97 | # end=date(int(y), int(m), int(d)), 98 | # index=True) 99 | 100 | # niftyBank = get_history(symbol='NIFTY BANK', 101 | # start=date(int(y), int(m), int(d)), 102 | # end=date(int(y), int(m), int(d)), 103 | # index=True) 104 | 105 | # print("====NIFTY 50====") 106 | # print(nifty,"\n") 107 | # print("====BANK NIFTY====") 108 | # print(niftyBank,"\n") 109 | # else: 110 | # print("Waiting for market to open and bot to analyse market till 9:30") 111 | 112 | time.sleep(60) -------------------------------------------------------------------------------- /tradingWindow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/md-raghib/tradingBotPython/47cdd8911192bb3b538db40c90bca6285c114136/tradingWindow.png --------------------------------------------------------------------------------