strategy – Basic strategy classes

class pyalgotrade.strategy.Position(entryOrder)

Base class for positions.

Parameters:entryOrder (pyalgotrade.broker.Order) – The order used to enter the position.

Note

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

getEntryOrder()

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

getExitOnSessionClose()

Returns True if an order to exit the position should be automatically placed.

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()

Returns the difference between the order prices. It does include commisions.

getQuantity()

Returns the number of shares used to enter this position.

getResult()

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

setExitOnSessionClose(exitOnSessionClose)

Set to True to automatically place the exit order in the last bar for the session.

class pyalgotrade.strategy.Strategy(barFeed, cash=0)

Base class for strategies.

Parameters:
  • barFeed (pyalgotrade.barfeed.BarFeed.) – The bar feed to use to backtest the strategy.
  • cash (int/float.) – The amount of cash available.

Note

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

enterLong(instrument, quantity, goodTillCanceled=False)

Generates a buy market order to enter a long position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry/exit orders are good till canceled.
Return type:

The Position entered.

enterShort(instrument, quantity, goodTillCanceled=False)

Generates a sell market order to enter a short position.

Parameters:
  • instrument (string.) – Instrument identifier.
  • quantity (int.) – Entry order quantity.
  • goodTillCanceled (boolean.) – True if the entry/exit orders are good till canceled.
Return type:

The Position entered.

exitPosition(position)

Generates the exit order for the position.

Parameters:position (Position.) – A position returned by enterLong() or enterShort().
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 enterLong() or enterShort().
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 enterLong() or enterShort().
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 enterLong() or enterShort().
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 enterLong() or enterShort().
onFinish(bars)

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

Parameters:bars (pyalgotrade.bar.Bars.) – The latest bars processed.
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

technical – Technical indicators

Next topic

broker – Order management classes

This Page