I tried two constructions:
first with exits and enters:
//exit: short
if (Portfolio[symbol].IsShort) {
if (price > (bb.UpperBand * (1+tolerance)) && rsi > 70) {
Liquidate(symbol);
}
}
//exit: long
if (Portfolio[symbol].IsLong) {
if (price < (bb.LowerBand * (1+tolerance)) && rsi < 30) {
Liquidate(symbol);
}
}
//long
if (!Portfolio.HoldStock) {
if (price < (bb.LowerBand * (1+tolerance)) && rsi < 30) {
LimitOrder(symbol, Math.Abs(holdings) + quantity, high);
}
}
//short
if (!Portfolio.HoldStock) {
if (price > (bb.UpperBand * (1+tolerance)) && rsi > 70) {
LimitOrder(symbol, -(Math.Abs(holdings) + quantity), low);
}
}
and second:
if (holdings > 0 || holdings == 0) {
if (price > (bb.UpperBand * (1+tolerance)) && rsi > 70 ) {
LimitOrder(symbol, -(holdings + quantity), low);
}
} else if (holdings < 0 || holdings == 0) {
if (price < (bb.LowerBand * (1+tolerance)) && rsi < 30) {
LimitOrder(symbol, Math.Abs(holdings) + quantity, high);
}
}
So, why do I get different results? Where is the problem with my logic? Is there any template for enters and exits? Thank you for attention.
JayJayD
Take for example the case in which you reach the OnDAta method with a short position and the condition
price > (bb.UpperBand * (1+tolerance)) && rsi > 70
is True.
In the first case, the algorithm will liquidate all positions (first if line 62) and in the fourth if (line 89) the algorithm will send a Limit order to enter to the market.
In the second case, you only will send a Limit order to close the open short position.
As far I know, there isn’t a template for entries and exits. But there you can check in this example the CrossingMovingAveragesSignals class, maybe you can use the structure to implement your ideas.
Akira Maki
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!