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:
- Using a simple interface to place market orders:
- Using a long/short position based interface:
- pyalgotrade.strategy.Strategy.enterLong()
- pyalgotrade.strategy.Strategy.enterShort()
- pyalgotrade.strategy.Strategy.enterLongLimit()
- pyalgotrade.strategy.Strategy.enterShortLimit()
- pyalgotrade.strategy.Strategy.enterLongStop()
- pyalgotrade.strategy.Strategy.enterShortStop()
- pyalgotrade.strategy.Strategy.enterLongStopLimit()
- pyalgotrade.strategy.Strategy.enterShortStopLimit()
- pyalgotrade.strategy.Strategy.exitPosition()
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. |
Generates the exit order for the position.
Parameters: |
|
---|
Note
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 only called if the order was placed using the broker interface directly.
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 backtest the strategy.
Base class for positions.
Parameters: |
|
---|
Note
This is a base class and should not be used directly.
Returns True if the entry order was filled.
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.
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.