| Overall Statistics |
|
Total Trades 120 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Probabilistic Sharpe Ratio 0% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $0.00 Estimated Strategy Capacity $2600000.00 Lowest Capacity Asset NAS100USD 8I |
#region imports from AlgorithmImports import * #endregion START_HOUR = 9 # Hour of day to send first order START_MINUTE = 35 # Minute of hour to send first order CFD = "NAS100USD" INTERVAL = 9 LOSS_AMOUNT = 4 LOT_SIZE = 1 STOP_LOSS_TYPE = "DOLLAR" # Pick between "DOLLAR" and "PERCENT" STOP_LOSS_PERCENT = 0.09 STOP_LOSS_DOLLAR = 9 LOSS_THRESHOLD = 5 STOP_FOR = 50
# region imports
from AlgorithmImports import *
from datetime import *
import config
# endregion
class MuscularApricotHorse(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 7, 21)
self.SetEndDate(2022, 7, 22)
self.SetCash(100000)
self.SetBrokerageModel(BrokerageName.OandaBrokerage, AccountType.Margin)
self.Start_Time = time(config.START_HOUR, config.START_MINUTE)
self.NAS_100 = self.AddCfd(config.CFD, Resolution.Second, Market.Oanda).Symbol
self.Interval = timedelta(seconds=config.INTERVAL)
self.Loss_Amount = config.LOSS_AMOUNT
self.Direction = "LONG"
self.Lot_Size = config.LOT_SIZE
self.Stop_Loss_Percent = config.STOP_LOSS_PERCENT/100
self.Stop_Loss_Dollar = config.STOP_LOSS_DOLLAR
self.Loss_Threshold = config.LOSS_THRESHOLD
self.Loss_Counter = 0
self.Stop_For = config.STOP_FOR
self.Short_Circuit_Time = None
self.Allow_Entry = True
self.Long_Entry_Time = None
self.Short_Entry_Time = None
self.First_Order = True
self.Stop_Loss_Type = config.STOP_LOSS_TYPE
self.Long_Buy_Ticket = None
self.Long_Sell_Ticket = None
self.Short_Sell_Ticket = None
self.Short_Cover_Ticket = None
self.Long_Buy_Fill_Price = None
self.Long_Sell_Fill_Price = None
self.Short_Sell_Fill_Price = None
self.Short_Cover_Fill_Price = None
self.Start_Now = False
self.Entry_Time_From = time(10,0) # First number is hour and second number is minute
self.Entry_Time_To = time(11,0) # First number is hour and second number is minute
self.Debug(self.Securities[self.NAS_100].SymbolProperties.LotSize)
self.Debug(self.Securities[self.NAS_100].Exchange.Hours)
self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.Every(timedelta(seconds=config.INTERVAL)),
self.Place_Order)
self.SetWarmup(10)
def Reset_Counter(self):
self.Schedule.Remove(self.Scheduled_Event)
self.Loss_Counter = 0
self.Allow_Entry = True
def Stop_Loss(self):
open_Orders = self.Portfolio.Transactions.GetOpenOrders(self.NAS_100)
if len(open_Orders) > 0:
self.Debug("OPEN ORDERS")
return
if self.Portfolio[self.NAS_100].IsShort:
self.Short_Cover_Ticket = self.MarketOrder(self.NAS_100, self.Lot_Size, tag = "SHORT STOP")
self.Determine_Direction()
if self.Portfolio[self.NAS_100].IsLong:
self.Long_Sell_Ticket = self.MarketOrder(self.NAS_100, -self.Lot_Size, tag = "LONG STOP")
self.Determine_Direction()
def Place_Order(self):
if self.IsWarmingUp:
return
if self.Portfolio[self.NAS_100].IsShort:
self.Short_Cover_Ticket = self.MarketOrder(self.NAS_100, self.Lot_Size, tag = "SHORT COVER")
self.Determine_Direction()
if not self.First_Order:
if self.Allow_Entry:
if not self.Portfolio[self.NAS_100].Invested:
if self.Direction == "LONG":
self.Long_Buy_Ticket = self.MarketOrder(self.NAS_100, self.Lot_Size, tag = "LONG BUY")
self.Long_Entry_Time = self.Time
self.Allow_Entry = False
self.First_Order = False
if not self.First_Order:
if self.Allow_Entry:
if not self.Portfolio[self.NAS_100].Invested:
if self.Direction == "SHORT":
self.Short_Sell_Ticket = self.MarketOrder(self.NAS_100, -self.Lot_Size, tag = "SHORT SELL")
self.Short_Entry_Time = self.Time
self.Allow_Entry = False
self.First_Order = False
if self.Portfolio[self.NAS_100].IsLong:
self.Long_Sell_Ticket = self.MarketOrder(self.NAS_100, -self.Lot_Size, tag = "LONG SELL")
self.Determine_Direction()
if not self.First_Order:
if self.Allow_Entry:
if not self.Portfolio[self.NAS_100].Invested:
if self.Direction == "LONG":
self.Long_Buy_Ticket = self.MarketOrder(self.NAS_100, self.Lot_Size, tag = "LONG BUY")
self.Long_Entry_Time = self.Time
self.Allow_Entry = False
self.First_Order = False
if not self.First_Order:
if self.Allow_Entry:
if not self.Portfolio[self.NAS_100].Invested:
if self.Direction == "SHORT":
self.Short_Sell_Ticket = self.MarketOrder(self.NAS_100, -self.Lot_Size, tag = "SHORT SELL")
self.Short_Entry_Time = self.Time
self.Allow_Entry = False
self.First_Order = False
def Determine_Direction(self):
if self.IsWarmingUp:
return
if self.Long_Buy_Ticket is not None:
if self.Long_Buy_Ticket.Status == OrderStatus.Filled:
self.Long_Buy_Fill_Price = self.Long_Buy_Ticket.AverageFillPrice
self.Long_Buy_Ticket = None
if self.Long_Sell_Ticket is not None:
if self.Long_Sell_Ticket.Status == OrderStatus.Filled:
self.Long_Sell_Fill_Price = self.Long_Sell_Ticket.AverageFillPrice
Return = self.Long_Sell_Fill_Price - self.Long_Buy_Fill_Price
if Return < -self.Loss_Amount:
self.Direction = "SHORT"
self.Allow_Entry = True
self.Long_Sell_Ticket = None
if Return < 0:
self.Loss_Counter = self.Loss_Counter + 1
if self.Loss_Counter >= self.Loss_Threshold:
self.Allow_Entry = False
x = int((self.Time+ timedelta(seconds=self.Stop_For)).strftime('%H'))
y = int((self.Time+ timedelta(seconds=self.Stop_For)).strftime('%M'))
self.Scheduled_Event = self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.At(x, y),
self.Reset_Counter)
if self.Short_Sell_Ticket is not None:
if self.Short_Sell_Ticket.Status == OrderStatus.Filled:
self.Short_Sell_Fill_Price = self.Short_Sell_Ticket.AverageFillPrice
self.Short_Sell_Ticket = None
if self.Short_Cover_Ticket is not None:
if self.Short_Cover_Ticket.Status == OrderStatus.Filled:
self.Short_Cover_Fill_Price = self.Short_Cover_Ticket.AverageFillPrice
Return = self.Short_Sell_Fill_Price - self.Short_Cover_Fill_Price
if Return < -self.Loss_Amount:
self.Direction = "LONG"
self.Allow_Entry = True
self.Short_Cover_Ticket = None
if Return < 0:
self.Loss_Counter = self.Loss_Counter + 1
if self.Loss_Counter >= self.Loss_Threshold:
x = int((self.Time+ timedelta(seconds=self.Stop_For)).strftime('%H'))
y = int((self.Time+ timedelta(seconds=self.Stop_For)).strftime('%M'))
self.Allow_Entry = False
self.Scheduled_Event = self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.At(x, y),
self.Reset_Counter)
def OnData(self, data: Slice):
if self.IsWarmingUp:
return
if self.Stop_Loss_Type == "PERCENT":
if self.Portfolio[self.NAS_100].UnrealizedProfitPercent < -self.Stop_Loss_Percent:
# self.Debug(f"STOP LOSS HIT {self.Portfolio[self.NAS_100].UnrealizedProfitPercent/100}%")
self.Allow_Entry = False
self.Stop_Loss()
if self.Stop_Loss_Type == "DOLLAR":
if self.Portfolio[self.NAS_100].UnrealizedProfit < -self.Stop_Loss_Dollar:
# self.Debug(f"STOP LOSS HIT {self.Portfolio[self.NAS_100].UnrealizedProfit}$")
self.Allow_Entry = False
self.Stop_Loss()
if self.Long_Buy_Ticket is not None:
if self.Long_Buy_Ticket.Status == OrderStatus.Filled:
self.Long_Buy_Fill_Price = self.Long_Buy_Ticket.AverageFillPrice
self.Long_Buy_Ticket = None
if self.Long_Sell_Ticket is not None:
if self.Long_Sell_Ticket.Status == OrderStatus.Filled:
self.Long_Sell_Fill_Price = self.Long_Sell_Ticket.AverageFillPrice
Return = self.Long_Sell_Fill_Price - self.Long_Buy_Fill_Price
if Return < -self.Loss_Amount:
self.Direction = "SHORT"
self.Allow_Entry = True
self.Long_Sell_Ticket = None
if Return < 0:
self.Loss_Counter = self.Loss_Counter + 1
if self.Loss_Counter >= self.Loss_Threshold:
self.Allow_Entry = False
x = int((self.Time+ timedelta(seconds=self.Stop_For)).strftime('%H'))
y = int((self.Time+ timedelta(seconds=self.Stop_For)).strftime('%M'))
self.Scheduled_Event = self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.At(x, y),
self.Reset_Counter)
if self.Short_Sell_Ticket is not None:
if self.Short_Sell_Ticket.Status == OrderStatus.Filled:
self.Short_Sell_Fill_Price = self.Short_Sell_Ticket.AverageFillPrice
self.Short_Sell_Ticket = None
if self.Short_Cover_Ticket is not None:
if self.Short_Cover_Ticket.Status == OrderStatus.Filled:
self.Short_Cover_Fill_Price = self.Short_Cover_Ticket.AverageFillPrice
Return = self.Short_Sell_Fill_Price - self.Short_Cover_Fill_Price
if Return < -self.Loss_Amount:
self.Direction = "LONG"
self.Allow_Entry = True
self.Short_Cover_Ticket = None
if Return < 0:
self.Loss_Counter = self.Loss_Counter + 1
if self.Loss_Counter >= self.Loss_Threshold:
x = int((self.Time+ timedelta(seconds=self.Stop_For)).strftime('%H'))
y = int((self.Time+ timedelta(seconds=self.Stop_For)).strftime('%M'))
self.Allow_Entry = False
self.Scheduled_Event = self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.At(x, y),
self.Reset_Counter)
current_Time = self.Time
#if (current_Time.strftime('%H%M') > self.Entry_Time_From.strftime('%H%M') and current_Time.strftime('%H%M') < self.Entry_Time_To.strftime('%H%M')):
if current_Time.strftime('%H%M') >= self.Start_Time.strftime('%H%M'):
if self.First_Order:
if self.Allow_Entry:
if not self.Portfolio[self.NAS_100].Invested:
if self.Direction == "LONG":
self.Long_Buy_Ticket = self.MarketOrder(self.NAS_100, self.Lot_Size, tag = "LONG BUY")
self.Long_Entry_Time = self.Time
self.Allow_Entry = False
self.First_Order = False
if self.Allow_Entry:
if not self.Portfolio[self.NAS_100].Invested:
if self.Direction == "SHORT":
self.Short_Sell_Ticket = self.MarketOrder(self.NAS_100, -self.Lot_Size, tag = "SHORT SELL")
self.Short_Entry_Time = self.Time
self.Allow_Entry = False
self.First_Order = False
# if self.Portfolio[self._continuousContract.Mapped].IsLong:
# if self.Time >= self.Long_Entry_Time + self.Interval:
# self.Long_Sell_Ticket = self.MarketOrder(self._continuousContract.Mapped, -self.Lot_Size, tag = "LONG SELL")
# if self.Portfolio[self._continuousContract.Mapped].IsShort:
# if self.Time >= self.Short_Entry_Time + self.Interval:
# self.Short_Cover_Ticket = self.MarketOrder(self._continuousContract.Mapped, self.Lot_Size, tag = "SHORT COVER")