dev.clore.ai
  • Getting Started
  • Introduction to Clore API
    • API Key Management
    • Transaction Processing with Clore Coin
    • RESTful Protocols in Clore
  • Marketplace Automation and Resource Optimization
    • Automated Spot Price Adjustment for Cost Optimization
    • Automated Server Retrieval and Analysis
    • Spot Price Optimization
    • Predictive Market Analytics
  • Server Management and Configuration
    • Bulk Server Onboarding and Dynamic Pricing Configuration
  • Dynamic Pricing and Profit Maximization
    • Dynamic Pricing Adjustments Based on Server Profitability
    • Automated Price Adjustment Based on Market Demand
    • Automated Spot Rental Based on Price Thresholds
  • Rental Strategies and Arbitrage
  • Monitoring and Notifications
    • Automated Monitoring and Notification of Rental Status
    • Automated Alert System for Low Server Utilization
    • Automated Retrieval and Analysis of Available Servers on the Marketplace
  • Security and Compliance
  • UI Automation and Visualization
  • API Performance Optimization
  • Community Extensions and Integrations
  • Advanced Data Processing and Analysis
  • Scalability and Infrastructure Management
  • Machine Learning and AI Integrations
    • Integrating ML Models into Server Operations
  • Developer Tools and SDKs
    • Setting Up the Clore Developer Toolkit
    • Using Clore SDK for Rapid Prototyping
  • Billing, Accounting, and Financial Reporting
  • Workflow Automation and Scripting
  • Multi-Cloud and Hybrid Cloud Integrations
  • Security Monitoring and Incident Management
  • Blockchain Interactions and Smart Contracts
  • Resource Optimization and Cost-Saving Techniques
Powered by GitBook
On this page
  1. Marketplace Automation and Resource Optimization

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.

import requests

# Parameters
server_id = 6  # Replace with the target server's ID
auth_token = 'YOUR_API_KEY'
url = f'https://api.clore.ai/v1/spot_marketplace?market={server_id}'

headers = {
    'Authorization': f'Bearer {auth_token}'
}

# Retrieve spot marketplace data
response = requests.get(url, headers=headers)

if response.status_code == 200:
    spot_data = response.json()
    offers = spot_data.get('market', {}).get('offers', [])
else:
    print(f"Error {response.status_code}: {response.text}")
    offers = []

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:

  1. Retrieve and analyze current marketplace offers for competitive pricing.

  2. Set target prices based on competition to increase rental chances.

  3. 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.

PreviousMarketplace Automation and Resource OptimizationNextAutomated Server Retrieval and Analysis

Last updated 6 months ago