Exemplo n.º 1
0
    def main(self, stock, action, quantity):
        username = ""
        password = ""

        rh = Robinhood()
        rh.login(username=username, password=password)

        instrument = rh.instruments(stock)[0]

        if (action == "buy"):
            rh.place_buy_order(instrument, quantity)
            self.enteredTrade = True

        if (action == "sell"):
            rh.place_sell_order(instrument, quantity)
            self.enteredTrade = False
Exemplo n.º 2
0
while 1:
    current_price = get_current_price(rh)

    for instrument_url, info in current_price.items():
        if instrument_url in bought:
            bought_info = bought[instrument_url]
            # Compute percentage
            if bought_info["price"] > 1e-8:
                percent = (info["price"] / bought_info["price"]) - 1
                print(info["symbol"], percent)
                #TODO: could sell based of percentage

            # Compute price difference is below threshold, then sell the stock
            price_diff = info["price"] - bought_info["price"]

            # If the diff
            if price_diff < PRICE_DIFF_LIMIT:
                print("Selling {} shares of {}".format(info["quantity"],
                                                       info["symbol"]))
                instrument = {"url": instrument_url, "symbol": info["symbol"]}
                rh.place_sell_order(instrument, info["quantity"])

                # refresh bought list
                bought = obtain_bought(rh)

        # Detected a new entry, so refresh bought list
        else:
            bought = obtain_bought(rh)

    # Refresh every hour
    time.sleep(REFRESH_TIME)
Exemplo n.º 3
0
class RHTrader:
    """
    Algorithmic trader using Robinhood API to enter positions based on RSI period and historical prices.
    """
    def __init__(self, username, pwd, rsi=5):
        self.rh = Robinhood()
        self.rh.login(username=username, password=pwd)
        self.rsiPeriod = rsi
        self.enteredPosition = False
        self.s = sched.scheduler(time.time, time.sleep)
        self.data = np.array([])
        self.closePrices = []

    def populate(self):
        historical_quotes = self.rh.get_historical_quotes(
            "F", "5minute", "day")
        index = 0
        support = 0
        resistance = 0
        for key in historical_quotes["results"][0]["historicals"]:
            if index >= len(historical_quotes["results"][0]["historicals"]) - (
                    self.rsiPeriod + 1):
                if (index >= (self.rsiPeriod - 1) and datetime.strptime(
                        key['begins_at'], '%Y-%m-%dT%H:%M:%SZ').minute == 0):
                    support = 0
                    resistance = 0
                    print("Resetting support and resistance")
                if float(key['close_price']) < support or support == 0:
                    support = float(key['close_price'])
                    print("Current Support is : ")
                    print(support)
                if float(key['close_price']) > resistance:
                    resistance = float(key['close_price'])
                    print("Current Resistance is : ")
                    print(resistance)
                self.closePrices.append(float(key['close_price']))
            index += 1
        self.data = np.array(self.closePrices)

    def execute(self, sc):
        if len(self.closePrices) > self.rsiPeriod:
            # Calculate RSI
            rsi = ti.rsi(self.data, period=self.rsiPeriod)
            instrument = self.rh.instruments("F")[0]
            # If rsi is less than or equal to 30 buy
            if rsi[len(rsi) - 1] <= 30 and not self.enteredPosition:
                print("Buying RSI is below 30!")
                self.rh.place_buy_order(instrument, 1)
                self.enteredPosition = True
            # Sell when RSI reaches 70
            if rsi[len(rsi) - 1] >= 70 and self.enteredPosition:
                print("Selling RSI is above 70!")
                self.rh.place_sell_order(instrument, 1)
                self.enteredPosition = False
            print(rsi)
            # call this method again every 5 minutes for new price changes
        self.s.enter(300, 1, self.execute, (sc, ))

    def run(self):
        self.s.enter(1, 1, self.execute, (self.s, ))
        self.s.run()