# region imports
from AlgorithmImports import *
# endregion
# Import datetime
import datetime
class MeasuredYellowGreenLemur(QCAlgorithm):
def initialize(self):
# Set start date
self.set_start_date(2022, 3, 1)
# Set cash
self.set_cash(1000000)
# List of stocks/ETFs
list_of_tickers = ["AAPL","GOOG","SPY"]
# MACD dictionary
self.MACD_dictionary = {}
# Loop through list
for ticker in list_of_tickers:
# Register
equity = self.add_equity(ticker, [Link])
# Get symbol
symbol = [Link]
# Create MACD
MACD_object = MovingAverageConvergenceDivergence(12, 26, 9)
# Store MACD
self.MACD_dictionary[symbol] = MACD_object
# Register MACD for automatic update
self.register_indicator(symbol, self.MACD_dictionary[symbol])
def on_data(self, data: Slice):
# Get open order tickets
open_order_tickets = [Link].get_open_order_tickets()
# List to store open order symbols
open_order_symbols = []
# Loop open order tickets
for ticket in open_order_tickets:
# Store ticket symbol in open order symbols list
open_order_symbols.append([Link])
# Loop MACD dictionary
for symbol in self.MACD_dictionary:
# If MACD is ready
if self.MACD_dictionary[symbol].is_ready == True:
# If no open order for asset
if symbol not in open_order_symbols:
# If MACD greater than MACD signal
if self.MACD_dictionary[symbol].[Link] >
self.MACD_dictionary[symbol].[Link]:
# If not invested
if [Link][symbol].invested == False:
# Submit limit order
self.limit_order(symbol = symbol, quantity = 10,
limit_price = [Link][symbol].close, tag = "1")
# If short
elif [Link][symbol].is_short == True:
# Submit limit order
self.limit_order(symbol = symbol, quantity = 20,
limit_price = [Link][symbol].close, tag = "2")
# If MACD lower than MACD signal
elif self.MACD_dictionary[symbol].[Link] <
self.MACD_dictionary[symbol].[Link]:
# If not invested
if [Link][symbol].invested == False:
# Submit limit order
self.limit_order(symbol = symbol, quantity = -10,
limit_price = [Link][symbol].close, tag = "3")
# If long
elif [Link][symbol].is_long == True:
# Submit limit order
self.limit_order(symbol = symbol, quantity = -20,
limit_price = [Link][symbol].close, tag = "4")
# Loop through open order ticekts
for ticket in open_order_tickets:
# Get ticket submission time
ticket_submission_time = [Link]
# Current Utc time
current_utc_time = self.utc_time
# If current UTC time is 5 minutes from order submission time
if current_utc_time > ticket_submission_time +
[Link](minutes = 5):
# Get ticket tag
ticket_tag = [Link]
# Get ticket symbol
ticket_symbol = [Link]
# Update the order tag and limit price
updateSettings = UpdateOrderFields()
# If ticket goes long
if ticket_tag == "1":
# Update limit price to 0.5% above current price
updateSettings.limit_price =
[Link][ticket_symbol].close * 1.005
# If ticket closes short and goes long
elif ticket_tag == "2":
# Update limit price to 1% above current price
updateSettings.limit_price =
[Link][ticket_symbol].close * 1.01
# If ticket goes short
elif ticket_tag == "3":
# Update limit price to 0.5% below current price
updateSettings.limit_price =
[Link][ticket_symbol].close * 0.995
# If ticket closes long and goes short
elif ticket_tag == "4":
# Update limit price to 1% below current price
updateSettings.limit_price =
[Link][ticket_symbol].close * 0.99
# Update
[Link](updateSettings)