: | : | :期货程序化 | :期货程序化研究 | :期货量化学习 | :期货量化 |
返回列表 发帖

MultiCharts投资组合交易者策略范例(MultiCharts Portfolio Trader Strategy Examples,PT范例):轮动策略(Rotation Strategy)

MultiCharts投资组合交易者策略范例(MultiCharts Portfolio Trader Strategy Examples,PT范例):轮动策略(Rotation Strategy)

MultiCharts投资组合交易者策略范例(MultiCharts Portfolio Trader Strategy Examples,PT范例):轮动策略(Rotation Strategy)

该策略由 kbeary33 在 MultiCharts 论坛 (http://www.multicharts.com/discussion/viewtopic.php?f=19&t=45413) 上提出。

策略描述

轮动策略是一种简单的策略,通过使用投资组合中的每种工具来计算特定指标。为指标值最佳的工具品种建仓。

以变化率指标为例。这组工具由用户在投资组合交易应用程序中确定。进入多头头寸的工具品种数量由 BuyBestX 输入配置。标准止损 + 盈利目标策略用于退出头寸。

策略开发

Portfolio_Rotation 信号

该信号为投资组合中的所有工具生成输入指令并计算指标值。

指标公式输入到输入栏中,并在每个条形图上进行计算。
  1. inputs:
  2. Formula (PercentChange(close, 14));
  3. variables: formulaValue(0);
  4. formulaValue = Formula;
复制代码
为了进一步比较并决定输入位置,然后将公式输入全局变量:
  1. pmm_set_my_named_num ("RotationalValue", formulaValue);
复制代码
生成所有工具品种的进场:
  1. buy("LE") next bar market;
  2. sellshort("SE") next bar market;
复制代码
为了管理资本,将使用用户设置的标准投资组合设置,即每份合约保证金和每份合约潜在损失:
  1. pmm_set_my_named_num("MoneyCostForInvestPerCtrct", pmms_to_portfolio_currency(MoneyCostForInvestPerCtrct));
复制代码
  1. var: PotentialEntryPrice(close), MoneyCostForInvestPerCtrct(0);
  2. if (entryprice > 0) then PotentialEntryPrice = entryprice;
  3. MoneyCostForInvestPerCtrct =
  4. pmms_calc_money_cost_for_entry_per_cntrct(PotentialEntryPrice, Portfolio_GetMarginPerContract)
  5. +
  6. pmms_calc_money_cost_for_entry_per_cntrct(PotentialEntryPrice, Portfolio_GetMaxPotentialLossPerContract);
复制代码
计算一手价格值时,该值将转换为投资组合货币,并写入投资组合全局变量。
  1. pmm_set_my_named_num("MoneyCostForInvestPerCtrct", pmms_to_portfolio_currency(MoneyCostForInvestPerCtrct));
复制代码
以投资组合资本的百分比为单位设定的标准止损和盈利目标将用于退出。
  1. inputs: StopLossPcntsOfPortfolio(0.1),
  2. ProfitTargetPcntsOfPortfolio(0.1);
  3. variable: value(0);
  4. setstopposition;
  5. value = StopLossPcntsOfPortfolio * 0.01 * Portfolio_Equity;
  6. setstoploss(convert_currency(datetime[0], portfolio_CurrencyCode, SymbolCurrencyCode, value));
  7. value = ProfitTargetPcntsOfPortfolio * 0.01 * Portfolio_Equity;
  8. setprofittarget(convert_currency(datetime[0], portfolio_CurrencyCode, SymbolCurrencyCode, value));
复制代码
Portfolio_Rotation_MM 信号

该信号用作投资组合中的资金管理信号。该研究会验证所有投资组合工具的指标值,并管理开仓。开仓的投资组合工具数量由用户设定:
  1. inputs:
  2. BuyBestX(10),
  3. SellWorstY(10);
复制代码
将提取所有策略的指标值,并创建每次计算的指标值排序列表。为此,我们需要使用包含指标值和策略索引的二维数组:
  1. variables: idx(0), strategyIdx(0), strategyValue(0);
  2. arrays: allStrategies[10000, 1](-1);
复制代码
在每次计算被拒绝之前生成进场指令:
  1. pmms_strategies_deny_entries_all;
复制代码
在数组中填入指标值和每个策略的索引,然后按值对数组进行排序:
  1. for strategyIdx = 0 to pmms_strategies_count - 1 begin
  2. strategyValue = pmms_get_strategy_named_num(strategyIdx, "RotationalValue");
  3. allStrategies[strategyIdx , 0] = strategyValue;
  4. allStrategies[strategyIdx , 1] = strategyIdx;
  5. end;
  6. Sort2DArrayByKey(allStrategies, pmms_strategies_count, 1);
复制代码
让我们计算一下现在处于仓位的策略数量。根据最佳指标指数,BuyBestX 工具应持有多头头寸,SellWorstY 工具应持有空头头寸:
  1. variables: inLong(0), inShort(0);
  2. arrays: strategiesLong[](-1), strategiesShort[](-1);
  3. inLong = pmms_strategies_in_long_count(strategiesLong);
  4. inShort = pmms_strategies_in_short_count(strategiesShort);
复制代码
在计算过程中,我们会根据指标索引循环查看应处于多头位置的策略(其中,如果策略已经处于多头位置,我们会在 strategiesLong 数组中对其进行跟踪):
  1. for idx = 0 to BuyBestX - 1 begin
  2. cur_idx = allStrategies[idx, 1];
  3. if (not array_contains(strategiesLong, cur_idx)) then
  4. pmms_strategy_allow_long_entries(cur_idx)
  5. else
  6. strategiesLong[array_indexof(strategiesLong, cur_idx)] = -1;
  7. if UsePortfolioMoneyPcnt then
  8. pmms_strategy_set_entry_contracts(
  9. cur_idx,
  10. pmms_calc_contracts_for_entry( PortfolioMoneyPcntForEntry, cur_idx )
  11. );
  12. End;
复制代码
根据指标指数,已就位但未跻身最佳行列的策略将被强制平仓:
  1. for idx = 0 to inLong - 1 begin
  2. value1 = strategiesLong[idx];
  3. if value1 >= 0 then begin
  4. pmms_strategy_close_position(value1);
  5. end;
  6. end;
复制代码
空头入场策略也同样得到处理。

论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
 
期货论坛 - 版权/免责声明   1.本站发布源码(包括函数、指标、策略等)均属开放源码,用意在于让使用者学习程序化语法撰写,使用者可以任意修改语法內容并调整参数。仅限用于个人学习使用,请勿转载、滥用,严禁私自连接实盘账户交易
  2.本站发布资讯(包括文章、视频、历史记录、教材、评论、资讯、交易方案等)均系转载自网络主流媒体,内容仅为作者当日个人观点,本网转载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。本网不对该类信息或数据做任何保证。不对您构成任何投资建议,不能依靠信息而取代自身独立判断,不对因使用本篇文章所诉信息或观点等导致的损失承担任何责任。
  3.本站发布资源(包括书籍、杂志、文档、软件等)均从互联网搜索而来,仅供个人免费交流学习,不可用作商业用途,本站不对显示的内容承担任何责任。请在下载后24小时内删除。如果喜欢,请购买正版,谢谢合作!
  4.龙听期货论坛原创文章属本网版权作品,转载须注明来源“龙听期货论坛”,违者本网将保留追究其相关法律责任的权力。本论坛除发布原创文章外,亦致力于优秀财经文章的交流分享,部分文章推送时若未能及时与原作者取得联系并涉及版权问题时,请及时联系删除。联系方式:http://www.qhlt.cn/thread-262-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

信号:Portfolio_Rotation
  1. inputs:
  2.         Formula(PercentChange(close, 14));

  3. variables: formulaValue(0);

  4. formulaValue = Formula;

  5. if getappinfo(aiisportfoliomode) <> 1 then
  6.         raiseruntimeerror("Signal can be used in Portfolio only.");

  7. pmm_set_my_named_num("RotationalValue", formulaValue);

  8. buy("LE") next bar market;
  9. sellshort("SE") next bar market;

  10. pmm_set_my_status(
  11.         iffstring(marketposition = 0, "Flat",
  12.                 iffstring(marketposition = 1, "Long", "Short")
  13.         )
  14. );

  15. // money management
  16. var: PotentialEntryPrice(close), MoneyCostForInvestPerCtrct(0);

  17. if (entryprice > 0) then PotentialEntryPrice = entryprice;

  18. MoneyCostForInvestPerCtrct =
  19.         pmms_calc_money_cost_for_entry_per_cntrct(PotentialEntryPrice, Portfolio_GetMarginPerContract)
  20.         +
  21.         pmms_calc_money_cost_for_entry_per_cntrct(PotentialEntryPrice, Portfolio_GetMaxPotentialLossPerContract);
  22.        
  23. if 0 > MoneyCostForInvestPerCtrct then
  24.         raiseruntimeerror( text("Error! Price = ", PotentialEntryPrice:0:6, ", PMargin = ", Portfolio_GetMarginPerContract, ", PMaxPLoss = ", Portfolio_GetMaxPotentialLossPerContract));
  25.        
  26. // MoneyCostForInvestPerCtrct in currency of the symbol. Convert it to portfolio currency ...
  27. pmm_set_my_named_num("MoneyCostForInvestPerCtrct", pmms_to_portfolio_currency(MoneyCostForInvestPerCtrct));

  28. // exits
  29. inputs: StopLossPcntsOfPortfolio(0.1),
  30.         ProfitTargetPcntsOfPortfolio(0.1);
  31. variable: value(0);

  32. setstopposition;
  33. value = StopLossPcntsOfPortfolio * 0.01 * Portfolio_Equity;
  34. setstoploss(convert_currency(datetime[0], portfolio_CurrencyCode, SymbolCurrencyCode, value));
  35. value = ProfitTargetPcntsOfPortfolio * 0.01 * Portfolio_Equity;
  36. setprofittarget(convert_currency(datetime[0], portfolio_CurrencyCode, SymbolCurrencyCode, value));
复制代码
信号:Portfolio_Rotation_MM
  1. once cleardebug;

  2. inputs:
  3.         BuyBestX(10),
  4.         SellWorstY(10);
  5.        
  6. Input: use_logging(false);

  7. variables: idx(0), strategyIdx(0), strategyValue(0);
  8. arrays: allStrategies[10000, 1](-1);

  9. if getappinfo(aiisportfoliomode) <> 1 then
  10.         raiseruntimeerror("Signal can be applied (as Money Management Signal) in Portfolio only.");
  11.        
  12. if pmms_strategies_count < BuyBestX + SellWorstY then
  13.         raiseruntimeerror(text("Portfolio has not enough instruments: instruments number = ", pmms_strategies_count, "; BuyBestX = ", BuyBestX, "; SellWorstY = ", SellWorstY));


  14. pmms_strategies_deny_entries_all;

  15. for strategyIdx = 0 to pmms_strategies_count - 1 begin
  16.         strategyValue = pmms_get_strategy_named_num(strategyIdx, "RotationalValue");
  17.         allStrategies[strategyIdx , 0] = strategyValue;
  18.         allStrategies[strategyIdx , 1] = strategyIdx;
  19. end;

  20. Sort2DArrayByKey(allStrategies, pmms_strategies_count, 1);

  21. variables: inLong(0), inShort(0);
  22. arrays: strategiesLong[](-1), strategiesShort[](-1);
  23. inLong = pmms_strategies_in_long_count(strategiesLong);
  24. inShort = pmms_strategies_in_short_count(strategiesShort);

  25. if use_logging then
  26.         print( "strategies in position: long=",inLong, ", short=", inShort );

  27. var : cur_idx(0);

  28. for idx = 0 to BuyBestX - 1 begin
  29.         cur_idx = allStrategies[idx, 1];
  30.        
  31.         if (not array_contains(strategiesLong, cur_idx)) then
  32.                 pmms_strategy_allow_long_entries(cur_idx)
  33.         else
  34.                 strategiesLong[array_indexof(strategiesLong, cur_idx)] = -1;
  35.                
  36.         if use_logging then
  37.                 print( "strategy ", pmms_strategy_symbol(cur_idx), "long entry" );
  38.                
  39.         if UsePortfolioMoneyPcnt then
  40.                 pmms_strategy_set_entry_contracts(
  41.                         cur_idx,
  42.                         pmms_calc_contracts_for_entry( PortfolioMoneyPcntForEntry, cur_idx )
  43.                 );
  44. end;

  45. for idx = pmms_strategies_count - 1 downto pmms_strategies_count - SellWorstY begin
  46.         cur_idx = allStrategies[idx, 1];

  47.         if (not array_contains(strategiesShort, cur_idx)) then
  48.                 pmms_strategy_allow_short_entries(cur_idx)
  49.         else
  50.                 strategiesShort[array_indexof(strategiesShort, cur_idx)] = -1;
  51.                
  52.         if use_logging then
  53.                 print( "strategy ", pmms_strategy_symbol(cur_idx), "short entry" );
  54.                
  55.         if UsePortfolioMoneyPcnt then
  56.                 pmms_strategy_set_entry_contracts(
  57.                         cur_idx,
  58.                         pmms_calc_contracts_for_entry( PortfolioMoneyPcntForEntry, cur_idx )
  59.                 );       
  60. end;

  61. // force positions close
  62. for idx = 0 to inLong - 1 begin
  63.         value1 = strategiesLong[idx];
  64.         if value1 >= 0 then begin
  65.                 pmms_strategy_close_position(value1);               
  66.                 if use_logging then
  67.                         print( "strategy ", pmms_strategy_symbol(value1), "force position close" );
  68.         end;
  69. end;

  70. for idx = 0 to inShort - 1 begin
  71.         value1 = strategiesShort[idx];
  72.         if value1 >= 0 then begin
  73.                 pmms_strategy_close_position(value1);
  74.                 if use_logging then
  75.                         print( "strategy ", pmms_strategy_symbol(value1), "force position close" );
  76.         end;
  77. end;


  78. // money management
  79. inputs:
  80.         UsePortfolioMoneyPcnt(False),
  81.         PortfolioMoneyPcntForEntry(1);

  82. if use_logging then
  83.         print("------------------------------------------------------------------")
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

为了防止中文表达不到位,下面附上英文原文
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

本帖隐藏的内容需要回复才可以浏览
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

返回列表