peterszombati / xapi-node

xStation5 Trading API for NodeJS/JS
https://peterszombati.github.io/xapi-node/
Other
58 stars 19 forks source link

how to adjust stop loss on open position #5

Closed bda2206 closed 4 years ago

bda2206 commented 4 years ago

Hi,

not exactly an issue, but I can't figure out how to adjust a stop loss on a position.

example greatly appreciated.

thanks

bda2206 commented 4 years ago

thanks for fast response! just say the market goes up, and i want a new higher stop loss on my existing position?

peterszombati commented 4 years ago

Hi,

on new position (limit or market execution will be the same) you have to set "sl" parameter to the exact price like 1.07 for EURUSD or something like that.

x.Socket.send.tradeTransaction({
    cmd: CMD_FIELD.BUY_LIMIT,
    customComment: null,
    expiration: new Date().getTime() + 60000 * 60 * 24 * 365,
    offset: 0,
    order: 0,
    price: 21900,
    sl: 21000, // Here for example
    symbol: 'US30',
    tp: 26500,
    type: TYPE_FIELD.OPEN,// for Modifying this will be => "TYPE_FIELD.MODIFY"
    volume: 0.2
}).then(({order}) => {
    console.log('Success ' + order);
}).catch(e => {
    console.error('Failed');
    console.error(e);
});

for modifying will be the same but you have to change "order" parameter to order id.

You can find this function docs in WS how it works: https://peterszombati.github.io/xapi-node/#tradeTransaction

bda2206 commented 4 years ago

oh ok. just set the orderId and the rest as is.. I'll try that. thanks heaps :)

peterszombati commented 4 years ago

@bda2206

in 2.3.1 I made simplier trade modifying you can try it.


x.Socket.send.tradeTransaction({
    order: 1234, // order / position number you can find it in (x.positions)
    type:TYPE_FIELD.MODIFY,
    sl:1.0 // add only what you want to modify
})
bda2206 commented 4 years ago

looks perfect, thanks heaps.

bda2206 commented 4 years ago

Hi Peter,

tried that, and a few other combinations. Getting an error..

reason: { code: 'EX000', explain: 'Invalid parameters' }, request: { json: '{"command":"tradeTransaction","arguments":{"tradeTransInfo":{"customComment":"x159049037252400050","expiration":1590490377524,"offset":0,"order":202739945,"price":0,"sl":1.0937,"tp":0,"type":3, "volume":0.01}},"customTag":"tradeTransaction_159049037252400050"}',

thanks in advance.

On Tue, May 26, 2020 at 12:23 PM Peter Szombati notifications@github.com wrote:

@bda2206 https://github.com/bda2206

in 2.3.1 I made simplier trade modifying you can try it.

x.Socket.send.tradeTransaction({ order: 1234, // order / position number you can find it in (x.positions) type:TYPE_FIELD.MODIFY, sl:1.0 // add only what you want to modify})```

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/peterszombati/xapi-node/issues/5#issuecomment-633774475, or unsubscribe https://github.com/notifications/unsubscribe-auth/APW7YMEBK7JNOPTWHFNYHTLRTMR3JANCNFSM4NJ25Q5Q .

peterszombati commented 4 years ago

Hi @bda2206

problem will be "price" : 0 did you set it for it or module when added missing parameters? are you sure did you use 2.3.1?

bda2206 commented 4 years ago

I added it to try and fix. Also fails without it.

On Tue, 26 May 2020, 21:13 Peter Szombati, notifications@github.com wrote:

Hi @bda2206 https://github.com/bda2206

problem will be "price" : 0 did you set it for it or module when added missing parameters?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/peterszombati/xapi-node/issues/5#issuecomment-633961212, or unsubscribe https://github.com/notifications/unsubscribe-auth/APW7YMGS3LV3PYAEMQYYPN3RTOP43ANCNFSM4NJ25Q5Q .

peterszombati commented 4 years ago

@bda2206 I added two log error in 2.3.2 which can describe if it not adding missing parameters why this do it

                if (type === TYPE_FIELD.MODIFY && position === undefined) {
                    if (!this.XAPI.isSubscribeTrades) {
                        Log.error('type === MODIFY in tradeTransaction will not work with missing parameters and subscribeTrades = false, you should set subscribeTrades = true in login config');
                    } else {
                        Log.error('type === MODIFY in tradeTransaction orderId = ' + order + ' not found, possible orderIds: ' + this.XAPI.positions.map(p => p.position).join(','))
                    }
                }
bda2206 commented 4 years ago

MY auth now looks like this

new XAPI({ accountId: accountNumber, password, host: "ws.xtb.com", // only for XTB accounts type: accountType, // or demo subscribeTrades: true });

the position opened looks like this This is the type "0" = OPEN stream response. (why does it have a close price?) EURUSD: { cmd: 0, order: 202750168, digits: 5, offset: 0, order2: 202750252, position: 202750168, symbol: 'EURUSD', comment: '', customComment: 'x159049393292600040_Order with no Take Profit', commission: 0, storage: 0, margin_rate: 0,

{ "reason": { "code": "EX000", "explain": "Invalid parameters" }, "transaction": { "command": "tradeTransaction", "type": "Socket", "request": { "json": "{\"command\":\"tradeTransaction\",\"arguments\":{\"tradeTransInfo\":{\"customComment\":\"x159049438377100740\",\"order\":202750168,\"sl\":1.0937,\"type\":3,\"volume\":0.01}},\"customTag\":\"t radeTransaction_159049438377100740\"}", "arguments": { "tradeTransInfo": { "customComment": "x159049438377100740", "order": 202750168, "sl": 1.0937, "type": 3, "volume": 0.01 } }, "sent": { "unit": [ 894471, 922910300 ], "UTCTimestamp": 1590494383771 } }, "response": { "status": false, "received": { "unit": [ 894472, 245459700 ], "UTCTimestamp": 1590494384094 }, "json": { "code": "EX000", "explain": "Invalid parameters" } }, "transactionId": "159049438377100740", "createdAt": { "unit": [ 894471, 922891500 ], "UTCTimestamp": 1590494383771 }, "status": 3, "transactionPromise": { "resolve": null, "reject": null }, "urgent": true } }

regards

Brian

On Tue, May 26, 2020 at 9:36 PM Peter Szombati notifications@github.com wrote:

@bda2206 https://github.com/bda2206 I added two log error in 2.3.2 which can describe if it not adding missing parameters why this do it

            if (type === TYPE_FIELD.MODIFY && position === undefined) {
                if (!this.XAPI.isSubscribeTrades) {
                    Log.error('type === MODIFY in tradeTransaction will not work with missing parameters and subscribeTrades = false, you should set subscribeTrades = true in login config');
                } else {
                    Log.error('type === MODIFY in tradeTransaction orderId = ' + order + ' not found, possible orderIds: ' + this.XAPI.positions.map(p => p.position).join(','))
                }
            }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/peterszombati/xapi-node/issues/5#issuecomment-633970592, or unsubscribe https://github.com/notifications/unsubscribe-auth/APW7YMH54DND3ODOHLSFRATRTOSSXANCNFSM4NJ25Q5Q .

peterszombati commented 4 years ago

@bda2206 Try update to 2.3.3 with npm install xapi-node and the problem is tradeTransaction request params required all params cmd, price, etc but xapi-node try to fill all missing params if it can't do it will be throw an Error and why it is not possible (in 2.3.3)

bda2206 commented 4 years ago

Hi Peter,

yes, no crash but now it submits another buy.

might do a trail offset [image: image.png]

On Tue, May 26, 2020 at 11:10 PM Peter Szombati notifications@github.com wrote:

@bda2206 https://github.com/bda2206 Try update to 2.3.3 with npm install xapi-node and the problem is tradeTransaction request params required all params cmd, price, etc but xapi-node try to fill all missing params if it can't it will be throw an Error

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/peterszombati/xapi-node/issues/5#issuecomment-634014648, or unsubscribe https://github.com/notifications/unsubscribe-auth/APW7YMFTBLI5F37ZGJW3TU3RTO5T7ANCNFSM4NJ25Q5Q .

peterszombati commented 4 years ago

I dont see your image

bda2206 commented 4 years ago

it was just a list of open positions in the trader interface.

I've got a trailing stop going, which is near enough for my purposes.

regards

On Wed, May 27, 2020 at 4:59 AM Peter Szombati notifications@github.com wrote:

I dont see your image

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/peterszombati/xapi-node/issues/5#issuecomment-634214735, or unsubscribe https://github.com/notifications/unsubscribe-auth/APW7YMFUQHZ7DSVZMN5KSQ3RTQGRTANCNFSM4NJ25Q5Q .