pythontradingpyalgotrade

Pyalgotrade advice needed for stoploss orders


I want to backtest a trading strategy in pyalgotrade, but I’m having problems submitting the stoploss order.

In the documentation it states: Positions are higher level abstractions for placing orders. They are escentially a pair of entry-exit orders and allow to track returns and PnL easier that placing orders manually.

I enter the position with

myPosition = self.enterLong(self.__instrument, amount, True)

This basically opens a new position in shares and buys at market price, which by itself works.

I’d then expect to place the stop order with

myPosition.exitStop(stoplossValue, True)

… but this behaves really strange!

If the position isFilled, which is the case when the enterLong order was executed, then exitStop raises an assert error, because it seems to expect the order to be “isActive” (which conflicts isFilled).

When I call exitStop before the order isFilled (while isActive), the code generates no assert error, but the active order gets canceled immediately.

It absolutely makes no sense to call exitStop when the initial order hasn’t been executed yet. Or am I totally off the wall with my thinking?

Unfortunately the pyalgotrade tutorial strategies do not use any stoploss logic (which is bad).


Solution

  • Since you already posted the same question to the library group, I'm not duplicating the answer here. Take a look at https://groups.google.com/forum/#!topic/pyalgotrade/WNNZQ0VvuTc