I am pretty new to this but was hoping to break into the algo trading world. I am looking for a couple of mean reversion algo's written in python. I am looking to start on the EUR/USD, and then branch out from there. I was hoping for a pretty simple algo so that i can use that as a blueprint and expand it once I get more experience. Any help would be appreactiated.
Bill McCabe
I am having trouble doing these things in python
#finding the current price and making that a live variable
#finding the mean and making that a live variable
#deciding on the difference needed to pull the triger
#finding out how to do a buy Order
#finding out how to store the current price as a non-live variable
#finding out how to take profit and stop loss
#finding out how to do a sell Order
Alexandre Catarino
We can find the price of a given security anytime in the Securities object:
price = self.Securities["EURUSD"].Price
However, if we are at the OnData event handler, we are receiving that data:
# OnData(self, slice) price = slice["EURUSD"].Value
The mean of a price series can be calculated using the SimpleMovingAverage indicator. The SMA helper method creates that indicator and updates it accordingly:
# Initialize # 30-day simple moving average with daily frequency self.sma = self.SMA("EURUSD", 30, Resolution.Daily) # OnData if price > self.sma.Current.Value: # Buy order, stop order and take profit order go here
Please checkout a working example below:
Bill McCabe
Thanks Alexandre! I'll take a look and try to make my own.
István Kürtösi
Hi Alexandre, thank you, I was followin your advice.
Is there a way to get the Bid or Ask price for a currencypair? I see this documentation for Options:
...but for FOREX I cannot find any reference in the documentation for "chain/chains". Any idea?
István Kürtösi
In the meanwhile I think I found the answer in other comments, so there are no Bid/Ask prices for FOREX:
Alexandre Catarino
When we access the data in the Slice object we receive in OnData(Slice), we can get the Bid and Ask like this:
// In OnData(Slice) // QuoteBar object has OHLC Bid and Ask var quoteBar = data[atmContract.Symbol]; bid = quoteBar.Bid.Close; ask = quoteBar.Ask.Close;
Alternatively, we can use the Security object in other methods:
var symbol = atmContract.Symbol; bid = Securities[symbol].BidPrice ask = Securities[symbol].AskPrice
István Kürtösi
Hi Alexandre,
thank you for your answer, so I was wrong above, sorry about it.
Also for others, probably it helps, here is an example of applying your code in Python:
quoteBar = slice['EURUSD'] bid = quoteBar.Bid.Close ask = quoteBar.Ask.Close
Bill McCabe
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!