The Following strategy calculates the EMA of ATR at the beginning of the run to establish a volatility baseline.
It then iterates through selected assets (SPY, AAPL, MSFT), applying its entry and exit logic to each based on the current market conditions as interpreted through its fuzzy logic framework.
Trades are made based on the composite fuzzy logic conditions, with the strategy entering long positions when the entry conditions are met and exiting positions when the exit conditions are met.


Code
#include <contract.c>

vars PriceClose, VolatilityEMA;

void init() {
  assetList("AssetsIB");
  BarPeriod = 60; // Setting bar period for the price series
  LookBack = 100; // Lookback period for technical indicators

  PriceClose = series(priceClose());
  initFuzzyLogicSettings();
  calculateEMAVolatility(20); // Initialize volatility calculation
}

void initFuzzyLogicSettings() {
  FuzzyRange = 0.05; // Adjust based on backtesting
}

void calculateEMAVolatility(int period) {
  vars ATRValues = series(ATR(period));
  VolatilityEMA = series(EMA(ATRValues, period));
}

void tradeOptions() {
  // Assuming current asset is SPY and we're setting up a strangle
  if (NumOpenLong == 0 && fuzzyEntryCondition()) {
    // Find contracts for the call and put options
    CONTRACT* callContract = contractFind("Call", 30, priceClose(0) + 10); // Example: 30 days to expiration, strike 10 points above
    CONTRACT* putContract = contractFind("Put", 30, priceClose(0) - 10); // Example: 30 days to expiration, strike 10 points below

    if(callContract && putContract) {
      // Setup the combo - buying 1 call and 1 put
      combo(callContract, 1, putContract, 1, 0, 0, 0, 0);
      
      // Enter the combo trade
      enterLong(comboLeg(1)); // Enter long for the call option leg
      enterLong(comboLeg(2)); // Enter long for the put option leg
      
      printf("\nEntered a Strangle on SPY");
    }
  }
}

void run() {
  StartDate = 2020;
  EndDate = 2024;
  assetList("AssetsIB");
  asset("SPY");

  while(asset(loop("SPY","AAPL","MSFT"))) {
    if (is(EXITRUN)) continue;
    tradeOptions();
  }
}

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

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