@Quantdog
If you are still having issues read on, else congrats on solving your issue!
I'm a little confused by your strategy, could you explain it one more time? Some of the things you mentioned are a little confusing
price*1.05>price
That statement is always True, likewise
vavg*2>vavg
is also always True.
For your lower bound logic the price statement you have is always evaluated as True, and your volume statement is always false.
In addition you are calling the history() function and not using the data. And when you call this line of code
volume=data[context.stock].volume
You are actually getting the volume traded for the current trade even and so that is not the average volume traded over the past 20 days as I'm guessing from what you wrote. If you want to get the average volume over the past 20 days an easy way is to get the volume data out of the DataFrame return by the history() function.
volume = history(20,'1d','volume')
and then...
vavg = volume[context.stock].mean()
is one way the get the average value of what ever data is contained in the given series.
So before you tackle the issues of re-balancing and margin you should take care of that. I'm listening on this thread so if you post a response I'll try to reply as soon as possible.
Disclaimer
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.