Skip to content

Commit

Permalink
fix order_percent; clear comment
Browse files Browse the repository at this point in the history
  • Loading branch information
hzliu committed Apr 3, 2017
1 parent 3243c75 commit a55a6cb
Showing 1 changed file with 1 addition and 89 deletions.
90 changes: 1 addition & 89 deletions rqalpha/api/api_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,6 @@ def order_shares(id_or_ins, amount, style=MarketOrder()):
#购买1000股的平安银行股票,并以限价单发送,价格为¥10:
order_shares('000001.XSHG', 1000, style=LimitOrder(10))
"""
# Place an order by specified number of shares. Order type is also
# passed in as parameters if needed. If style is omitted, it fires a
# market order by default.
# :PARAM id_or_ins: the instrument to be ordered
# :type id_or_ins: str or Instrument
# :param float amount: Number of shares to order. Positive means buy,
# negative means sell. It will be rounded down to the closest
# integral multiple of the lot size
# :param style: Order type and default is `MarketOrder()`. The
# available order types are: `MarketOrder()` and
# `LimitOrder(limit_price)`
# :return: A unique order id.
# :rtype: int
if amount is 0:
# 如果下单量为0,则认为其并没有发单,则直接返回None
return None
Expand Down Expand Up @@ -180,18 +167,6 @@ def order_lots(id_or_ins, amount, style=MarketOrder()):
order_lots('000001.XSHE', 10, style=LimitOrder(10))
"""
# Place an order by specified number of lots. Order type is also passed
# in as parameters if needed. If style is omitted, it fires a market
# order by default.
# :param id_or_ins: the instrument to be ordered
# :type id_or_ins: str or Instrument
# :param float amount: Number of lots to order. Positive means buy,
# negative means sell.
# :param style: Order type and default is `MarketOrder()`. The
# available order types are: `MarketOrder()` and
# `LimitOrder(limit_price)`
# :return: A unique order id.
# :rtype: int
order_book_id = assure_stock_order_book_id(id_or_ins)

round_lot = int(Environment.get_instance().get_instrument(order_book_id).round_lot)
Expand Down Expand Up @@ -229,20 +204,6 @@ def order_value(id_or_ins, cash_amount, style=MarketOrder()):
order_value('000001.XSHE', -10000)
"""
# Place an order by specified value amount rather than specific number
# of shares/lots. Negative cash_amount results in selling the given
# amount of value, if the cash_amount is larger than you current
# security’s position, then it will sell all shares of this security.
# Orders are always truncated to whole lot shares.
# :param id_or_ins: the instrument to be ordered
# :type id_or_ins: str or Instrument
# :param float cash_amount: Cash amount to buy / sell the given value of
# securities. Positive means buy, negative means sell.
# :param style: Order type and default is `MarketOrder()`. The
# available order types are: `MarketOrder()` and
# `LimitOrder(limit_price)`
# :return: A unique order id.
# :rtype: int
if not isinstance(style, OrderStyle):
raise RQInvalidArgument(_(u"style should be OrderStyle"))
if isinstance(style, LimitOrder):
Expand Down Expand Up @@ -281,7 +242,7 @@ def order_value(id_or_ins, cash_amount, style=MarketOrder()):
@ExecutionContext.enforce_phase(EXECUTION_PHASE.ON_BAR,
EXECUTION_PHASE.SCHEDULED)
@apply_rules(verify_that('id_or_ins').is_valid_stock(),
verify_that('percent').is_number().is_greater_than(-1).is_less_than(1),
verify_that('percent').is_number().is_greater_or_equal_than(-1).is_less_or_equal_than(1),
verify_that('style').is_instance_of((MarketOrder, LimitOrder)))
def order_percent(id_or_ins, percent, style=MarketOrder()):
"""
Expand All @@ -304,22 +265,6 @@ def order_percent(id_or_ins, percent, style=MarketOrder()):
#买入等于现有投资组合50%价值的平安银行股票。如果现在平安银行的股价是¥10/股并且现在的投资组合总价值是¥2000,那么将会买入200股的平安银行股票。(不包含交易成本和滑点的损失):
order_percent('000001.XSHG', 0.5)
"""
# Place an order for a security for a given percent of the current
# portfolio value, which is the sum of the positions value and
# ending cash balance. A negative percent order will result in
# selling given percent of current portfolio value. Orders are
# always truncated to whole shares. Percent should be a decimal
# number (0.50 means 50%), and its absolute value is <= 1.
# :param id_or_ins: the instrument to be ordered
# :type id_or_ins: str or Instrument
# :param float percent: Percent of the current portfolio value. Positive
# means buy, negative means selling give percent of the current
# portfolio value. Orders are always truncated according to lot size.
# :param style: Order type and default is `MarketOrder()`. The
# available order types are: `MarketOrder()` and
# `LimitOrder(limit_price)`
# :return: A unique order id.
# :rtype: int
if percent < -1 or percent > 1:
raise RQInvalidArgument(_(u"percent should between -1 and 1"))

Expand Down Expand Up @@ -354,20 +299,6 @@ def order_target_value(id_or_ins, cash_amount, style=MarketOrder()):
#如果现在的投资组合中持有价值¥3000的平安银行股票的仓位并且设置其目标价值为¥10000,以下代码范例会发送价值¥7000的平安银行的买单到市场。(向下调整到最接近每手股数即100的倍数的股数):
order_target_value('000001.XSHE', 10000)
"""
# Place an order to adjust a position to a target value. If there is no
# position for the security, an order is placed for the whole amount
# of target value. If there is already a position for the security,
# an order is placed for the difference between target value and
# current position value.
# :param id_or_ins: the instrument to be ordered
# :type id_or_ins: str or Instrument
# :param float cash_amount: Target cash value for the adjusted position
# after placing order.
# :param style: Order type and default is `MarketOrder()`. The
# available order types are: `MarketOrder()` and
# `LimitOrder(limit_price)`
# :return: A unique order id.
# :rtype: int
order_book_id = assure_stock_order_book_id(id_or_ins)

position = Environment.get_instance().portfolio.accounts[ACCOUNT_TYPE.STOCK].positions.get_or_create(order_book_id)
Expand Down Expand Up @@ -413,25 +344,6 @@ def order_target_percent(id_or_ins, percent, style=MarketOrder()):
#如果投资组合中已经有了平安银行股票的仓位,并且占据目前投资组合的10%的价值,那么以下代码会买入平安银行股票最终使其占据投资组合价值的15%:
order_target_percent('000001.XSHE', 0.15)
"""
# Place an order to adjust position to a target percent of the portfolio
# value, so that your final position value takes the percentage you
# defined of your whole portfolio.
# position_to_adjust = target_position - current_position
# Portfolio value is calculated as sum of positions value and ending
# cash balance. The order quantity will be rounded down to integral
# multiple of lot size. Percent should be a decimal number (0.50
# means 50%), and its absolute value is <= 1. If the
# position_to_adjust calculated is positive, then it fires buy
# orders, otherwise it fires sell orders.
# :param id_or_ins: the instrument to be ordered
# :type id_or_ins: str or Instrument
# :param float percent: Number of percent to order. It will be rounded down
# to the closest integral multiple of the lot size
# :param style: Order type and default is `MarketOrder()`. The
# available order types are: `MarketOrder()` and
# `LimitOrder(limit_price)`
# :return: A unique order id.
# :rtype: int
if percent < 0 or percent > 1:
raise RQInvalidArgument(_(u"percent should between 0 and 1"))
order_book_id = assure_stock_order_book_id(id_or_ins)
Expand Down

0 comments on commit a55a6cb

Please sign in to comment.