Exemple #1
0
def create_trailing_stop_loss_order(order_tradeID, order_distance,
                                    order_timeInForce):
    """ 
    Create a trailing stop loss order.
    Note: this is used to modify a pre-existing order and not to make a new one.
    """
    ordr = TrailingStopLossOrderRequest(
        # The ID of the Trade to close when the price threshold is breached.
        tradeID=order_tradeID,
        # The price distance (in price units) specified for the TrailingStopLoss Order.
        distance=order_distance,
        # The time-in-force requested for the TrailingStopLoss Order. Restricted to
        # “GTC”, “GFD” and “GTD” for TrailingStopLoss Orders.
        timeInForce=order_timeInForce
        # The date/time when the StopLoss Order will be cancelled if its
        # timeInForce is “GTD”.
        #gtdTime = order_gtdTime
    )
    # create the OrderCreate request
    r = orders.OrderCreate(accountID, data=ordr.data)
    try:
        # create the OrderCreate request
        rv = api.request(r)
    except oandapyV20.exceptions.V20Error as err:
        print(r.status_code, err)
    else:
        print(json.dumps(rv, indent=2))
Exemple #2
0
 def trailingStop(self, tradID, distance=0.25):
     activeStop = TrailingStopLossOrderRequest(tradeID=tradeID,
                                               distance=distance)
     r = orders.OrderCreate(accountID, tradeID, data=activeStop.data)
     self.api.request(r)
     if self.should_print == True:
         print(r.response)
     return r.response
async def create_order(market, buy_signal, sell_signal):
    # if buy_signal > sell_signal:
    #     mo = MarketOrderRequest(instrument=market, units=(buy_signal * 10000 - sell_signal * 10000))
    #     market_order = oanda.request(orders.OrderCreate(accountID, data=mo.data))
    # elif sell_signal > buy_signal:
    #     mo = MarketOrderRequest(instrument=market, units=(buy_signal * 10000 - sell_signal * 10000))
    mo = MarketOrderRequest(instrument=market,
                            units=(buy_signal * 10000 - sell_signal * 10000))
    market_order = oanda.request(orders.OrderCreate(accountID, data=mo.data))
    #order confirmation
    print(json.dumps(market_order, indent=4))
    #set trailing stop
    order_id = market_order['orderFillTransaction']['orderID']
    ordr = TrailingStopLossOrderRequest(tradeID=order_id, distance=20)
    trailing_stop = oanda.request(orders.OrderCreate(accountID,
                                                     data=ordr.data))
    #trailing stop confimrmation
    print(json.dumps(trailing_stop, indent=4))

    return market_order, trailing_stop
Exemple #4
0
def orderlaunch(args):

    pair_targeted, direction = args

    info = PricingInfo(accountID=accountID,
                       params={"instruments": pair_targeted})
    mkt_order = None

    if direction is 0:
        return False

    elif direction is 1:
        raw_current_price = api.request(info)
        bid_current = float(raw_current_price['prices'][0]['bids'][0]['price'])
        decim = str(bid_current)[::-1].find('.')
        if decim < 4:
            decim = 3
        if decim >= 4:
            decim = 5
        stop_loss = round(bid_current - bid_current * sl_tp_prc, decim)
        take_profit = round(bid_current + bid_current * sl_tp_prc, decim)
        if float(count_spe_trades(pair_targeted)) < 0:
            close(pair_targeted)
        mkt_order = MarketOrderRequest(
            instrument=pair_targeted,
            units=trade_size,
            takeProfitOnFill=TakeProfitDetails(price=take_profit).data,
            stopLossOnFill=StopLossDetails(price=stop_loss).data)

    elif direction is -1:
        raw_current_price = api.request(info)
        ask_current = float(raw_current_price['prices'][0]['asks'][0]['price'])
        decim = str(ask_current)[::-1].find('.')
        if decim < 4:
            decim = 3
        if decim >= 4:
            decim = 5
        stop_loss = round(ask_current + ask_current * sl_tp_prc, decim)
        take_profit = round(ask_current - ask_current * sl_tp_prc, decim)
        if float(count_spe_trades(pair_targeted)) > 0:
            close(pair_targeted)
        mkt_order = MarketOrderRequest(
            instrument=pair_targeted,
            units=(trade_size * -1),
            takeProfitOnFill=TakeProfitDetails(price=take_profit).data,
            stopLossOnFill=StopLossDetails(price=stop_loss).data)

    # create the OrderCreate request
    r = orders.OrderCreate(accountID, data=mkt_order.data)

    try:
        # send the OrderCreate request
        rv = api.request(r)
        print(json.dumps(rv, indent=2))

    except V20Error as err:
        print(r.status_code, err)
        return False

    else:
        try:
            if Trailing is True:
                key = 'orderFillTransaction'
                if key in rv:
                    trade_id = rv['orderFillTransaction']['tradeOpened'][
                        'tradeID']
                    bid_current = float(rv['orderFillTransaction']['fullPrice']
                                        ['bids'][0]['price'])
                    decim = str(bid_current)[::-1].find('.')
                    trail_point_ok = 0
                    if decim < 4:
                        trail_point_ok = trail_point * (1 / (10**(3 - 1)))
                    if decim >= 4:
                        trail_point_ok = trail_point * (1 / (10**(5 - 1)))
                    ordr = TrailingStopLossOrderRequest(
                        tradeID=trade_id, distance=trail_point_ok)
                    r = orders.OrderCreate(accountID, data=ordr.data)
                    rva = api.request(r)
                    print(json.dumps(rva, indent=2))

        except V20Error as err:
            print(r.status_code, err)
            return False

        else:
            return True
Exemple #5
0
def trailingStop(tradeID, distance=0.1):
    activeStop = TrailingStopLossOrderRequest(tradeID=tradeID,
                    r = orders.OrderCreate(accountID,
                    data=activeStop.data))
    api.request(r)
    return r.response