Mariner Backtesting - MoveBy

MoveBy() makes it easy to track how much a variable has moved during the life of the script.

MoveBy()

Creates the instance. The Value can be a specific number or variable. The MoveByvalue can be a percentage or a set number (i.e. if the trade moves in your favor by a point, etc.).

 MoveBy( InitialValue, IsIncrease, MoveByValue, IsPercent )

Parameters:

Name Type Default Information
InitialValue float required the initial value (starting point for tracking)
IsIncrease boolean required used to determine whether looking for increase or decrease in value.
MoveByValue float required the desired movement amount
IsPercent float required  

Attributes:

Name Type Default Information
Distance float parameter MoveByValue difference between the Target Value and the current value.
PercentDistance float 1.0 the percentage that price has moved from the initial price towards the Target Value.
TargetValue float calculated designated amount for price to move (set value or calculated from the specified percentage). ```python if IsIncrease: self.TargetValue = InitialValue + MoveByValue else: self.TargetValue = InitialValue - MoveByValue ```

Calculate()

Call to update the instance variables. Call method for checking instance variables.

 Calculate( CurrentValue )

Parameters:

Name Type Default Information
CurrentValue float required current value of number tracking
Working Example:
 from cloudquant.interfaces import Strategy
import ktgfunc


class MoveByExample(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == "AAL"

    def on_start(self, md, order, service, account):

        #print symbol and timestamp
        print(self.symbol + "\n"+ service.time_to_string(service.system_time) + "\tin on start\n")

        # create an object of the MoveBy class
        # set Initial Value to the previous close
        # IsIncrease is True when looking for the price to rise
        # MoveByValue is how much you want the price to move by.
        # Setting it to .2 means the target price is 20 cents above the prev_close
        # Set IsPercent to false when MoveByValue is not a percent
        self.entry_condition= ktgfunc.MoveBy(InitialValue=md.stat.prev_close,IsIncrease=True, MoveByValue=.2, IsPercent=False)

        # initialize exit condition
        self.exit_condition = None

        # use self.entry_condition.TargetValue to access the attribute for a print statement
        print(self.symbol + " closed at " + str(md.stat.prev_close) + " yeserday.\nIf the price rises $0.20 to " + str(self.entry_condition.TargetValue) + ", a position will be entered")

        # create variable used to determine if in position.
        self.in_position=False

    def on_minute_bar(self, event, md, order, service, account, bar):

        # on every bar the MoveBy attributes will be updated
        self.entry_condition.Calculate(md.L1.last)

        # update calculation if exit_condition has been created
        if self.exit_condition is not None:
            self.exit_condition.Calculate(md.L1.last)

        # buy if the stock has risen more than 20 cents, surpassing the target price
        # self.entry_condition.Distance() will return a negative value when it passes the target price
        if self.in_position is not True and self.entry_condition.Distance < 0:
            print '\n', service.time_to_string(event.timestamp)[11:19] + '\tin on_minute_bar'
            print 'Target price:', self.entry_condition.TargetValue, '\tLast Price:', md.L1.last
            print'[BUY]\t' + self.symbol +"'s price has increased more than $0.20 from the previous close. A buy order has been sent."

            #buy order
            order.algo_buy(self.symbol, "market", intent="init", order_quantity=100)

            # adjust variable to reflect current position
            self.in_position = True

            # return so that the below code is note executed until the next on_minute_bar
            return

        # if in a position, but exit_condition hasn't been set yet:
        if self.in_position and self.exit_condition is None:

            # create an object of the MoveBy class to exit a position
            # Setting it to .2 means the target price is 20 cents above the prev_close
            self.exit_condition = ktgfunc.MoveBy(InitialValue=account[self.symbol].position.entry_price, IsIncrease=True, MoveByValue=.3, IsPercent=False)
            print '\n\n', service.time_to_string(event.timestamp)[11:19] + '\tin on_minute_bar'
            print("Your position in " + self.symbol + " was entered at the price: " + str(
                account[self.symbol].position.entry_price) + ". \nIf the price rises $0.30 to " + str(
                self.exit_condition.TargetValue) + " or the price drops $0.50, the shares will be sold")

        # check that exit_condition has been created
        # sell if the stock has reached or passed the target price
        # which would mean the distance would be negative
        if self.exit_condition is not None and self.exit_condition.Distance <= 0:

            # sell order
            order.algo_sell(self.symbol, "market", intent="exit")

            print '\n\n', service.time_to_string(event.timestamp)[11:19] + '\tin on_minute_bar'
            print 'Target price:', self.exit_condition.TargetValue, '\tLast Price:', md.L1.last
            print "\n\n[SELL]\t" + self.symbol + " moved up at least 30 cents. A sell order has been sent"

            service.terminate()

        # check that exit_condition has been created
        # sell if the stock drops at least 20 cents from the purchase price.
        # meaning the distance from the target value would be above $0.50 from from +0.20
        elif self.exit_condition is not None and self.exit_condition.Distance > .5:

            # sell order
            order.algo_sell(self.symbol, "market", intent="exit")
            print '\n\n', service.time_to_string(event.timestamp)[11:19] + '\tin on_minute_bar'
            print 'Target price:', self.exit_condition.TargetValue, '\tLast Price:', md.L1.last
            print'[SELL]\t', self.symbol + "'s price decreased $0.50 cents from the target price. A sell order has been sent"

            service.terminate()

Console

AAL
2017-02-09 09:30:00.000000  in on start

AAL closed at 45.0600013733 yeserday.
If the price rises $0.20 to 45.2600013733, a position will be entered

09:59:05    in on_minute_bar
Target price: 45.2600013733     Last Price: 45.2700004578
[BUY]   AAL's price has increased more than $0.20 from the previous close. A buy order has been sent.
45.1399993896


10:00:02    in on_minute_bar
Your position in AAL was entered at the price: 45.2700004578.
If the price rises $0.30 to 45.4399993896 or the price drops $0.50, the shares will be sold


10:11:00    in on_minute_bar
Target price: 45.4399993896     Last Price: 45.5600013733


[SELL]  AAL moved up at least 30 cents. A sell order has been sent