Trading Post API Question

Trading Post API Question

in API Development

Posted by: ScarieXS.2490

ScarieXS.2490

Q:

Hey! I’m a bit new to the API and am mainly just experimenting with different things. The program I am building is essentially an automated accountant that just automatically keeps track of my purchases, sales, and running account value based upon what is in the bank, storage and wallet.

To give you an idea of the experience I have, the parts that are working are essentially the account appraisal parts. It successfully will pull the bank, warehouse, and wallet arrays and then run the relevant IDs to the trading post to get their values (as well as another call for vendor price and item name).

What I am trying to do now is have it watch my buy and sell orders. I know I can get it to simply list these orders, but I would like it to keep a running log of how many were successfully bought, successfully sold, and which orders were canceled.

As an example, if I made the following transactions:

Buy order for 5 of Item A
Sell order for 10 of Item B
Instant Buy of 1 of Item C
Cancel buy order of 3 for Item A
Successfully sold 6 of Item B

It should display the following:

EXPENSES
Bought 2 of Item A for 1g 0s 0c
Bought 1 of Item C for 2g 0s 0c
TAX: 15% of sales for 6 of Item B: 0g 90s 0c (Too lazy to make a fake amount)

INCOME
Sold 6 of Item B: 6g 0s 0c (This amount should not have taxes taken out since that would result in taxes being removed twice)

OUTSTANDING
Sell Order for 4 of Item B

CANCELS
Buy Order for 3 of Item A
———————————

I might just be over thinking this since I have been staring at code for a few hours know. I appreciate any help, and apologize if I have worded things terribly. If you need any clarification, I’ll be happy to explain any part of it in different words.

Thanks for the help!

Trading Post API Question

in API Development

Posted by: Darkashara.4871

Darkashara.4871

What I understand is that you want to know how to call the API to get your buy/sell transactions ?

Trading Post API Question

in API Development

Posted by: Lawton Campbell

Lawton Campbell

Web Programmer

So — the long answer is that the backend component that implements the trading post’s matching engine doesn’t store enough data for you to implement this 100%. You can get most of the way there, but some stuff might slip through the cracks.

Some initial notes:

  • For every order that you create (buy or sell) a single record is made in /v2/commerce/transactions/current/{buys,sells}. It’s assigned a unique id and a creation date.
  • Every time someone else’s order matches with yours, a record is made in /v2/commerce/transactions/history/{buys,sells}. The id of the historical record does not match the id of the your matching order.
  • When someone else’s order matches yours (but does not fully consume it), your current order’s quantity is adjusted. This leaves your original order available to be matched by future orders.
  • The “created” date between current/history will match up.
  • When an order is cancelled, it’s purged from current without leaving a record in history.

An example walkthrough, assuming only two people are interacting with a trading post that starts out empty:

  • You make a buy order for 250 Mithril Ore for 5c/unit. You’ll now have a record in /v2/commerce/transactions/current/buys that looks like:

{ “id” : 1, “created” : “timestamp1”, “item_id” : 19700, “quantity” : 250, “unit_price” : 5 }

You haven’t sold anything yet, so your history will be empty.

  • If you then list a sell order for mithril at 50c/unit (because buy low sell high), your current/buys will be unchanged but you’ll have a record in current/sells:

{ “id” : 2, “created” : “timestamp2”, “item_id” : 19700, “quantity” : 99, “unit_price” : 50 }

  • Then if someone comes along and purchases 1 of your mithril ore @ 50c, the current/sells entry will be amended to reflect the updated quantity, and you’ll have a record in history/sells noting that there was a purchase:

current/sells:
{ “id” : 2, “created” : “timestamp2”, “item_id” : 19700, “quantity” : 98, “unit_price” : 50 }

history/sells:
{ “id” : 3, “created” : “timestamp2”, “purchased” : “timestamp3”, “item_id” : 19700, “quantity” : 1, “unit_price” : 50 }

It should be noted here that the “created” timestamp is the same between the current order and historical order — but not necessarily unique. It should be enough to correlate between the two using item_id/unit_price, noting that you may have multiple listings for the same item_id/unit_price (which can safely be aggregated on your end). You’re going to lose track of some paid exchange fees one way or another.

  • If someone makes another purchase of your sale listing, a new history/sell record is made:

current/sells
{ “id” : 2, “created” : “timestamp2”, “item_id” : 19700, “quantity” : 93, “unit_price” : 50 }

history/sells:
{ “id” : 3, “created” : “timestamp2”, “purchased” : “timestamp3”, “item_id” : 19700, “quantity” : 1, “unit_price” : 50 }
{ “id” : 4, “created” : “timestamp2”, “purchased” : “timestamp4”, “item_id” : 19700, “quantity” : 5, “unit_price” : 50 }

The entry in current/sells will be removed once either (A) you cancel the order, or (B) the quantity reaches zero. Determining which happened requires you to store the old version of current/sells locally, notice that the record no longer exists, then search history/sells for matching transaction that would matches it. More tersely, you basically have to reconcile the current transactions with the historical logs on your end.

Anyway, I realize this is kind of a ramble and probably doesn’t help much, but it’s a rough overview of what the API is exposing and how you can get actual data out of it. You’ll have to ask a more specific question if you want a more specific answer, I’m afraid.