The backtest indicates that this algorithm becomes incredibly overdrawn. First why? Second how? and third how can I fix this? thanks
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
context.aapl = sid(24)
context
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
# Implement your algorithm logic here.
averageapple = data[context.aapl].mavg(1)
amountofshares = round(context.portfolio.cash / data[context.aapl].price, 0)
numbersn = amountofshares
# data[sid(X)] holds the trade event data for that security.
# data.portfolio holds the current portfolio state.
# Place orders with the order(SID, amount) method.
# TODO: implement your own logic here.
if data[context.aapl] == averageapple:
order(context.aapl, +amountofshares)
if data[context.aapl] > averageapple:
order(context.aapl, - numbersn)