def newOrder(instrument, p, pip, bid, ask):
    units = 1000
    if instrument == 'XAG_USD':
        units = 1
    if instrument == 'XAU_USD':
        units = 1
    if instrument == 'BCO_USD':
        units = 1
    if p > ask:
        side = 'buy'
        if pip == 0.001:
            price = round(((ask - bid) / 2 + bid), 3)
            takeProfit = round((ask + 0.007 * ask), 3)
            stopLoss = round((bid - 0.002 * ask), 3)
            trailingStop = round((bid - 0.002 * ask), 3)
        if pip == 0.00001:
            price = round(((ask - bid) / 2 + bid), 5)
            takeProfit = round((ask + 0.007 * ask), 5)
            stopLoss = round((bid - 0.002 * ask), 5)
            trailingStop = round((bid - 0.002 * ask), 5)
    if p < bid:
        side = 'sell'
        if pip == 0.001:
            price = round(((ask - bid) / 2 + bid), 3)
            takeProfit = round((bid - 0.007 * bid), 3)
            stopLoss = round((ask + 0.002 * bid), 3)
            trailingStop = round((ask + 0.002 * bid), 3)
        if pip == 0.00001:
            price = round(((ask - bid) / 2 + bid), 5)
            takeProfit = round((bid - 0.007 * bid), 5)
            stopLoss = round((ask + 0.002 * bid), 5)
            trailingStop = round((ask + 0.002 * bid), 5)
    test_order = Order(
        instrument=instrument,
        units=units,
        side=side,
        type="market",
        stopLoss=stopLoss,
        trailingStop=trailingStop,
        takeProfit=takeProfit,
        # price=price,
        # now = datetime.datetime.now()
        # expire = now + datetime.timedelta(days=1)
        # expiry="GTC"
        expiry=(datetime.datetime.now() +
                datetime.timedelta(days=50)).isoformat('T') + "Z"

        # expiry=(datetime.datetime.now() + datetime.timedelta(days=1))
        # expiry=datetime.datetime.now()
    )
    print(instrument, takeProfit, stopLoss)
    return test_order
Exemple #2
0
def market_order(symbol, units, side):
    bid = get_bid(symbol)
    if side == 'buy':
        sl = "{0:.1f}".format(bid * (1 - trading[symbol][side + '_sl']))
        tp = "{0:.1f}".format(bid * (1 + trading[symbol][side + '_tp']))
    else:
        sl = "{0:.1f}".format(bid * (1 + trading[symbol][side + '_sl']))
        tp = "{0:.1f}".format(bid * (1 - trading[symbol][side + '_tp']))
    market_order = Order(instrument=symbol,
                         units=units,
                         side=side,
                         type="market",
                         stopLoss=sl,
                         takeProfit=tp)
    return market_order
Exemple #3
0
import datetime

from pyoanda import Client, PRACTICE, Order

client = Client(
    environment=PRACTICE,
    account_id='9276489',
    access_token=
    '2b557bdd4fa3dee56c8a159ece012a48-5f5a29d25cb2e7ea1aaeba98f4bbca40')

test_order = Order(
    instrument="BCO_USD",
    units=1000,
    side="buy",
    type="market",
    trailingStop=51.10,
    takeProfit=58.55,
    # price=1.1100,
    # now = datetime.datetime.now()
    # expire = now + datetime.timedelta(days=1)
    # expiry="GTC"
    expiry=(datetime.datetime.now() +
            datetime.timedelta(days=50)).isoformat('T') + "Z"

    # expiry=(datetime.datetime.now() + datetime.timedelta(days=1))
    # expiry=datetime.datetime.now()
)

position = client.get_positions()
print(position)
order = client.create_order(order=test_order)
Exemple #4
0
def newOrder(instrument, p, bid, ask):
    units = 1000
    if instrument == 'XAG_USD':
        units = 1
    if instrument == 'XAU_USD':
        units = 1
    if instrument == 'BCO_USD':
        units = 1

    if oanda_usdjpy_topic[-3:] == "JPY":
        pip = float('0.001')
    else:
        pip = float('0.00001')

    if p > ask:
        side = 'buy'

        if pip == 0.00001:
            price = round(((ask - bid) / 2 + bid), 5)
            takeProfit = round((ask + 0.0005 * ask), 5)
            stopLoss = round((bid - 0.0005 * ask), 5)
            trailingStop = round((bid - 0.0007 * ask), 5)

        if pip == 0.001:
            price = round(((ask - bid) / 2 + bid), 3)
            takeProfit = round((ask + 0.0007 * ask), 3)
            stopLoss = round((bid - 0.0003 * ask), 3)
            trailingStop = round((bid - 0.0007 * ask), 3)

        if instrument == "USD_JPY":
            jpy_exposure = 1000 * float(oanda_eurjpy_bid)
            units = int(round(jpy_exposure / float(oanda_usdjpy_ask), 0))
            print(units)
    if p < bid:
        side = 'sell'

        if pip == 0.00001:
            price = round(((ask - bid) / 2 + bid), 5)
            takeProfit = round((bid - 0.0007 * bid), 5)
            stopLoss = round((ask + 0.0003 * bid), 5)
            trailingStop = round((ask + 0.0007 * bid), 5)

        if pip == 0.001:
            price = round(((ask - bid) / 2 + bid), 3)
            takeProfit = round((bid - 0.0007 * bid), 3)
            stopLoss = round((ask + 0.0003 * bid), 3)
            trailingStop = round((ask + 0.0007 * bid), 3)

        if instrument == "USD_JPY":
            jpy_exposure = 1000 * float(oanda_eurjpy_ask)
            units = int(round(jpy_exposure / float(oanda_usdjpy_bid), 0))
            print(units)

    print(stopLoss, takeProfit)
    test_order = Order(
        instrument=instrument,
        units=units,
        side=side,
        type="market",
        stopLoss=stopLoss,
        trailingStop=20,
        # distance= (2),
        takeProfit=takeProfit,
        price=price,
        # now = datetime.datetime.now()
        # expire = now + datetime.timedelta(days=1)
        expiry="GTC"
        # expiry=(datetime.datetime.now() + datetime.timedelta(days=50)).isoformat('T') + "Z"
        # expiry=(datetime.datetime.now() + datetime.timedelta(days=1))
        # expiry=datetime.datetime.now()
    )
    # print(instrument, takeProfit, stopLoss, trailingStop)
    return test_order
    def start(self):
        self.load_data()
        if (int(self.weekday) == 4 and int(self.now.hour) >= 17):  #Friday 5pm
            print 'market closed...'
            return None

        model = HFmodel(self.vol)
        model.calib(self.get_live_sprd())
        model.calc(self.mid_price, self.q, 0, 1)

        print >> self.f, 'market mid price: ' + str(self.mid_price)
        print >> self.f, 'model reservation price: ' + str(
            model.get_mid_rev_price())
        print >> self.f, 'model bid price: ' + str(model.get_opt_bid(
            self.prec))
        print >> self.f, 'model ask price: ' + str(model.get_opt_ask(
            self.prec))
        print >> self.f, 'gamma: ' + str(model.gamma)
        print >> self.f, 'inventory: ' + str(self.q)
        print >> self.f, 'volatility (5s): ' + str(self.vol)

        try:
            print 'heartbeat(' + self.underlying + ') ' + str(self.now) + '...'
            #close all outstanding orders
            resp_order = self.client.get_orders(instrument=self.underlying)
            for order in resp_order['orders']:
                resp_close_order = self.client.close_order(
                    order_id=order['id'])

            expiry_order = self.now + datetime.timedelta(days=1)
            expiry_order = expiry_order.isoformat('T') + "Z"
            order_ask = Order(
                instrument=self.underlying,
                units=self.set_obj.get_trade_size(),
                side="sell",
                type="limit",
                price=model.get_opt_ask(self.prec),
                expiry=expiry_order,
            )

            order_bid = Order(
                instrument=self.underlying,
                units=self.set_obj.get_trade_size(),
                side="buy",
                type="limit",
                price=model.get_opt_bid(self.prec),
                expiry=expiry_order,
            )
            #place order
            try:
                if self.q >= 1:  #long limit reached
                    resp_order_ask = self.client.create_order(order=order_ask)
                elif self.q <= -1:  #short limit reached
                    resp_order_bid = self.client.create_order(order=order_bid)
                else:
                    resp_order_ask = self.client.create_order(order=order_ask)
                    resp_order_bid = self.client.create_order(order=order_bid)
            except Exception as err:
                print err
                if ('halt' in str(err)) == True:
                    print 'market closed...'
                    return None
                else:
                    print "cannot place order..."

            time.sleep(self.set_obj.get_timer())
        except Exception as error:
            print error
            print self.underlying + ' disconnected, try to reconnect ' + str(
                self.now) + '...'
            self.connect()

        threading.Timer(1, self.start).start()
Exemple #6
0
"""How to create a stop order without pulling out all your hair :-)
"""
import datetime

from pyoanda import Client, SANDBOX, Order

client = Client(environment=SANDBOX)

test_order = Order(instrument="EUR_JPY",
                   units=10,
                   side="buy",
                   type="stop",
                   stopLoss=80.95,
                   takeProfit=170.56,
                   price=10.0,
                   expiry=datetime.datetime.now() + datetime.timedelta(days=1))

stop_order = client.create_order(order=test_order)