Пример #1
0
def get_price_change(row, pip_factor):
    """ If closeBid - openAsk > 0. Can buy. Return value positive.
        If closeAsk - openBid < 0. Can sell. Return value negative.
        Otherwise do nothing. Return 0.

        Args:
            row: list of Strings. A row from data file in /data/store/
            pip_factor: int. The multiplier for calculating pip from price.

        Returns:
            price_change: string. Profitable price change for the day in pips,
                formatted to 1 decimal place.
    """
    row = common.list_to_float(row[1:-1])

    if row[3] - row[4] > 0:
        diff = row[3] - row[4]
    elif row[7] - row[0] < 0:
        diff = row[7] - row[0]
    else:
        diff = 0

    price_change = common.price_to_pip(diff, pip_factor)

    return price_change
Пример #2
0
def get_profit_loss(row, units, **controls):
    """ Calculate profit/loss for a single day from the daily candle.

        Args:
            row: list of strings. Should contain: time, openBid, highBid,
                lowBid, closeBid, openAsk, highAsk, lowAsk, closeAsk, volume.
            units: signed int. Number of units for trade.
                Positive for buy and negative for sell.
            controls: named arguments, including:
                take_profit, stop_loss in price and trailing_stop in pips.

        Returns:
            profit_loss: float. Profit or loss from buy at open and sell at
                close, or sell at open and buy at close, or order triggers.
    """
    # Remove 'time' and 'volume' from the row.
    row = common.list_to_float(row[1:-1])

    # TODO: Take profit.
    # Determine if there's a stop loss price.
    if 'stop_loss' in controls:
        stop_loss_price = controls['stop_loss']
    elif 'trailing_stop' in controls:
        stop_loss_price = controls['trailing_stop']
    else:
        stop_loss_price = -1

    # Calculate profit/loss according to units and price.
    if units == 0:
        # No action.
        profit_loss = 0

    elif units > 0:
        # BUY.
        # If stop loss set and lowBid droped below it, it triggers.
        if stop_loss_price > 0 and row[2] < stop_loss_price:
            sold_price = stop_loss_price
        else:
            sold_price = row[3]
        # Now calculate profit/loss.
        profit_loss = units - units * row[4] / sold_price

    else:
        # SELL.
        # If stop loss set and highAsk rose above it, it triggers.
        if stop_loss_price > 0 and row[5] > stop_loss_price:
            bought_price = stop_loss_price
        else:
            bought_price = row[7]
        # Now calculate profit/loss.
        profit_loss = units - units * row[0] / bought_price

    return profit_loss
Пример #3
0
def list_to_features(row, pip_factor):
    """ Return the row without the date, openBid and volume.
        Then take away the openBid price.

        Args:
            row: list of Strings. A row from data file in ./store/
            pip_factor: int. The multiplier for calculating pip from price.

        Returns:
            features: list of floats. The quantities are: highBid, lowBid,
                closeBid, openAsk, highAsk, lowAsk and closeAsk.
                All relative to openBid, and in pips.
    """
    row = common.list_to_float(row[1:-1])
    row = [x - row[0] for x in row[1:]]
    features = [common.price_to_pip(x, pip_factor) for x in row]

    return features