Back

Need help finding a simple mean reversion algo

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.  

Update Backtest








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

0

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:

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.


Thanks Alexandre! I'll take a look and try to make my own.

0

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: 

https://www.quantconnect.com/terminal/#open/681495

...but for FOREX I cannot find any reference in the documentation for "chain/chains". Any idea? 

0

In the meanwhile I think I found the answer in other comments, so there are no Bid/Ask prices for FOREX:

https://www.quantconnect.com/forum/discussion/952/resolutiontick--askbid-data
0

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
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.


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
0

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