Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. /*
  2. =================
  3. = DUAL MOMENTUM =
  4. =================
  5.  
  6.    Recommended Assets:
  7.        - US: IVV or VOO or VTI
  8.        - WORLD: VEU or VXUS or VEA (+CWI)
  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 DAYS 252 // comment out to use daily closing prices
  17. string asset_us = "IVV";
  18. string asset_world = "VEU";
  19. string asset_bonds = "AGG";
  20. string asset_tbills = "BIL";
  21. string current_position = "";
  22.  
  23. // Function uset to exit the old position (if there's any) and enter the new one
  24. void changeAsset(string old_asset, string new_asset){  
  25.     // If there is no change skip the rest
  26.     if(!strcmp(old_asset, new_asset)){
  27.         print(TO_FILE, "No change in held asset (%s).", old_asset);
  28.         return;
  29.     }
  30.    
  31.     // Exit previous position
  32.     print(TO_FILE, "Changing position from %s to %s.\n", old_asset, new_asset);
  33.     if(strcmp(old_asset, "-")){
  34.         asset(old_asset);
  35.         exitLong();
  36.     }
  37.    
  38.     // Enter new position
  39.     asset(new_asset);
  40.     enterLong();
  41.     current_position = new_asset;
  42. }
  43.  
  44. function run()
  45. {
  46.    BarPeriod = 1440;
  47.     LookBack = DAYS + 21;
  48.     StartDate = 20070101;
  49.     EndDate = 20180802;
  50.     Verbose = 0;
  51.    
  52.     var return_us = 0;
  53.     var return_world = 0;
  54.     var return_tbills = 0;
  55.  
  56.    
  57.     // If it is the last trading day of the month
  58.     while(asset(loop(asset_us, asset_world, asset_tbills, 0)))
  59.     {
  60.         if(is(INITRUN)) {
  61.             set(PRELOAD+LOGFILE);
  62.             assetHistory(asset_us, FROM_AV);
  63.             assetHistory(asset_world, FROM_AV);
  64.             assetHistory(asset_bonds, FROM_AV);
  65.             assetHistory(asset_tbills, FROM_AV);
  66.         }
  67.    
  68.         // Calculate return always on the first trading day of the month
  69.         if(tdm() == 1 && !is(LOOKBACK)){
  70.             var *asset_return;
  71.            
  72.             if(!strcmp(Asset, asset_us)){
  73.                 asset_return = &return_us;
  74.             }
  75.             else if(!strcmp(Asset, asset_world)){
  76.                 asset_return = &return_world;
  77.             }
  78.             else if(!strcmp(Asset, asset_tbills)){
  79.                 asset_return = &return_tbills;
  80.             }
  81.  
  82.             var old_price = priceClose(DAYS+1);
  83.             *asset_return = (priceClose(1)-old_price)/old_price;
  84.             print(TO_FILE, "\n%s return = (%f-%f)/%f = %f", Asset,
  85.                     priceClose(), old_price, old_price, *asset_return);
  86.            
  87.         }
  88.     }
  89.  
  90.     // If it is the first trading day of the month and the returns were calculated
  91.     if(tdm() == 1 && !is(LOOKBACK) && (abs(return_us) + abs(return_world) + abs(return_tbills) != 0)) {
  92.         print(TO_FILE, "\nReturns (US, WORLD, TBILLS): %f, %f, %f\n",
  93.                 return_us, return_world, return_tbills);
  94.        
  95.         if(return_us - return_tbills >= 0){
  96.             if(return_us > return_world)
  97.                 changeAsset(current_position, asset_us);
  98.             else if(return_world - return_tbills >= 0)
  99.                 changeAsset(current_position, asset_world);
  100.             else
  101.                 changeAsset(current_position, asset_bonds);
  102.         }
  103.         else {
  104.             changeAsset(current_position, asset_bonds);
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement