My code seems to buy when there is a signal, however, when the signal is under 0 my program does not sell. Essentially my algorithm buys and buys and buys putting my paper trading bank account into debt... Please Help here is my code:
import math
# model parameters
price_scale = 150
k = 2*math.pi/price_scale
phase = 2*math.pi*0
amplitude = 1
def initialize(context):
context.stock = sid(30160)
context.max_notional = 1000000.1
context.min_notional = 0
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
close_p = data[context.stock].close_price
low_p = data[context.stock].low
signal = amplitude*math.cos(k*(close_p+low_p)+phase)
# plot a time series of the signal
record(signal=signal)
notional = context.portfolio.positions[context.stock].amount * close_p
if signal >= 0 and notional < context.max_notional:
# buy
order(context.stock, 100)
elif notional > context.min_notional:
# sell
order(context.stock, -100)