SHARE
TWEET

Dual Momentum in Zorro

a guest May 15th, 2017 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. =================
  3. = DUAL MOMENTUM =
  4. =================
  5.  
  6.    Recommended Assets:
  7.        - US: IVV or VOO or VTI  
  8.        - WORLD: VEU or VXUS or VEA  
  9.        - BONDS: AGG or BND or TLT  
  10.        - TBILLS: BIL or SHV (if IR near 0)
  11. */
  12.    
  13. #include <profile.c>
  14.  
  15. //Settings:
  16. #define MONTHS 12
  17. //#define DAYS 252 // comment out to use daily closing prices
  18. string asset_us = "IVV";
  19. string asset_world = "VEU";
  20. string asset_bonds = "AGG";
  21. string asset_tbills = "BIL";
  22. string current_position = "";
  23. var return_us = 0;
  24. var return_world = 0;
  25. var return_tbills = 0;
  26. var prices_us[MONTHS];
  27. var prices_world[MONTHS];
  28. var prices_tbills[MONTHS];
  29.  
  30. void changeAsset(string old_asset, string new_asset){  
  31.     // If there is no change skip the rest
  32.     if(!strcmp(old_asset, new_asset)){
  33.         print(TO_FILE, "No change in held asset.");
  34.         return;
  35.     }
  36.    
  37.     // Exit previous position
  38.     string prev_position = old_asset;
  39.     if(!strcmp(prev_position, "")) prev_position = "NONE"; // just for the log
  40.     print(TO_FILE, "Changing position from %s to %s.\n", prev_position, new_asset);
  41.     if(strcmp(old_asset, "")){
  42.         asset(old_asset);
  43.         exitLong();
  44.     }
  45.    
  46.     // Enter new position
  47.     asset(new_asset);
  48.     enterLong();
  49.     current_position = new_asset;
  50. }
  51.  
  52. void addPrice(var asset_price, vars price_array){
  53.     print(TO_FILE, "\nMonthly closing price for %s is %f", Asset, asset_price);
  54.     int i;
  55.     for(i=0; i<MONTHS-1; i++){
  56.         price_array[i] = price_array[i+1];
  57.     }
  58.     price_array[MONTHS-1] = asset_price;
  59. }
  60.  
  61. function run()
  62. {
  63.    BarPeriod = 1440;
  64.    #ifdef DAYS
  65.         LookBack = DAYS + 21;
  66.     #else
  67.         LookBack = MONTHS * 21;
  68.     #endif
  69.     StartDate = 20080615;
  70.     Verbose = 0;
  71.    
  72.     if(is(INITRUN)) {
  73.         set(PRELOAD+LOGFILE);
  74.         assetHistory(asset_us, FROM_YAHOO);
  75.         assetHistory(asset_world, FROM_YAHOO);
  76.         assetHistory(asset_bonds, FROM_YAHOO);
  77.         assetHistory(asset_tbills, FROM_YAHOO);
  78.         // Loop through the assets to avoid Error 030
  79.         while(asset(loop("EUR/USD", asset_us, asset_world, asset_bonds, asset_tbills)));
  80.     }
  81.  
  82.     if(tdm() == tom()){
  83.         while(asset(loop("EUR/USD", asset_us, asset_world, asset_tbills)))
  84.         {
  85.             var *prices;
  86.             var *asset_return;
  87.            
  88.             if(!strcmp(Asset, asset_us)){
  89.                 prices = prices_us;
  90.                 asset_return = &return_us;
  91.             }
  92.             else if(!strcmp(Asset, asset_world)){
  93.                 prices = prices_world;
  94.                 asset_return = &return_world;
  95.             }
  96.             else if(!strcmp(Asset, asset_tbills)){
  97.                 prices = prices_tbills;
  98.                 asset_return = &return_tbills;
  99.             }
  100.            
  101.             if(strcmp(Asset, "EUR/USD")){
  102.                 var old_price = prices[0];
  103.                 #ifdef DAYS
  104.                     old_price = priceClose(DAYS);
  105.                 #endif
  106.                 addPrice(priceClose(), prices);
  107.                 if(old_price > 0){
  108.                     *asset_return = (priceClose()-old_price)/old_price;
  109.                     print(TO_FILE, "\n%s return = (%f-%f)/%f = %f", Asset,
  110.                             priceClose(), old_price, old_price, *asset_return);
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     if(tdm() == 1 && !is(LOOKBACK) && (abs(return_us) + abs(return_world) + abs(return_tbills) != 0)) {
  117.         print(TO_FILE, "\nReturns (US, WORLD, TBILLS): %f, %f, %f\n",
  118.                 return_us, return_world, return_tbills);
  119.        
  120.         if(return_us - return_tbills >= 0){
  121.             if(return_us > return_world)
  122.                 changeAsset(current_position, asset_us);
  123.             else if(return_world - return_tbills >= 0)
  124.                 changeAsset(current_position, asset_world);
  125.             else
  126.                 changeAsset(current_position, asset_bonds);
  127.         }
  128.         else {
  129.             changeAsset(current_position, asset_bonds);
  130.         }
  131.     }
  132. }
RAW Paste Data
Top