Skip to content

Logging Orders #53

Closed
Closed
@bscully27

Description

@bscully27

Desired Behavior

To dig into each backtest order, specifically to evaluate target prices which I don't believe are working correctly or I'm not calling properly. Didn't see an example in the docs.

I tried adding self.orders to _broker.log but without success - the idea was use copy.copy(self.orders) in backtesting.py for a PIT (point in time) snapshot.

So 2 points I'd like addressed:

  1. How to properly set target prices.
  2. How to enhance the logs with more Order granularity.

Additional Log Attempt

In class _Broker.next(), I called this function when opening or closing a position.

for example in class _Broker.next():
        if entry or orders._close:
            self._update_order_log(self.orders, i)
where:
    def _update_order_log(self, order, i):
            self.log.order_info[i] = copy.copy(order)

then in class _Broker:

    class _Log:
        def __init__(self, length):
        ...
        self.order_info = {k: '' for k in range(length)}

Steps to Reproduce

class SmaCrossTarget(Strategy):
    def init(self):
        self.sma1 = self.I(SMA, self.data.Close, 2)
        self.sma2 = self.I(SMA, self.data.Close, 6)
        self.Close = self.data.Close
        
    def next(self):
        if (self.sma1[-2] < self.sma2[-2]  and  self.sma1[-1] > self.sma2[-1]):
            self.buy(tp=self.Close[-1]*1.05)
        elif (self.sma1[-2] > self.sma2[-2]  and  self.sma1[-1] < self.sma2[-1]):
            self.sell()

GOOG = GOOG.head(40)
bt = Backtest(GOOG, SmaCrossTarget, cash=1000, commission=.001)
result = bt.run()
result._trade_data.dropna(subset=['Entry Price']).head(3)
               Equity  Exit Entry  Exit Position  Entry Price  Entry Direction  Exit Price Exit Comment      P/L  Returns
2004-08-30 1,030.0300         nan            nan     105.2800          -1.0000         nan                   nan      nan
2004-09-09 1,021.8743      7.0000        -9.4890     102.5300           1.0000    102.5300          NaN  25.0957   0.0244
2004-09-28 1,154.8585     14.0000         9.9880     121.3000          -1.0000    121.3000          NaN 186.4511   0.1825

The 3rd line should have been closing the position when target_price was hit.

Additional info

  • Backtesting version: 0.1.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    APINeeds API-related discussionenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions