I just reviewed the code that was included in the algorithm I sent over and noticed that it seems to be missing a lot. It's most definitely not “functioning.” We have onData, onDataConsolidated, and “futureExecution" in a library. Apologies!
class OnData:
def __init__(self, context):
self.context = context
def RunOnData(self, slice, assetType):
portfolioProfitLoss = self.context.Portfolio.TotalProfit
if portfolioProfitLoss != 0.0:
portfolioQuotient = portfolioProfitLoss / self.context.portfolioAmmount
portfolioPercent = portfolioQuotient * 100
if (portfolioPercent <= -3.0):
self.context.contracts.clear()
# Max loss for each contract.
for contract in self.context.contracts:
profitLoss = self.context.Portfolio[contract].UnrealizedProfit
if (profitLoss != 0.0):
quotient = profitLoss / self.context.portfolioAmmount
percent = quotient * 100
if percent <= -1.0:
self.context.contracts.remove(contract)
for self.liquidContract in self.context.contracts:
multiplier = None
if assetType == "Future":
contractString = str(self.liquidContract)
market = contractString.split()[0]
if market == "GC":
multiplier = 10
if market == "NQ":
multiplier = 5
if market == "HG":
multiplier = 12.5
if market == "NG":
multiplier = 10
if market == "CL":
multiplier = 10
if not self.liquidContract in self.context.symbolData:
continue
self.quickTradeBarWindow = self.context.symbolData[self.liquidContract].quickTradeBarWindow
if self.quickTradeBarWindow == None:
continue
if slice.ContainsKey(self.liquidContract):
self.quickTradeBarWindow.Add(self.context.CurrentSlice[self.liquidContract])
self.context.symbolData[self.liquidContract].quickTradeBarWindow = self.quickTradeBarWindow
if not self.quickTradeBarWindow.IsReady:
continue
self.openOrders = self.context.Transactions.GetOpenOrders(self.liquidContract)
# VARIABLES & LIST
self.rules = []
self.longOrders = []
self.shortOrders = []
self.bull_losing_outcome = False
self.bear_losing_outcome = False
# Get or set correct variable values
self.bearFive = self.context.bearFiveDict.get(self.liquidContract)
self.bullFive = self.context.bullFiveDict.get(self.liquidContract)
self.bullOne = self.context.bullOneDict.get(self.liquidContract)
self.bearOne = self.context.bearOneDict.get(self.liquidContract)
self.FiveUp = self.context.FiveUpDict.get(self.liquidContract)
self.FiveDown = self.context.FiveDownDict.get(self.liquidContract)
self.first_low = self.context.first_lowDict.get(self.liquidContract)
self.second_low = self.context.second_lowDict.get(self.liquidContract)
self.first_high = self.context.first_highDict.get(self.liquidContract)
self.second_high = self.context.second_highDict.get(self.liquidContract)
self.first_position_bull = self.context.first_position_bullDict.get(self.liquidContract)
self.second_position_bull = self.context.second_position_bullDict.get(self.liquidContract)
self.first_position_bear = self.context.first_position_bearDict.get(self.liquidContract)
self.second_position_bear = self.context.second_position_bearDict.get(self.liquidContract)
self.entry_minute = self.context.entry_minuteDict.get(self.liquidContract)
self.support = self.context.supportDict.get(self.liquidContract)
self.resistance = self.context.resistanceDict.get(self.liquidContract)
self.breakout = self.context.breakoutDict.get(self.liquidContract)
self.breakdown = self.context.breakdownDict.get(self.liquidContract)
self.short_stop = self.context.short_stopDict.get(self.liquidContract)
self.second_short_stop = self.context.second_short_stopDict.get(self.liquidContract)
self.long_stop = self.context.long_stopDict.get(self.liquidContract)
self.second_long_stop = self.context.second_long_stopDict.get(self.liquidContract)
self.long_entry = self.context.long_entryDict.get(self.liquidContract)
self.second_long_entry = self.context.second_long_entryDict.get(self.liquidContract)
self.short_entry = self.context.short_entryDict.get(self.liquidContract)
self.second_short_entry = self.context.second_short_entryDict.get(self.liquidContract)
self.long_exit = self.context.long_exitDict.get(self.liquidContract)
self.second_long_exit = self.context.second_long_exitDict.get(self.liquidContract)
self.short_exit = self.context.short_exitDict.get(self.liquidContract)
self.second_short_exit = self.context.second_short_exitDict.get(self.liquidContract)
self.partial_exit_fill_safety_long = self.context.partial_exit_fill_safety_longDict.get(self.liquidContract)
self.partial_fill_liq_price = self.context.partial_fill_liq_priceDict.get(self.liquidContract)
self.partial_exit_fill_safety_short = self.context.partial_exit_fill_safety_shortDict.get(self.liquidContract)
self.risk_target = self.context.risk_targetDict.get(self.liquidContract)
self.buying_power = self.context.buying_powerDict.get(self.liquidContract)
self.share_size = self.context.share_sizeDict.get(self.liquidContract)
self.dollar_size = self.context.dollar_sizeDict.get(self.liquidContract)
self.max_price_long = self.context.max_price_longDict.get(self.liquidContract)
self.max_price_short = self.context.max_price_shortDict.get(self.liquidContract)
self.trading_hours = self.context.trading_hoursDict.get(self.liquidContract)
if self.context.rulesDict.get(self.liquidContract) == None:
self.rules = []
else:
self.rules = self.context.rulesDict.get(self.liquidContract)
if self.context.longOrdersDict.get(self.liquidContract) == None:
self.longOrders = []
else:
self.longOrders = self.context.longOrdersDict.get(self.liquidContract)
if self.context.shortOrdersDict.get(self.liquidContract) == None:
self.shortOrders = []
else:
self.shortOrders = self.context.shortOrdersDict.get(self.liquidContract)
if self.context.bull_losing_outcomeDict.get(self.liquidContract) == None:
self.bull_losing_outcome = False
else:
self.bull_losing_outcome = self.context.bull_losing_outcomeDict.get(self.liquidContract)
if self.context.bear_losing_outcomeDict.get(self.liquidContract) == None:
#Used to check if the first trade of a break was a loser.
self.bear_losing_outcome = False
else:
self.bear_losing_outcome = self.context.bear_losing_outcomeDict.get(self.liquidContract)
#UPDATE QUICK WINDOW
self.trading_hours = True
#TRADING HOURS
#if self.context.Time.hour > 5 and self.context.Time.hour < 16:
# self.trading_hours = True
#else:
# self.trading_hours = False
#if self.context.Time.hour >= 15 and self.context.Time.minute >= 58 or self.context.Time.hour >= 16:
# self.context.Liquidate()
# continue
#LIQUIDATES POSITIONS IN THE EVENT OF A DOUBLE-FILL
if self.openOrders is [] and self.context.Portfolio.Invested:
self.context.Liquidate(self.liquidContract)
#FRACTAL FOR BULLISH ENTRY AND HIGHER HIGH CALC
if (self.quickTradeBarWindow[3].Low > self.quickTradeBarWindow[1].Low < self.quickTradeBarWindow[2].Low) and (self.quickTradeBarWindow[0].Low > self.quickTradeBarWindow[1].Low):
self.OneUp = True
else:
self.OneUp = False
if (self.quickTradeBarWindow[4].High < self.quickTradeBarWindow[2].High > self.quickTradeBarWindow[3].High) and (self.quickTradeBarWindow[1].High < self.quickTradeBarWindow[2].High > self.quickTradeBarWindow[0].High):
self.OneDown = True
else:
self.OneDown = False
if self.OneUp is True:
self.bullOne = 1
else:
self.bullOne = 0
if self.OneDown is True:
self.bearOne = -1
else:
self.bearOne = 0
if self.OneUp is True:
self.max_price_short = self.quickTradeBarWindow[0].Low
#FRACTAL FOR BEARISH ENTRY AND LOWER LOW CALC
if (self.quickTradeBarWindow[4].Low > self.quickTradeBarWindow[2].Low < self.quickTradeBarWindow[3].Low) and (self.quickTradeBarWindow[1].Low > self.quickTradeBarWindow[2].Low < self.quickTradeBarWindow[0].Low):
self.OneUpBear = True
else:
self.OneUpBear = False
if (self.quickTradeBarWindow[3].High < self.quickTradeBarWindow[1].High > self.quickTradeBarWindow[2].High) and (self.quickTradeBarWindow[0].High < self.quickTradeBarWindow[1].High):
self.OneDownBear = True
else:
self.OneDownBear = False
if self.OneUpBear is True:
self.bullOneRed = 1
else:
self.bullOneRed = 0
if self.OneDownBear is True:
self.bearOneRed = -1
else:
self.bearOneRed = 0
if self.OneDownBear is True:
self.max_price_long = self.quickTradeBarWindow[0].High
#HIGHER HIGHS...
if self.bearOne == -1 and self.first_high is None:
self.first_high = self.quickTradeBarWindow[2].High
#If we have the first high but not the second...
elif self.bearOne == -1 and self.first_high is not None and self.second_high is None:
self.second_high = self.quickTradeBarWindow[2].High
#If we have the first two highs and there's a new fractal...
elif self.bearOne == -1 and self.first_high is not None and self.second_high is not None:
self.pass_v = self.second_high
self.second_high = self.quickTradeBarWindow[2].High
self.first_high = self.pass_v
self.context.bullOneDict[self.liquidContract] = self.bullOne
self.context.bearOneDict[self.liquidContract] = self.bearOne
self.context.first_highDict[self.liquidContract] = self.first_high
self.context.second_highDict[self.liquidContract] = self.second_high
self.context.rulesDict[self.liquidContract] = self.rules
self.context.max_price_shortDict[self.liquidContract] = self.max_price_short
self.context.max_price_longDict[self.liquidContract] = self.max_price_long
self.context.trading_hoursDict[self.liquidContract] = self.trading_hours
if self.second_high is None or self.first_high is None:
continue
if self.second_high == self.first_high and "Highs" not in self.rules:
self.rules.append("Highs")
elif self.second_high > self.first_high and "Highs" in self.rules:
self.rules.remove("Highs")
#LLs...
if self.bullOneRed == 0 and self.first_low is None:
self.first_low = self.quickTradeBarWindow[2].Low
#If we have the first high but not the second...
elif self.bullOneRed == 1 and self.first_low is not None and self.second_low is None:
self.second_low = self.quickTradeBarWindow[2].Low
#If we have the first two highs and there's a new fractal...
elif self.bullOneRed == 1 and self.first_low is not None and self.second_low is not None:
self.pass_v = self.second_low
self.second_low = self.quickTradeBarWindow[2].Low
self.first_low = self.pass_v
self.context.first_lowDict[self.liquidContract] = self.first_low
self.context.second_lowDict[self.liquidContract] = self.second_low
self.context.rulesDict[self.liquidContract] = self.rules
if self.second_low is None or self.first_low is None:
continue
if self.second_low == self.first_low and "Lows" not in self.rules:
self.rules.append("Lows")
elif self.second_low > self.first_low and "Lows" in self.rules:
self.rules.remove("Lows")
invested = self.context.Portfolio.Invested
close_price = self.quickTradeBarWindow[0].Close
self.context.Debug("RULES: " + str(self.rules))
self.context.Debug("BREAKDOWN: " + str(self.breakdown))
self.context.Debug("BREAKOUT: " + str(self.breakout))
self.context.Debug("BULL FRACTAL: " + str(self.bullOne))
self.context.Debug("BEAR FRACTAL: " + str(self.bearOneRed))
self.context.Debug("BREAKDOWN: " + str(self.breakdown))
self.context.Debug("SUPPORT: " + str(self.support))
self.context.Debug("RESISTANCE: " + str(self.resistance))
self.context.Debug("INVESTED: " + str(invested))
self.context.Debug("CLOSE PRICE: " + str(close_price))
self.context.Debug("CONTRACT: " + str(self.liquidContract))
self.context.rulesDict[self.liquidContract] = self.rules
maxEntryPriceShort = None
if not len(self.context.Transactions.GetOpenOrders()) > 0:
if assetType == "Future":
from futureExecution import FutureExecution
newFutureExecution = FutureExecution(self.context)
newFutureExecution.FutureExecution(self.liquidContract, multiplier, self.quickTradeBarWindow, self.bearOneRed)
if assetType == "Equity" or assetType == "Crypto":
from equityCryptoExecution import EquityCryptoExecution
newEquityCryptoExecution = EquityCryptoExecution(self.context)
newEquityCryptoExecution.EquityCryptoExecution(self.liquidContract, self.quickTradeBarWindow, self.bearOneRed)
if assetType == "Forex":
from forexExecution import ForexExecution
newForexExecution = ForexExecution(self.context)
newForexExecution.ForexExecution(self.liquidContract, self.quickTradeBarWindow, self.bearOneRed)
# Set or update dictionaries
self.first_position_bull = self.context.first_position_bullDict.get(self.liquidContract)
self.second_position_bull = self.context.second_position_bullDict.get(self.liquidContract)
self.first_position_bear = self.context.first_position_bearDict.get(self.liquidContract)
self.second_position_bear = self.context.second_position_bearDict.get(self.liquidContract)
self.entry_minute = self.context.entry_minuteDict.get(self.liquidContract)
self.short_stop = self.context.short_stopDict.get(self.liquidContract)
self.second_short_stop = self.context.second_short_stopDict.get(self.liquidContract)
self.long_stop = self.context.long_stopDict.get(self.liquidContract)
self.second_long_stop = self.context.second_long_stopDict.get(self.liquidContract)
self.long_entry = self.context.long_entryDict.get(self.liquidContract)
self.second_long_entry = self.context.second_long_entryDict.get(self.liquidContract)
self.short_entry = self.context.short_entryDict.get(self.liquidContract)
self.second_short_entry = self.context.second_short_entryDict.get(self.liquidContract)
self.long_exit = self.context.long_exitDict.get(self.liquidContract)
self.second_long_exit = self.context.second_long_exitDict.get(self.liquidContract)
self.short_exit = self.context.short_exitDict.get(self.liquidContract)
self.second_short_exit = self.context.second_short_exitDict.get(self.liquidContract)
self.risk_target = self.context.risk_targetDict.get(self.liquidContract)
self.buying_power = self.context.buying_powerDict.get(self.liquidContract)
self.share_size = self.context.share_sizeDict.get(self.liquidContract)
self.dollar_size = self.context.dollar_sizeDict.get(self.liquidContract)
if self.context.longOrdersDict.get(self.liquidContract) == None:
self.longOrders = []
else:
self.longOrders = self.context.longOrdersDict.get(self.liquidContract)
if self.context.shortOrdersDict.get(self.liquidContract) == None:
self.shortOrders = []
else:
self.shortOrders = self.context.shortOrdersDict.get(self.liquidContract)
if self.context.bull_losing_outcomeDict.get(self.liquidContract) == None:
self.bull_losing_outcome = False
else:
self.bull_losing_outcome = self.context.bull_losing_outcomeDict.get(self.liquidContract)
if self.context.bear_losing_outcomeDict.get(self.liquidContract) == None:
#Used to check if the first trade in a thesis was a loser.
self.bear_losing_outcome = False
else:
self.bear_losing_outcome = self.context.bear_losing_outcomeDict.get(self.liquidContract)
self.context.rulesDict[self.liquidContract] = self.rules
self.context.bearFiveDict[self.liquidContract] = self.bearFive
self.context.bullFiveDict[self.liquidContract] = self.bullFive
self.context.bullOneDict[self.liquidContract] = self.bullOne
self.context.bearOneDict[self.liquidContract] = self.bearOne
self.context.FiveUpDict[self.liquidContract] = self.FiveUp
self.context.FiveDownDict[self.liquidContract] = self.FiveDown
self.context.first_lowDict[self.liquidContract] = self.first_low
self.context.second_lowDict[self.liquidContract] = self.second_low
self.context.first_highDict[self.liquidContract] = self.first_high
self.context.second_highDict[self.liquidContract] = self.second_high
self.context.bull_losing_outcomeDict[self.liquidContract] = self.bull_losing_outcome
self.context.bear_losing_outcomeDict[self.liquidContract] = self.bear_losing_outcome
self.context.first_position_bullDict[self.liquidContract] = self.first_position_bull
self.context.second_position_bullDict[self.liquidContract] = self.second_position_bull
self.context.first_position_bearDict[self.liquidContract] = self.first_position_bear
self.context.second_position_bearDict[self.liquidContract] = self.second_position_bear
self.context.entry_minuteDict[self.liquidContract] = self.entry_minute
self.context.supportDict[self.liquidContract] = self.support
self.context.resistanceDict[self.liquidContract] = self.resistance
self.context.breakoutDict[self.liquidContract] = self.breakout
self.context.breakdownDict[self.liquidContract] = self.breakdown
self.context.short_stopDict[self.liquidContract] = self.short_stop
self.context.second_short_stopDict[self.liquidContract] = self.second_short_stop
self.context.long_stopDict[self.liquidContract] = self.long_stop
self.context.second_long_stopDict[self.liquidContract] = self.second_long_stop
self.context.long_entryDict[self.liquidContract] = self.long_entry
self.context.second_long_entryDict[self.liquidContract] = self.second_long_entry
self.context.short_entryDict[self.liquidContract] = self.short_entry
self.context.second_short_entryDict[self.liquidContract] = self.second_short_entry
self.context.long_exitDict[self.liquidContract] = self.long_exit
self.context.second_long_exitDict[self.liquidContract] = self.second_long_exit
self.context.short_exitDict[self.liquidContract] = self.short_exit
self.context.second_short_exitDict[self.liquidContract] = self.second_short_exit
self.context.longOrdersDict[self.liquidContract] = self.longOrders
self.context.shortOrdersDict[self.liquidContract] = self.shortOrders
self.context.partial_exit_fill_safety_longDict[self.liquidContract] = self.partial_exit_fill_safety_long
self.context.partial_fill_liq_priceDict[self.liquidContract] = self.partial_fill_liq_price
self.context.partial_exit_fill_safety_shortDict[self.liquidContract] = self.partial_exit_fill_safety_short
self.context.risk_targetDict[self.liquidContract] = self.risk_target
self.context.buying_powerDict[self.liquidContract] = self.buying_power
self.context.share_sizeDict[self.liquidContract] = self.share_size
self.context.trading_hoursDict[self.liquidContract] = self.trading_hours
self.context.max_price_shortDict[self.liquidContract] = self.max_price_short
self.context.max_price_longDict[self.liquidContract] = self.max_price_long
if self.dollar_size != None:
self.context.dollar_sizeDict[self.liquidContract] = self.dollar_size
self.context.symbolData[self.liquidContract].quickTradeBarWindow = self.quickTradeBarWindow
Our code in onDataConsolidated:
class OnDataConsolidated:
def __init__(self, context):
self.context = context
def RunOnDataConsolidated(self, bar):
#5M DATA SETUP
self.liquidContract = bar.Symbol
self.bar = bar
#GET AND SET ROLLING WINDOW FOR ASSET:
self.longTradeBarWindow = self.context.symbolData[self.liquidContract].longTradeBarWindow
if self.longTradeBarWindow == None:
return
self.longTradeBarWindow.Add(self.bar)
self.context.symbolData[self.liquidContract].longTradeBarWindow = self.longTradeBarWindow
if not self.longTradeBarWindow.IsReady:
return
self.bull_losing_outcome = False
self.bear_losing_outcome = False
#Set variables
self.support = self.context.supportDict.get(self.liquidContract)
self.resistance = self.context.resistanceDict.get(self.liquidContract)
self.breakdown = self.context.breakdownDict.get(self.liquidContract)
self.breakout = self.context.breakoutDict.get(self.liquidContract)
self.bearFive = self.context.bearFiveDict.get(self.liquidContract)
self.bullFive = self.context.bullFiveDict.get(self.liquidContract)
self.bullOne = self.context.bullOneDict.get(self.liquidContract)
self.bearOne = self.context.bearOneDict.get(self.liquidContract)
self.FiveUp = self.context.FiveUpDict.get(self.liquidContract)
self.FiveDown = self.context.FiveDownDict.get(self.liquidContract)
self.first_position_bull = self.context.first_position_bullDict.get(self.liquidContract)
self.second_position_bull = self.context.second_position_bullDict.get(self.liquidContract)
self.second_position_bear = self.context.second_position_bearDict.get(self.liquidContract)
self.first_position_bear = self.context.first_position_bearDict.get(self.liquidContract)
self.partial_exit_fill_safety_long = self.context.partial_exit_fill_safety_longDict.get(self.liquidContract)
self.partial_exit_fill_safety_short = self.context.partial_exit_fill_safety_shortDict.get(self.liquidContract)
self.partial_fill_liq_price = self.context.partial_fill_liq_priceDict.get(self.liquidContract)
if self.context.bull_losing_outcomeDict.get(self.liquidContract) == None:
self.bull_losing_outcome = False
else:
self.bull_losing_outcome = self.context.bull_losing_outcomeDict.get(self.liquidContract)
if self.context.bear_losing_outcomeDict.get(self.liquidContract) == None:
#Used to check if the first trade in a thesis was a loser.
self.bear_losing_outcome = False
else:
self.bear_losing_outcome = self.context.bear_losing_outcomeDict.get(self.liquidContract)
# END OF VARIABLES
# START OF LOGIC
firstPartLow = ((self.longTradeBarWindow[4].Low > self.longTradeBarWindow[2].Low) and (self.longTradeBarWindow[2].Low < self.longTradeBarWindow[3].Low))
secondPartLow = ((self.longTradeBarWindow[1].Low > self.longTradeBarWindow[2].Low) and (self.longTradeBarWindow[2].Low < self.longTradeBarWindow[0].Low))
firstPartHigh = ((self.longTradeBarWindow[4].High < self.longTradeBarWindow[2].High) and (self.longTradeBarWindow[2].High > self.longTradeBarWindow[3].High))
secondPartHigh = ((self.longTradeBarWindow[1].High < self.longTradeBarWindow[2].High) and (self.longTradeBarWindow[2].High > self.longTradeBarWindow[0].High))
#5M UP-FRACTAL FORMULA
if firstPartLow and secondPartLow:
self.FiveUp = True
else:
self.FiveUp = False
#5M DN-FRACTAL FORMULA
if firstPartHigh and secondPartHigh:
self.FiveDown = True
else:
self.FiveDown = False
#FRACTAL TRUE/FALSE
self.bullFive = 1 if self.FiveUp is True else 0
self.bearFive = -1 if self.FiveDown is True else 0
#SET SUPPORT, RESET NON-DIRECTIONAL STATUS
if self.bullFive == 1:
self.support = self.longTradeBarWindow[2].Low
self.breakdown = False
self.first_position_bear = False
self.second_position_bear = False
#Resets the losing_outcome becuase there's a new thesis
self.bear_losing_outcome = False
#SET RESISTANCE, RESET NON-DIRECTIONAL STATUS
if self.bearFive == -1:
self.resistance = self.longTradeBarWindow[2].High
self.breakout = False
self.first_position_bull = False
self.second_position_bull = False
#Reset the losing_outcome becuase there's a new thesis
self.bull_losing_outcome = False
self.context.supportDict[self.liquidContract] = self.support
self.context.breakdownDict[self.liquidContract] = self.breakdown
self.context.resistanceDict[self.liquidContract] = self.resistance
self.context.breakoutDict[self.liquidContract] = self.breakout
self.context.bearFiveDict[self.liquidContract] = self.bearFive
self.context.bullFiveDict[self.liquidContract] = self.bullFive
self.context.bullOneDict[self.liquidContract] = self.bullOne
self.context.bearOneDict[self.liquidContract] = self.bearOne
self.context.FiveUpDict[self.liquidContract] = self.FiveUp
self.context.FiveDownDict[self.liquidContract] = self.FiveDown
self.context.first_position_bullDict[self.liquidContract] = self.first_position_bull
self.context.second_position_bullDict[self.liquidContract] = self.second_position_bull
self.context.second_position_bearDict[self.liquidContract] = self.second_position_bear
self.context.first_position_bearDict[self.liquidContract] = self.first_position_bear
self.context.bull_losing_outcomeDict[self.liquidContract] = self.bull_losing_outcome
self.context.bear_losing_outcomeDict[self.liquidContract] = self.bear_losing_outcome
if self.support is None:
return
if self.resistance is None:
return
#SET BULLISH DIRECTIONAL STATUS
if self.context.Securities[self.liquidContract].Close > self.resistance:
self.breakout = True
#SET BEARISH DIRECTIONAL STATUS
if self.context.Securities[self.liquidContract].Close < self.support:
self.breakdown = True
if self.partial_fill_liq_price != None and self.partial_exit_fill_safety_long is True and self.context.Securities[self.liquidContract].Low <= self.partial_fill_liq_price:
self.context.Liquidate(self.liquidContract)
self.partial_exit_fill_safety_long = False
self.partial_fill_liq_price = None
if self.partial_fill_liq_price != None and self.partial_exit_fill_safety_short is True and self.context.Securities[self.liquidContract].Low >= self.partial_fill_liq_price:
self.context.Liquidate(self.liquidContract)
self.partial_exit_fill_safety_short = False
self.partial_fill_liq_price = None
#SET BULLISH DIRECTIONAL STATUS
if self.context.Securities[self.liquidContract].Close > self.resistance:
self.breakout = True
#SET BEARISH DIRECTIONAL STATUS
if self.context.Securities[self.liquidContract].Close < self.support:
self.breakdown = True
self.context.supportDict[self.liquidContract] = self.support
self.context.resistanceDict[self.liquidContract] = self.resistance
self.context.breakoutDict[self.liquidContract] = self.breakout
self.context.breakdownDict[self.liquidContract] = self.breakdown
self.context.bearFiveDict[self.liquidContract] = self.bearFive
self.context.bullFiveDict[self.liquidContract] = self.bullFive
self.context.bullOneDict[self.liquidContract] = self.bullOne
self.context.bearOneDict[self.liquidContract] = self.bearOne
self.context.FiveUpDict[self.liquidContract] = self.FiveUp
self.context.FiveDownDict[self.liquidContract] = self.FiveDown
self.context.first_position_bullDict[self.liquidContract] = self.first_position_bull
self.context.second_position_bullDict[self.liquidContract] = self.second_position_bull
self.context.second_position_bearDict[self.liquidContract] = self.second_position_bear
self.context.first_position_bearDict[self.liquidContract] = self.first_position_bear
self.context.bull_losing_outcomeDict[self.liquidContract] = self.bull_losing_outcome
self.context.bear_losing_outcomeDict[self.liquidContract] = self.bear_losing_outcome
self.context.partial_exit_fill_safety_shortDict[self.liquidContract] = self.partial_exit_fill_safety_short
self.context.partial_exit_fill_safety_longDict[self.liquidContract] = self.partial_exit_fill_safety_long
self.context.symbolData[self.liquidContract].longTradeBarWindow = self.longTradeBarWindow
self.context.partial_fill_liq_priceDict[self.liquidContract] = self.partial_fill_liq_price
The code we're using to execute trades:
class FutureExecution:
def __init__(self, context):
self.context = context
def FutureExecution(self, contract, multiplier, tradeWindow, bearOneRed):
self.bearOneRed = bearOneRed
self.quickTradeBarWindow = tradeWindow
self.liquidContract = contract
self.rules = []
self.longOrders = []
self.shortOrders = []
self.bull_losing_outcome = False
self.bear_losing_outcome = False
self.long = False
self.short = False
# Get or set correct variable values
self.bullOne = self.context.bullOneDict.get(self.liquidContract)
self.bearOne = self.context.bearOneDict.get(self.liquidContract)
self.first_low = self.context.first_lowDict.get(self.liquidContract)
self.second_low = self.context.second_lowDict.get(self.liquidContract)
self.first_high = self.context.first_highDict.get(self.liquidContract)
self.second_high = self.context.second_highDict.get(self.liquidContract)
self.first_position_bull = self.context.first_position_bullDict.get(self.liquidContract)
self.second_position_bull = self.context.second_position_bullDict.get(self.liquidContract)
self.first_position_bear = self.context.first_position_bearDict.get(self.liquidContract)
self.second_position_bear = self.context.second_position_bearDict.get(self.liquidContract)
self.entry_minute = self.context.entry_minuteDict.get(self.liquidContract)
self.support = self.context.supportDict.get(self.liquidContract)
self.resistance = self.context.resistanceDict.get(self.liquidContract)
self.breakout = self.context.breakoutDict.get(self.liquidContract)
self.breakdown = self.context.breakdownDict.get(self.liquidContract)
self.short_stop = self.context.short_stopDict.get(self.liquidContract)
self.second_short_stop = self.context.second_short_stopDict.get(self.liquidContract)
self.long_stop = self.context.long_stopDict.get(self.liquidContract)
self.second_long_stop = self.context.second_long_stopDict.get(self.liquidContract)
self.long_entry = self.context.long_entryDict.get(self.liquidContract)
self.second_long_entry = self.context.second_long_entryDict.get(self.liquidContract)
self.short_entry = self.context.short_entryDict.get(self.liquidContract)
self.second_short_entry = self.context.second_short_entryDict.get(self.liquidContract)
self.long_exit = self.context.long_exitDict.get(self.liquidContract)
self.second_long_exit = self.context.second_long_exitDict.get(self.liquidContract)
self.short_exit = self.context.short_exitDict.get(self.liquidContract)
self.second_short_exit = self.context.second_short_exitDict.get(self.liquidContract)
self.partial_exit_fill_safety_long = self.context.partial_exit_fill_safety_longDict.get(self.liquidContract)
self.partial_fill_liq_price = self.context.partial_fill_liq_priceDict.get(self.liquidContract)
self.partial_exit_fill_safety_short = self.context.partial_exit_fill_safety_shortDict.get(self.liquidContract)
self.risk_target = self.context.risk_targetDict.get(self.liquidContract)
self.buying_power = self.context.buying_powerDict.get(self.liquidContract)
self.share_size = self.context.share_sizeDict.get(self.liquidContract)
self.dollar_size = self.context.dollar_sizeDict.get(self.liquidContract)
self.max_price_long = self.context.max_price_longDict.get(self.liquidContract)
self.max_price_short = self.context.max_price_shortDict.get(self.liquidContract)
self.trading_hours = self.context.trading_hoursDict.get(self.liquidContract)
self.symbolEntryPrice = self.context.symbolEntryPriceDict.get(self.liquidContract)
self.symbolStopPrice = self.context.symbolStopPriceDict.get(self.liquidContract)
self.symbolExitPrice = self.context.symbolExitPriceDict.get(self.liquidContract)
if self.context.shortDict.get(self.liquidContract) == None:
self.short = False
else:
self.short = self.context.shortDict.get(self.liquidContract)
if self.context.longDict.get(self.liquidContract) == None:
self.long = False
else:
self.long = self.context.longDict.get(self.liquidContract)
if self.context.rulesDict.get(self.liquidContract) == None:
self.rules = []
else:
self.rules = self.context.rulesDict.get(self.liquidContract)
if self.context.longOrdersDict.get(self.liquidContract) == None:
self.longOrders = []
else:
self.longOrders = self.context.longOrdersDict.get(self.liquidContract)
if self.context.shortOrdersDict.get(self.liquidContract) == None:
self.shortOrders = []
else:
self.shortOrders = self.context.shortOrdersDict.get(self.liquidContract)
if self.context.bull_losing_outcomeDict.get(self.liquidContract) == None:
self.bull_losing_outcome = False
else:
self.bull_losing_outcome = self.context.bull_losing_outcomeDict.get(self.liquidContract)
if self.context.bear_losing_outcomeDict.get(self.liquidContract) == None:
#Used to check if the first trade in a thesis was a loser.
self.bear_losing_outcome = False
else:
self.bear_losing_outcome = self.context.bear_losing_outcomeDict.get(self.liquidContract)
maxEntryPriceShort = None
properties = self.context.Securities[self.liquidContract].SymbolProperties
minPriceVar = properties.MinimumPriceVariation
minPriceVar = str(minPriceVar)
decimalCount = minPriceVar[::-1].find('.')
if self.symbolStopPrice is not None and self.context.Securities[self.liquidContract].Price <= self.symbolStopPrice and self.long is True:
self.bull_losing_outcome = True
self.long = False
if self.symbolStopPrice is not None and self.context.Securities[self.liquidContract].Price >= self.symbolStopPrice and self.short is True:
self.bear_losing_outcome = True
self.short = False
#BEARISH EXECUTION LOGIC = 5M CLOSING BREAK, NOT HOLDING, 1M FRACTAL, ENTRY PRICE < SUPPORT
if self.trading_hours is True and self.breakdown is True and self.bearOneRed == -1 and self.quickTradeBarWindow[0].Close < self.support and "Lows" in self.rules and self.second_position_bear is False:
self.context.Debug("contract" + str(self.liquidContract))
self.context.Debug("BEAR IF EXECUTION MET")
self.short = True
#ENTRY AT THE CLOSE OF THE CANDLE FOLLOWING THE FRACTAL
self.entry_price = self.quickTradeBarWindow[0].High
self.entry_price = round(self.entry_price, decimalCount)
#STOP ONE TICK ABOVE THE HIGHEST FRACTAL POINT
self.stop_price = self.quickTradeBarWindow[1].Low + 1.01
self.symbolStopPrice = self.stop_price
self.stop_price = round(self.stop_price, decimalCount)
#TICK DISTANCE FOR EXIT
self.pass_value = (self.stop_price - self.entry_price) * 5
#2:1 EXIT PRICE
self.exit_price = self.entry_price - self.pass_value
#AVAILABLE CAPITAL
self.buying_power = self.context.Portfolio.Cash
#DOLLAR RISK TARGET (0.25%)
self.risk_target = self.buying_power / 400
#ENTRY-STOP DOLLAR DISTANCE
self.handleSpread = (self.pass_value / 2) / float(minPriceVar)
self.risk_per_share = self.handleSpread * multiplier
#IDEAL SHARE SIZE (avaiable capital not considered)
if self.risk_target != 0 and self.risk_per_share != 0:
self.share_size = self.risk_target / self.risk_per_share
#PRICE OF IDEAL SIZE
self.dollar_size = self.share_size * self.entry_price
#COMPARES IDEAL SIZE WITH CAPITAL
if self.share_size > 10:
#NEW SHARE SIZE
self.share_size = 10
self.context.dollar_sizeDict[self.liquidContract] = self.dollar_size
maxEntryPriceShort = self.entry_price > self.second_low or self.entry_price > self.max_price_short
#TEMP CONDITION THAT REDUCES MASSIVE TRADES
if maxEntryPriceShort and self.first_position_bear is False and not self.context.Portfolio.Invested:
self.context.Debug("LAST BEAR CONDITION")
#SHORT
self.short_entry = self.context.LimitOrder(self.liquidContract, -self.share_size, self.entry_price)
#CUT LOSSES
self.short_stop = self.context.StopMarketOrder(self.liquidContract, self.share_size, self.stop_price)
#TAKE PROFIT
self.short_exit = self.context.LimitOrder(self.liquidContract, self.share_size, self.exit_price)
#STORES ORDER TICKETS IN A LIST
self.shortOrders = [self.short_entry, self.short_stop, self.short_exit]
#STORES FIRST TRADE AS HAVING HAPPENED
self.first_position_bear = True
#LOGS TIME ORDERS WERE PLACED
self.entry_minute = self.context.Time.minute
entryIsZero = None
entryBool = None
if self.entry_minute != None:
entryIsZero = self.context.Time.minute == 0 and self.entry_minute != 0
entryBool = self.context.Time.minute > self.entry_minute or entryIsZero
if entryIsZero is not None and maxEntryPriceShort and self.first_position_bear is True and self.second_position_bear is False and entryBool and self.bear_losing_outcome is True and not self.context.Portfolio.Invested:
self.context.Debug("LAST BEAR CONDITION" + str(self.liquidContract))
#SHORT
self.second_short_entry = self.context.LimitOrder(self.liquidContract, -self.share_size, self.entry_price)
#CUT LOSSES
self.second_short_stop = self.context.StopMarketOrder(self.liquidContract, self.share_size, self.stop_price)
#TAKE PROFIT
self.second_short_exit = self.context.LimitOrder(self.liquidContract, self.share_size, self.exit_price)
#STORES ORDER TICKETS IN A LIST
self.shortOrders = [self.second_short_entry, self.second_short_stop, self.second_short_exit]
#STORES SECOND POSITION
self.second_position_bear = True
#BREAKOUT EXECUTION (NEED TO ADD PRICE CONDITION)
if self.trading_hours is True and self.breakout is True and self.bullOne == 1 and self.quickTradeBarWindow[0].Close > self.resistance and "Highs" in self.rules and self.second_position_bull is False:
self.context.Debug("contract" + str(self.liquidContract))
self.context.Debug("BULL IF EXECUTION MET")
self.long = True
#ENTRY AT THE CLOSE OF THE CANDLE FOLLOWING THE FRACTAL
self.entry_price = self.quickTradeBarWindow[0].Close
self.entry_price = round(self.entry_price, decimalCount)
#STOP ONE TICK BELOW THE LOWEST FRACTAL POINT
self.stop_price = self.quickTradeBarWindow[1].Low - 0.01
self.symbolStopPrice = self.stop_price
self.stop_price = round(self.stop_price, decimalCount)
#TICK DISTANCE FOR EXIT
self.pass_value = (self.entry_price - self.stop_price) * 2
#2:1 EXIT PRICE
self.exit_price = (self.pass_value + self.quickTradeBarWindow[0].Close)
#AVAILABLE CAPITAL
self.buying_power = self.context.Portfolio.Cash
#DOLLAR RISK TARGET (0.25%)
self.risk_target = self.buying_power / 400
#ENTRY-STOP DOLLAR DISTANCE
self.handleSpread = (self.pass_value / 2) / float(minPriceVar)
self.risk_per_share = self.handleSpread * multiplier
#IDEAL SHARE SIZE (avaiable capital not considered)
if self.risk_target != 0 and self.risk_per_share != 0:
self.share_size = self.risk_target / self.risk_per_share
#PRICE OF IDEAL SIZE
self.dollar_size = self.share_size * self.entry_price
#COMPARES IDEAL SIZE WITH CAPITAL
if self.share_size > 10:
#NEW SHARE SIZE
self.share_size = 10
maxEntryPriceLong = self.entry_price < self.max_price_long or self.entry_price < self.second_high
#TEMP CONDITION THAT REDUCES MASSIVE TRADES
if maxEntryPriceLong and self.first_position_bull is False and not self.context.Portfolio.Invested:
self.context.Debug("LAST BULL CONDITION" + str(self.liquidContract))
self.long_entry = self.context.LimitOrder(self.liquidContract, self.share_size, self.entry_price)
self.long_stop = self.context.StopMarketOrder(self.liquidContract, -self.share_size, self.stop_price)
self.long_exit = self.context.LimitOrder(self.liquidContract, -self.share_size, self.exit_price)
self.longOrders = [self.long_entry, self.long_stop, self.long_exit]
self.first_position_bull = True
self.entry_minute = self.context.Time.minute
entryIsZero = None
entryBool = None
if self.entry_minute != None:
entryIsZero = self.context.Time.minute == 0 and self.entry_minute != 0
entryBool = self.context.Time.minute > self.entry_minute or entryIsZero
if entryIsZero is not None and maxEntryPriceShort and self.first_position_bull is True and self.second_position_bull is False and entryBool and self.bull_losing_outcome == True and not self.context.Portfolio.Invested:
self.context.Debug("LAST BULL CONDITION" + str(self.liquidContract))
self.second_long_entry = self.LimitOrder(self.liquidContract, self.share_size, self.entry_price)
self.second_long_stop = self.StopMarketOrder(self.liquidContract, -self.share_size, self.stop_price)
self.second_long_exit = self.LimitOrder(self.liquidContract, -self.share_size, self.exit_price)
self.longOrders = [self.second_long_entry, self.second_long_stop, self.second_long_exit]
self.second_position_bull = True
self.context.rulesDict[self.liquidContract] = self.rules
self.context.bullOneDict[self.liquidContract] = self.bullOne
self.context.bearOneDict[self.liquidContract] = self.bearOne
self.context.first_lowDict[self.liquidContract] = self.first_low
self.context.second_lowDict[self.liquidContract] = self.second_low
self.context.first_highDict[self.liquidContract] = self.first_high
self.context.second_highDict[self.liquidContract] = self.second_high
self.context.bull_losing_outcomeDict[self.liquidContract] = self.bull_losing_outcome
self.context.bear_losing_outcomeDict[self.liquidContract] = self.bear_losing_outcome
self.context.first_position_bullDict[self.liquidContract] = self.first_position_bull
self.context.second_position_bullDict[self.liquidContract] = self.second_position_bull
self.context.first_position_bearDict[self.liquidContract] = self.first_position_bear
self.context.second_position_bearDict[self.liquidContract] = self.second_position_bear
self.context.entry_minuteDict[self.liquidContract] = self.entry_minute
self.context.supportDict[self.liquidContract] = self.support
self.context.resistanceDict[self.liquidContract] = self.resistance
self.context.breakoutDict[self.liquidContract] = self.breakout
self.context.breakdownDict[self.liquidContract] = self.breakdown
self.context.short_stopDict[self.liquidContract] = self.short_stop
self.context.second_short_stopDict[self.liquidContract] = self.second_short_stop
self.context.long_stopDict[self.liquidContract] = self.long_stop
self.context.second_long_stopDict[self.liquidContract] = self.second_long_stop
self.context.long_entryDict[self.liquidContract] = self.long_entry
self.context.second_long_entryDict[self.liquidContract] = self.second_long_entry
self.context.short_entryDict[self.liquidContract] = self.short_entry
self.context.second_short_entryDict[self.liquidContract] = self.second_short_entry
self.context.long_exitDict[self.liquidContract] = self.long_exit
self.context.second_long_exitDict[self.liquidContract] = self.second_long_exit
self.context.short_exitDict[self.liquidContract] = self.short_exit
self.context.second_short_exitDict[self.liquidContract] = self.second_short_exit
self.context.longOrdersDict[self.liquidContract] = self.longOrders
self.context.shortOrdersDict[self.liquidContract] = self.shortOrders
self.context.partial_exit_fill_safety_longDict[self.liquidContract] = self.partial_exit_fill_safety_long
self.context.partial_fill_liq_priceDict[self.liquidContract] = self.partial_fill_liq_price
self.context.partial_exit_fill_safety_shortDict[self.liquidContract] = self.partial_exit_fill_safety_short
self.context.risk_targetDict[self.liquidContract] = self.risk_target
self.context.buying_powerDict[self.liquidContract] = self.buying_power
self.context.share_sizeDict[self.liquidContract] = self.share_size
self.context.trading_hoursDict[self.liquidContract] = self.trading_hours
self.context.max_price_shortDict[self.liquidContract] = self.max_price_short
self.context.max_price_longDict[self.liquidContract] = self.max_price_long
if self.symbolStopPrice != None:
self.context.symbolStopPriceDict[self.liquidContract] = self.symbolStopPrice
if self.symbolExitPrice != None:
self.context.symbolExitPriceDict[self.liquidContract] = self.symbolExitPrice
if self.symbolEntryPrice != None:
self.context.symbolEntryPriceDict[self.liquidContract] = self.symbolEntryPrice
if self.dollar_size != None:
self.context.dollar_sizeDict[self.liquidContract] = self.dollar_size
self.context.longDict[self.liquidContract] = self.long
self.context.shortDict[self.liquidContract] = self.short