Im using XTB’s api xApi. I need help using ‘offset’ and ‘sl’ when making a trade

`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.`

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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)
</code>
<code> 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) </code>
  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.

New contributor

user144131 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật