hootnot / oanda-api-v20

OANDA REST-V20 API wrapper. Easy access to OANDA's REST v20 API with oandapyV20 package. Checkout the Jupyter notebooks!
MIT License
397 stars 107 forks source link

Creating Market Bracket Orders with Pips instead of Price #145

Closed Jammawind closed 1 year ago

Jammawind commented 5 years ago

Hello,

Is there a way to make StopLoss and TakeProfit orders as a pip difference based on the Market order instead of a set price.

The current method only allows you to put in a price for the StopLoss and Take Profit. I would like to be able to use a constant SL and TP for my trades.

Thank you, Artis Johnson

hootnot commented 5 years ago

Hi,

Asuming you use the contrib.request classes: I took a quick look and saw that for TrailingStopLossOrderRequest the distance parameter is available. But not for the classes you mention. I will have a look at it somewhere next days

gkourogiorgas commented 4 years ago

Any updates? I can use distance in stoplossOnFill and set stop loss relative to the opening price of the trade. Is there anything new on the take profit front? What is the workaround?

hootnot commented 4 years ago

it is a few lines of code to calculate things in a way that it can be handled in an order request. But I will leave that to the programmer

gkourogiorgas commented 4 years ago

How about this:

p1={
    "order": {
    "type":"MARKET",
    "timeInForce": "FOK", 
    "instrument": "EUR_USD"
   , "units": "-100",
    "positionFill": "DEFAULT"
    } 
    }
r = orders.OrderCreate(accountID=accountID, data=p1)
rv=client.request(r)

u1={
  "takeProfit": {
    "timeInForce": "GTC",
    "price": str(float(rv['orderFillTransaction']['tradeOpened']['price'])-0.0001)
  },
  "stopLoss": {
    "timeInForce": "GTC",
    "price": str(float(rv['orderFillTransaction']['tradeOpened']['price'])+0.01)
  }
}
r = trades.TradeCRCDO(accountID=accountID,
                                 tradeID=rv['orderFillTransaction']['tradeOpened']['tradeID'],
                                  data=u1)
client.request(r)
hootnot commented 4 years ago

Feel free to program it the way you like. The examples in https://github.com/hootnot/oandapyV20-examples show a clear example in src/contrib_mo_tp_sl.py using the contrib classes:

EUR_USD_STOP_LOSS = 1.05
EUR_USD_TAKE_PROFIT = 1.10

# The orderspecs
mktOrder = MarketOrderRequest(
    instrument="EUR_USD",
    units=10000,
    takeProfitOnFill=TakeProfitDetails(price=EUR_USD_TAKE_PROFIT).data,
    stopLossOnFill=StopLossDetails(price=EUR_USD_STOP_LOSS).data
)
print("Market Order specs: \n{}".format(json.dumps(mktOrder.data, indent=4)))

# create the OrderCreate request
r = orders.OrderCreate(accountID, data=mktOrder.data)
gkourogiorgas commented 4 years ago

I see. Can you use distance for stop loss? In the API doc it doesn't mention distance in TP, unfortunately.

hootnot commented 4 years ago

OANDA doesn't support the distance parameter in a TP, see #147 .

But you can write your own class or subclass TakeProfitDetails class and make it handle the distance the you like it.