Automated Spot Price Adjustment for Cost Optimization
Objective: This example demonstrates how to automate the adjustment of a server's spot price to stay competitive on the marketplace. By continuously monitoring other offers and adjusting the spot price within specified boundaries, users can maximize rental opportunities and revenue.
Step 1: Retrieve Spot Marketplace Data for a Specific Server
The first step is to get the current offers in the spot marketplace for a specific server ID, allowing us to assess the price competition.
Step 2: Analyze Competing Offers and Set Target Price
Here, we will analyze the current spot offers, then define a strategy to set our target price below the lowest existing bid, ensuring it remains competitive.
# Set price adjustment parameters
min_price_btc = 0.000001 # Minimum price allowed
max_price_btc = 0.00001 # Maximum price allowed
price_adjustment_step = 0.0000005 # Adjustment increment
# Determine the lowest active bid price
lowest_bid = min((offer['bid'] for offer in offers if offer['active']), default=max_price_btc)
# Calculate target price to underbid the lowest offer by a small increment
target_price = max(lowest_bid - price_adjustment_step, min_price_btc)
target_price = min(target_price, max_price_btc) # Ensure it doesn't exceed max allowed price
print(f"Setting target spot price to {target_price} BTC.")
Step 3: Adjust the Server's Spot Price Based on Target
Using the set_spot_price endpoint, the code will attempt to adjust the spot price to the target value if it's different from the current spot price.
# Adjust server's spot price to the target value
adjust_price_url = 'https://api.clore.ai/v1/set_spot_price'
adjust_data = {
"order_id": server_id,
"desired_price": target_price
}
adjust_response = requests.post(adjust_price_url, headers=headers, json=adjust_data)
if adjust_response.status_code == 200:
print(f"Spot price adjusted to {target_price} BTC.")
else:
print(f"Failed to adjust price: {adjust_response.json().get('error')}")
Step 4: Automate Price Monitoring and Adjustment Loop
To keep the server’s price competitive, this loop will repeat the price adjustment process periodically, adjusting only when a price change is detected.
import time
# Define refresh interval
refresh_interval = 300 # Check every 5 minutes
while True:
response = requests.get(url, headers=headers)
if response.status_code == 200:
offers = response.json().get('market', {}).get('offers', [])
lowest_bid = min((offer['bid'] for offer in offers if offer['active']), default=max_price_btc)
target_price = max(lowest_bid - price_adjustment_step, min_price_btc)
target_price = min(target_price, max_price_btc)
# Adjust price only if it's different from the current target
if target_price != lowest_bid:
adjust_data = {
"order_id": server_id,
"desired_price": target_price
}
adjust_response = requests.post(adjust_price_url, headers=headers, json=adjust_data)
if adjust_response.status_code == 200:
print(f"Spot price adjusted to {target_price} BTC.")
else:
print(f"Failed to adjust price: {adjust_response.json().get('error')}")
time.sleep(refresh_interval) # Wait before the next check
Summary
This example shows how to:
Retrieve and analyze current marketplace offers for competitive pricing.
Set target prices based on competition to increase rental chances.
Implement an automated loop that monitors and updates the spot price as needed to remain competitive.
This process is ideal for server owners aiming to maximize server usage on the spot marketplace by dynamically adjusting their prices based on real-time market data.