Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- =================
- = DUAL MOMENTUM =
- =================
- Recommended Assets:
- - US: IVV or VOO or VTI
- - WORLD: VEU or VXUS or VEA
- - BONDS: AGG or BND or TLT
- - TBILLS: BIL or SHV (if IR near 0)
- */
- #include <profile.c>
- //Settings:
- #define MONTHS 12
- //#define DAYS 252 // comment out to use daily closing prices
- string asset_us = "IVV";
- string asset_world = "VEU";
- string asset_bonds = "AGG";
- string asset_tbills = "BIL";
- string current_position = "";
- var return_us = 0;
- var return_world = 0;
- var return_tbills = 0;
- var prices_us[MONTHS];
- var prices_world[MONTHS];
- var prices_tbills[MONTHS];
- void changeAsset(string old_asset, string new_asset){
- // If there is no change skip the rest
- if(!strcmp(old_asset, new_asset)){
- print(TO_FILE, "No change in held asset.");
- return;
- }
- // Exit previous position
- string prev_position = old_asset;
- if(!strcmp(prev_position, "")) prev_position = "NONE"; // just for the log
- print(TO_FILE, "Changing position from %s to %s.\n", prev_position, new_asset);
- if(strcmp(old_asset, "")){
- asset(old_asset);
- exitLong();
- }
- // Enter new position
- asset(new_asset);
- enterLong();
- current_position = new_asset;
- }
- void addPrice(var asset_price, vars price_array){
- print(TO_FILE, "\nMonthly closing price for %s is %f", Asset, asset_price);
- int i;
- for(i=0; i<MONTHS-1; i++){
- price_array[i] = price_array[i+1];
- }
- price_array[MONTHS-1] = asset_price;
- }
- function run()
- {
- BarPeriod = 1440;
- #ifdef DAYS
- LookBack = DAYS + 21;
- #else
- LookBack = MONTHS * 21;
- #endif
- StartDate = 20080615;
- Verbose = 0;
- if(is(INITRUN)) {
- set(PRELOAD+LOGFILE);
- assetHistory(asset_us, FROM_YAHOO);
- assetHistory(asset_world, FROM_YAHOO);
- assetHistory(asset_bonds, FROM_YAHOO);
- assetHistory(asset_tbills, FROM_YAHOO);
- // Loop through the assets to avoid Error 030
- while(asset(loop("EUR/USD", asset_us, asset_world, asset_bonds, asset_tbills)));
- }
- if(tdm() == tom()){
- while(asset(loop("EUR/USD", asset_us, asset_world, asset_tbills)))
- {
- var *prices;
- var *asset_return;
- if(!strcmp(Asset, asset_us)){
- prices = prices_us;
- asset_return = &return_us;
- }
- else if(!strcmp(Asset, asset_world)){
- prices = prices_world;
- asset_return = &return_world;
- }
- else if(!strcmp(Asset, asset_tbills)){
- prices = prices_tbills;
- asset_return = &return_tbills;
- }
- if(strcmp(Asset, "EUR/USD")){
- var old_price = prices[0];
- #ifdef DAYS
- old_price = priceClose(DAYS);
- #endif
- addPrice(priceClose(), prices);
- if(old_price > 0){
- *asset_return = (priceClose()-old_price)/old_price;
- print(TO_FILE, "\n%s return = (%f-%f)/%f = %f", Asset,
- priceClose(), old_price, old_price, *asset_return);
- }
- }
- }
- }
- if(tdm() == 1 && !is(LOOKBACK) && (abs(return_us) + abs(return_world) + abs(return_tbills) != 0)) {
- print(TO_FILE, "\nReturns (US, WORLD, TBILLS): %f, %f, %f\n",
- return_us, return_world, return_tbills);
- if(return_us - return_tbills >= 0){
- if(return_us > return_world)
- changeAsset(current_position, asset_us);
- else if(return_world - return_tbills >= 0)
- changeAsset(current_position, asset_world);
- else
- changeAsset(current_position, asset_bonds);
- }
- else {
- changeAsset(current_position, asset_bonds);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement