Back

Best Way to work with Decimals?

Hello everyone,

I am trying to work with Forex data. I would like to do operations on prices. I am still new at Python, and learned that floating numbers give strange results. I am trying to use Decimal to avoid this, but am still getting strange results. At the top of my code I have:

from decimal import Decimal

class myAlgo(QCAlgorithm):
...

And within OnData I have: 

def OnData(self, data):

self.bid = Decimal(data["EURUSD"].Bid.Close)
self.ask = Decimal(data["EURUSD"].Ask.Close)

self.resultOne = Decimal(self.ask - self.bid)
self.resultTwo = Decimal(self.bid + 0.001)

But these are my results:

bid: 1.0868
ask: 1.08694

resultOne: 0.000140000000000029
resultTwo: 1.0877999999999999

As if I'm still using floats. I am trying to get results of 0.00014 and 1.0878 respectively. 

Any help is appreciated.

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.


I'm also going to be doing a lot of comparison operations (> ; >= ; ==) so if working with decimals/ floats is too imprecise/dangerous for this please let me know. Thank you!

0

Hi LE,

In Python, the difference between Decimal, double and float is the level of precision each offers. Decimal is the most precise, with up to 28 significant digits, while float is the least precise, offering up to 7 significant digits. Since most Forex bid/ask prices are precise to 4 decimal places, we can round our Decimals to 4 decimals places using round(number, digits). This will make sure all our operations return numbers with consistent precision because all our prices are equally precise.

Best
Rahul

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.


Thank you Rahul! 

Thanks to this information I have simplified my code and forgone the use of Decimals altogether. 

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