Untitled

mail@pastecode.io avatar
unknown
python
a year ago
6.5 kB
3
Indexable
Never
# region imports
from AlgorithmImports import *
# endregion

class AlertBrownCaribou(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 1, 1)  # Set Start Datexw
        self.SetEndDate(2023, 3, 9)
        self.SetCash(100000)  # Set Strategy Cash 
        #self.symbol = self.AddForex('EURUSD', Resolution.Minute, Market.Oanda).Symbol
        self.symbol = self.AddCrypto("BTCUSD", Resolution.Minute, Market.Bitfinex).Symbol

        self.init_vars()
       
    def init_vars(self):
         
        self.market_opening_hour = 2
        self.market_opening_minute = 59
        self.market_close_hour = 23
        self.market_close_minute = 0
        self.market_is_open = False

        self.premarket_opening_hour = 2
        self.premarket_opening_minute = 0
        self.premarket_time = False
        self.quatity = 0
        self.max_point = 0
        self.max_break = False
        self.last_swing_low = None

        self.FVG_potential_area = []
        self.FVG_high = None
        self.FVG_low = None

        self.entryTicket = None
        self.stop_loss_ticket = None
        self.take_profit_ticket = None
        self.inside_trade = False

    def find_swing_low(self, low_series):
        swings = []
        for index in range(1 , len(low_series) - 1):
            if low_series[index - 1] > low_series[index] and low_series[index + 1] > low_series[index]:
                swings.append(low_series[index]) 
        if len(swings) == 0:
            return None
        return swings[-1]
    
    def find_FVG(self, array):
        # for item in array:
        #     self.Log(item.High)
        if len(array) < 2:
            return None, None
        for i in range(0, len(array) - 2):
            if array[i].Low > array[i + 2].High:
                return array[i].Low,  array[i + 2].High
        return None, None
    
    def OnData(self, data: Slice):
        if self.Time.hour == self.premarket_opening_hour and self.Time.minute == self.premarket_opening_minute:
            self.premarket_time = True
            self.market_is_open = False
        if self.Time.hour == self.market_opening_hour and self.Time.minute ==  self.market_opening_minute:
            self.market_is_open = True
            self.premarket_time = False
        if (self.Time.hour == self.market_close_hour and self.Time.minute > self.market_close_minute) or (self.Time.hour > self.market_close_hour):
           self.init_vars()
           self.Transactions.CancelOpenOrders(self.symbol)
           self.Liquidate()
           self.inside_trade = False
        #     self.Log('market is close')
        # if self.market_is_open:
        #     self.Log('market open')
        # if self.premarket_time:
        #     self.Log('pre market')
        if self.premarket_time: 
            self.init_vars()
            if self.max_point < data[self.symbol].High: #update max point
                self.max_point = data[self.symbol].High

        if self.market_is_open:           
            if self.max_point < data[self.symbol].High:
                self.FVG_potential_area = []
                self.max_break = True
                history_close_price = self.History(self.symbol, 7, Resolution.Minute).low
                #update max and last_swing_low
                self.max_point = data[self.symbol].High
                self.last_swing_low = self.find_swing_low(history_close_price)
                # self.Log(f'max market:{self.max_point} ')
                # self.Log(f'swing low before:{self.last_swing_low} ')
            if self.max_break and self.last_swing_low is not None and not self.inside_trade:
                self.FVG_potential_area.append(data[self.symbol])
                if self.last_swing_low > data[self.symbol].Low:
                    self.FVG_high , self.FVG_low = self.find_FVG(self.FVG_potential_area)
                    if self.FVG_high is not None and self.FVG_low is not None:

                        
                        
                        entry_point =  round(self.FVG_low, 5)
                        #stop_loss =  round(self.max_point, 5)                                                
                        stop_loss =  round(self.max_point*1.0503, 5)                        
                        quantity = abs((self.Portfolio.Cash ) / (stop_loss - entry_point))/1000

                        quantity=1
                        self.Log(f'quantity is: {quantity}')
                        take_profit = round(entry_point - 4*(stop_loss-entry_point),5)

                        #take_profit = round(3 * self.FVG_low - 1.5 * self.max_point,5)/2
                        self.Log(f'max {self.max_point}')
                        self.Log(f'fvg low : {self.FVG_low}')
                        self.Log(f'fvg high : {self.FVG_high}')
                        self.Log(f'stop_loss : {stop_loss}')
                        self.Log(f'take_profit : {take_profit}')
                        self.Log(f'entry_point : {entry_point}')
                        self.entryTicket = self.LimitOrder(self.symbol, -quantity, entry_point, 'Short position')
                        self.stop_loss_ticket = self.StopMarketOrder(self.symbol, quantity, stop_loss, 'stop loss')
                        #self.take_profit_ticket = self.LimitOrder(self.symbol, quantity, take_profit, 'take profit')
                        self.take_profit_ticket = self.LimitOrder(self.symbol, quantity, take_profit, 'take profit new')
                        self.inside_trade = True


                        self.max_break = False
                        self.FVG_high = None
                        self.FVG_low = None
                        self.last_swing_low = None

                
    def OnOrderEvent(self, orderEvent):
        # Check if the order is filled
        
         if self.entryTicket is not None and self.stop_loss_ticket is not None and self.take_profit_ticket is not None:
            if (orderEvent.OrderId == self.stop_loss_ticket.OrderId or orderEvent.OrderId == self.take_profit_ticket.OrderId):
                if orderEvent.Status == OrderStatus.Filled:
                    self.Log(f'our cash here {self.Portfolio.Cash}' )
                    self.Transactions.CancelOpenOrders(self.symbol)
                    self.Liquidate()
                    self.inside_trade = False
                    temp_max = self.max_point
                    self.init_vars()
                    self.max_point = temp_max