Solana Trading Strategy: Price + On-Chain Metrics

alpha
By Koglen in Other Published January 2025 👁 586 views 💬 0 comments

Description

Trading bot designed to utilize both historical price data and real-time on-chain metrics fetched using the Solscan API. This script assumes Solana (SOL) is the target cryptocurrency, and it dynamically incorporates on-chain activity to refine trading decisions.
HaasScript
// Solana Trading Strategy: Price + On-Chain Metrics

// Initialize Variables
var fastMA = ema(9)  // 9-period EMA
var slowMA = ema(21) // 21-period EMA
var rsiValue = rsi(14) // RSI with a 14-period window
var onChainActivity = 0 // Placeholder for on-chain activity metric
var transactionVolume = 0 // Placeholder for transaction volume metric

// Risk Management Parameters
var stopLossPct = 2.0  // 2% stop-loss
var takeProfitPct = 5.0  // 5% take-profit
var positionOpen = false
var entryPrice = 0

// External API Integration (Solscan)
function fetchOnChainMetrics() {
    var apiKey = "APIKEY"
    var baseUrl = "https://pro-api.solscan.io/v2"
    var endpoint = "/analysis/active-addresses"  // Example endpoint
    var headers = {
        "Authorization": "Bearer " + apiKey
    }

    // Make API Request
    var response = httpGet(baseUrl + endpoint, headers)
    if (response.status == 200) {
        // Parse JSON response
        var data = jsonDecode(response.body)
        onChainActivity = data.activeAddresses // Adjust based on the actual API response structure
        transactionVolume = data.transactionVolume // Example: Adjust as needed
    } else {
        log("API Request Failed. Status: ", response.status)
    }
}

// Entry Logic
if (fastMA > slowMA && rsiValue > 50 && onChainActivity > threshold) {
    if (!positionOpen) {
        fetchOnChainMetrics()  // Update on-chain metrics before entering a position
        entryPrice = marketPrice()
        marketBuy()
        log("Buy executed at: ", entryPrice)
        positionOpen = true
    }
}

// Exit Logic (Stop-Loss or Take-Profit)
if (positionOpen) {
    var currentPrice = marketPrice()
    var stopLossPrice = entryPrice * (1 - stopLossPct / 100)
    var takeProfitPrice = entryPrice * (1 + takeProfitPct / 100)

    if (currentPrice <= stopLossPrice) {
        marketSell()
        log("Stop-loss triggered. Sell executed at: ", currentPrice)
        positionOpen = false
    } else if (currentPrice >= takeProfitPrice) {
        marketSell()
        log("Take-profit triggered. Sell executed at: ", currentPrice)
        positionOpen = false
    }
}

// Exit on Indicator Reversal
if (fastMA < slowMA || rsiValue < 40) {
    if (positionOpen) {
        marketSell()
        log("Sell executed on indicator reversal at: ", marketPrice())
        positionOpen = false
    }
}

// Regularly Fetch On-Chain Data
fetchOnChainMetrics()  // Called periodically (e.g., every 5 minutes)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!