Hi guys,
I was playing around and I found something strange.
I was trying to learn a bit about limit orders and created a simple algo that measures the hourly opening range (first 20 minutes for ex) and places 2 limit orders at top of opening range and bottom of opening range (thinking the price might revert to it, but that's not important).
Because I forgot to initialize the minimum and maximum prices each hour, it kinda does a rolling min/max of these, which wouldn't make a lot of sense in real life.
What is weird is that it generates a kinda implausible profit (and 98% win rate) while logging margin calls and insufficient margin (which I take as a sign that I've blown my account - which would make sense as per the mistake I mentioned above about initializing min/max hourly).
What do you guys make of this?
Thanks,
Dan
Rahul Chowdhury
Hi Dan,
We should check whether we have enough margin for a trade before placing it. The easiest way to prevent overusing margin is to only enter new positions when we aren't already invested. This can be done by checking
if not self.Portfolio.Invested: self.or_t = self.or_h - self.or_l #long limit order self.llo = self.LimitOrder('EURUSD',1000,round(self.or_l,5) - self.soft_entry,"limit order below OR") #short limit order self.slo = self.LimitOrder('EURUSD',-1000,round(self.or_h,5) + self.soft_entry,"limit order above OR")
However, if we want to place new trades while we have an open position, we can use
self.CalculateOrderQuantity(symbol, target)
to calculate our order size given the proportion of our margin we want to use.
For example, if we want to dedicate all of our margin with 100x leverage to one trade. We should use
self.CalculateOrderQuantity(symbol, 100)
Alternatively we can also check our margin remaining to calculate the order size for our trades.
# We can find the total margin remaining using
margin = self.Portfolio.MarginRemaining
#Our total buying power is
buying_power = margin * leverage
# Then we can calculate our ordersize
leverage = 100
price = data["EURUSD"].Close
ordersize = margin_allocated * leverage / price
Best
Rahul
Dan Aldea
Thanks Rahul!
Makes sense, should do that in a more thourough implementation.
What is still a bit strange to me is the way the wins/losses are calculated and the profit. In my case, because of the mistakes I've made, I should've had a close to 100% loss rate and -100% profit, esp with the margin calls.
Dan Aldea
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!