Back

How to get current ticker price from within OnData?

I'm new to QuantConnect. Very impressed so far.

I'm trying to backtest an external system I wrote that produces buy/short signals on stocks using QC's python environment. I am simply reading in a csv formatted blob of text into a dictionary and executing trades based on that. In order to determine the volume of the trade, however, I want to get the current price info so that I can make a better order with limits and calculate how many shares to buy.

In the attached code, `self.Identity(symbol).Current.Value` always returns 0.0. How can I get the price info? Preferrably something like hi/lo or bid/ask values.

Another question: How do I configure the trade fee?

Also, many of the trades come back as Invalid with no explanation. I assume that some of these are due to the fact that hedging is not supported, but not all the invalid trades are hedges. How can I get more details on the Invalid status?

To get this far I've had to explore the Lean source, but it's slow going as I learn the structure.

Thanks

Update Backtest








HI, at the beginning i looked the first stuff up in the documentation which is the tab right on the top.
There is in the first lines something like:
Securities["IBM"].Price

But the thing you need later is in chapter: handling data

if you don't understand what they mean with that small code lines thats no problem. Take a look at some examples in the university or clone some examples of others you will find in the community to take a look how others are solving stuff.
:)
0

ups sorry i copied the wrong code. i forgot to klick on the pyhton examples. my fault. sorry

 

 

0

Jordan, I am also new and I cloned your code because I need similar features from a CSV list of trades with dates. I have made some changes and will try to share it back to you and the community when I get it working right, but in the mean time, I saw this in the documentation which you asked about:

"You can set your brokerage account type in your initialization with SetBrokerageModel(BrokerageName broker, AccountType account);.

"The BrokerageName enum supports values of Default, TradierBrokerage, InteractiveBrokersBrokerage, FxcmBrokerage and OandaBrokerage. When setting the brokerage name we also set the trading fee structures for that brokerage."
0

P.S. Also in https://www.quantconnect.com/docs#Reality-Modelling
is this:

//Set IBM to have a constant $1 transaction fee. Securities["IBM"].FeeModel = new ConstantFeeTransactionModel(1);
0

And, to answer your main question, self.Securities["AAPL"].Price

There is some discussion in the docs about how to get OHLC bars from the OnData() slice, but I wasn't able to get that to work in Python.
0

We can get OHLC (and/or Bid/Ask if we are trading FX and CDF) from the OnData parameter:

def OnData(self, slice):
stock_price = slice[symbol].Price

Please checkout the attached project where we changed the original code.

About custom fees:
It is a feature that will be implemented in the new python framework.

1

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.


Update Backtest





0

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.


Loading...

This discussion is closed