algodex / trading-bot

https://algodex-trading-bot.vercel.app
GNU Affero General Public License v3.0
2 stars 2 forks source link

Feature: Arbitrage support #159

Open ericsharma opened 1 year ago

ericsharma commented 1 year ago

ℹ Overview

When the price of an asset on tinyman is different than the bid/ask spread on the algodex orderbook, maker orders are placed when taker execution should occur.

Example

USDC/Algo orderbook before running the bot

Screen Shot 2022-11-17 at 10 57 54 AM

Ask: 0.89 algo Bid: 0.83 algo

USDC/Algo bot settings

Screen Shot 2022-11-17 at 11 04 16 AM

Tinyman price: 1.3766 algo

USDC/Algo orderbook after running the bot

Screen Shot 2022-11-17 at 11 05 45 AM

Summary of problem

The bot adds a buy order at a price higher than an existing sell order in our orderbook which breaks the expected behavior of the orderbook.

Proposed solution

Fetch the algodex orderbook for the targret asset and prevent the bot from running when the above conditions are met.

The above behavior is an arbitrage opportunity

We can have a general message template that alerts the user of the pricing mismatch and how best to exploit it. So given the example above, instead of placing the order we would alert the user with a message:

You can currently buy USDC for 0.89 algo on Algodex and you can sell USDC for 1.3766 algo on tinyman. Our liquidity bot can only operate when the market price is in equilibrium

atrefonas commented 1 year ago

Hi Eric,

The bot should just buy the incorrectly priced orders.

The bot should still run even if other users place orders that are out of line with the rest of the market. The bot should only be creating new maker orders based on Tinyman prices and exploiting arbitrage opportunities otherwise as taker orders.

It’s fine to release the v1 of the bot without arbitrage support, although would be better to have it.

On Thu, Nov 17, 2022 at 11:25 AM Eric Sharma @.***> wrote:

ℹ Overview

When the price of an asset on tinyman is different than the bid/ask spread on the algodex orderbook, maker orders are placed when taker execution should occur. Example USDC/Algo orderbook before running the bot

[image: Screen Shot 2022-11-17 at 10 57 54 AM] https://user-images.githubusercontent.com/26069470/202495276-ad8dce38-fd8e-461e-a220-2e5b0b945478.png Ask: 0.89 algo Bid: 0.83 algo USDC/Algo bot settings

[image: Screen Shot 2022-11-17 at 11 04 16 AM] https://user-images.githubusercontent.com/26069470/202496761-80f7e7c5-0894-45b5-b1ee-7f35d139c460.png Tinyman price: 1.3766 algo USDC/Algo orderbook after running the bot

[image: Screen Shot 2022-11-17 at 11 05 45 AM] https://user-images.githubusercontent.com/26069470/202497989-e13d0b44-f530-4715-ab0a-d2ba038ccad4.png

The bot adds a buy order at a price higher than an existing sell order in our orderbook which breaks the expected behavior of the orderbook. Proposed solution

Fetch the algodex orderbook for the targret asset and prevent the bot from running when the above conditions are met. The above behavior is an arbitrage opportunity

We can have a general message template that alerts the user of the pricing mismatch and how best to exploit it. So given the example above, instead of placing the order we would alert the user with a message:

You can currently buy USDC for 0.89 algo on Algodex and you can sell USDC for 1.3766 algo on tinman. Our liquidity bot can only operate when the market price is in equilibrium

— Reply to this email directly, view it on GitHub https://github.com/algodex/trading-bot/issues/159, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARL6JW5SKXFWOXFYIB55SDTWIZMBPANCNFSM6AAAAAASDR6W2U . You are receiving this because you are subscribed to this thread.Message ID: @.***>

atrefonas commented 1 year ago

This also isn’t a problem on Mainnet since other people actively exploit arb opportunities.

On Thu, Nov 17, 2022 at 11:28 AM Alexander Trefonas @.***> wrote:

Hi Eric,

The bot should just buy the incorrectly priced orders.

The bot should still run even if other users place orders that are out of line with the rest of the market. The bot should only be creating new maker orders based on Tinyman prices and exploiting arbitrage opportunities otherwise as taker orders.

It’s fine to release the v1 of the bot without arbitrage support, although would be better to have it.

On Thu, Nov 17, 2022 at 11:25 AM Eric Sharma @.***> wrote:

ℹ Overview

When the price of an asset on tinyman is different than the bid/ask spread on the algodex orderbook, maker orders are placed when taker execution should occur. Example USDC/Algo orderbook before running the bot

[image: Screen Shot 2022-11-17 at 10 57 54 AM] https://user-images.githubusercontent.com/26069470/202495276-ad8dce38-fd8e-461e-a220-2e5b0b945478.png Ask: 0.89 algo Bid: 0.83 algo USDC/Algo bot settings

[image: Screen Shot 2022-11-17 at 11 04 16 AM] https://user-images.githubusercontent.com/26069470/202496761-80f7e7c5-0894-45b5-b1ee-7f35d139c460.png Tinyman price: 1.3766 algo USDC/Algo orderbook after running the bot

[image: Screen Shot 2022-11-17 at 11 05 45 AM] https://user-images.githubusercontent.com/26069470/202497989-e13d0b44-f530-4715-ab0a-d2ba038ccad4.png

The bot adds a buy order at a price higher than an existing sell order in our orderbook which breaks the expected behavior of the orderbook. Proposed solution

Fetch the algodex orderbook for the targret asset and prevent the bot from running when the above conditions are met. The above behavior is an arbitrage opportunity

We can have a general message template that alerts the user of the pricing mismatch and how best to exploit it. So given the example above, instead of placing the order we would alert the user with a message:

You can currently buy USDC for 0.89 algo on Algodex and you can sell USDC for 1.3766 algo on tinman. Our liquidity bot can only operate when the market price is in equilibrium

— Reply to this email directly, view it on GitHub https://github.com/algodex/trading-bot/issues/159, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARL6JW5SKXFWOXFYIB55SDTWIZMBPANCNFSM6AAAAAASDR6W2U . You are receiving this because you are subscribed to this thread.Message ID: @.***>

ericsharma commented 1 year ago

Agreed that these types of price discrepancies are much rarer on main-net and going back to your point about exploiting arbitrage opportunities as taker orders: In the above example, you are saying that instead of placing the algo buy order ithe bot should have executed on the cheaper sell order right?

atrefonas commented 1 year ago

Yes ideally - it could actually be as simple as changing it from creating a “maker” order to a “both” order actually. But I didn’t test that originally a few months back since I wanted to just get something working quickly.

On Thu, Nov 17, 2022 at 11:34 AM Eric Sharma @.***> wrote:

Agreed that these types of price discrepancies are much rarer on main-net and going back to your point about exploiting arbitrage opportunities as taker orders: In the above example, you are saying that instead of placing the algo buy order ithe bot should have executed on the cheaper sell order right?

— Reply to this email directly, view it on GitHub https://github.com/algodex/trading-bot/issues/159#issuecomment-1318900406, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARL6JW6W2BDH4NE7QMRV223WIZNCHANCNFSM6AAAAAASDR6W2U . You are receiving this because you commented.Message ID: @.***>

ericsharma commented 1 year ago

I tested it with execution of both instead of maker and it does execute on the better orderbook deal but then another issue arises:

atrefonas commented 1 year ago

I think let's just save this for the V2. Like I mentioned this whole thing isn't a real issue on Mainnet because other people are running their own bots that actively do arbitrage.

On Thu, Nov 17, 2022 at 11:41 AM Eric Sharma @.***> wrote:

I tested it with execution of both instead of masker and it does execute on the better orderbook deal but then another issue arises:

  • The bot places both legs of the trade using both execution
  • one leg executes while the other stays open
  • the bot see's that only one leg is open so it replaces the missing leg at the current tinyman price
  • the cycle repeats. As a consequence, the user will end up incrementally buying the open order on the favorable leg which will probably be surprising without some type of message

— Reply to this email directly, view it on GitHub https://github.com/algodex/trading-bot/issues/159#issuecomment-1318909575, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARL6JW7WZPGIAFXR33YFM23WIZN5LANCNFSM6AAAAAASDR6W2U . You are receiving this because you commented.Message ID: @.***>