#include <contract.c>
vars PriceClose;
void run() {
BarPeriod = 1440; // Daily bars for trend analysis
LookBack = 30; // Looking back 30 bars for accurate trend detection
StartDate = 2020;
EndDate = 2024;
assetList("AssetsIB");
asset("SPY");
PriceClose = series(priceClose());
AmericanOption = 1; // Trading American Options
if (is(EXITRUN)) return; // Exit if at the end of the backtest
// Ensure the contract chain is updated for SPY
if (!contractUpdate(Asset, 0, CALL | PUT)) return;
// Entry Logic: Detect a rising trend to sell a call option
if (NumOpenLong + NumOpenShort == 0) { // Check if there's no open position
var trendStrength = risingF(PriceClose); // Assess the rising trend strength
if (trendStrength > 0.5) { // Threshold for a strong rising trend
// Define the strike price a bit above the current price for selling a call
var StrikeCall = priceClose(0) + 100; // Offset from the current price for the strike
int CallContract = contractFind(CALL, 45, StrikeCall); // Find a call option 45 days to expiration
if (CallContract >= 0) {
CONTRACT* c = contract(CallContract);
if (combo(c, -1, 0, 0, 0, 0, 0, 0) > 0) { // Prepare the combo for selling 1 call
enterShort(comboLeg(1)); // Enter short for the call option leg
}
}
}
}
// Exit Logic: Close the short call option position on a falling trend
for(open_trades) {
if (TradeIsOption && TradeIsShort) {
var reversalStrength = fallingF(PriceClose); // Assess the strength of the falling trend
if (reversalStrength > 0.5) { // Threshold indicating a strong downward trend
exitTrade(ThisTrade); // Close the call option position
}
}
}
}