I can answer that, it is sort of an illusion, works like this: At 100M there are many more partial fills. When buys go thru and sells do not (everybody's trying to sell), the algorithm dips into margin. Returns are calculated based on starting capital so margin is not considered. As a result, margin looks like profit
See this 4 line backtest illustrating the point. Also on that page various solutions to protect yourself from the problem. Or just run this:
def initialize(context):
schedule_function(trade, date_rules.every_day(), time_rules.market_open())
def trade(context, data):
order(sid(8554), 3)
#record(cash = context.portfolio.cash) # uncomment to reveal the margin
I haven't looked at your code, I'm saying this because it has always been the pattern. Besides the links on that page above, https://www.quantopian.com/posts/margin is easy to use for charting overnight margin for visibility into what margin in your algorithm is up to.
If that tool reveals margin jumping at a particular point and you would like to find out why and see about handling something differently, use https://www.quantopian.com/posts/track-orders and set one or more start dates (and/or stop dates) in its options section, copy the logging output, make your change, run again, and diff the two, I like CompareIt for that.