Asset Ownership: This strategy assumes you initially buy shares of SPY if you don't already own them.

Option Sale: It sells a call option on SPY with a strike price determined by StrikeOffset above the current price and targets DaysToExpiration for expiration.

Position Management: The script also checks for the option position to close it if the premium target is reached or the expiration is near.


Code
#include <contract.c>

// Global parameters for the covered call strategy
var PremiumTarget = 100; // Target premium income from selling the call
int DaysToExpiration = 45; // Target days until expiration for the call option
var StrikeOffset = 100; // Offset from the current price for strike selection

void run() {
  // Setup basic parameters
  BarPeriod = 1440; // Use daily bars
  LookBack = 0; // No need for historical bars in this setup
  StartDate = 2020;
  EndDate = 2024; // Set your backtest period
  assetList("AssetsIB");
  asset("SPY"); // Example using SPY ETF as the underlying asset
  
  // Ensure we're trading in American Options for SPY
  AmericanOption = 1;

  // Update the contract chain for the underlying asset
  if(!contractUpdate(Asset, 0, CALL)) return;

  // Trading logic executed once per day
  if(is(EXITRUN)) return; // Skip logic at the end of the backtest

  // Check if we already own SPY
  if(!NumOpenLong) enterLong(1); // Enter long position if we don't own SPY

  // Sell a call option if we haven't already
  if(NumOpenShort == 0) {
    var CurrentPrice = priceClose(0);
    var StrikeCall = CurrentPrice + StrikeOffset;

    // Finding the call option contract
    CONTRACT* callContract = contractFind(CALL, DaysToExpiration, StrikeCall);
    if(callContract) {
      // Enter a short position by selling the call option
      enterShort(1, callContract); 
    }
  }

  // Managing the open option position
  for(open_trades) {
    CONTRACT* c = ThisTrade->Contract;
    if(TradeIsOption && TradeIsShort && (comboProfit(c->fAsk, 1) > PremiumTarget || daysToExpiration(c) < 5)) {
      exitTrade(ThisTrade); // Close the call option if premium target is reached or approaching expiration
    }
  }
}

// A more refined function for calculating days to expiration based on contract data
int daysToExpiration(CONTRACT* c) {
  if(!c) return 0;
  return (c->Expiry - wdate()) / 86400; // Convert seconds to days
}

Last edited by TipmyPip; 04/01/24 21:17.

ZorroTraderGPT - https://bit.ly/3Gbsm4S