Closed wybren84 closed 6 years ago
Hi @wybren84 ,
Without code it is a bit guessing. Regarding your last line: that is true, if you issue an order and it gets filled, you have a trade leading to a position. If you were neutral your position will be equal to the number of units you gave in your order. Now if you issue another order in the same instrument, on the same side (again long if the first was long, or short if the first was short), you get a position that has doubled. And you find the two tradeID's belonging to that position if you request the position details.
If you want to have AND long AND short simultanously in the same instrument, you need to have hedging enabled. I think that is what you look for.
Thank you for your quick response, @hootnot. How do I enable hedging? And is it useful to do?
I used the next code. The second time I changed units to 10 and changed the stop loss and take profit.
import json
from oandapyV20.contrib.requests import (
MarketOrderRequest,
TakeProfitDetails,
StopLossDetails
)
import oandapyV20.endpoints.orders as orders
import oandapyV20
from exampleauth import exampleAuth
accountID, access_token = exampleAuth()
api = oandapyV20.API(access_token=access_token)
# EUR_USD (today 1.175)
EUR_USD_STOP_LOSS = 1.16
EUR_USD_TAKE_PROFIT = 1.21
# The orderspecs
mktOrder = MarketOrderRequest(
instrument="EUR_USD",
units=-10,
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)
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))
Using this code you issue a short order for 10 units ending up with a position of -10. Now running the same once again you would have two trades that have lead to a position of -20. If you attached the TP and SL orders as in the code, then these will apply for both halfs of the position.
When issueing a long order of 10 the position would reduce to -10. So you still have 1 of the shorts that is open and with the SL and TP attached.
Issueing a long order of 20 after that you end up with a position being 10 long. Your short related TP and SL are cancelled.
Maybe you should play with this in a jupyter notebook. After placing an order and it gets filled you can ask for transactions, positions and open orders that were triggered by the FILL. You can place your comments in the notebook and things will fall in place. For instance how reducing is handled on a position. You can check on how it handles -10, -10, +5, +20 and see how handles the reducing and TP and SL orders.
If you want hedging or not ? You should figure that out for yourself. I don't use it myself. But OANDA added it to the V20, so there probably are traders using strategies with hedging.
Thanks again, @hootnot! I will keep playing with the code. One final question: how do I enable hedging?
I took a look at: https://www.oanda.com/forex-trading/platform/v20-hedging, on practice accounts it is not available currently
I've changed the example "conrtib_mo_tp_sl.py" a little bit to take a short position on EUR_DOL by changing the units to a negative value (e.g. -10 units). If I take a long position of 10 units EUR_DOL subsequently I end up ending the short position. How do I get a separate trade for that long position instead of modifying the short position taken earlier?
However, if I take a short position and after that another short position, I get two separate trades. Thanks in advance for your response!