Hi everyone, I'm looking for a simple way to code adding to a open position. I have an entry defined with a few criteria, I want to only have a max of 4 entries every time my criteria occurs. I'd like to adjust each entry amount like the SetHoldings, 0.2 for one, 0.4 for two, 0.6 for three, 1 for fourth.
I saw the post about Martingale, but that seems more code to implement than I want to deal with since I'm new at Python.
If anyone already has a simple example that I can work with that would be great.
Rahul Chowdhury
Hey Mark,
We can use a dictionary to hold the sequence of position sizings with which you wish to enter the trade. You can then use a counter along that sequence to size your positions. I implemented a SMA crossover entry criteria, along with the type of positioning you're looking for. It also implements selling shares in the same portions: 0.6, 0.4, 0.2, 0.
class TransdimensionalHorizontalShield(QCAlgorithm): def Initialize(self): self.SetStartDate(2019, 9, 1) # Set Start Date self.SetCash(100000) # Set Strategy Cash self.AddEquity("SPY", Resolution.Daily) # dictionary to hold our target position sizes self.targets = {0: 0, 1 : 0.2, 2 : 0.4, 3 : 0.6, 4 : 1} # counter which tracks where in the sequence of sizes we are self.counter = 0 # defining our indicator and setting warm up self.sma = self.SMA("SPY", 14, Resolution.Daily) self.SetWarmUp(14) def OnData(self, data): if self.IsWarmingUp: return price = data["SPY"].Close SMA = self.sma.Current.Value # For a SMA long crossover, we buy, otherwise we sell if price >= SMA: # if the next counter is not in our dictionary we return if self.counter + 1 not in self.targets: return self.counter += 1 self.SetHoldings("SPY", self.targets[self.counter]) else: # if the next counter is not in our dictionary we return if self.counter - 1 not in self.targets: return self.counter -= 1 self.SetHoldings("SPY", self.targets[self.counter])
Â
Mark hatlan
Wow thanks for that. A few basic questions since I am still new to python.
Row 27, why the +1?
Row 30 why have the +=1?Â
I'm trying to wrap my head around getting the self.counter to reference the set.Holdings.
Alexandre Catarino
Hi mark hatlan ,
At line 27, we check whether adding one to the counter would still result in a key of the self.targets dictionary. If so, we update it (line 30) in order to be used in the following statement.
For example, say we have already set the holdings to 100%, in this case, self.counter is currently 5. So, if the condition is met again, we would have to add 1 to self.counter. Since self.targets dictionary doesn't have the 6 as a key, it is an invalid key and we can skip the rest of the logic.
I hope I made it clearer. :-)
Mark hatlan
Yes thanks Alexandre, that is exactly what I needed.
Mark hatlan
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!