├── .gitignore ├── README.md ├── bot.py ├── config.py └── requirements.txt /.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 | # stochastic-alpaca-crypto-trading-api 2 | buys and sells solana on a schedule using the alpaca crypto api 3 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import config 2 | import vectorbt as vbt 3 | import pandas as pd 4 | import pandas_ta as ta 5 | from datetime import datetime 6 | from alpaca_trade_api.rest import REST, TimeFrame 7 | 8 | alpaca = REST(config.API_KEY, config.SECRET_KEY, 'https://paper-api.alpaca.markets') 9 | 10 | in_position_quantity = 0 11 | pending_orders = {} 12 | dollar_amount = 10000 13 | logfile = 'trade.log' 14 | 15 | def check_order_status(): 16 | global in_position_quantity 17 | 18 | removed_order_ids = [] 19 | 20 | print("{} - checking order status".format(datetime.now().isoformat())) 21 | 22 | if len(pending_orders.keys()) > 0: 23 | print("found pending orders") 24 | for order_id in pending_orders: 25 | order = alpaca.get_order(order_id) 26 | 27 | if order.filled_at is not None: 28 | filled_message = "order to {} {} {} was filled {} at price {}\n".format(order.side, order.qty, order.symbol, order.filled_at, order.filled_avg_price) 29 | print(filled_message) 30 | with open(logfile, 'a') as f: 31 | f.write(str(order)) 32 | f.write(filled_message) 33 | 34 | if order.side == 'buy': 35 | in_position_quantity = float(order.qty) 36 | else: 37 | in_position_quantity = 0 38 | 39 | removed_order_ids.append(order_id) 40 | else: 41 | print("order has not been filled yet") 42 | 43 | for order_id in removed_order_ids: 44 | del pending_orders[order_id] 45 | 46 | 47 | def send_order(symbol, quantity, side): 48 | print("{} - sending {} order".format(datetime.now().isoformat(), side)) 49 | order = alpaca.submit_order(symbol, quantity, side, 'market') 50 | print(order) 51 | pending_orders[order.id] = order 52 | 53 | 54 | def get_bars(): 55 | print("{} - getting bars".format(datetime.now().isoformat())) 56 | data = vbt.CCXTData.download(['SOLUSDT'], start='30 minutes ago', timeframe='1m') 57 | df = data.get() 58 | df.ta.stoch(append=True) 59 | print(df) 60 | 61 | last_k = df['STOCHk_14_3_3'].iloc[-1] 62 | last_d = df['STOCHd_14_3_3'].iloc[-1] 63 | last_close = df['Close'].iloc[-1] 64 | 65 | print(last_k) 66 | print(last_d) 67 | print(last_close) 68 | 69 | if last_d < 20 and last_k > last_d: 70 | if in_position_quantity == 0: 71 | # buy 72 | send_order('SOLUSD', dollar_amount / last_close, 'buy') 73 | else: 74 | print("== already in position, nothing to do ==") 75 | 76 | if last_d > 80 and last_k < last_d: 77 | if in_position_quantity > 0: 78 | # sell 79 | send_order('SOLUSD', in_position_quantity, 'sell') 80 | else: 81 | print("== you have nothing to sell ==") 82 | 83 | 84 | manager = vbt.ScheduleManager() 85 | manager.every().do(check_order_status) 86 | manager.every().minute.at(':00').do(get_bars) 87 | manager.start() 88 | 89 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | API_KEY = 'yourkey' 2 | SECRET_KEY = 'yoursecret' -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | vectorbt==0.23.2 2 | numpy==1.21 3 | ccxt 4 | alpaca_trade_api 5 | --------------------------------------------------------------------------------