Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data.Custom import *
from QuantConnect.Orders import *
from QuantConnect.Securities.Option import OptionPriceModels
from datetime import timedelta, datetime
import csv
import io

class SPXWTradingAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2023, 1, 1)
        self.SetCash(100000)
        self.symbol = self.AddOption("SPX", Resolution.Minute).Symbol
        self.SetBenchmark(self.symbol)
        self.UniverseSettings.Resolution = Resolution.Daily
        self.UniverseSettings.MinimumTimeInUniverse = timedelta(0)
        self.UniverseSettings.Leverage = 1
        self.pending_orders = {}

    def OnData(self, data):
        csv_string = self.Download("https://docs.google.com/spreadsheets/d/1wwadCU8msu6FEUJt1ANoZS2qMO2MWiheARrdm7zaQlM/pub?output=csv")
        orders = list(csv.DictReader(io.StringIO(csv_string)))
        for order in orders:
            if 'Signal' in order and order['Signal'] == 'LONG':
                trigger_time = datetime.fromtimestamp(int(order['Trigger Time']))
                if self.Time.date() == trigger_time.date():
                    self.Trade(order)

    def Trade(self, order):
        try:
            expiry = datetime.strptime(str(order['TWS Contract Date']), '%Y%m%d')
            optionchain = self.OptionChainProvider.GetOptionContractList(self.symbol, self.Time)
            contracts = [i for i in optionchain if i.ID.Date.date() == expiry.date()]
            for i in range(1, 5):
                if order[f'Right {i}'] == 'C':
                    contract = [i for i in contracts if i.ID.StrikePrice == order[f'Strike {i}'] and i.ID.OptionRight == OptionRight.Call]
                    if contract:
                        self.Buy(contract[0], order[f'Multiplier {i}'] * order['Order Quantity'])
                elif order[f'Right {i}'] == 'P':
                    contract = [i for i in contracts if i.ID.StrikePrice == order[f'Strike {i}'] and i.ID.OptionRight == OptionRight.Put]
                    if contract:
                        self.Sell(contract[0], order[f'Multiplier {i}'] * order['Order Quantity'])
        except Exception as e:
            self.Debug(str(e))

    def OnOrderEvent(self, orderEvent):
        if orderEvent.Status == OrderStatus.Filled:
            self.Debug(str(orderEvent))