Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def handle_data_api(context, data):
if context.incr == 0:
assert 0 not in context.portfolio.positions
else:
assert context.portfolio.positions[0].amount == \
context.incr, "Orders not filled immediately."
assert context.portfolio.positions[0].last_sale_price == \
data.current(sid(0), "price"), \
"Orders not filled at current price."
context.incr += 1
order(sid(0), 1)
record(incr=context.incr)
amount=position.amount, price=context.price, profit=profit
)
)
else:
log.info("no buy or sell opportunity found")
else:
# Buy when not holding and got buy signal
if isBuy(context, analysis):
if context.portfolio.cash < context.price * context.ORDER_SIZE:
log.warn(
"Skipping signaled buy due to cash amount: {} < {}".format(
context.portfolio.cash, (context.price * context.ORDER_SIZE)
)
)
return
order(
asset=context.asset,
amount=context.ORDER_SIZE,
limit_price=context.price * (1 + context.SLIPPAGE_ALLOWED),
)
log.info(
"Bought {amount} @ {price}".format(amount=context.ORDER_SIZE, price=context.price)
)
def _stop_loss_sell(self, context, position):
order(
asset=self.state.asset,
amount=-position.amount,
# limit_price=self.state.price * (1 - self.state.SLIPPAGE_ALLOWED),
)
profit = (self.state.price * position.amount) - (
position.cost_basis * position.amount
)
msg = "Sold {amount} @ {price} Profit: {profit}; Produced by stop-loss signal at {date}".format(
amount=position.amount,
price=self.state.price,
profit=profit,
date=get_datetime(),
)
if is_buy:
if buy_increment is None:
log.info('the rsi is too high to consider buying {}'.format(rsi))
return
if price * buy_increment > cash:
log.info('not enough base currency to consider buying')
return
log.info(
'buying position cheaper than cost basis {} < {}'.format(
price,
cost_basis
)
)
order(
asset=context.asset,
amount=buy_increment,
limit_price=price * (1 + context.SLIPPAGE_ALLOWED)
)
'found {amount} positions with cost basis {cost_basis}'.format(
amount=amount,
cost_basis=cost_basis
)
)
stop = context.position['stop']
target = cost_basis * (1 + context.TARGET)
if price >= target:
context.position['cost_basis'] = price
context.position['stop'] = context.STOP
stop_target = context.STOP_LOSS if stop is None else context.STOP
if price < cost_basis * (1 - stop_target):
log.info('executing stop loss')
order(
asset=context.asset,
amount=-amount,
limit_price=price * (1 - context.SLIPPAGE_ALLOWED),
)
# action = 0
context.position = None
else:
if signal == 'long':
log.info('opening position')
buy_amount = context.MAX_HOLDINGS / price
order(
asset=context.asset,
amount=buy_amount,
limit_price=price * (1 + context.SLIPPAGE_ALLOWED),
)
amount=position.amount, price=context.price, profit=profit
)
)
else:
log.info("no buy or sell opportunity found")
else:
# Buy when not holding and got buy signal
if isBuy(context, analysis):
if context.portfolio.cash < context.price * context.ORDER_SIZE:
log.warn(
"Skipping signaled buy due to cash amount: {} < {}".format(
context.portfolio.cash, (context.price * context.ORDER_SIZE)
)
)
return
order(
asset=context.asset,
amount=context.ORDER_SIZE,
limit_price=context.price * (1 + context.SLIPPAGE_ALLOWED),
)
log.info(
"Bought {amount} @ {price}".format(amount=context.ORDER_SIZE, price=context.price)
)
amount=position.amount, price=context.price, profit=profit
)
)
else:
log.info("no buy or sell opportunity found")
else:
# Buy when not holding and got buy signal
if isBuy(context, analysis):
if context.portfolio.cash < context.price * context.ORDER_SIZE:
log.warn(
"Skipping signaled buy due to cash amount: {} < {}".format(
context.portfolio.cash, (context.price * context.ORDER_SIZE)
)
)
return
order(
asset=context.asset,
amount=context.ORDER_SIZE,
limit_price=context.price * (1 + context.SLIPPAGE_ALLOWED),
)
log.info(
"Bought {amount} @ {price}".format(amount=context.ORDER_SIZE, price=context.price)
)
def _default_buy(self, context, size=None, price=None, slippage=None):
if context.asset not in context.portfolio.positions:
order(
asset=context.asset,
amount=context.ORDER_SIZE,
limit_price=context.price * (1 + context.SLIPPAGE_ALLOWED),
)
self.log.info(
"Bought {amount} @ {price}".format(amount=context.ORDER_SIZE, price=context.price)
)
order_target_percent(
asset=context.asset,
target=0,
limit_price=context.price * (1 - context.SLIPPAGE_ALLOWED),
)
log.info(
"Sold {amount} @ {price} Profit: {profit}".format(
amount=position.amount, price=context.price, profit=profit
)
)
else:
log.info("no buy or sell opportunity found")
else:
# Buy when not holding and got buy signal
if isBuy(context, analysis):
order(
asset=context.asset,
amount=context.ORDER_SIZE,
limit_price=context.price * (1 + context.SLIPPAGE_ALLOWED),
)
log.info(
"Bought {amount} @ {price}".format(amount=context.ORDER_SIZE, price=context.price)
)