Install Alva Extension to get Credits for free

Install
Alva Extension

Can you provide me with the code for my strategy below?  Indicator: MACD Time Frame: Daily Token: ETH Buy Signal: When the MACD line crosses above the signal line. Sell Signal: When the MACD line crosses below the signal line. Risk Management: Use the MACD crossovers for buy and sell signals. Stop-loss at 5% below the purchase price and a take-profit at 10% above the purchase price. Risk Management: Initial Capital: $100,000 Commission: 0.001 Slippage: 0.001 Start Time: Default End Time: Default

Answered

Based on the provided information and the MACD indicator code example, here is the JavaScript code for your strategy:

function strategy(prices, dealer, timeSeries) {
  // Extract the OHLCV data for ETH/USDT
  const ethData = prices["BINANCE_SPOT_ETH_USDT"];
  
  // Extract the closing prices
  const closings = ethData.map(candle => candle.close);

  // Calculate the MACD with a short period of 12, long period of 26, and signal period of 9
  const macdResult = indicators.macd(closings, 12, 26, 9);
  
  // Initialize variables to keep track of positions and purchase prices
  let position = 0;
  let purchasePrice = 0;
  
  // Iterate over the price data
  for (let i = 0; i < ethData.length; i++) {
    const date = ethData[i].date;
    const close = ethData[i].close;
    
    // Check for buy signal
    if (macdResult.macdLine[i] > macdResult.signalLine[i] && position === 0) {
      // Buy signal: Buy ETH
      const balance = dealer.getBalance(date);
      const size = balance / close; // Buy as much ETH as possible with the current balance
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", size);
      position = size;
      purchasePrice = close;
    }
    // Check for sell signal
    else if (macdResult.macdLine[i] < macdResult.signalLine[i] && position > 0) {
      // Sell signal: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
    // Check for stop-loss
    else if (position > 0 && close <= purchasePrice * 0.95) {
      // Stop-loss: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
    // Check for take-profit
    else if (position > 0 && close >= purchasePrice * 1.10) {
      // Take-profit: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
  }
}

Now, I will proceed with backtesting this strategy using the specified parameters.

function strategy(prices, dealer, timeSeries) {
  // Extract the OHLCV data for ETH/USDT
  const ethData = prices["BINANCE_SPOT_ETH_USDT"];
  
  // Extract the closing prices
  const closings = ethData.map(candle => candle.close);

  // Calculate the MACD with a short period of 12, long period of 26, and signal period of 9
  const macdResult = indicators.macd(closings, 12, 26, 9);
  
  // Initialize variables to keep track of positions and purchase prices
  let position = 0;
  let purchasePrice = 0;
  
  // Iterate over the price data
  for (let i = 0; i < ethData.length; i++) {
    const date = ethData[i].date;
    const close = ethData[i].close;
    
    // Check for buy signal
    if (macdResult.macdLine[i] > macdResult.signalLine[i] && position === 0) {
      // Buy signal: Buy ETH
      const balance = dealer.getBalance(date);
      const size = balance / close; // Buy as much ETH as possible with the current balance
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", size);
      position = size;
      purchasePrice = close;
    }
    // Check for sell signal
    else if (macdResult.macdLine[i] < macdResult.signalLine[i] && position > 0) {
      // Sell signal: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
    // Check for stop-loss
    else if (position > 0 && close <= purchasePrice * 0.95) {
      // Stop-loss: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
    // Check for take-profit
    else if (position > 0 && close >= purchasePrice * 1.10) {
      // Take-profit: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
  }
}

Strategy Code

Here's the JavaScript code for the strategy you requested:

function strategy(prices, dealer, timeSeries) {
  // Extract the OHLCV data for ETH/USDT
  const ethData = prices["BINANCE_SPOT_ETH_USDT"];
  
  // Extract the closing prices
  const closings = ethData.map(candle => candle.close);

  // Calculate the MACD with a short period of 12, long period of 26, and signal period of 9
  const macdResult = indicators.macd(closings, 12, 26, 9);
  
  // Initialize variables to keep track of positions and purchase prices
  let position = 0;
  let purchasePrice = 0;
  
  // Iterate over the price data
  for (let i = 0; i < ethData.length; i++) {
    const date = ethData[i].date;
    const close = ethData[i].close;
    
    // Check for buy signal
    if (macdResult.macdLine[i] > macdResult.signalLine[i] && position === 0) {
      // Buy signal: Buy ETH
      const balance = dealer.getBalance(date);
      const size = balance / close; // Buy as much ETH as possible with the current balance
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", size);
      position = size;
      purchasePrice = close;
    }
    // Check for sell signal
    else if (macdResult.macdLine[i] < macdResult.signalLine[i] && position > 0) {
      // Sell signal: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
    // Check for stop-loss
    else if (position > 0 && close <= purchasePrice * 0.95) {
      // Stop-loss: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
    // Check for take-profit
    else if (position > 0 && close >= purchasePrice * 1.10) {
      // Take-profit: Sell all ETH
      dealer.executeAction(date, "BINANCE_SPOT_ETH_USDT", -position);
      position = 0;
    }
  }
}

Backtest Performance

  • Period: 2023-09-13 to 2024-09-12 (1 year)
  • Start Equity: $100,000.00
  • End Equity: $132,382.31
  • Equity Return: 32.38%
  • Compound Annual Growth Rate (CAGR): 32.38%
  • Max Drawdown: 31.45%
  • MDD Recovery Time: 4392 hours
  • Historic Volatility (Annualized): 33.68%
  • Sharpe Ratio: 0.74
  • Calmar Ratio: 1.03

Current Portfolio Summary

  • Date: 2024-09-12
  • Cash: $0.00
  • Positions:
    • BINANCE_SPOT_ETH_USDT: 56.49 shares, $132,218.90

Insights and Suggestions

  • Performance: The strategy yielded a 32.38% return over the past year, which is quite impressive.
  • Risk: The maximum drawdown of 31.45% indicates significant risk, but the strategy managed to recover.
  • Volatility: The annualized volatility is 33.68%, which is relatively high.
  • Sharpe Ratio: A Sharpe Ratio of 0.74 suggests that the returns are reasonable for the level of risk taken.
  • Calmar Ratio: A Calmar Ratio of 1.03 is decent, indicating that the strategy is effective in balancing return and drawdown.

The strategy seems to be performing well, but you should be aware of the high volatility and drawdown. It might be beneficial to monitor the strategy closely and consider additional risk management techniques if necessary.

Ask anything about crypto