trade: trade

View source: R/trade.R

tradeR Documentation

trade

Description

Model deployment and reporting

Usage

trade(
  action,
  leverageFactors = c(1, 1, 1, 1.5),
  maxLeverage = 2.5,
  wealthScale = 1,
  fullPath = "~/td"
)

Arguments

action

character; 'cron', 'force', or 'report' (see Examples)

leverageFactors

numeric; leverages for BAV, GANN, IKM, and KDA, respectively

maxLeverage

numeric; the maximum number of units allowed (see Examples)

wealthScale

numeric; multiplier which adjusts the amount of capital allocated (see Examples)

fullPath

character; the full path to the directory containing sensitive TD credentials (see Note below)

Details

The only user-accessible function of this package, it optimizes a portfolio of various convergence/divergence models across multiple asset classes and timeframes, generating the appropriate number of shares to buy. That information is then relayed to TD Ameritrade via the rameritrade package so that the code can be deployed.

This package serves as a non-profitable example of a portfolio of quantitative trading models. It should not be used by anyone.

Please read the Notes section in its entirety.

Value

Depending on the action argument, returns nothing or the number of shares (see Examples; also creates data files and trade reports).

Note

In the directory indicated by the argument fullPath, you should place 2 files:

  • consumerKey.rds

  • refreshToken.rds

See the rameritrade documentation under References for further directions on creating these files.

If you change your password with TD Ameritrade, you will need to generate a new Refresh Token.

You should make 2 cronjobs when executing this function if action is missing or set to cron:

  • One from around 12h57-8 EST (in the event the market closes at 13h EST)

  • One from around 15h57-8 EST (for normal market close at 16h EST)

The cronjob time is dependent upon:

  • The connection speed and processing power of the VPS

  • The date (on the last/first trading day of the month, for instance, parameters will be optimized)

  • If this is your first time running the function (parameters will be optimized)

See the References section for a link to Crontab Guru.

Note that this function should take about 1 minute to execute.

If the system time of the VPS is different than America/New_York, an error will be thrown; edit the trade function to remove/change this feature.

If you have more than one account number, only the first account specified by rameritrade::td_accountData() will be used. Manually edit the make_orders function if you would like to change this.

This function will generate a directory named data in your working directory. You can ignore this directory, but do not delete it because doing so will result in this function taking longer to execute. The directory contains no sensitive data.

Your timezone will be changed to 'America/New_York'. If you are not using a GNU/Linux system, an error may be generated by this feature and you will need to manually edit the first 'if' statement of this function.

References

https://github.com/exploringfinance/rameritrade/
https://developer.tdameritrade.com/
https://developer.tdameritrade.com/content/place-order-samples
https://www.wintick.com/members/symbolGuide/tda
https://crontab.guru/
https://quantstrattrader.com/2019/02/27/kda-robustness-results/
https://www.nyse.com/markets/hours-calendars

Examples

## Not run: 
# ACTION
## Normal usage when deployed automatically on a VPS
## Intended to be triggered by a cronjob just before market close
## Regardless of choice for the action argument, data files are created
## in ./data which are used for internal processing; also creates
## ~/fullPath/tradeReport.rds if action == 'cron', 'force', or is missing
trade()
trade('cron')

## Manually forces a trade regardless of time and creates
## ~/fullPath/tradeReport.rds
trade('force')

## Does not trade at all, but rather just reports the shares that will be
## traded
trade('report')

# ACCESSING THE TRADE REPORT
readRDS('~/fullPath/tradeReport.rds')

# LEVERAGE FACTORS
## Adjust the leverage used for each strategy to your risk:return framework
## The ordering is: BAV, GANN, IKM, KDA
## It's okay if the sum of leverageFactors exceeds the leverage provided by
## your broker because the number of units allocated is limited by the
## maxLeverage argument
trade(leverageFactors = c(0.5, 0.5, 0, 1.5))

# MAX LEVERAGE
## Adjust how many units you want to buy, leaving some headroom for drawdowns
## and HTB securities
## In the below function call, despite 1 unit being allocated to each model,
## only 2 units will be purchased at a time because models are inactivated
## based on their order; if BAV and GANN are both active, for example, they
## take priority, so no capital will be allocated to GANN, IKM, or KDA even
## if thoses models generate a long signal at the same time
trade(leverageFactors = c(1, 1, 1, 1), maxLeverage = 2)

# WEALTH SCALE
## Example: only allocate half of your net liquidating value (eg, NLV, wealth)
trade(wealthScale = 0.5)

## Example: If you have a $100k account, the maximum value of this portfolio
## will be $60k (you allocate 1/5 of your NLV to the portfolio which is $20k,
## and you use margin to buy up to 3 units)
trade(maxLeverage = 3, wealthScale = 0.2)

# FULL PATH
## NB: this directory contains two sensitive files for the TD API, as well as
## trade reports
## Keep this data in a safe place and make GPG-encrypted backups
trade(fullPath = '~/my/secret/path')
## or...
trade(fullPath = '~/my/secret/path/')

## End(Not run)

causality-loop/mnmt documentation built on June 17, 2022, 5:14 a.m.