AminHP / gym-mtsim

A general-purpose, flexible, and easy-to-use simulator alongside an OpenAI Gym trading environment for MetaTrader 5 trading platform (Approved by OpenAI Gym)
MIT License
412 stars 101 forks source link

Question: about _get_unit_ratio #5

Closed sadimoodi closed 2 years ago

sadimoodi commented 2 years ago

Hello, Thank you so much for the simple and wonderful library. can you please explain what is this fucntion doing? why do we need to get the unit ratio to calculate order profit?

    def _get_unit_ratio(self, symbol: str, time: datetime) -> float:
        symbol_info = self.symbols_info[symbol]
        if self.unit == symbol_info.currency_profit:
            return 1.

        if self.unit == symbol_info.currency_margin:
            return 1 / self.price_at(symbol, time)['Close']

        currency = symbol_info.currency_profit
        unit_symbol_info = self._get_unit_symbol_info(currency)
        if unit_symbol_info is None:
            raise SymbolNotFound(f"unit symbol for '{currency}' not found")

        unit_price = self.price_at(unit_symbol_info.name, time)['Close']
        if unit_symbol_info.currency_margin == self.unit:
            unit_price = 1. / unit_price

        return unit_price
AminHP commented 2 years ago

I think these links explain it very well with some examples. Maybe I should put them in the Reference section of the README file.

https://www.investopedia.com/articles/forex/12/calculating-profits-and-losses-of-forex-trades.asp https://alpari.com/en/faq/trading_terms/calculating_profits_and_losses/

sadimoodi commented 2 years ago

@AminHP there is no mention to the ratio concept in either of the references you provided above, however, i am able to comprehend the concept: if the profit currency is = to the self currency then ratio is 1 (no effect on the profit calculation) if the currency is different then we get the ratio of that currency then mutiply by the profit.

AminHP commented 2 years ago

Yeah, that's right. The ratio itself is something I came up with in order to summarize the concept. But the concept was explained in the references and you perceived it.