Skip to content

0.2.4 -> 0.3.1-0.3.0 self.buy/sell not puting order anymore? #273

Closed
@Benouare

Description

@Benouare

Hi,

Before to start, just want to tell that I know that the _.strategy is not in the official documentation, i know that using it could be risky because it could change at anytime without support.

Expected Behavior

Requirements :
Backtesting==0.2.4

class Test(Strategy):
    def init(self):
        price = self.data.Close

    def next(self):
        if not self.position:
            if self.data.index[-1].date() == date(2010, 5, 13):
                print("buy")
                self.buy()

In 0.2.4, if today == 2010, 5, 13, and if my strategy is doing self.buy, I expect that my strategy will buy the stock tomorrow at the open price. But for now/today, I "just" expect, that the lib add and order in the orders list.

data = GOOG
the_date = pd.to_datetime(datetime(2010, 5, 13))
data = data.loc[:the_date]
bt = Backtest(data, Test,
                      cash=10000, commission=.002)
output = bt.run()
print(output._strategy.orders)

it will gives to me an array with one order inside. The order is not contingent (not at sl/tp) :

(<Order size=1.0, contingent=0>,)

This order will be executed tomorrow when the stockmarket will open (@ open price).

Actual Behavior

In 0.3.1, with the same data, the same strategy, the order array is empty.
I would like to know if i missunderstood something, if there is an other way to know if there is a pending buy/sell order, or if this is a bug.

Steps to Reproduce

  1. pip install backtesting==0.2.4
  2. pytest test.py
    That will gives you :
buy
(<Order size=1.0, contingent=0>,)
<Order size=1.0, contingent=0>
  1. pip install backtesting==0.3.1
  2. pytest test.py
    That will gives you :
buy
()
IndexError: tuple index out of range

import unittest
from datetime import date, datetime

import pandas as pd
from backtesting import Backtest, Strategy
from backtesting.test import GOOG, SMA


class Test(Strategy):
    def init(self):
        price = self.data.Close

    def next(self):
        if not self.position:
            if self.data.index[-1].date() == date(2010, 5, 13):
                print("buy")
                self.buy()


class BasicTestSuite(unittest.TestCase):
    """Basic test cases."""

    def test_lib(self):
        data = GOOG
        the_date = pd.to_datetime(datetime(2010, 5, 13))
        data = data.loc[:the_date]

        bt = Backtest(data, Test,
                      cash=10000, commission=.002)
        output = bt.run()
        print(output._strategy.orders)
        print(output._strategy.orders[0])
        self.assertEqual(len(output._strategy.orders), 1)


if __name__ == '__main__':
    unittest.main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions