junta / tradingview-alert-connector

Integrating TradingView strategy alert and executing automated trading for futures DEXes
https://tv-connector.gitbook.io/docs/
MIT License
76 stars 42 forks source link

dydx v4 trade (intermittently) not executed with/without error #28

Closed layhau closed 5 months ago

layhau commented 6 months ago

Hi, I've been using this with dydx v4 for few weeks now, it looks like it's not as robust / consistent in trade execution, keep experiencing alerts are sent to Render but certain trades are not triggered for some reason, with or without error. Below is an example of error which I managed to get from Render logs but sometimes I don't even see any error but trades just not executed. Wonder if anyone else experiencing same problem as I do?

Recieved GET request. API Key for dYdX is not set as environment variable PERPETUAL_PRIVATE_KEY for Perpetual Protocol is not set as environment variable GMX_PRIVATE_KEY for GMX is not set as environment variable TypeError: Cannot read properties of undefined (reading 'getBalance') at /app/dist/services/gmx/getAccount.js:18:38 at Generator.next () at /app/dist/services/gmx/getAccount.js:8:71 at new Promise () at awaiter (/app/dist/services/gmx/getAccount.js:4:12) at gmxGetAccount (/app/dist/services/gmx/getAccount.js:15:29) at /app/dist/controllers/index.js:30:40 at Generator.next () at /app/dist/controllers/index.js:8:71 at new Promise () at awaiter (/app/dist/controllers/index.js:4:12) at /app/dist/controllers/index.js:25:31 at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) at next (/app/node_modules/express/lib/router/route.js:144:13) at Route.dispatch (/app/node_modules/express/lib/router/route.js:114:3) at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)

junta commented 6 months ago

hey, I've improved the logic to send order for dydx v4 and pushed new codes

I think it's more stable now.

could you deploy the latest version's tool and try again?

layhau commented 6 months ago

sure, will observe for a while and update if problem persist. thanks a lot!

layhau commented 6 months ago

not looking good, looks like when last alert was triggered, it keeps retry and ended up multiple orders (total 5) were placed instead of 1.

Error: Order is not found/filled. Retry again, count: 5 at /app/dist/services/dydx_v4/createOrder.js:44:23 at Generator.next () at fulfilled (/app/dist/services/dydx_v4/createOrder.js:5:58) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

junta commented 6 months ago

hey I've adjusted retry part a bit

could you try again with the latest version of the tool?

layhau commented 6 months ago

just had another alert triggered, apparently order not executed after 2 attempts, error as below:

Error: Order is not found/filled. Retry again, count: 2 at /app/dist/services/dydx_v4/createOrder.js:44:23 at Generator.next () at fulfilled (/app/dist/services/dydx_v4/createOrder.js:5:58) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) Client ID: 1267792284 BroadcastErrorObject: Broadcasting transaction failed: GoodTilBlock 11502623 is less than the current blockHeight 11502625: The next block height is greater than the GoodTilBlock of the message at TendermintClient.broadcastTransactionSync (/app/node_modules/@dydxprotocol/v4-client-js/build/src/clients/modules/tendermintClient.js:69:19) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { result: { code: 10, codespace: 'clob', log: 'GoodTilBlock 11502623 is less than the current blockHeight 11502625: The next block height is greater than the GoodTilBlock of the message', data: Uint8Array(0) [], events: [], gasWanted: 0n, gasUsed: 0n, hash: Uint8Array(32) [ 140, 70, 3, 37, 37, 232, 227, 230, 229, 239, 94, 17, 85, 60, 253, 110, 187, 207, 181, 215, 122, 68, 96, 9, 195, 246, 129, 112, 201, 76, 121, 99 ] }, code: 10, codespace: 'clob' }

BradT7 commented 6 months ago

This is an ongoing issue for me as well. Not running inside docker, just yarn start in terminal

I've modified src/services/dydx_v4/createOrder.ts increasing the count, and increasing the time by 5 second increments before it checks for order completed, but also getting the same error. At least it's not missing the trade entirely now, stopping after the current default of 4 (0,1,2,3) 10 second attempts.

line 34 const maxTries = 8; (from 3) line 56 await _sleep(30000); (from 10000)

This has gotten the error Error: Order is not found/filled. Retry again, count: 0 to not go past 1 so far (or 2 attempts). And it will keep trying even if it takes a couple minutes, although I'm not thrilled about the 30 seconds.

I'm not sure what is safer keeping await_sleep lower and bumping up the maxTries much higher, but my thought was give it more time to fill the original market order. I've noticed the market orders can take longer to fill at times, especially compared to v3.

If I see dYdX v4 Address: and dYdX v4 Order ID: at the bottom of terminal, then I know the trade has gone through.

...
  hash: Uint8Array(32) [
  ...
  ]
}
dYdX v4 Address: dydx1...
Error: Order is not found/filled. Retry again, count: 1
    at /.../.../.../dist/services/dydx_v4/createOrder.js:44:23
    at Generator.next (<anonymous>)
    at fulfilled (/.../.../.../dist/services/dydx_v4/createOrder.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Client ID:  1798343950
Transaction Result:  {
  code: 0,
  codespace: '',
  log: '[]',
  data: Uint8Array(0) [],
  events: [],
  gasWanted: 0n,
  gasUsed: 0n,
  hash: Uint8Array(32) [
     45, 212, 245, 111,  94,  33,  47, 247,
    134,  24,  17, 202,  96, 111, 158,  28,
    200, 193, 251, 116, 157, 139,  83,  66,
    172,  19, 111, 181, 178,   8,   4, 249
  ]
}
dYdX v4 Address: dydx1...
dYdX v4 Order ID:  xxxxxxxx-...

It is annoying to always need to login and check this every time I get an email alert. But at least I've been able to prevent it from missing a trade entirely with these small changes.

I recently found this cool link to help track the trades, without having to login to the server and connect to dydx.trade https://community.chaoslabs.xyz/dydx-v4/risk/accounts/dydx1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/subAccount/0/overview

Not sure if it's possible to record the TV price vs the Fill price like v3 used to do in the csv logs, that was a nice feature to track slippage easier.

Hopefully some additional logic can be built out in time, but for now this has been my work around.

Also, I have gotten that log: 'GoodTilBlock... error when I had a lower await_sleep but I found out the trade still does go through, as long as the maxTries isn't hit, and it shows dYdX v4 Address: dydx1... dYdX v4 Order ID: xxxxxxxx-... at the bottom of terminal.

Cheers, hope this helps.

junta commented 6 months ago

@BradT7 appreciate your input! it helps a lot I've pushed new codes just now(updated dydx v4 js library version, and adjusted placeOrder() parameters.

hope it will help to make it more stable. I'll monitor it on my end

layhau commented 6 months ago

looks like it's completely not working now after latest release update: "[dydx v4]update v4-client-js version, adjust parameters". All i can see from the logs are as follows:

Recieved Tradingview strategy alert: { market: 'ETH_USD', position: 'short', price: '3511.5', exchange: 'dydxv4', size: '1.6338', reverse: false, order: 'sell', strategy: xxxx passphrase: xxxxx } strategyData { reverse: false, isFirstOrder: 'false', position: -44.63519999999969 } latestPrice 3511.5 orderParams for dydx { market: 'ETH-USD', side: 'SELL', size: 1.6338, price: 3511.5 }

junta commented 6 months ago

I've switched to another validator endpoint, and I think it works well now

If you want to switch by yourself, please modify https://github.com/junta/tradingview-alert-connector/blob/main/config/production.yaml#L20