Overall Statistics
from AlgorithmImports import *
import math

class LogicalYellowBadger(QCAlgorithm):

	def Initialize(self):

		# Backtesting / Papertrading parameters if necessary
		self.SetCash(100000)
		self.SetStartDate(2022, 9, 9)
		self.SetEndDate(2022, 11, 11)
		
		# Create futures contract symbol for ES using expiration date {update as necessary}
		# Request contract data with tick resolution
		self.esContract = Symbol.CreateFuture(Futures.Indices.SP500EMini, Market.CME, datetime(2022,12,16))
		self.es = self.AddFutureContract(self.esContract, Resolution.Tick)

		# Set fees to zero for backtesting purposes
		self.Securities[self.es.Symbol].SetFeeModel(ConstantFeeModel(0))

		# Variables to record W/L ratio & highest/lowest intraday P/L
		self.tallyWin = 0
		self.tallyLoss = 0
		self.tallyEven = 0
		self.tallyTtl = 0
		self.shortWin = 0
		self.longWin = 0
		# Variables for intraday high/low P/L
		self.lowBal = self.Portfolio.Cash
		self.highBal = self.Portfolio.Cash
		self.begBal = self.Portfolio.Cash
		self.endBal = self.Portfolio.Cash
		self.lowTime = 0
		self.highTime = 0
		self.startTime = 0
		self.drawD = 0
		self.dDTime = 0

		# Variables for both short and long entry and exit tickets, trade cooldowns, prices for trailing stops and cancellations, and contracts per trade
		self.longEntryTicket = None
		self.shortEntryTicket = None
		self.sExTick = None
		self.lExTick = None
		self.liquidateTicket = None

		self.exitFillTime = datetime.min
		self.cancelTime = datetime.min

		self.trgPrice = 0
		self.highestPrice = 0
		self.lowestPrice = 25000
        
		# Quantity automatically scales for back testing purposes
		self.qty = math.floor(15000 / 15000)

        # Variable for trigger range
		# If this is increased more levels may need to be removed {current minimum distance b/w levels is 3.25, or self.trgEntry * 2 + one tick[.25]}
		self.trgEntry = 1.5

		# Variable for entry trail stop
		self.trailEntry = 3

		# Variable for order cancel based on PA
		self.entryCancel = 4

		self.stopLoss = 2.5

		# Variables for cooldowns {in seconds}
		self.exitCool = 30
		self.cancelCool = 15

		# no entries occur outside of RTH
		
		self.stopPreM = datetime.min
		self.stopAftM = datetime.min
# Future Back Tests:
	# TP = +0.5; SL = -2.5 (SL=-2.25@+.75)
	# TP = +0.5; SL = -2.5 (SL=-2@+.75)
	# ... cont if necessary
	# TP = +0.75; SL = 2
	# if better, SL = 1.5; if worse, SL = 2.25
	# ... cont until best is found for .75 TP
	# Test updated SL@+.75 with the best .75TP
	# Change start time to 10:01am, test best .75TP & .5TP
	# Test trading halts at certain drawdowns
		# Start with -$500, then -$500trail @ +$500, 
	def OnData(self, slice):

		# Plot W/L and high/low intraday P/L at EOD & update ending balance
		if self.Time == self.Time.replace(hour=15, minute=59, second=59):
			self.endBal = self.Portfolio.Cash
			if self.endBal == self.Portfolio.Cash:
				self.Plot("W/L", "Wins", self.tallyWin)
				self.Plot("W/L", "Losses", self.tallyLoss)
				self.Plot("W/L", "Draws", self.tallyEven)
				self.Plot("P/L", "Highest", self.highBal - self.begBal)
				self.Plot("P/L", "Lowest", self.lowBal - self.begBal)
				self.Plot("P/L", "Ending", self.endBal - self.begBal)
				self.Plot("Intraday Times", "High Time", int(round(self.highTime / 60)))
				self.Plot("Intraday Times", "Low Time", int(round(self.lowTime / 60)))
				self.Plot("Intraday Times", "Max DD Time", int(round(self.dDTime / 60)))
				self.Plot("Misc. Stats", "Max Drawdown", self.drawD)
				self.Plot("Misc. Stats", "W/L", self.tallyWin * 100 / self.tallyTtl)
				self.Plot("Misc. Stats", "Long W/L", self.longWin * 100 / self.tallyTtl)
				self.Plot("Misc. Stats", "Short W/L", self.shortWin * 100 / self.tallyTtl)
		# Reset balances daily
		if self.Time == self.Time.replace(hour=9, minute=30, second=0):
			self.highBal = self.Portfolio.Cash
			self.lowBal = self.Portfolio.Cash
			self.begBal = self.Portfolio.Cash
			self.startTime = int(round(self.Time.timestamp()))
			self.lowTime = 0
			self.highTime = 0
			self.drawD = 0
			self.dDTime = 0
			# Quantity automatically scales for back testing purposes
			if self.begBal == self.Portfolio.Cash:
				self.qty = math.floor(15000 / 15000)

		# no entries occur outside of RTH
		if self.Time == self.Time.replace(hour=9,minute=31):
			self.stopPreM = self.Time.replace(hour=9,minute=32,second=59)
			self.stopAftM = self.Time.replace(hour=15,minute=55,second=59)

		# Variable for ES contract price
		price = self.Securities[self.es.Symbol].Price
		
		# Cooldowns
		if (self.Time - self.exitFillTime).seconds <= self.exitCool or (self.Time - self.cancelTime).seconds <= self.cancelCool:
			return

		# Submit entry order if there are no positions or open orders and time conditions are met
		if not self.Portfolio.Invested and not self.Transactions.GetOpenOrders(self.es.Symbol) and self.stopPreM < self.Time < self.stopAftM:
			
			# Short entries
			if self.Time == self.Time.replace(year=2022,month=9,day=9):
				if 4095+self.trgEntry>=price>4095 or 4078+self.trgEntry>=price>4078 or 4064+self.trgEntry>=price>4064 or 4057+self.trgEntry>=price>4057 or 4045+self.trgEntry>=price>4045:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=12):
				if 4141+self.trgEntry>=price>4141 or 4127+self.trgEntry>=price>4127 or 4115+self.trgEntry>=price>4115 or 4106+self.trgEntry>=price>4106 or 4000+self.trgEntry>=price>4000:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=13):
				if 4046+self.trgEntry>=price>4046 or 4036.5+self.trgEntry>=price>4036.5 or 4027.5+self.trgEntry>=price>4027.5 or 4015+self.trgEntry>=price>4015 or 4005+self.trgEntry>=price>4005 or 4000+self.trgEntry>=price>4000 or 3995+self.trgEntry>=price>3995:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=14):
				if 3983+self.trgEntry>=price>3983 or 3971+self.trgEntry>=price>3971 or 3964+self.trgEntry>=price>3964 or 3951+self.trgEntry>=price>3951 or 3942+self.trgEntry>=price>3942 or 3934+self.trgEntry>=price>3934 or 3924+self.trgEntry>=price>3924 or 3916 or 3979+self.trgEntry>=price>3979 or 3938+self.trgEntry>=price>3938:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=15):
				if 3981+self.trgEntry>=price>3981 or 3974+self.trgEntry>=price>3974 or 3959+self.trgEntry>=price>3959 or 3953+self.trgEntry>=price>3953 or 3944+self.trgEntry>=price>3944 or 3929+self.trgEntry>=price>3929 or 3919+self.trgEntry>=price>3919 or 3948.5+self.trgEntry>=price>3948.5 or 3977.5+self.trgEntry>=price>3977.5 or 3900+self.trgEntry>=price>3900:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=16):
				if 3901+self.trgEntry>=price>3901 or 3890+self.trgEntry>=price>3890 or 3882+self.trgEntry>=price>3882 or 3873+self.trgEntry>=price>3873 or 3858+self.trgEntry>=price>3858 or 3850+self.trgEntry>=price>3850 or 3863+self.trgEntry>=price>3863 or 3894+self.trgEntry>=price>3894:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=19):
				if 3895+self.trgEntry>=price>3895 or 3889+self.trgEntry>=price>3889 or 3877+self.trgEntry>=price>3877 or 3869+self.trgEntry>=price>3869 or 3860+self.trgEntry>=price>3860 or 3854+self.trgEntry>=price>3854 or 3900+self.trgEntry>=price>3900:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=20):
				if 3893+self.trgEntry>=price>3893 or 3881+self.trgEntry>=price>3881 or 3872+self.trgEntry>=price>3872 or 3866+self.trgEntry>=price>3866 or 3855+self.trgEntry>=price>3855 or 3846+self.trgEntry>=price>3846:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=21):
				if 3926+self.trgEntry>=price>3926 or 3913+self.trgEntry>=price>3913 or 3904+self.trgEntry>=price>3904 or 3900+self.trgEntry>=price>3900 or 3893+self.trgEntry>=price>3893 or 3884+self.trgEntry>=price>3884 or 3875+self.trgEntry>=price>3875 or 3871+self.trgEntry>=price>3871 or 3862+self.trgEntry>=price>3862 or 3843+self.trgEntry>=price>3843 or 3800+self.trgEntry>=price>3800:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=22):
				if 3801+self.trgEntry>=price>3801 or 3794+self.trgEntry>=price>3794 or 3781+self.trgEntry>=price>3781 or 3774+self.trgEntry>=price>3774 or 3763+self.trgEntry>=price>3763 or 3767+self.trgEntry>=price>3767:
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=23):
				if 3736+self.trgEntry>=price>3736 or 3723+self.trgEntry>=price>3723 or 3716+self.trgEntry>=price>3716 or 3701+self.trgEntry>=price>3701 or 3691+self.trgEntry>=price>3691 or 3685+self.trgEntry>=price>3685:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=26):
				if 3727+self.trgEntry>=price>3727 or 3712+self.trgEntry>=price>3712 or 3700+self.trgEntry>=price>3700 or 3693+self.trgEntry>=price>3693 or 3678+self.trgEntry>=price>3678 or 3670+self.trgEntry>=price>3670 or 3662+self.trgEntry>=price>3662 or 3718+self.trgEntry>=price>3718:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=27):
				if 3734+self.trgEntry>=price>3734 or 3725+self.trgEntry>=price>3725 or 3716+self.trgEntry>=price>3716 or 3704+self.trgEntry>=price>3704 or 3700+self.trgEntry>=price>3700 or 3696+self.trgEntry>=price>3696 or 3688+self.trgEntry>=price>3688 or 3677+self.trgEntry>=price>3677 or 3665+self.trgEntry>=price>3665 or 3730+self.trgEntry>=price>3730:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=28):
				if 3713+self.trgEntry>=price>3713 or 3700+self.trgEntry>=price>3700 or 3686+self.trgEntry>=price>3686 or 3674+self.trgEntry>=price>3674 or 3668+self.trgEntry>=price>3668 or 3658+self.trgEntry>=price>3658 or 3681+self.trgEntry>=price>3681:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=29):
				if 3699+self.trgEntry>=price>3699 or 3694+self.trgEntry>=price>3694 or 3688+self.trgEntry>=price>3688 or 3678+self.trgEntry>=price>3678 or 3669+self.trgEntry>=price>3669 or 3658+self.trgEntry>=price>3658 or 3640+self.trgEntry>=price>3640 or 3673+self.trgEntry>=price>3673 or 3653+self.trgEntry>=price>3653:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=30):
				if 3685+self.trgEntry>=price>3685 or 3672+self.trgEntry>=price>3672 or 3658+self.trgEntry>=price>3658 or 3648+self.trgEntry>=price>3648 or 3635+self.trgEntry>=price>3635 or 3625+self.trgEntry>=price>3625 or 3612+self.trgEntry>=price>3612 or 3640.5+self.trgEntry>=price>3640.5 or 3600+self.trgEntry>=price>3600:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=3):
				if 3690+self.trgEntry>=price>3690 or 3682+self.trgEntry>=price>3682 or 3670+self.trgEntry>=price>3670 or 3659+self.trgEntry>=price>3659 or 3642+self.trgEntry>=price>3642 or 3634+self.trgEntry>=price>3634 or 3623+self.trgEntry>=price>3623 or 3614+self.trgEntry>=price>3614 or 3700+self.trgEntry>=price>3700:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=4):
				if 3788+self.trgEntry>=price>3788 or 3775+self.trgEntry>=price>3775 or 3766+self.trgEntry>=price>3766 or 3760+self.trgEntry>=price>3760 or 3800+self.trgEntry>=price>3800:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=5):
				if 3819+self.trgEntry>=price>3819 or 3809.5+self.trgEntry>=price>3809.5 or 3801+self.trgEntry>=price>3801 or 3794+self.trgEntry>=price>3794 or 3780.25+self.trgEntry>=price>3780.25 or 3769.5+self.trgEntry>=price>3769.5 or 3760+self.trgEntry>=price>3760 or 3749+self.trgEntry>=price>3749 or 3737+self.trgEntry>=price>3737 or 3727+self.trgEntry>=price>3727 or 3787+self.trgEntry>=price>3787 or 3744+self.trgEntry>=price>3744:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=6):
				if 3810+self.trgEntry>=price>3810 or 3799.5+self.trgEntry>=price>3799.5 or 3786+self.trgEntry>=price>3786 or 3777+self.trgEntry>=price>3777 or 3772.5+self.trgEntry>=price>3772.5 or 3765+self.trgEntry>=price>3765 or 3754.5+self.trgEntry>=price>3754.5 or 3791+self.trgEntry>=price>3791:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=7):
				if 3710+self.trgEntry>=price>3710 or 3700+self.trgEntry>=price>3700 or 3686+self.trgEntry>=price>3686 or 3678+self.trgEntry>=price>3678 or 3673+self.trgEntry>=price>3673:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=10):
				if 3658+self.trgEntry>=price>3658 or 3650+self.trgEntry>=price>3650 or 3641+self.trgEntry>=price>3641 or 3631+self.trgEntry>=price>3631 or 3615+self.trgEntry>=price>3615 or 3605+self.trgEntry>=price>3605 or 3600+self.trgEntry>=price>3600 or 3623.5+self.trgEntry>=price>3623.5:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=11):
				if 3653.5+self.trgEntry>=price>3653.5 or 3647+self.trgEntry>=price>3647 or 3641+self.trgEntry>=price>3641 or 3629+self.trgEntry>=price>3629 or 3621+self.trgEntry>=price>3621 or 3610+self.trgEntry>=price>3610 or 3599.5+self.trgEntry>=price>3599.5 or 3586+self.trgEntry>=price>3586:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=12):
				if 3616+self.trgEntry>=price>3616 or 3601+self.trgEntry>=price>3601 or 3597+self.trgEntry>=price>3597 or 3587+self.trgEntry>=price>3587:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=13):
				if 3570+self.trgEntry>=price>3570 or 3562+self.trgEntry>=price>3562 or 3547+self.trgEntry>=price>3547 or 3536+self.trgEntry>=price>3536 or 3530+self.trgEntry>=price>3530 or 3516+self.trgEntry>=price>3516 or 3501+self.trgEntry>=price>3501 or 3644+self.trgEntry>=price>3644 or 3582+self.trgEntry>=price>3582 or 3620+self.trgEntry>=price>3620:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=14):
				if 3726+self.trgEntry>=price>3726 or 3718+self.trgEntry>=price>3718 or 3709+self.trgEntry>=price>3709 or 3700+self.trgEntry>=price>3700 or 3695+self.trgEntry>=price>3695 or 3685+self.trgEntry>=price>3685 or 3674+self.trgEntry>=price>3674 or 3665+self.trgEntry>=price>3665 or 3600+self.trgEntry>=price>3600:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=17):
				if 3700+self.trgEntry>=price>3700 or 3693+self.trgEntry>=price>3693 or 3683+self.trgEntry>=price>3683 or 3674+self.trgEntry>=price>3674 or 3666+self.trgEntry>=price>3666:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=18):
				if 3776+self.trgEntry>=price>3776 or 3766+self.trgEntry>=price>3766 or 3756+self.trgEntry>=price>3756 or 3744+self.trgEntry>=price>3744 or 3733+self.trgEntry>=price>3733 or 3720+self.trgEntry>=price>3720 or 3700+self.trgEntry>=price>3700 or 3724+self.trgEntry>=price>3724 or 3706+self.trgEntry>=price>3706:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=19):
				if 3734+self.trgEntry>=price>3734 or 3722+self.trgEntry>=price>3722 or 3703+self.trgEntry>=price>3703 or 3696+self.trgEntry>=price>3696 or 3685+self.trgEntry>=price>3685 or 3677+self.trgEntry>=price>3677 or 3667+self.trgEntry>=price>3667:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=20):
				if 3749+self.trgEntry>=price>3749 or 3740+self.trgEntry>=price>3740 or 3729+self.trgEntry>=price>3729 or 3719+self.trgEntry>=price>3719 or 3700+self.trgEntry>=price>3700 or 3695+self.trgEntry>=price>3695 or 3682+self.trgEntry>=price>3682 or 3669+self.trgEntry>=price>3669 or 3677+self.trgEntry>=price>3677:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=21):
				if 3661+self.trgEntry>=price>3661 or 3671+self.trgEntry>=price>3671 or 3685+self.trgEntry>=price>3685 or 3700+self.trgEntry>=price>3700 or 3748+self.trgEntry>=price>3748:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=24):
				if 3815+self.trgEntry>=price>3815 or 3805+self.trgEntry>=price>3805 or 3800+self.trgEntry>=price>3800 or 3795+self.trgEntry>=price>3795 or 3777+self.trgEntry>=price>3777 or 3769+self.trgEntry>=price>3769 or 3748+self.trgEntry>=price>3748 or 3763+self.trgEntry>=price>3763:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=25):
				if 3859+self.trgEntry>=price>3859 or 3844+self.trgEntry>=price>3844 or 3832+self.trgEntry>=price>3832 or 3812+self.trgEntry>=price>3812 or 3801.5+self.trgEntry>=price>3801.5 or 3792+self.trgEntry>=price>3792 or 3778+self.trgEntry>=price>3778 or 3817+self.trgEntry>=price>3817:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=26):
				if 3900+self.trgEntry>=price>3900 or 3893+self.trgEntry>=price>3893 or 3882+self.trgEntry>=price>3882 or 3864+self.trgEntry>=price>3864 or 3848+self.trgEntry>=price>3848 or 3834+self.trgEntry>=price>3834 or 3854+self.trgEntry>=price>3854 or 3874+self.trgEntry>=price>3874:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=27):
				if 3874+self.trgEntry>=price>3874 or 3864+self.trgEntry>=price>3864 or 3853+self.trgEntry>=price>3853 or 3844+self.trgEntry>=price>3844 or 3835+self.trgEntry>=price>3835 or 3821+self.trgEntry>=price>3821 or 3808+self.trgEntry>=price>3808 or 3800+self.trgEntry>=price>3800 or 3793+self.trgEntry>=price>3793:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=28):
				if 3900+self.trgEntry>=price>3900 or 3870+self.trgEntry>=price>3870 or 3852+self.trgEntry>=price>3852 or 3835+self.trgEntry>=price>3835 or 3822+self.trgEntry>=price>3822:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=31):
				if 3910+self.trgEntry>=price>3910 or 3900+self.trgEntry>=price>3900 or 3892+self.trgEntry>=price>3892 or 3872+self.trgEntry>=price>3872 or 3883+self.trgEntry>=price>3883:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=1):
				if 3927+self.trgEntry>=price>3927 or 3915+self.trgEntry>=price>3915 or 3900+self.trgEntry>=price>3900 or 3882+self.trgEntry>=price>3882 or 3863+self.trgEntry>=price>3863 or 3846+self.trgEntry>=price>3846 or 3873+self.trgEntry>=price>3873 or 3904+self.trgEntry>=price>3904:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=2):
				if 3901.5+self.trgEntry>=price>3901.5 or 3895+self.trgEntry>=price>3895 or 3886+self.trgEntry>=price>3886 or 3873+self.trgEntry>=price>3873 or 3865+self.trgEntry>=price>3865 or 3857+self.trgEntry>=price>3857 or 3847+self.trgEntry>=price>3847 or 3833+self.trgEntry>=price>3833 or 3820+self.trgEntry>=price>3820 or 3807+self.trgEntry>=price>3807 or 3800+self.trgEntry>=price>3800 or 3881+self.trgEntry>=price>3881:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=3):
				if 3760+self.trgEntry>=price>3760 or 3748+self.trgEntry>=price>3748 or 3739+self.trgEntry>=price>3739 or 3727+self.trgEntry>=price>3727 or 3700+self.trgEntry>=price>3700 or 3690+self.trgEntry>=price>3690 or 3756.5+self.trgEntry>=price>3756.5:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=4):
				if 3800+self.trgEntry>=price>3800 or 3791+self.trgEntry>=price>3791 or 3777+self.trgEntry>=price>3777 or 3768+self.trgEntry>=price>3768 or 3759+self.trgEntry>=price>3759 or 3750+self.trgEntry>=price>3750 or 3736+self.trgEntry>=price>3736 or 3726+self.trgEntry>=price>3726 or 3715+self.trgEntry>=price>3715:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=7):
				if 3816+self.trgEntry>=price>3816 or 3803+self.trgEntry>=price>3803 or 3799+self.trgEntry>=price>3799 or 3784+self.trgEntry>=price>3784 or 3771+self.trgEntry>=price>3771:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=8):
				if 3867+self.trgEntry>=price>3867 or 3858+self.trgEntry>=price>3858 or 3843+self.trgEntry>=price>3843 or 3830+self.trgEntry>=price>3830 or 3817+self.trgEntry>=price>3817 or 3800+self.trgEntry>=price>3800 or 3787+self.trgEntry>=price>3787 or 3834+self.trgEntry>=price>3834 or 3821+self.trgEntry>=price>3821:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=9):
				if 3831+self.trgEntry>=price>3831 or 3822+self.trgEntry>=price>3822 or 3812+self.trgEntry>=price>3812 or 3800+self.trgEntry>=price>3800 or 3789+self.trgEntry>=price>3789 or 3775+self.trgEntry>=price>3775 or 3759+self.trgEntry>=price>3759:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=10):
				if 3867+self.trgEntry>=price>3867 or 3888+self.trgEntry>=price>3888 or 3899+self.trgEntry>=price>3899 or 3907+self.trgEntry>=price>3907 or 3918+self.trgEntry>=price>3918 or 3933+self.trgEntry>=price>3933:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=11):
				if 4011+self.trgEntry>=price>4011 or 4000+self.trgEntry>=price>4000 or 3995+self.trgEntry>=price>3995 or 3986+self.trgEntry>=price>3986 or 3975+self.trgEntry>=price>3975 or 3966+self.trgEntry>=price>3966 or 3949+self.trgEntry>=price>3949 or 3957+self.trgEntry>=price>3957:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.longEntryTicket = self.StopMarketOrder(self.es.Symbol, -self.qty, price-5)
						self.trgPrice = price


			# Long entries
			if self.Time == self.Time.replace(year=2022,month=9,day=9):
				if 4095-self.trgEntry<=price<4095 or 4078-self.trgEntry<=price<4078 or 4064-self.trgEntry<=price<4064 or 4057-self.trgEntry<=price<4057 or 4045-self.trgEntry<=price<4045:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=12):
				if 4141-self.trgEntry<=price<4141 or 4127-self.trgEntry<=price<4127 or 4115-self.trgEntry<=price<4115 or 4106-self.trgEntry<=price<4106 or 4000-self.trgEntry<=price<4000:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=13):
				if 4046-self.trgEntry<=price<4046 or 4036.5-self.trgEntry<=price<4036.5 or 4027.5-self.trgEntry<=price<4027.5 or 4015-self.trgEntry<=price<4015 or 4005-self.trgEntry<=price<4005 or 4000-self.trgEntry<=price<4000 or 3995-self.trgEntry<=price<3995:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=14):
				if 3983-self.trgEntry<=price<3983 or 3971-self.trgEntry<=price<3971 or 3964-self.trgEntry<=price<3964 or 3951-self.trgEntry<=price<3951 or 3942-self.trgEntry<=price<3942 or 3934-self.trgEntry<=price<3934 or 3924-self.trgEntry<=price<3924 or 3916 or 3979-self.trgEntry<=price<3979 or 3938-self.trgEntry<=price<3938:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=15):
				if 3981-self.trgEntry<=price<3981 or 3974-self.trgEntry<=price<3974 or 3959-self.trgEntry<=price<3959 or 3953-self.trgEntry<=price<3953 or 3944-self.trgEntry<=price<3944 or 3929-self.trgEntry<=price<3929 or 3919-self.trgEntry<=price<3919 or 3948.5-self.trgEntry<=price<3948.5 or 3977.5-self.trgEntry<=price<3977.5 or 3900-self.trgEntry<=price<3900:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=16):
				if 3901-self.trgEntry<=price<3901 or 3890-self.trgEntry<=price<3890 or 3882-self.trgEntry<=price<3882 or 3873-self.trgEntry<=price<3873 or 3858-self.trgEntry<=price<3858 or 3850-self.trgEntry<=price<3850 or 3863-self.trgEntry<=price<3863 or 3894-self.trgEntry<=price<3894:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=19):
				if 3895-self.trgEntry<=price<3895 or 3889-self.trgEntry<=price<3889 or 3877-self.trgEntry<=price<3877 or 3869-self.trgEntry<=price<3869 or 3860-self.trgEntry<=price<3860 or 3854-self.trgEntry<=price<3854 or 3900-self.trgEntry<=price<3900:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=20):
				if 3893-self.trgEntry<=price<3893 or 3881-self.trgEntry<=price<3881 or 3872-self.trgEntry<=price<3872 or 3866-self.trgEntry<=price<3866 or 3855-self.trgEntry<=price<3855 or 3846-self.trgEntry<=price<3846:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=21):
				if 3926-self.trgEntry<=price<3926 or 3913-self.trgEntry<=price<3913 or 3904-self.trgEntry<=price<3904 or 3900-self.trgEntry<=price<3900 or 3893-self.trgEntry<=price<3893 or 3884-self.trgEntry<=price<3884 or 3875-self.trgEntry<=price<3875 or 3871-self.trgEntry<=price<3871 or 3862-self.trgEntry<=price<3862 or 3843-self.trgEntry<=price<3843 or 3800-self.trgEntry<=price<3800:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=22):
				if 3801-self.trgEntry<=price<3801 or 3794-self.trgEntry<=price<3794 or 3781-self.trgEntry<=price<3781 or 3774-self.trgEntry<=price<3774 or 3763-self.trgEntry<=price<3763 or 3767-self.trgEntry<=price<3767:
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=23):
				if 3736-self.trgEntry<=price<3736 or 3723-self.trgEntry<=price<3723 or 3716-self.trgEntry<=price<3716 or 3701-self.trgEntry<=price<3701 or 3691-self.trgEntry<=price<3691 or 3685-self.trgEntry<=price<3685:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=26):
				if 3727-self.trgEntry<=price<3727 or 3712-self.trgEntry<=price<3712 or 3700-self.trgEntry<=price<3700 or 3693-self.trgEntry<=price<3693 or 3678-self.trgEntry<=price<3678 or 3670-self.trgEntry<=price<3670 or 3662-self.trgEntry<=price<3662 or 3718-self.trgEntry<=price<3718:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=27):
				if 3734-self.trgEntry<=price<3734 or 3725-self.trgEntry<=price<3725 or 3716-self.trgEntry<=price<3716 or 3704-self.trgEntry<=price<3704 or 3700-self.trgEntry<=price<3700 or 3696-self.trgEntry<=price<3696 or 3688-self.trgEntry<=price<3688 or 3677-self.trgEntry<=price<3677 or 3665-self.trgEntry<=price<3665 or 3730-self.trgEntry<=price<3730:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=28):
				if 3713-self.trgEntry<=price<3713 or 3700-self.trgEntry<=price<3700 or 3686-self.trgEntry<=price<3686 or 3674-self.trgEntry<=price<3674 or 3668-self.trgEntry<=price<3668 or 3658-self.trgEntry<=price<3658 or 3681-self.trgEntry<=price<3681:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=29):
				if 3699-self.trgEntry<=price<3699 or 3694-self.trgEntry<=price<3694 or 3688-self.trgEntry<=price<3688 or 3678-self.trgEntry<=price<3678 or 3669-self.trgEntry<=price<3669 or 3658-self.trgEntry<=price<3658 or 3640-self.trgEntry<=price<3640 or 3673-self.trgEntry<=price<3673 or 3653-self.trgEntry<=price<3653:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=9,day=30):
				if 3685-self.trgEntry<=price<3685 or 3672-self.trgEntry<=price<3672 or 3658-self.trgEntry<=price<3658 or 3648-self.trgEntry<=price<3648 or 3635-self.trgEntry<=price<3635 or 3625-self.trgEntry<=price<3625 or 3612-self.trgEntry<=price<3612 or 3640.5-self.trgEntry<=price<3640.5 or 3600-self.trgEntry<=price<3600:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=3):
				if 3690-self.trgEntry<=price<3690 or 3682-self.trgEntry<=price<3682 or 3670-self.trgEntry<=price<3670 or 3659-self.trgEntry<=price<3659 or 3642-self.trgEntry<=price<3642 or 3634-self.trgEntry<=price<3634 or 3623-self.trgEntry<=price<3623 or 3614-self.trgEntry<=price<3614 or 3700-self.trgEntry<=price<3700:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=4):
				if 3788-self.trgEntry<=price<3788 or 3775-self.trgEntry<=price<3775 or 3766-self.trgEntry<=price<3766 or 3760-self.trgEntry<=price<3760 or 3800-self.trgEntry<=price<3800:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=5):
				if 3819-self.trgEntry<=price<3819 or 3809.5-self.trgEntry<=price<3809.5 or 3801-self.trgEntry<=price<3801 or 3794-self.trgEntry<=price<3794 or 3780.25-self.trgEntry<=price<3780.25 or 3769.5-self.trgEntry<=price<3769.5 or 3760-self.trgEntry<=price<3760 or 3749-self.trgEntry<=price<3749 or 3737-self.trgEntry<=price<3737 or 3727-self.trgEntry<=price<3727 or 3787-self.trgEntry<=price<3787 or 3744-self.trgEntry<=price<3744:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=6):
				if 3810-self.trgEntry<=price<3810 or 3799.5-self.trgEntry<=price<3799.5 or 3786-self.trgEntry<=price<3786 or 3777-self.trgEntry<=price<3777 or 3772.5-self.trgEntry<=price<3772.5 or 3765-self.trgEntry<=price<3765 or 3754.5-self.trgEntry<=price<3754.5 or 3791-self.trgEntry<=price<3791:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=7):
				if 3710-self.trgEntry<=price<3710 or 3700-self.trgEntry<=price<3700 or 3686-self.trgEntry<=price<3686 or 3678-self.trgEntry<=price<3678 or 3673-self.trgEntry<=price<3673:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=10):
				if 3658-self.trgEntry<=price<3658 or 3650-self.trgEntry<=price<3650 or 3641-self.trgEntry<=price<3641 or 3631-self.trgEntry<=price<3631 or 3615-self.trgEntry<=price<3615 or 3605-self.trgEntry<=price<3605 or 3600-self.trgEntry<=price<3600 or 3623.5-self.trgEntry<=price<3623.5:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=11):
				if 3653.5-self.trgEntry<=price<3653.5 or 3647-self.trgEntry<=price<3647 or 3641-self.trgEntry<=price<3641 or 3629-self.trgEntry<=price<3629 or 3621-self.trgEntry<=price<3621 or 3610-self.trgEntry<=price<3610 or 3599.5-self.trgEntry<=price<3599.5 or 3586-self.trgEntry<=price<3586:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=12):
				if 3616-self.trgEntry<=price<3616 or 3601-self.trgEntry<=price<3601 or 3597-self.trgEntry<=price<3597 or 3587-self.trgEntry<=price<3587:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=13):
				if 3570-self.trgEntry<=price<3570 or 3562-self.trgEntry<=price<3562 or 3547-self.trgEntry<=price<3547 or 3536-self.trgEntry<=price<3536 or 3530-self.trgEntry<=price<3530 or 3516-self.trgEntry<=price<3516 or 3501-self.trgEntry<=price<3501 or 3644-self.trgEntry<=price<3644 or 3582-self.trgEntry<=price<3582 or 3620-self.trgEntry<=price<3620:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=14):
				if 3726-self.trgEntry<=price<3726 or 3718-self.trgEntry<=price<3718 or 3709-self.trgEntry<=price<3709 or 3700-self.trgEntry<=price<3700 or 3695-self.trgEntry<=price<3695 or 3685-self.trgEntry<=price<3685 or 3674-self.trgEntry<=price<3674 or 3665-self.trgEntry<=price<3665 or 3600-self.trgEntry<=price<3600:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=17):
				if 3700-self.trgEntry<=price<3700 or 3693-self.trgEntry<=price<3693 or 3683-self.trgEntry<=price<3683 or 3674-self.trgEntry<=price<3674 or 3666-self.trgEntry<=price<3666:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=18):
				if 3776-self.trgEntry<=price<3776 or 3766-self.trgEntry<=price<3766 or 3756-self.trgEntry<=price<3756 or 3744-self.trgEntry<=price<3744 or 3733-self.trgEntry<=price<3733 or 3720-self.trgEntry<=price<3720 or 3700-self.trgEntry<=price<3700 or 3724-self.trgEntry<=price<3724 or 3706-self.trgEntry<=price<3706:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=19):
				if 3734-self.trgEntry<=price<3734 or 3722-self.trgEntry<=price<3722 or 3703-self.trgEntry<=price<3703 or 3696-self.trgEntry<=price<3696 or 3685-self.trgEntry<=price<3685 or 3677-self.trgEntry<=price<3677 or 3667-self.trgEntry<=price<3667:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=20):
				if 3749-self.trgEntry<=price<3749 or 3740-self.trgEntry<=price<3740 or 3729-self.trgEntry<=price<3729 or 3719-self.trgEntry<=price<3719 or 3700-self.trgEntry<=price<3700 or 3695-self.trgEntry<=price<3695 or 3682-self.trgEntry<=price<3682 or 3669-self.trgEntry<=price<3669 or 3677-self.trgEntry<=price<3677:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=21):
				if 3661-self.trgEntry<=price<3661 or 3671-self.trgEntry<=price<3671 or 3685-self.trgEntry<=price<3685 or 3700-self.trgEntry<=price<3700 or 3748-self.trgEntry<=price<3748:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=24):
				if 3815-self.trgEntry<=price<3815 or 3805-self.trgEntry<=price<3805 or 3800-self.trgEntry<=price<3800 or 3795-self.trgEntry<=price<3795 or 3777-self.trgEntry<=price<3777 or 3769-self.trgEntry<=price<3769 or 3748-self.trgEntry<=price<3748 or 3763-self.trgEntry<=price<3763:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=25):
				if 3859-self.trgEntry<=price<3859 or 3844-self.trgEntry<=price<3844 or 3832-self.trgEntry<=price<3832 or 3812-self.trgEntry<=price<3812 or 3801.5-self.trgEntry<=price<3801.5 or 3792-self.trgEntry<=price<3792 or 3778-self.trgEntry<=price<3778 or 3817-self.trgEntry<=price<3817:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=26):
				if 3900-self.trgEntry<=price<3900 or 3893-self.trgEntry<=price<3893 or 3882-self.trgEntry<=price<3882 or 3864-self.trgEntry<=price<3864 or 3848-self.trgEntry<=price<3848 or 3834-self.trgEntry<=price<3834 or 3854-self.trgEntry<=price<3854 or 3874-self.trgEntry<=price<3874:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=27):
				if 3874-self.trgEntry<=price<3874 or 3864-self.trgEntry<=price<3864 or 3853-self.trgEntry<=price<3853 or 3844-self.trgEntry<=price<3844 or 3835-self.trgEntry<=price<3835 or 3821-self.trgEntry<=price<3821 or 3808-self.trgEntry<=price<3808 or 3800-self.trgEntry<=price<3800 or 3793-self.trgEntry<=price<3793:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=28):
				if 3900-self.trgEntry<=price<3900 or 3870-self.trgEntry<=price<3870 or 3852-self.trgEntry<=price<3852 or 3835-self.trgEntry<=price<3835 or 3822-self.trgEntry<=price<3822:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=10,day=31):
				if 3910-self.trgEntry<=price<3910 or 3900-self.trgEntry<=price<3900 or 3892-self.trgEntry<=price<3892 or 3872-self.trgEntry<=price<3872 or 3883-self.trgEntry<=price<3883:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=1):
				if 3927-self.trgEntry<=price<3927 or 3915-self.trgEntry<=price<3915 or 3900-self.trgEntry<=price<3900 or 3882-self.trgEntry<=price<3882 or 3863-self.trgEntry<=price<3863 or 3846-self.trgEntry<=price<3846 or 3873-self.trgEntry<=price<3873 or 3904-self.trgEntry<=price<3904:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=2):
				if 3901.5-self.trgEntry<=price<3901.5 or 3895-self.trgEntry<=price<3895 or 3886-self.trgEntry<=price<3886 or 3873-self.trgEntry<=price<3873 or 3865-self.trgEntry<=price<3865 or 3857-self.trgEntry<=price<3857 or 3847-self.trgEntry<=price<3847 or 3833-self.trgEntry<=price<3833 or 3820-self.trgEntry<=price<3820 or 3807-self.trgEntry<=price<3807 or 3800-self.trgEntry<=price<3800 or 3881-self.trgEntry<=price<3881:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=3):
				if 3760-self.trgEntry<=price<3760 or 3748-self.trgEntry<=price<3748 or 3739-self.trgEntry<=price<3739 or 3727-self.trgEntry<=price<3727 or 3700-self.trgEntry<=price<3700 or 3690-self.trgEntry<=price<3690 or 3756.5-self.trgEntry<=price<3756.5:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=4):
				if 3800-self.trgEntry<=price<3800 or 3791-self.trgEntry<=price<3791 or 3777-self.trgEntry<=price<3777 or 3768-self.trgEntry<=price<3768 or 3759-self.trgEntry<=price<3759 or 3750-self.trgEntry<=price<3750 or 3736-self.trgEntry<=price<3736 or 3726-self.trgEntry<=price<3726 or 3715-self.trgEntry<=price<3715:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=7):
				if 3816-self.trgEntry<=price<3816 or 3803-self.trgEntry<=price<3803 or 3799-self.trgEntry<=price<3799 or 3784-self.trgEntry<=price<3784 or 3771-self.trgEntry<=price<3771:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=8):
				if 3867-self.trgEntry<=price<3867 or 3858-self.trgEntry<=price<3858 or 3843-self.trgEntry<=price<3843 or 3830-self.trgEntry<=price<3830 or 3817-self.trgEntry<=price<3817 or 3800-self.trgEntry<=price<3800 or 3787-self.trgEntry<=price<3787 or 3834-self.trgEntry<=price<3834 or 3821-self.trgEntry<=price<3821:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=9):
				if 3831-self.trgEntry<=price<3831 or 3822-self.trgEntry<=price<3822 or 3812-self.trgEntry<=price<3812 or 3800-self.trgEntry<=price<3800 or 3789-self.trgEntry<=price<3789 or 3775-self.trgEntry<=price<3775 or 3759-self.trgEntry<=price<3759:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=10):
				if 3867-self.trgEntry<=price<3867 or 3888-self.trgEntry<=price<3888 or 3899-self.trgEntry<=price<3899 or 3907-self.trgEntry<=price<3907 or 3918-self.trgEntry<=price<3918 or 3933-self.trgEntry<=price<3933:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
			if self.Time == self.Time.replace(year=2022,month=11,day=11):
				if 4011-self.trgEntry<=price<4011 or 4000-self.trgEntry<=price<4000 or 3995-self.trgEntry<=price<3995 or 3986-self.trgEntry<=price<3986 or 3975-self.trgEntry<=price<3975 or 3966-self.trgEntry<=price<3966 or 3949-self.trgEntry<=price<3949 or 3957-self.trgEntry<=price<3957:
					if not self.Transactions.GetOpenOrders(self.es.Symbol):
						self.shortEntryTicket = self.StopMarketOrder(self.es.Symbol, self.qty, price+5)
						self.trgPrice = price
	
		# Trailing stop and possible cancellation if a short entry order is open 
		if self.longEntryTicket is not None and self.longEntryTicket.Status != OrderStatus.Filled:
        	# Trailing stop
			if price < self.lowestPrice:
				self.lowestPrice = price
			if self.lowestPrice + self.trailEntry <= price:
				updateFields = UpdateOrderFields()
				updateFields.StopPrice = price+5
				self.longEntryTicket.Update(updateFields)
            # Cancel order and save time if price is not rejecting the level
			if price <= self.trgPrice - self.entryCancel:
				self.longEntryTicket.Cancel()
				self.cancelTime = self.Time
			# Cancel order if near end of RTH
			if self.Time > self.stopAftM:
				self.longEntryTicket.Cancel()
			# Reset long entry ticket, lowest price, and trigger price variables if order is canceled
			if self.longEntryTicket.Status == OrderStatus.Canceled:
				self.longEntryTicket = None
				self.lowestPrice = 25000
				self.trgPrice = 0

		# Trailing stop and possible cancellation if a long entry order is open 
		if self.shortEntryTicket is not None and self.shortEntryTicket.Status != OrderStatus.Filled:
			# Trailing stop
			if price > self.highestPrice:
				self.highestPrice = price
			if self.highestPrice - self.trailEntry >= price:
				updateFields = UpdateOrderFields()
				updateFields.StopPrice = price-5
				self.shortEntryTicket.Update(updateFields)
			# Cancel order and save time if price is not rejecting the level
			if price >= self.trgPrice + self.entryCancel:
				self.shortEntryTicket.Cancel()
				self.cancelTime = self.Time
			# Cancel order if near end of RTH
			if self.Time > self.stopAftM:
				self.shortEntryTicket.Cancel()
			# Reset long entry ticket, lowest price, and trigger price variables if order is canceled
			if self.shortEntryTicket.Status == OrderStatus.Canceled:
				self.shortEntryTicket = None
				self.highestPrice = 25000
				self.trgPrice = 0

		# Trailing stop and possible cancellation and liquidation if a short closing order is open
		if self.sExTick is not None and self.Portfolio.Invested:
			# Trailing stop which updates as the position moves favorably
			if self.longEntryTicket.AverageFillPrice + self.stopLoss <= price:
				updateFields = UpdateOrderFields()
				updateFields.LimitPrice = price + 2
				self.sExTick.Update(updateFields)
			if self.longEntryTicket.AverageFillPrice - .75 >= price:
				self.stopLoss = 2.25
			
		# Trailing stop and possible cancellation and liquidation if a long closing order is open
		if self.lExTick is not None and self.Portfolio.Invested:
			# Trailing stop which updates as the position moves favorably
			if self.shortEntryTicket.AverageFillPrice - self.stopLoss >= price:
				updateFields = UpdateOrderFields()
				updateFields.LimitPrice = price - 2
				self.lExTick.Update(updateFields)
			if self.shortEntryTicket.AverageFillPrice + .75 <= price:
				self.stopLoss = 2.25

	def OnOrderEvent(self, orderEvent):
		if orderEvent.Status != OrderStatus.Filled:
			return

		# Submit short exit order when long entry is filled
		if self.longEntryTicket is not None and self.Portfolio.Invested:
			self.sExTick = self.LimitOrder(self.es.Symbol, self.qty, self.longEntryTicket.AverageFillPrice - 0.5)
			# Reset price variables
			self.trgPrice = 0
			self.highestPrice = 0
			self.lowestPrice = 25000

		# Submit long exit order when short entry is filled
		if self.shortEntryTicket is not None and self.Portfolio.Invested:
			self.lExTick = self.LimitOrder(self.es.Symbol, -self.qty, self.shortEntryTicket.AverageFillPrice + 0.5)
			# Reset price variables
			self.trgPrice = 0
			self.highestPrice = 0
			self.lowestPrice = 25000

		# Save time and reset price and ticket variables when a short exit order fills & record W/L & high/low intraday P/L
		if self.sExTick is not None and self.sExTick.OrderId == orderEvent.OrderId:

			self.tallyTtl += 1
			if self.longEntryTicket.AverageFillPrice > self.sExTick.AverageFillPrice:
				self.tallyWin += 1
				self.shortWin += 1
			if self.longEntryTicket.AverageFillPrice < self.sExTick.AverageFillPrice:
				self.tallyLoss += 1
			if self.longEntryTicket.AverageFillPrice == self.sExTick.AverageFillPrice:
				self.tallyEven += 1

			if self.Portfolio.Cash > self.highBal:
				self.highBal = self.Portfolio.Cash
				self.highTime = int(round(self.Time.timestamp())) - self.startTime
			if self.Portfolio.Cash < self.lowBal:
				self.lowBal = self.Portfolio.Cash
				self.lowTime = int(round(self.Time.timestamp())) - self.startTime
			if self.Portfolio.Cash < self.highBal:
				if self.highBal - self.Portfolio.Cash > self.drawD:
					self.drawD = self.highBal - self.Portfolio.Cash
					self.dDTime = int(round(self.Time.timestamp())) - self.startTime

			self.exitFillTime = self.Time
			self.longEntryTicket = None
			self.sExTick = None
			self.highestPrice = 0
			self.lowestPrice = 25000
			self.stopLoss = 2.5

		# Save time and reset price and ticket variables when a long exit order fills and record W/L & high/low intraday P/L
		if self.lExTick is not None and self.lExTick.OrderId == orderEvent.OrderId:

			self.tallyTtl += 1
			if self.shortEntryTicket.AverageFillPrice < self.lExTick.AverageFillPrice:
				self.tallyWin += 1
				self.longWin += 1
			if self.shortEntryTicket.AverageFillPrice > self.lExTick.AverageFillPrice:
				self.tallyLoss += 1
			if self.shortEntryTicket.AverageFillPrice == self.lExTick.AverageFillPrice:
				self.tallyEven += 1

			if self.Portfolio.Cash > self.highBal:
				self.highBal = self.Portfolio.Cash
				self.highTime = int(round(self.Time.timestamp())) - self.startTime
			if self.Portfolio.Cash < self.lowBal:
				self.lowBal = self.Portfolio.Cash
				self.lowTime = int(round(self.Time.timestamp())) - self.startTime
			if self.Portfolio.Cash < self.highBal:
				if self.highBal - self.Portfolio.Cash > self.drawD:
					self.drawD = self.highBal - self.Portfolio.Cash
					self.dDTime = int(round(self.Time.timestamp())) - self.startTime

			self.exitFillTime = self.Time
			self.shortEntryTicket = None
			self.lExTick = None
			self.highestPrice = 0
			self.lowestPrice = 25000
			self.stopLoss = 2.5