strategy – Basic strategy classes

class pyalgotrade.strategy.Position(strategy, entryOrder, goodTillCanceled)

Base class for positions.

Parameters:

Note

This is a base class and should not be used directly.

entryFilled()

Returns True if the entry order was filled.

exitFilled()

Returns True if the exit order was filled.

getEntryOrder()

Returns the pyalgotrade.broker.Order used to enter the position.

getExitOnSessionClose()

Returns True if an order to exit the position should be automatically submitted when the session is about to close.

getExitOrder()

Returns the pyalgotrade.broker.Order used to exit the position. If this position hasn’t been closed yet, None is returned.

getInstrument()

Returns the instrument used for this position.

getNetProfit(includeCommissions=True)

Returns the position’s net profit.

getQuantity()

Returns the number of shares used to enter this position.

getResult()

Returns the ratio between the order prices. It doesn’t include commisions.

getReturn(includeCommissions=True)

Returns the position’s returns.

setExitOnSessionClose(exitOnSessionClose)

Set to True to automatically place an exit order when the session is about to close. Only useful for intraday trading.

Note

If the entry order was not filled by the time the session is about to close, it will get canceled.

class pyalgotrade.strategy.Strategy(barFeed, cash=1000000, broker_=None)

Base class for strategies.

Parameters:

Note

This is a base class and should not be used directly.

attachAnalyzer(strategyAnalyzer)

Adds a pyalgotrade.stratanalyzer.StrategyAnalyzer.

enterLong(instrument, quantity, goodTillCanceled=False)

Generates a buy pyalgotrade.broker.MarketOrder to enter a long position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The Position entered.

enterLongLimit(instrument, limitPrice, quantity, goodTillCanceled=False)

Generates a buy pyalgotrade.broker.LimitOrder to enter a long position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • limitPrice (float.) – Limit price.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The Position entered.

enterLongStop(instrument, stopPrice, quantity, goodTillCanceled=False)

Generates a buy pyalgotrade.broker.StopOrder to enter a long position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • stopPrice (float.) – Stop price.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The Position entered.

enterLongStopLimit(instrument, limitPrice, stopPrice, quantity, goodTillCanceled=False)

Generates a buy pyalgotrade.broker.StopLimitOrder order to enter a long position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • limitPrice (float.) – Limit price.
  • stopPrice (float.) – Stop price.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The Position entered.

enterShort(instrument, quantity, goodTillCanceled=False)

Generates a sell short pyalgotrade.broker.MarketOrder to enter a short position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The Position entered.

enterShortLimit(instrument, limitPrice, quantity, goodTillCanceled=False)

Generates a sell short pyalgotrade.broker.LimitOrder to enter a short position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • limitPrice (float.) – Limit price.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The Position entered.

enterShortStop(instrument, stopPrice, quantity, goodTillCanceled=False)

Generates a sell short pyalgotrade.broker.StopOrder to enter a short position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • stopPrice (float.) – Stop price.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The Position entered.

enterShortStopLimit(instrument, limitPrice, stopPrice, quantity, goodTillCanceled=False)

Generates a sell short pyalgotrade.broker.StopLimitOrder order to enter a short position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • limitPrice (float.) – Limit price.
  • stopPrice (float.) – The Stop price.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The Position entered.

exitPosition(position, limitPrice=None, stopPrice=None, goodTillCanceled=None)

Generates the exit order for the position.

Parameters:
  • position (Position.) – A position returned by any of the enterLongXXX or enterShortXXX methods.
  • limitPrice (float.) – The limit price.
  • stopPrice (float.) – The stop price.
  • goodTillCanceled (boolean.) – True if the exit order is good till canceled. If False then the order gets automatically canceled when the session closes. If None, then it will match the entry order.

Note

  • If the entry order was not filled yet, it will be canceled.
  • If a previous exit order for this position was filled, this won’t have any effect.
  • If a previous exit order for this position is pending, it will get canceled and the new exit order submitted.
  • If limitPrice is not set and stopPrice is not set, then a pyalgotrade.broker.MarketOrder is used to exit the position.
  • If limitPrice is set and stopPrice is not set, then a pyalgotrade.broker.LimitOrder is used to exit the position.
  • If limitPrice is not set and stopPrice is set, then a pyalgotrade.broker.StopOrder is used to exit the position.
  • If limitPrice is set and stopPrice is set, then a pyalgotrade.broker.StopLimitOrder is used to exit the position.
getBroker()

Returns the pyalgotrade.broker.Broker used to handle order executions.

getCurrentDateTime()

Returns the datetime.datetime for the current pyalgotrade.bar.Bar.

getFeed()

Returns the pyalgotrade.barfeed.BarFeed that this strategy is using.

onBars(bars)

Override (mandatory) to get notified when new bars are available. The default implementation raises an Exception.

This is the method to override to enter your trading logic and enter/exit positions.

Parameters:bars (pyalgotrade.bar.Bars.) – The current bars.
onEnterCanceled(position)

Override (optional) to get notified when the order submitted to enter a position was canceled. The default implementation is empty.

Parameters:position (Position.) – A position returned by any of the enterLongXXX or enterShortXXX methods.
onEnterOk(position)

Override (optional) to get notified when the order submitted to enter a position was filled. The default implementation is empty.

Parameters:position (Position.) – A position returned by any of the enterLongXXX or enterShortXXX methods.
onExitCanceled(position)

Override (optional) to get notified when the order submitted to exit a position was canceled. The default implementation is empty.

Parameters:position (Position.) – A position returned by any of the enterLongXXX or enterShortXXX methods.
onExitOk(position)

Override (optional) to get notified when the order submitted to exit a position was filled. The default implementation is empty.

Parameters:position (Position.) – A position returned by any of the enterLongXXX or enterShortXXX methods.
onFinish(bars)

Override (optional) to get notified when the strategy finished executing. The default implementation is empty.

Parameters:bars (pyalgotrade.bar.Bars.) – The last bars processed.
onOrderUpdated(order)

Override (optional) to get notified when an order gets updated. This is only called if the order was placed using the broker interface directly.

Parameters:order (pyalgotrade.broker.Order.) – The order updated.
onStart()

Override (optional) to get notified when the strategy starts executing. The default implementation is empty.

run()

Call once (and only once) to backtest the strategy.

Previous topic

broker – Order management classes

Next topic

stratanalyzer – Strategy analyzers

This Page