`Hi Im trying to automate trades in xAPI. sell trades work fine using :
This is to open a sell trade ie cmd = 1 type = 0.
offset = 50 sl = 50 I get an open trade with a 5 pip trailing stop. This is what I expected.
When trying to make a buy trade is cmd = 0 type = 0 using:
offset = -50 sl = -50 a buy trade opens without a stop or trailing stop.
offset = 50 sl = 50 ERROR invalid sl/tp value.
offset = 1 sl = -50 ERROR invalid trailing stop.
offset = 0 sl = -50 a buy trade opens without a stop or trailing stop.
offset = -1 sl = -50 a buy trade opens without a stop or trailing stop.
offset = -10 sl = -50 a buy trade opens without a stop or trailing stop.
From these attempts it appears to me that sl needs to be negative for the buy trade ie stop loss is below the trade price.
Im fairly sure the problem is with offset but cant make head nor tail of it.
I was expecting the first result attempt to work.`
def open_account():
'''
opens an instanece to the API
'''
# print('open api')
global open_ctr
#open_ctr += 1
host = 'xapia.x-station.eu'
##this is the address for the demo account##'xapia.x- station.eu' real account is 'xapi.xtb.com
port = 5124 ##demo acc 5124 real 5112
USERID = # Account number
PASSWORD = # my password
host = socket.getaddrinfo(host, port)[0][4][0]
global S
global END
context=ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = False
context.load_default_certs()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
S = context.wrap_socket(s, server_hostname=host)
S.connect(('xapia.x-station.eu',5124))
S.settimeout(5)
parameters = {
"command" : "login",
"arguments" : {
"userId": USERID,
"password": PASSWORD
}
}
packet = json.dumps(parameters, indent=4)
S.send(packet.encode("UTF-8"))
END = b'nn'
response=b''
while True:
try:
response += S.recv(8192)
except:
print('67 login fail')
open_account
if END in response:
pass
open_ctr+=1
break
def Close_account():
'''
Closes the open Instance
'''
# print('98 close api')
global open_ctr
# open_ctr -= 1
parameters = {
"command" : "logout"
}
packet = json.dumps(parameters, indent=4)
try:
S.send(packet.encode("UTF-8"))
response = S.recv(8192)
open_ctr-=1
except:
#print('Close account Failed ')
Close_account()
def open_a_trade(symbol, price , volume , cmd, sl, offset, tp=0):
'''
opens the trade
'''
end_date=get_server_time()
order_result.iloc[0:0]
order_result.loc[1,'datetime']=end_date
order_result['symbol']=symbol
order_result['cmd']=cmd
order_result['offset']=offset
order_result['sl']=sl
order_result['tp']=tp
order_result['volume']=volume
order_result['price']=price
open_account()
parameters={
"command": "tradeTransaction",
"arguments": {
"tradeTransInfo": {
"cmd": cmd,
"customComment": '',
"expiration": 0,
"offset": offset,
"order": 0,
"price": price,
"sl": sl,
"symbol": symbol,
"type": 0,
"volume": volume
}
}
}
packet = json.dumps(parameters, indent=4)
S.send(packet.encode("UTF-8"))
response_open_trade = S.recv(328192)
## open trade was timing out , changed open response time in open_account to 5s from 3.5s
order_number_response=json.loads(response_open_trade)
if order_number_response['status']==False:
order_result['order_number_response']=order_number_response['status']
print('427 order result')
print(order_result)
order_result.to_csv('order_result.csv', mode='a', index=False, header=False)
else:
order_result['order_number_response']=order_number_response['returnData']['order']
print('427 order result')
print(order_result)
order_result.to_csv('order_result.csv', mode='a', index=False, header=False)
# print('427 open worked')
Close_account()
# set values for the trade
symbol='GBPUSD'
price=.61801
volume=.2
cmd=0
offset=0
sl=-50
tp=0
print('943 ' , sl)
open_a_trade(symbol, price , volume , cmd, sl, offset, tp)
Does anyone know of any resources that are available that explain the xapi in better detail than the xApi playground ?
Ive tried xtb customer support they dont give any support for the API.
user144131 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.