Example #1
0
def test_symbol():
    symbol = get_mt4_symbol('EUR/USD')
    assert symbol == 'EURUSD'

    symbol = get_mt4_symbol('EUR_USD')
    assert symbol == 'EURUSD'

    symbol = get_mt4_symbol('EURUSD')
    assert symbol == 'EURUSD'
Example #2
0
def calculate_price(base_price, side, pips, instrument):
    """
    calculate_price(1.3320, OrderSide.BUY, 20, 'EURUSD') = 1.3340
    """
    instrument = get_mt4_symbol(instrument)
    pip_unit = pip(instrument)
    base_price = Decimal(str(base_price))
    pips = Decimal(str(pips))

    if side == OrderSide.BUY:
        return base_price + pips * pip_unit
    elif side == OrderSide.SELL:
        return base_price - pips * pip_unit
Example #3
0
    def line_to_event(self, line):
        """
        GBP/USD,20181202 22:01:01.100,1.27211,1.27656
        to a TickPriceEvent
        """
        fields = line.split(',')

        event = TickPriceEvent(broker='Back test broker',
                               instrument=get_mt4_symbol(fields[0]),
                               time=str_to_datetime(fields[1],
                                                    '%Y%m%d %H:%M:%S.%f'),
                               bid=Decimal(fields[2]),
                               ask=Decimal(fields[3]))
        return event
Example #4
0
def pip(symbol, price=None, _abs=False):
    """
    pip('EURUSD') = 0.0001
    pip('EURUSD', -0.00352) = -35.2
    pip('EURUSD', -0.00352, True) = 35.2
    """
    symbol = get_mt4_symbol(symbol)
    if symbol not in PIP_DICT:
        raise Exception('%s not in PIP_DICT.' % symbol)

    pip_unit = PIP_DICT[symbol]
    if price is not None:
        price = Decimal(str(price))
        if _abs:
            price = abs(price)
        return (price / pip_unit).quantize(Decimal("0.1"))

    return pip_unit
Example #5
0
 def __init__(self,
              broker,
              account_id,
              trade_id,
              instrument,
              side,
              lots,
              open_time,
              open_price,
              stop_loss=None,
              take_profit=None,
              magic_number=None):
     self.broker = broker
     self.account_id = account_id
     self.trade_id = trade_id
     self.instrument = get_mt4_symbol(instrument)
     self.side = side
     self.lots = lots
     self.open_time = open_time
     self.open_price = Decimal(str(open_price))
     self.stop_loss = Decimal(str(stop_loss)) if stop_loss else None
     self.take_profit = Decimal(str(take_profit)) if take_profit else None
     self.magic_number = magic_number
     super(TradeOpenEvent, self).__init__()
Example #6
0
 def __init__(self,
              broker,
              account_id,
              trade_id,
              instrument,
              side,
              lots,
              profit,
              close_time,
              close_price,
              pips=None,
              open_time=None):
     self.broker = broker
     self.account_id = account_id
     self.trade_id = trade_id
     self.instrument = get_mt4_symbol(instrument)
     self.side = side
     self.lots = lots
     self.profit = Decimal(str(profit))
     self.close_price = Decimal(str(close_price))
     self.close_time = close_time
     self.open_time = open_time
     self.pips = Decimal(str(pips)) if pips else None
     super(TradeCloseEvent, self).__init__()