strategy – Basic strategy classes

Strategies are the classes that you define, that implement a certain trading strategy. When to buy, when to sell, etc. Buying and selling can be done in 3 different ways:

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 pyalgotrade.strategy.position.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 pyalgotrade.strategy.position.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 pyalgotrade.strategy.position.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 pyalgotrade.strategy.position.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 pyalgotrade.strategy.position.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 pyalgotrade.strategy.position.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 pyalgotrade.strategy.position.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 pyalgotrade.strategy.position.Position entered.

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

Generates the exit order for the position.

Parameters:
  • position (pyalgotrade.strategy.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 (pyalgotrade.strategy.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 (pyalgotrade.strategy.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 (pyalgotrade.strategy.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 (pyalgotrade.strategy.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.

order(instrument, quantity, onClose=False, goodTillCanceled=False)

Places a market order.

Parameters:
  • instrument (string.) – Instrument identifier.
  • quantity (int.) – The amount of shares. Positive means buy, negative means sell.
  • onClose (boolean.) – True if the order should be filled as close to the closing price as possible (Market-On-Close order). Default is False.
  • goodTillCanceled (boolean.) – True if the order is good till canceled. If False then the order gets automatically canceled when the session closes.
Return type:

The pyalgotrade.broker.MarketOrder submitted.

run()

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

class pyalgotrade.strategy.position.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)

Calculates the PnL for the position.

Parameters:includeCommissions (boolean.) – True to include commisions in the calculation.
Return type:A float with the PnL.

Note

The position must be closed.

getQuantity()

Returns the number of shares used to enter this position.

getReturn(includeCommissions=True)

Calculates the returns for the position.

Parameters:includeCommissions (boolean.) – True to include commisions in the calculation.
Return type:A float between 0 and 1.

Note

The position must be closed.

getUnrealizedNetProfit(marketPrice)

Calculates the unrealized PnL for the position.

Parameters:marketPrice (float.) – Price used to calculate the PnL. This value is used as the current price and compared against your entry price.
Return type:A float with the unrealized PnL.

Note

The position must be open.

getUnrealizedReturn(marketPrice)

Calculates the unrealized returns for the position.

Parameters:marketPrice (float.) – Price used to calculate the return. This value is used as the current price and compared against your entry price.
Return type:A float between 0 and 1.

Note

The position must be open.

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.

Previous topic

broker – Order management classes

Next topic

stratanalyzer – Strategy analyzers

This Page