Python 및 Robinhood를 사용한 알고리즘 거래
주식 시장에서 돈을 버는 것은 종종 많은 스트레스, 시간 및 위험을 수반합니다. 투자에 필요한 스트레스와 시간을 줄일 수 있는 방법이 있다면?
로빈후드 가입
Robinhood는 수수료가 없는 주식 거래 및 투자 앱으로 사람들이 빠르고 쉽게 주식 시장에 투자를 시작할 수 있습니다. 이 회사는 최소 계정 없음, 거래 수수료 없음, 옵션 거래 기능과 같은 많은 이점을 가지고 있습니다.
또한 이 플랫폼은 직관적이며 사용자에게 50일 및 200일 이동 평균, 주식 매수 또는 매도 여부에 대한 애널리스트 등급, 다양한 출처의 최신 뉴스와 같은 정보를 제공합니다.
아래 링크를 사용하여 Robinhood에 가입할 수 있으며, 우리 둘 다 $5에서 최대 $200까지 가치가 있는 무료 주식을 받게 됩니다.
알고리즘
우리가 사용할 알고리즘은 주식 시장 분석가가 일반적으로 사용하는 이동 평균 교차입니다. 이 알고리즘은 단기간의 주가의 산술 평균인 단순이동평균(SMA)을 모니터링하고 이를 장기간의 이동평균과 비교합니다.
단기 SMA가 골든 크로스라고 하는 장기 SMA 위로 교차할 때 주식을 매수하라는 신호가 됩니다. 반대로 단기 SMA가 장기 SMA를 하회하는 데드크로스(Death Cross)가 주식 매도 신호입니다.
코딩
시작하려면 먼저 robin_stocks 라이브러리를 설치해야 Robinhood 계정에 연결할 수 있습니다.
pip install robin_stocks
그런 다음 필요한 라이브러리를 가져오고 Robinhood 계정의 자격 증명을 제공합니다.
import robin_stocks as r
import time
# Replace "your_username" and "your_password" with your own credentials
r.login(username="your_username", password="your_password")
# Desired stock symbol
stock_symbol = "AAPL"
# Obtain moving averages
MA_50 = r.stocks.get_crypto_historicals(symbol=stock_symbol, interval='day', span='50day')[0]['close_price']
MA_200 = r.stocks.get_crypto_historicals(symbol=stock_symbol, interval='day', span='200day')[0]['close_price']
while True:
# Current stock price
current_price = float(r.stocks.get_latest_price(stock_symbol)[0])
# Calculate new moving averages
MA_50 = (49 * MA_50 + current_price) / 50
MA_200 = (199 * MA_200 + current_price) / 200
# Condition to buy or sell
if MA_50 > MA_200:
# Buy
invest_amount = 1000
account_balance = float(r.crypto.get_crypto_currency_balance('USD')['quantity'])
if account_balance >= invest_amount:
r.orders.order_sell_crypto_by_price(stock_symbol, invest_amount)
print("Bought", stock_symbol, "at", current_price, "on", time.strftime("%c"))
else:
# Sell
sell_amount = 1000
position = r.crypto.get_crypto_currency_balance(stock_symbol)
if float(position['quantity']) >= sell_amount:
r.orders.order_sell_crypto_by_price(stock_symbol, -sell_amount)
print("Sold", stock_symbol, "at", current_price, "on", time.strftime("%c"))
# Wait for one minute before checking the new stock price
time.sleep(60)
import robin_stocks as r
import time
# Replace "your_username" and "your_password" with your own credentials
r.login(username="your_username", password="your_password")
# Desired stock symbol
stock_symbol = "AAPL"
# Obtain moving averages
MA_50 = r.stocks.get_crypto_historicals(symbol=stock_symbol, interval='day', span='50day')[0]['close_price']
MA_200 = r.stocks.get_crypto_historicals(symbol=stock_symbol, interval='day', span='200day')[0]['close_price']
while True:
# Current stock price
current_price = float(r.stocks.get_latest_price(stock_symbol)[0])
# Calculate new moving averages
MA_50 = (49 * MA_50 + current_price) / 50
MA_200 = (199 * MA_200 + current_price) / 200
# Condition to buy or sell
if MA_50 > MA_200:
# Buy
invest_amount = 1000
account_balance = float(r.crypto.get_crypto_currency_balance('USD')['quantity'])
if account_balance >= invest_amount:
r.orders.order_sell_crypto_by_price(stock_symbol, invest_amount)
print("Bought", stock_symbol, "at", current_price, "on", time.strftime("%c"))
else:
# Sell
sell_amount = 1000
position = r.crypto.get_crypto_currency_balance(stock_symbol)
if float(position['quantity']) >= sell_amount:
r.orders.order_sell_crypto_by_price(stock_symbol, -sell_amount)
print("Sold", stock_symbol, "at", current_price, "on", time.strftime("%c"))
# Wait for one minute before checking the new stock price
time.sleep(60)
Python을 사용하여 자신만의 거래 알고리즘을 구현하는 것을 즐기시기 바랍니다. 질문이 있는 경우 아래에 댓글을 달아 주시면 기꺼이 답변해 드리겠습니다!

![연결된 목록이란 무엇입니까? [1 부]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































