How to use the broker.Broker.get_bid function in broker

To help you get started, we’ve selected a few broker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github paperduck / algo / src / strategies / fifty.py View on Github external
Log.write('"fifty.py" _babysit(): BUY trade has closed. (BUY)')
                                    self.open_trade_ids.remove(open_trade_id)
                                    # If SL hit, reverse direction.
                                    if closed[1] == TradeClosedReason.STOP_LOSS_ORDER:
                                        self.go_long = False
                                else:
                                    Log.write('"fifty.py" _babysit(): Failed to modify BUY trade.')
                                    raise Exception
                            else:                                       
                                Log.write('"fifty.py" _babysit(): Modified BUY trade with ID (',\
                                     open_trade_id, ').')
                    else:
                        Log.write('"fifty.py" _babysit(): Failed to get bid while babysitting.')
                        raise Exception
                else: # currently short
                    cur_bid = Broker.get_bid(instrument)
                    if cur_bid != None:
                        if sl - cur_bid > self.sl_price_diff:
                            new_sl = cur_bid + self.sl_price_diff
                            resp = Broker.modify_trade(
                                trade_id=open_trade_id, 
                                stop_loss_price=str(round(new_sl, 2))
                            )
                            if resp == None:
                                closed = Broker.is_trade_closed(open_trade_id)
                                Log.write('fifty.py babysit(): is_trade_closed returned:\n{}'.format(closed))
                                if closed[0]:
                                    Log.write('"fifty.py" _babysit(): SELL trade has closed. (BUY)')
                                    self.open_trade_ids.remove(open_trade_id)
                                    # If SL hit, reverse direction.
                                    if closed[1] == TradeClosedReason.STOP_LOSS_ORDER:
                                        self.go_long = True
github paperduck / algo / src / strategies / fifty.py View on Github external
elif len(spreads) < 1:
            Log.write('"fifty.py" in _scan(): len(spreads) == {}.'
                .format(len(spreads))) 
            raise Exception
        # This only checks for one instrument.
        elif not spreads[0]['tradeable']:
                Log.write('"fifty.py" in _scan(): Instrument {} not tradeable.'
                    .format(instrument.get_name())) 
                return None
        else:
            spread = spreads[0]['spread']
            if spread < 3:
                Log.write('fifty.py _scan(): spread = {}'.format(spread))
                if self.go_long: # buy
                    Log.write('"fifty.py" _scan(): Going long.') 
                    cur_bid = Broker.get_bid(instrument)
                    if cur_bid != None:
                        # Rounding the raw bid didn't prevent float inaccuracy
                        # cur_bid = round(cur_bid_raw, 2)
                        tp = round(cur_bid + self.tp_price_diff, 2)
                        sl = round(cur_bid - self.sl_price_diff, 2)
                    else:
                        Log.write('"fifty.py" in _scan(): Failed to get bid.')
                        raise Exception
                else: # sell
                    Log.write('"fifty.py" _scan(): Shorting.') 
                    self.go_long = False
                    cur_bid = Broker.get_bid(instrument)
                    if cur_bid != None:
                        tp = round(cur_bid - self.tp_price_diff, 2)
                        sl = round(cur_bid + self.sl_price_diff, 2)
                    else:
github paperduck / algo / src / daemon.py View on Github external
# Nothing is being suggested.
                pass
            else:
                # An order was suggested by a strategy, so place the order.
                #   Don't use all the money available.
                SLIPPAGE_WIGGLE = 0.95
                ###available_money = Broker.get_margin_available(Config.account_id) * SLIPPAGE_WIGGLE
                available_money = 100 # USD - testing
                #   Get the current price of one unit.
                instrument_price = 0
                Log.write('best opp: {}'.format(best_opp))
                go_long = best_opp.order.units > 0
                if go_long:
                    instrument_price = Broker.get_ask(best_opp.order.instrument)
                else:
                    instrument_price = Broker.get_bid(best_opp.order.instrument)
                #   How much leverage available:
                margin_rate = Broker.get_margin_rate(best_opp.order.instrument) 
                #   TODO: A bit awkward, but overwrite the existing value that was used to 
                #   determine long/short.
                units = available_money
                units /= cls.num_strategies_with_no_positions() # save money for other strategies
                units /= margin_rate
                units = int(units) # floor
                if units <= 0: # verify
                    Log.write('daemon.py run(): units <= 0')
                    raise Exception # abort
                if not go_long: # negative means short
                    units = -units
                best_opp.order.units = units
                Log.write('daemon.py run(): Executing opportunity:\n{}'.format(best_opp))
                order_result = Broker.place_order(best_opp.order)
github paperduck / algo / src / strategies / fifty.py View on Github external
Log.write('fifty.py _scan(): spread = {}'.format(spread))
                if self.go_long: # buy
                    Log.write('"fifty.py" _scan(): Going long.') 
                    cur_bid = Broker.get_bid(instrument)
                    if cur_bid != None:
                        # Rounding the raw bid didn't prevent float inaccuracy
                        # cur_bid = round(cur_bid_raw, 2)
                        tp = round(cur_bid + self.tp_price_diff, 2)
                        sl = round(cur_bid - self.sl_price_diff, 2)
                    else:
                        Log.write('"fifty.py" in _scan(): Failed to get bid.')
                        raise Exception
                else: # sell
                    Log.write('"fifty.py" _scan(): Shorting.') 
                    self.go_long = False
                    cur_bid = Broker.get_bid(instrument)
                    if cur_bid != None:
                        tp = round(cur_bid - self.tp_price_diff, 2)
                        sl = round(cur_bid + self.sl_price_diff, 2)
                    else:
                        Log.write('"fifty.py" in _scan(): Failed to get ask.') 
                        raise Exception
                # Prepare the order and sent it back to daemon.
                units = 1 if self.go_long else -1
                confidence = 50
                order = Order(
                    instrument=instrument,
                    order_type="MARKET", # matches Oanda's OrderType definition
                    stop_loss={ "price" : str(sl) },
                    take_profit={ "price" : str(tp) },
                    units=units
                )