backtesting.py
def _process_position(self, symbol, mdata_idx, mdata):
        '''
        mdata_idx - position in market data when event happens
        '''
        mdata_len = len(mdata['open'])
        open_idx = min(mdata_idx + 1, mdata_len - 1)  # we can buy at day idx+1
        close_idx = min(mdata_idx + 1 + self._hold_days, mdata_len - 1)

        if close_idx - open_idx < self._hold_days / 2:
            return  # skip events if we don't have enough days

        if has_split_dividents(mdata, max(open_idx - 5, 0), close_idx):
            return  # skip events if split/dividents happens

        open_position = mdata['open'][open_idx]
        close_position = mdata['open'][close_idx]
        limit_level = min(mdata['low'][open_idx:close_idx]) if self._buy_side else max(mdata['high'][open_idx:close_idx])

        if odd_data(open_position, close_position):
            return  # skip odd events

        profit = self._process_long_position(open_position, close_position, limit_level) if self._buy_side else self._process_short_position(open_position, close_position, limit_level)

        self.txns.append((symbol, mdata['date'][open_idx], mdata['date'][close_idx], open_position, close_position, profit))
        self.balance += profit

        mdata_idx - position in market data when event happens
        
position in market data when event happens
position in market data when event happens
def _process_position(self, symbol, mdata_idx, mdata):
        mdata_len = len(mdata['open'])
        open_idx = min(mdata_idx + 1, mdata_len - 1)  
        close_idx = min(mdata_idx + 1 + self._hold_days, mdata_len - 1)
        if close_idx - open_idx < self._hold_days / 2:
            return  
        if has_split_dividents(mdata, max(open_idx - 5, 0), close_idx):
            return  
        open_position = mdata['open'][open_idx]
        close_position = mdata['open'][close_idx]
        limit_level = min(mdata['low'][open_idx:close_idx]) if self._buy_side else max(mdata['high'][open_idx:close_idx])
        if odd_data(open_position, close_position):
            return  
        profit = self._process_long_position(open_position, close_position, limit_level) if self._buy_side else self._process_short_position(open_position, close_position, limit_level)
        self.txns.append((symbol, mdata['date'][open_idx], mdata['date'][close_idx], open_position, close_position, profit))
        self.balance += profit
def _process_position ( self , symbol , mdata_idx , mdata ) : mdata_len = len ( mdata [ 'open' ] ) open_idx = min ( mdata_idx + 1 , mdata_len - 1 ) close_idx = min ( mdata_idx + 1 + self . _hold_days , mdata_len - 1 ) if close_idx - open_idx < self . _hold_days / 2 : return if has_split_dividents ( mdata , max ( open_idx - 5 , 0 ) , close_idx ) : return open_position = mdata [ 'open' ] [ open_idx ] close_position = mdata [ 'open' ] [ close_idx ] limit_level = min ( mdata [ 'low' ] [ open_idx : close_idx ] ) if self . _buy_side else max ( mdata [ 'high' ] [ open_idx : close_idx ] ) if odd_data ( open_position , close_position ) : return profit = self . _process_long_position ( open_position , close_position , limit_level ) if self . _buy_side else self . _process_short_position ( open_position , close_position , limit_level ) self . txns . append ( ( symbol , mdata [ 'date' ] [ open_idx ] , mdata [ 'date' ] [ close_idx ] , open_position , close_position , profit ) ) self . balance += profit
mdata_idx - position in market data when event happens
mdata_idx - position in market data when event happens
'''
        mdata_idx - position in market data when event happens
        ''' # we can buy at day idx+1 # skip events if we don't have enough days # skip events if split/dividents happens # skip odd events
self
symbol
mdata_idx
mdata