Strategies are the classes that you define that implement the trading logic, when to buy, when to sell, etc. Buying and selling can be done in 3 different ways:
- Using a simple interface to place market orders:
- Using a long/short position based interface:
- pyalgotrade.strategy.BaseStrategy.enterLong()
- pyalgotrade.strategy.BaseStrategy.enterShort()
- pyalgotrade.strategy.BaseStrategy.enterLongLimit()
- pyalgotrade.strategy.BaseStrategy.enterShortLimit()
- pyalgotrade.strategy.BaseStrategy.enterLongStop()
- pyalgotrade.strategy.BaseStrategy.enterShortStop()
- pyalgotrade.strategy.BaseStrategy.enterLongStopLimit()
- pyalgotrade.strategy.BaseStrategy.enterShortStopLimit()
Using the pyalgotrade.broker.Broker interface directly.
Base class for strategies.
Parameters: |
|
---|
Note
This is a base class and should not be used directly.
Generates a buy pyalgotrade.broker.MarketOrder to enter a long position.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.strategy.position.Position entered. |
Generates a buy pyalgotrade.broker.LimitOrder to enter a long position.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.strategy.position.Position entered. |
Generates a buy pyalgotrade.broker.StopOrder to enter a long position.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.strategy.position.Position entered. |
Generates a buy pyalgotrade.broker.StopLimitOrder order to enter a long position.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.strategy.position.Position entered. |
Generates a sell short pyalgotrade.broker.MarketOrder to enter a short position.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.strategy.position.Position entered. |
Generates a sell short pyalgotrade.broker.LimitOrder to enter a short position.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.strategy.position.Position entered. |
Generates a sell short pyalgotrade.broker.StopOrder to enter a short position.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.strategy.position.Position entered. |
Generates a sell short pyalgotrade.broker.StopLimitOrder order to enter a short position.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.strategy.position.Position entered. |
Returns the pyalgotrade.broker.Broker used to handle order executions.
Returns the datetime.datetime for the current pyalgotrade.bar.Bar.
Returns the pyalgotrade.barfeed.BarFeed that this strategy is using.
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. |
---|
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. |
---|
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. |
---|
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. |
---|
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. |
---|
Override (optional) to get notified when the strategy finished executing. The default implementation is empty.
Parameters: | bars (pyalgotrade.bar.Bars.) – The last bars processed. |
---|
Override (optional) to get notified when an order gets updated. This is not called for orders placed using any of the enterLong or enterShort methods.
Parameters: | order (pyalgotrade.broker.Order.) – The order updated. |
---|
Override (optional) to get notified when the strategy starts executing. The default implementation is empty.
Places a market order.
Parameters: |
|
---|---|
Return type: | The pyalgotrade.broker.MarketOrder submitted. |
Call once (and only once) to run the strategy.
Stops a running strategy.
Bases: pyalgotrade.strategy.BaseStrategy
Base class for backtesting strategies.
Parameters: |
|
---|
Note
This is a base class and should not be used directly.
Base class for positions.
Parameters: |
|
---|
Note
This is a base class and should not be used directly.
Cancels the entry order if its active.
Cancels the exit order if its active.
Returns True if the entry order is active.
Returns True if the entry order was filled.
Generates the exit order for the position.
Parameters: |
|
---|
Note
Returns True if the exit order is active.
Returns True if the exit order was filled.
Returns the pyalgotrade.broker.Order used to enter the position.
Returns True if an order to exit the position should be automatically submitted when the session is about to close.
Returns the pyalgotrade.broker.Order used to exit the position. If this position hasn’t been closed yet, None is returned.
Returns the instrument used for this position.
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.
Returns the number of shares used to enter this position.
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.
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.
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.
Returns True if the position is open.
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.