Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phemex: short trade not recognizable (fetchMyTrades) - sides switched #22512

Closed
egabosh opened this issue May 16, 2024 · 13 comments
Closed

Phemex: short trade not recognizable (fetchMyTrades) - sides switched #22512

egabosh opened this issue May 16, 2024 · 13 comments
Assignees
Labels

Comments

@egabosh
Copy link

egabosh commented May 16, 2024

Operating System

Debian 12

Programming Languages

Python

CCXT Version

4.3.23

Description

Moin,

a “short” trade via fetchMyTrades() is not recognizable for me in the CCXT prepared output.
Compared to “long” trades, only the “side” seems to be reversed.
For a long buy the side is “buy” and for a short buy the side is “sell”
This may be logical if it is a short trade, as the bet is on falling prices
However, it is not obvious that it is a short trade.
In the output from Phemex, a short or long trade seems to be recognizable by the “posSide”. (if posSide is 1= Long-Trade - if posSide is 2 = Short Trade).
However, this information does not appear to be evaluated by CCXT but only side.

Long buy (CCXT-side buy - Phemex: "side": "1"; "posSide": "1")

    "info": {
      "createdAt": "1714497232243",
      "symbol": "BTCUSDT",
      "currency": "USDT",
      "action": "1",
      "tradeType": "1",
      "execQtyRq": "0.003",
      "execPriceRp": "60200",
      "side": "1",
      "orderQtyRq": "0.003",
      "priceRp": "60200",
      "execValueRv": "180.6",
      "feeRateRr": "0.000162",
      "execFeeRv": "0",
      "ordType": "2",
      "execId": "XXXXXX",
      "execStatus": "6",
      "posSide": "1",
      "ptFeeRv": "0.03496779455",
      "ptPriceRp": "0.836689885"
    },
    "id": "XXXXXX",
    "symbol": "BTC/USDT:USDT",
    "timestamp": 1714497232243,
    "datetime": "2024-04-30T17:13:52.243Z",
    "order": null,
    "type": "limit",
    "side": "buy",
    "takerOrMaker": null,
    "price": 60200,
    "amount": 0.003,
    "cost": 180.6,
    "fee": {
      "cost": 0.03496779455,
      "rate": 0.000162,
      "currency": "PT"
    },
    "fees": [
      {
        "cost": 0.03496779455,
        "rate": 0.000162,
        "currency": "PT"
      }
    ]
  },

Short buy (CCXT-side sell - Phemex: "side": "2"; "posSide": "2")

    "info": {
      "createdAt": "1715810227351",
      "symbol": "BTCUSDT",
      "currency": "USDT",
      "action": "1",
      "tradeType": "1",
      "execQtyRq": "0.01",
      "execPriceRp": "65910.5",
      "side": "2",
      "orderQtyRq": "0.01",
      "priceRp": "63933.2",
      "execValueRv": "659.105",
      "feeRateRr": "0.00055",
      "execFeeRv": "0.36250775",
      "ordType": "1",
      "execId": "XXXXXX",
      "execStatus": "7",
      "posSide": "2",
      "ptFeeRv": "0",
      "ptPriceRp": "0"
    },
    "id": "XXXXXX",
    "symbol": "BTC/USDT:USDT",
    "timestamp": 1715810227351,
    "datetime": "2024-05-15T21:57:07.351Z",
    "order": null,
    "type": "market",
    "side": "sell",
    "takerOrMaker": null,
    "price": 65910.5,
    "amount": 0.01,
    "cost": 659.105,
    "fee": {
      "cost": 0.36250775,
      "rate": 0.00055,
      "currency": "USDT"
    },
    "fees": [
      {
        "cost": 0.36250775,
        "rate": 0.00055,
        "currency": "USDT"
      }
    ]
  },

Long sell (CCXT-side sell - Phemex: "side": "2"; "posSide": "1")

    "info": {
      "createdAt": "1714990724076",
      "symbol": "BTCUSDT",
      "currency": "USDT",
      "action": "1",
      "tradeType": "1",
      "execQtyRq": "0.003",
      "execPriceRp": "64935",
      "side": "2",
      "orderQtyRq": "0.003",
      "priceRp": "51600",
      "execValueRv": "194.805",
      "feeRateRr": "0.000495",
      "execFeeRv": "0",
      "ordType": "3",
      "execId": "XXXXXX",
      "execStatus": "7",
      "posSide": "1",
      "ptFeeRv": "0.110012249248",
      "ptPriceRp": "0.876524893"
    },
    "id": "XXXXXX",
    "symbol": "BTC/USDT:USDT",
    "timestamp": 1714990724076,
    "datetime": "2024-05-06T10:18:44.076Z",
    "order": null,
    "type": null,
    "side": "sell",
    "takerOrMaker": null,
    "price": 64935,
    "amount": 0.003,
    "cost": 194.805,
    "fee": {
      "cost": 0.110012249248,
      "rate": 0.000495,
      "currency": "PT"
    },
    "fees": [
      {
        "cost": 0.110012249248,
        "rate": 0.000495,
        "currency": "PT"
      }
    ]
  },

Short sell (CCXT-side buy - Phemex: "side": "1"; "posSide": "2")

    "info": {
      "createdAt": "1715818144091",
      "symbol": "BTCUSDT",
      "currency": "USDT",
      "action": "1",
      "tradeType": "1",
      "execQtyRq": "0.01",
      "execPriceRp": "66573.7",
      "side": "1",
      "orderQtyRq": "0.01",
      "priceRp": "75326.2",
      "execValueRv": "665.737",
      "feeRateRr": "0.00055",
      "execFeeRv": "0.36615535",
      "ordType": "3",
      "execId": "XXXXXX",
      "execStatus": "7",
      "posSide": "2",
      "ptFeeRv": "0",
      "ptPriceRp": "0"
    },
    "id": "XXXXXX",
    "symbol": "BTC/USDT:USDT",
    "timestamp": 1715818144091,
    "datetime": "2024-05-16T00:09:04.091Z",
    "order": null,
    "type": null,
    "side": "buy",
    "takerOrMaker": null,
    "price": 66573.7,
    "amount": 0.01,
    "cost": 665.737,
    "fee": {
      "cost": 0.36615535,
      "rate": 0.00055,
      "currency": "USDT"
    },
    "fees": [
      {
        "cost": 0.36615535,
        "rate": 0.00055,
        "currency": "USDT"
      }
    ]
  }

So for short trades posSide always seems to be 2 and for long trades 1.
Please provide a fix that takes into account whether it is a short or long trade by evaluating posSide from Phemex.
As it currently stands, you get the wrong results if you only pay attention to the CCXT processed output.

Thank you very much

Code

print(exchange.fetchMyTrades(symbol='BTC/USDT:USDT', limit=500, params={'paginate': False}))
@carlosmiei carlosmiei self-assigned this May 16, 2024
@carlosmiei
Copy link
Collaborator

Hello @egabosh,
Thanks for reporting it.

Just to make sure I understood, are you saying that if you made a buy that impacted the short position (reducing it for instance), then the trade should contain sell as the side?

@egabosh
Copy link
Author

egabosh commented May 16, 2024

Moin @carlosmiei,

not quite. Sorry, I'll try to describe it again in more detail:

If I invest in a short position (buy), then Phemex sets “side”: “2”, which is interpreted by CCXT as “sell”.
However, Phemex also sets the variable “posSide”: “2”, which is set to 1 for a long trade.

Short is basically an inverse long. This therefore makes sense in terms of Phemex logic.

This does not seem to be taken into account in the CCXT processed output.
Therefore, in my opinion, it would be necessary in CCXT either to specify whether it is a long or short trade, which as far as I know is evident from “posSide”, or to switch sides in the case of a short trade.
Depending on what fits better in CCXT.

It would only be important for me to be able to recognize in the CCXT output whether it is a short or long trade, as otherwise misinterpretations would occur.

So currently the output of CCXT is incorrect because it is incomplete.

@carlosmiei
Copy link
Collaborator

@egabosh This is confusing me a bit

If I invest in a short position (buy)

Upon trading futures, we don't say we bought a short, we say you sold/shorted X amount of the Y contract, so usually we don't mix buy+short terminology

@carlosmiei
Copy link
Collaborator

The posSide in that case can be used for inferring if the trade increased/reduced the exposure of the position, but the side should still be sell

@egabosh
Copy link
Author

egabosh commented May 16, 2024

The posSide in that case can be used for inferring if the trade increased/reduced the exposure of the position, but the side should still be sell

Yes, exactly. It would only be important to know whether the trade is a short or a long trade.
fetchMyTrades() outputs all short and long trades and it is not clear from the CCXT output whether the trade in question is a short or long position.
This information is only available in the variable posSide (not to be confused with Side) provided by Phemex.

@carlosmiei
Copy link
Collaborator

@egabosh I'm afraid that information does not belong to trade, a trade is a execution report and should state if that particular trade bought or sold some X amount of contracts, regardless of the position impacted.

@egabosh
Copy link
Author

egabosh commented May 16, 2024

Without this information, I cannot recognize from the CCXT output whether I have opened a short or long position in the trade.
So whether I clicked on “Open Long” or “Open Short”.
See screenshot:
ksnip_20240516-123910
In the json output of phemex this is indicated by posSide.
So I need this information about the CCXT output.

@carlosmiei
Copy link
Collaborator

@egabosh That information should be available as you intend inside the Order structure, but not in the Trade

@egabosh
Copy link
Author

egabosh commented May 16, 2024

@carlosmiei OK. but then should the the side in CCXT be switched from "buy" to "sell" and "sell" to "buy" if Phemex posSide is 2 I think.

@carlosmiei
Copy link
Collaborator

@egabosh Now that I'm thinking, not sure if even in Order we should apply that logic, because one buy order can be used to shrink a short position and one sell order to shrink a buy but the order was indeed a buy/sell. I think you would have to use the Position + Order to determine the impact the order had on your current position.

Besides that, upon placing the order, depending on the positionMode you know in advance if the order will impact the short/long leg, if you're in the one-way mode you can use things like reduceOnly to ensure that the order produces the desired effect.

@egabosh
Copy link
Author

egabosh commented May 16, 2024

@carlosmiei Ah. I'll give it some more thought but the more I think about it, I think you're right.
Sorry for the confusion.

@carlosmiei
Copy link
Collaborator

@egabosh No problem let us know if anything!

@egabosh
Copy link
Author

egabosh commented May 16, 2024

@egabosh That information should be available as you intend inside the Order structure, but not in the Trade

@carlosmiei: Didn't find the Information in any order got by fetch_orders() interpreted by CCXT too (only information is posSide from Phemex. Example:

    "info": {
      "createdAt": "1715818144087",
      "symbol": "BTCUSDT",
      "orderQtyRq": "0.01",
      "side": "1",
      "posSide": "2",
      "priceRp": "75326.2",
      "execQtyRq": "0.01",
      "leavesQtyRq": "0",
      "execPriceRp": "66573.7",
      "orderValueRv": "665.737",
      "leavesValueRv": "0",
      "cumValueRv": "665.737",
      "stopDirection": "1",
      "stopPxRp": "66500",
      "trigger": "1",
      "actionBy": "4",
      "execFeeRv": "0.36615535",
      "ordType": "3",
      "ordStatus": "7",
      "clOrdId": "XXXXXXXXXXXXXXXX",
      "orderId": "XXXXXXXXXXXXXXXX",
      "execStatus": "7",
      "bizError": "0",
      "totalPnlRv": null,
      "avgTransactPriceRp": null,
      "orderDetailsVos": null,
      "tradeType": "1",
      "updatedAt": "1715818144091"
    },
    "id": "XXXXXXXXXXXXXXXX",
    "clientOrderId": "XXXXXXXXXXXXXXXX",
    "datetime": "2024-05-16T00:09:04.087Z",
    "timestamp": 1715818144087,
    "lastTradeTimestamp": null,
    "symbol": "BTC/USDT:USDT",
    "type": null,
    "timeInForce": null,
    "postOnly": false,
    "reduceOnly": null,
    "side": "buy",
    "price": 75326.2,
    "stopPrice": 66500,
    "triggerPrice": 66500,
    "takeProfitPrice": null,
    "stopLossPrice": null,
    "amount": 0.01,
    "filled": 0.01,
    "remaining": 0,
    "cost": 665.737,
    "average": 66573.7,
    "status": "closed",
    "fee": null,
    "trades": [],
    "fees": [],
    "lastUpdateTimestamp": null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants