Hi Guys,
I have just started using this system and I am still in the learning phase. This is a very beginner question. I was trying to code a simple algorithm for which I needed to compare the Closing price of the previous two candles. So for example, if I was doing something like this on mql4 I would have used Close[1] > Close[2] to get the close price. But, how would I get something like this on this system. Could you guys help me out or point me to the right direction. Any help would be greatly appreciated.
Alexandre Catarino
In QuantConnect/Lean, we cannot access past values automaticly like in MQL4.
We need to rely on the RollingWindow class that saves past values:
// Rolling window of closing values (decimal) private RollingWindow<decimal> Close;
We then access past values similarly to MQL4. In our case, zero index means current value, 1 previous one, etc:
// In OnData even handler: // Buy if current close is higher than previous // and no holdings in the portfolio if (!Portfolio.Invested && Close[0] > Close[1]) { SetHoldings(_spy, 1); Debug(Time + " -> Buy signal: " + Close[0] + " . " + Close[1]); }
Zobad Mahmood
Wow thanks Alexandre for a quick reply, the explanation and the example were clear and made sense. Another thing that I wanted to ask about RollingWindow that was confusing me:
1)You say that we that RollingWindow store past values, does it also include the high,low,open prices? If not, what do we send into the angular brackets for RollingWindow so we can retreive these values
2) I Want to store the bollinger band values of those past candles and be able to retrieve the values for those candles, I want to able to retrieve both upper, lower and middle bollinger band value. What should I send into the angular bracket so that I am able to retrieve those values.
3) Can you give me any advice on how do I go about learning
Thanks Again.
Alexandre Catarino
RollingWindow is a "smart" C# List, we can save any type we want. In the previous example, we were saving System.Decimal. However we can save a TradeBar that contains the OHLCV information and an indicator:
private RollingWindow<decimal> Close; private RollingWindow<TradeBar> Bar; private RollingWindow<BollingerBandState> BB;
Please checkout the attached project.
Note that we needed to create a helper/wrapper class BollingerBandState to hold the current state of a bollinger band instance.
The best place to learn is reading the docs, following QuantConnect University examples and, of course, searching answers for our questions in this community.
Durable
@Alexandre I am new here. How come Close[0] is the current and Close[1] is the previous? I thought it would make more sense if Close[0] is the previous one. (Is it designed to insert into the list the opposite way?)
Alexandre Catarino
By definition, a rolling window has a moving reference (the origin of the time axis) which is always "now". The convention is that the origin of the time axis is zero (t = 0). Therefore Close[t=0], is the Close at the reference.
If C# allowed negative index (like Python does), we would probably use Close[-1], Close[-2], etc... but it does not.
Also, it is more intuitive, in my humble opinion, to access a value with that design. Say we want the Close from 10 days ago: Close[10], independently of out window size. On the other hand, with your reasoning, we would have to know the window size and the Close from 10 days ago would come as Close[Close.Count - 10].
Zobad Mahmood
@Alexandre Catarino I have some more questions that I needed help with .
1)I have send a Market Order, eg. a buy order, how do I go about closing this order and how do I check the status of the order.
2) How do I check from within the code whether the order was buy or sell
3)Is there a way of sending a stop-loss order?
4) How do I find out currency related information such as lot size, minimum stoploss level etc(these are for forex related symbols)
Thanks
Alexandre Catarino
We can check the status of all orders using the Transactions property. For example, we can get an order that is recorded by its ID.
var order = Transactions.GetOrderById(_ticket.OrderId);
_ticket represents an OrderTicket that we define when we submit an order:
_ticket = MarketOrder("EURUSD", 1000);
The order above holds all the information we need to take action:
Please checkout the docs for orders type.
In order to place a stop loss, please use the StopMarketOrder.
The information about a given symbol can be find in its SymbolPropeties:
var properties = Securities["EURUSD"].SymbolProperties; properties.Description .ContractMultiplier .LotSize .MinimumPriceVariation .QuoteCurrency
At this moment, we do not have information about minimum stoploss level.
Zobad Mahmood
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!