Module areixio.strategy

Classes

class Strategy

Strategy Base

Ancestors

Instance variables

var available_balance : float

Methods

def before_order_place(self, order: OrderData) ‑> bool

The function will be invoked before trading (Can conduct money management and risk management here)

Args

order : [dict]
order you have placed

Returns

[bool]
True, if trade is executable
def buy(self, symbol: str, quantity: float, price: float = None, stop_price: float = None, trail_price: float = None, trail_percent: float = None, position_side: Union[str, PositionSide, ForwardRef(None)] = PositionSide.NET, exchange: Union[str, Exchange, ForwardRef(None)] = None, msg: str = '', ttl: Union[str, int, ForwardRef(None)] = 'GTC', **kwargs) ‑> OrderData

Create a buy (long) order and send it to the broker

Args

symbol (str):
- The instrument symbol that you would like to trade.
- The symbol must exists in self.ctx.feeds
quantity (float):
- The quantity of the order.
- The quanity must be positive.
price (float, optional):
- The price of the order.
- The price must be positive.
- If price is None, the order would be a Market order
- If price is specified, the order would be a Limit order. (also can be regarded as TakeProfit order)
- Defaults to None.
stop_price (float, optional):
- If stop_price is specified, the order would be a Stop order.
- If side is 'SELL' with stop_price,
- when the stop price is greater than last price, the type of order would be TakeProfit.
- when the stop price is less or equal than last price, the type of order would be StopLoss.
- If side is 'BUY' with stop_price,
- when the stop price is greater or equal than last price, the type of order would be StopLoss.
- when the stop price is less than last price, the type of order would be TakeProfit.
- If both stop_price and price are specified, the order would be a StopLimit order in general.
- When the price is matched, the StopLimit order will become a Limit order
- Defaults to None.
trail_price : float, optional
trail_price. Defaults to None.
trail_percent : float, optional
trail_percent. Defaults to None.
position_side : Optional[Union[str, PositionSide]], optional
position_side. Defaults to PositionSide.NET.
exchange : Optional[Union[str, Exchange]], optional
exchange. Defaults to None.
msg : str, optional
message. Defaults to "".
ttl : Optional[Union[str, int]], optional
time to live, also known as time in force. Defaults to "GTC".

Returns

OrderData
description
def cancel_all(self, symbol: str = None, exchange: Exchange = None, position_side: PositionSide = PositionSide.NET)

Cancel all the orders

Args

symbol : str, optional
description. Defaults to None.
def cancel_order(self, order: OrderData) ‑> OrderData

Cancels the order in the broker

Args

order : OrderData
description

Returns

OrderData
description
def close(self, symbol: str, price: float = None, stop_price: float = None, trail_price: float = None, trail_percent: float = None, position_side: Union[str, PositionSide, ForwardRef(None)] = PositionSide.NET, exchange: Union[str, Exchange, ForwardRef(None)] = None, msg: str = '', ttl: Union[str, int, ForwardRef(None)] = 'GTC', **kwargs) ‑> OrderData

Close the position

Args

symbol : str
description
price : float, optional
description. Defaults to None.
stop_price : float, optional
description. Defaults to None.
trail_price : float, optional
description. Defaults to None.
trail_percent : float, optional
description. Defaults to None.
position_side : Optional[Union[str, PositionSide]], optional
description. Defaults to PositionSide.NET.
exchange : Optional[Union[str, Exchange]], optional
description. Defaults to None.
msg : str, optional
description. Defaults to "".
ttl : Optional[Union[str, int]], optional
description. Defaults to "GTC".

Returns

OrderData
description
def draw_figure(self)

Plot custom bokeh figure into report html

def on_bar(self, tick: datetime.datetime)

This method must be implemented, and write your own trading strategy here

Call the strategy on_bar method to let the strategy evaluate the new data

Args

tick : [datetime]
datetime of current bar
def on_order_amend(self, order: OrderData)

The function will be invoked after an order is amend

Args

order : [dict]
order you have placed
def on_order_cancel(self, order: OrderData)

The function will be invoked after an order is failed(cancel)

Args

order : [dict]
order you have placed
def on_order_fail(self, trade: TradeData)

The function will be invoked after an order is failed(timeout/expired/margin....)

Args

order : [dict]
order you have placed
def on_order_fill(self, order: OrderData)

The function will be invoked after an order is executed or filled, it results in a trade

Args

order : [dict]
order you have placed
def on_order_place(self, order: OrderData)

The function will be invoked after an order is placed

Args

order : [dict]
order you have placed
def on_tick(self, tick: datetime.datetime)

Call the strategy on_bar method to let the strategy evaluate the new data

Args

tick : [datetime]
datetime of current bar
def order_amount(self, symbol: str, amount: float, side: Union[str, Side, ForwardRef(None)], price: float = None, stop_price: float = None, trail_price: float = None, trail_percent: float = None, position_side: Union[str, PositionSide, ForwardRef(None)] = PositionSide.NET, exchange: Union[str, Exchange, ForwardRef(None)] = None, msg: str = '', ttl: Union[str, int, ForwardRef(None)] = 'GTC', **kwargs) ‑> OrderData

Create an order and the quantity will depend on your placed amount.

Args

amount (float): - The amount that you would like to place, NOTE: as order cost (without considering leverage ) The rest of the parameters see also Strategy.buy()

Returns

OrderData
order you have placed
def order_lotsize(self, symbol: str, lots: float, side: Union[str, Side, ForwardRef(None)], price: float = None, stop_price: float = None, trail_price: float = None, trail_percent: float = None, position_side: Union[str, PositionSide, ForwardRef(None)] = PositionSide.NET, exchange: Union[str, Exchange, ForwardRef(None)] = None, msg: str = '', ttl: Union[str, int, ForwardRef(None)] = 'GTC', **kwargs) ‑> OrderData

Create an order and the quantity will depend on the lots and lot size

Args

lots (float): - The lots that you would like to place the order - The order quantity will automatically be the lots times lot_size side (Optional[Union[str, Side]]): - The side that you would like to place the order - Must be one of the: - Side.BUY - Side.SELL - Side.SHORTSELL The rest of the parameters see also Strategy.buy()

Returns

OrderData
order you have placed
def order_target_percent(self, symbol: str, target_percent: float, price: float = None, stop_price: float = None, trail_price: float = None, trail_percent: float = None, position_side: Union[str, PositionSide, ForwardRef(None)] = PositionSide.NET, exchange: Union[str, Exchange, ForwardRef(None)] = None, msg: str = '', ttl: Union[str, int, ForwardRef(None)] = 'GTC', **kwargs) ‑> OrderData

Place an order to rebalance a position to have final value of target percentage of current portfolio value

Args

target_percent (float): - The target percentage(in real number) The rest of the parameters see also Strategy.buy()

Returns

OrderData
order you have placed
def order_target_quantity(self, symbol: str, overall_quantity: float, price: float = None, stop_price: float = None, trail_price: float = None, trail_percent: float = None, position_side: Union[str, PositionSide, ForwardRef(None)] = PositionSide.NET, exchange: Union[str, Exchange, ForwardRef(None)] = None, msg: str = '', ttl: Union[str, int, ForwardRef(None)] = 'GTC', **kwargs) ‑> OrderData

Place an order to rebalance a position to have final size of target

Args

overall_quantity (float): - The overall quantity that this position that you would like to hold The rest of the parameters see also Strategy.buy()

Returns

OrderData
order you have placed
def order_target_value(self, symbol: str, overall_amount: float, price: float = None, stop_price: float = None, trail_price: float = None, trail_percent: float = None, position_side: Union[str, PositionSide, ForwardRef(None)] = PositionSide.NET, exchange: Union[str, Exchange, ForwardRef(None)] = None, msg: str = '', ttl: Union[str, int, ForwardRef(None)] = 'GTC', **kwargs) ‑> OrderData

Place an order to rebalance a position to have final value of target

Args

overall_amount (float): - The overall amount that this position that you would like to hold The rest of the parameters see also Strategy.buy()

Returns

OrderData
order you have placed
def sell(self, symbol: str, quantity: float, price: float = None, stop_price: float = None, trail_price: float = None, trail_percent: float = None, position_side: Union[str, PositionSide, ForwardRef(None)] = PositionSide.NET, exchange: Union[str, Exchange, ForwardRef(None)] = None, msg: str = '', ttl: Union[str, int, ForwardRef(None)] = 'GTC', **kwargs) ‑> OrderData

To create a selll (short) order and send it to the broker

Args

The parameters see also Strategy.buy()

Returns

[dict]
order you have placed

Inherited members