Skip to content
The Chat Archive
gpt-4-0314· OpenAI Arena Human Preference 55K

write hummingbot script, buy and sell following fast and slow MA cross

  1. User

    write hummingbot script, buy and sell following fast and slow MA cross

  2. Assistant

    Below is a basic Hummingbot script that buys when a fast Moving Average (MA) crosses above the slow Moving Average and sells when it crosses below.

    ####################################
    ##    My Hummingbot MA Strategy   ##
    ####################################
    
    # Load necessary libraries
    import numpy as np
    
    # Configure strategy parameters
    fast_ma_len = 5
    slow_ma_len = 15
    min_cross_threshold = 0.01
    
    # Calculate moving averages
    def calculate_moving_average(data, _len):
        return data[-_len:].mean()
    
    # Define cross detect function
    def detect_cross(fast_ma, slow_ma, cross_threshold):
        if fast_ma <= slow_ma * (1 + cross_threshold):
            return 'buy'
        elif fast_ma >= slow_ma * (1 - cross_threshold):
            return 'sell'
        else:
            return 'hold'
    
    # Get exchange and trading pair
    exchange = get_active_exchange()
    trading_pair = exchange.trading_pair
    
    # Get trading pair's price history
    price_history = get_mid_price_history(exchange=exchange, trading_pair=trading_pair)
    
    # Create a numpy array with price history
    price_history_np = np.array(price_history)
    
    # Calculate moving averages
    fast_ma = calculate_moving_average(price_history_np, fast_ma_len)
    slow_ma = calculate_moving_average(price_history_np, slow_ma_len)
    
    # Detect cross
    signal = detect_cross(fast_ma, slow_ma, min_cross_threshold)
    
    # Execute strategy
    if signal == 'buy':
        execute_buy(exchange=exchange, trading_pair=trading_pair)
    elif signal == 'sell':
        execute_sell(exchange=exchange, trading_pair=trading_pair)
    else:
        pass
    

    This script calculates the fast and slow MAs from the provided price history for the specified lengths and checks for the MA cross. If a valid cross exists, the script will execute a buy or sell order accordingly. Please make sure to replace the placeholder functions like execute_buy, execute_sell, get_active_exchange, and get_mid_price_history with actual Hummingbot functionalities.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.