Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
log.info("Position Zero")
return
# Cost Basis
cost_basis = position.cost_basis
log.info(
"Holdings: {amount} @ {cost_basis}".format(
amount=position.amount, cost_basis=cost_basis
)
)
# Sell when holding and got sell singnal
if isSell(context, analysis):
profit = (context.price * position.amount) - (cost_basis * position.amount)
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):
if context.portfolio.cash < context.price * context.ORDER_SIZE:
log.warn(
log.info(
"found {amount} positions with cost basis {cost_basis}".format(
amount=position.amount, cost_basis=cost_basis
)
)
if position.amount >= context.TARGET_POSITIONS:
log.info("reached positions target: {}".format(position.amount))
return
if price < cost_basis:
is_buy = True
elif (position.amount > 0 and price > cost_basis * (1 + context.PROFIT_TARGET)):
profit = (price * position.amount) - (cost_basis * position.amount)
log.info("closing position, taking profit: {}".format(profit))
order_target_percent(
asset=context.asset, target=0, limit_price=price * (1 - context.SLIPPAGE_ALLOWED)
)
else:
log.info("no buy or sell opportunity found")
else:
is_buy = True
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("Position Zero")
return
# Cost Basis
cost_basis = position.cost_basis
log.info(
"Holdings: {amount} @ {cost_basis}".format(
amount=position.amount, cost_basis=cost_basis
)
)
# Sell when holding and got sell singnal
if isSell(context, analysis):
profit = (context.price * position.amount) - (cost_basis * position.amount)
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):
if context.portfolio.cash < context.price * context.ORDER_SIZE:
log.warn(
self.log.info("Using default sell function")
position = context.portfolio.positions.get(self.state.asset)
if position == 0:
self.log.warn("Position Zero, skipping sell")
return
# Cost Basis
cost_basis = position.cost_basis
self.log.notice(
"Holdings: {amount} @ {cost_basis}".format(
amount=position.amount, cost_basis=cost_basis
)
)
# Sell when holding and got sell singnal
profit = (self.state.price * position.amount) - (cost_basis * position.amount)
order_target_percent(
asset=self.state.asset,
target=0,
limit_price=self.state.price * (1 - self.state.SLIPPAGE_ALLOWED),
)
msg = "Sold {amount} @ {price} Profit: {profit}".format(
amount=position.amount, price=self.state.price, profit=profit
)
self.log.notice(msg)
self.notify(msg)
longs = all_assets[pipeline_data.longs]
shorts = all_assets[pipeline_data.shorts]
record(
universe_size=len(all_assets),
leverage=context.account.leverage,
)
# Build a 2x-leveraged, equal-weight, long-short portfolio.
one_third = 1.0 / 3.0
for asset in longs:
order_target_percent(asset, one_third)
for asset in shorts:
order_target_percent(asset, -one_third)
# Remove any assets that should no longer be in our portfolio.
portfolio_assets = longs | shorts
positions = context.portfolio.positions
for asset in viewkeys(positions) - set(portfolio_assets):
# This will fail if the asset was removed from our portfolio because it
# was delisted.
if data.can_trade(asset):
order_target_percent(asset, 0)
log.info("Position Zero")
return
# Cost Basis
cost_basis = position.cost_basis
log.info(
"Holdings: {amount} @ {cost_basis}".format(
amount=position.amount, cost_basis=cost_basis
)
)
# Sell when holding and got sell singnal
if isSell(context, analysis):
profit = (context.price * position.amount) - (cost_basis * position.amount)
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):
if context.portfolio.cash < context.price * context.ORDER_SIZE:
log.warn(
)
# Set a style for limit orders,
limit_price = price * 1.005
order_target_percent(
context.market, 1, limit_price=limit_price
)
context.traded_today = True
elif rsi[-1] >= context.RSI_OVERBOUGHT and pos_amount > 0:
log.info(
'{}: selling - price: {}, rsi: {}'.format(
data.current_dt, price, rsi[-1]
)
)
limit_price = price * 0.995
order_target_percent(
context.market, 0, limit_price=limit_price
)
context.traded_today = True
# Cost Basis
cost_basis = position.cost_basis
log.info(
'Holdings: {amount} @ {cost_basis}'.format(
amount=position.amount,
cost_basis=cost_basis
)
)
# Sell when holding and got sell singnal
if isSell(context, analysis):
profit = (context.price * position.amount) - (
cost_basis * position.amount)
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):
# 'shorts'.
pipeline_data = context.pipeline_data
all_assets = pipeline_data.index
longs = all_assets[pipeline_data.longs]
shorts = all_assets[pipeline_data.shorts]
record(
universe_size=len(all_assets),
leverage=context.account.leverage,
)
# Build a 2x-leveraged, equal-weight, long-short portfolio.
one_third = 1.0 / 3.0
for asset in longs:
order_target_percent(asset, one_third)
for asset in shorts:
order_target_percent(asset, -one_third)
# Remove any assets that should no longer be in our portfolio.
portfolio_assets = longs | shorts
positions = context.portfolio.positions
for asset in viewkeys(positions) - set(portfolio_assets):
# This will fail if the asset was removed from our portfolio because it
# was delisted.
if data.can_trade(asset):
order_target_percent(asset, 0)
# Check that the order has not already been placed
open_orders = get_open_orders()
if context.asset not in open_orders:
# check that the asset of interest can currently be traded
if data.can_trade(context.asset):
# Trading logic: if price is less than the buy threshold, mean
# reversion should drive price up. Algorithm invests 100% in the
# asset. In the opposite case, mean reversion should drive price
# down. Algorithm invests 50% in cash and 50% in the asset. If
# price is between buy and sell thresholds, algorithm invests 25%
# in cash and 75% in the asset.
if price < buy_threshold:
order_target_percent(context.asset, 1.0)
elif price > sell_threshold:
order_target_percent(context.asset, 0.5)
else:
order_target_percent(context.asset, 0.75)
record(
price=price,
leverage=context.account.leverage,
sma=sma,
std=std,
buy_threshold=buy_threshold,
sell_threshold=sell_threshold,
)