Trading Examples
================

Grand Exchange Basics
---------------------

Buy and sell items on the Grand Exchange:

.. code-block:: python

   from artifacts import ArtifactsClient
   
   with ArtifactsClient(token="your_token") as client:
       char = client.character("Trader")
       
       # Move to Grand Exchange
       char.move(x=5, y=1)  # Example: GE location
       
       # Sell items
       result = char.ge_sell(code="copper_ore", quantity=10, price=50)
       print(f"Listed 10 copper ore at 50 gold each")
       
       # Buy items
       result = char.ge_buy(code="iron_ore", quantity=5, price=100)
       print(f"Bought 5 iron ore at 100 gold each")

Checking Market Prices
----------------------

View current market prices:

.. code-block:: python

   from artifacts import ArtifactsClient
   
   with ArtifactsClient(token="your_token") as client:
       # Get all GE items
       ge_items = client.get_all_ge_items()
       
       # Find specific item
       for item in ge_items.data:
           if item.code == "copper_ore":
               print(f"Copper Ore:")
               print(f"  Stock: {item.stock}")
               print(f"  Sell price: {item.sell_price}")
               print(f"  Buy price: {item.buy_price}")
               break

Managing Orders
---------------

View and cancel your active orders:

.. code-block:: python

   from artifacts import ArtifactsClient
   
   with ArtifactsClient(token="your_token") as client:
       char = client.character("Trader")
       
       # Move to GE
       char.move(x=5, y=1)
       
       # Get active sell orders
       orders = char.ge_get_orders()
       
       print("Active sell orders:")
       for order in orders.data:
           print(f"  {order.quantity}x {order.code} @ {order.price} gold")
       
       # Cancel an order if needed
       if orders.data:
           order_id = orders.data[0].id
           char.ge_cancel(order_id=order_id)
           print(f"Cancelled order {order_id}")

Price History
-------------

Track item prices over time:

.. code-block:: python

   from artifacts import ArtifactsClient
   
   with ArtifactsClient(token="your_token") as client:
       # Get GE item with history
       history = client.get_ge_item(code="copper_ore")
       
       print(f"Price history for {history.data.code}:")
       for record in history.data.history:
           print(f"  {record.date}: {record.price} gold")

Automated Trading Bot
---------------------

Buy low, sell high automatically:

.. code-block:: python

   from artifacts import ArtifactsClient, wait_for_cooldown
   import time
   
   with ArtifactsClient(token="your_token") as client:
       char = client.character("AutoTrader")
       
       # Move to GE
       char.move(x=5, y=1)
       time.sleep(5)  # Initial cooldown
       
       items_to_trade = ["copper_ore", "iron_ore", "coal"]
       
       for item_code in items_to_trade:
           # Check current price
           ge_item = client.get_ge_item(code=item_code)
           current_price = ge_item.data.sell_price
           
           # Buy if price is low
           if current_price < 100:  # Your threshold
               print(f"Buying {item_code} at {current_price}")
               result = char.ge_buy(
                   code=item_code, 
                   quantity=10, 
                   price=current_price
               )
               wait_for_cooldown(result.data.cooldown_expiration)
           
           # Sell if you have stock and price is high
           char_data = char.get()
           for slot in char_data.inventory:
               if slot.code == item_code and current_price > 150:
                   print(f"Selling {item_code} at {current_price + 10}")
                   result = char.ge_sell(
                       code=item_code,
                       quantity=slot.quantity,
                       price=current_price + 10
                   )
                   wait_for_cooldown(result.data.cooldown_expiration)
                   break

Bulk Trading
------------

Trade large quantities efficiently:

.. code-block:: python

   from artifacts import AsyncArtifactsClient
   import asyncio
   
   async def bulk_sell(char, items):
       await char.move(x=5, y=1)
       
       for item_code, quantity, price in items:
           result = await char.ge_sell(
               code=item_code,
               quantity=quantity,
               price=price
           )
           print(f"Listed {quantity}x {item_code} at {price} gold")
           
           if result.data.cooldown > 0:
               await asyncio.sleep(result.data.cooldown)
   
   async def main():
       async with AsyncArtifactsClient(token="your_token") as client:
           trader = client.character("BulkTrader")
           
           items_to_sell = [
               ("copper_ore", 100, 50),
               ("iron_ore", 50, 100),
               ("coal", 75, 80)
           ]
           
           await bulk_sell(trader, items_to_sell)
   
   asyncio.run(main())

Market Analysis
---------------

Analyze profitable items:

.. code-block:: python

   from artifacts import ArtifactsClient
   
   with ArtifactsClient(token="your_token") as client:
       # Get all GE items
       ge_items = client.get_all_ge_items()
       
       # Find items with good margins
       profitable = []
       
       for item in ge_items.data:
           if item.buy_price and item.sell_price:
               margin = item.sell_price - item.buy_price
               margin_percent = (margin / item.buy_price) * 100
               
               if margin_percent > 20:  # 20% margin
                   profitable.append({
                       "code": item.code,
                       "margin": margin,
                       "margin_percent": margin_percent,
                       "stock": item.stock
                   })
       
       # Sort by margin percentage
       profitable.sort(key=lambda x: x["margin_percent"], reverse=True)
       
       print("Top profitable items:")
       for item in profitable[:10]:
           print(f"  {item['code']}: {item['margin_percent']:.1f}% "
                 f"({item['margin']} gold, stock: {item['stock']})")

Cross-Character Trading
-----------------------

Transfer items between your characters via bank:

.. code-block:: python

   from artifacts import ArtifactsClient, wait_for_cooldown
   
   with ArtifactsClient(token="your_token") as client:
       gatherer = client.character("Gatherer")
       crafter = client.character("Crafter")
       
       # Gatherer deposits resources
       gatherer.move(x=4, y=1)  # Bank location
       wait_for_cooldown(gatherer.get().cooldown_expiration)
       
       result = gatherer.bank_deposit(code="copper_ore", quantity=50)
       print("Gatherer deposited 50 copper ore")
       
       # Crafter withdraws resources
       crafter.move(x=4, y=1)
       wait_for_cooldown(crafter.get().cooldown_expiration)
       
       result = crafter.bank_withdraw(code="copper_ore", quantity=50)
       print("Crafter withdrew 50 copper ore")
       
       # Crafter can now use the resources for crafting
       crafter.move(x=1, y=1)  # Workshop
       wait_for_cooldown(crafter.get().cooldown_expiration)
