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

MultiCharts投资组合交易者策略范例(MultiCharts Portfolio Trader Strategy Examples,PT范例):排序策略(Rank Strategy)

MultiCharts投资组合交易者策略范例(MultiCharts Portfolio Trader Strategy Examples,PT范例):排序策略(Rank Strategy)

MultiCharts投资组合交易者策略范例(MultiCharts Portfolio Trader Strategy Examples,PT范例):排序策略(Rank Strategy)

本策略由 Angelos Diamantis 提出。

策略描述

该策略基于计算一个指标,该指标适用于投资组合中的每个工具。确定所有指标值后,根据指标值从高到低进行排列。对于指标值最佳的投资工具建立多头头寸,而对于指标值最差的投资工具建立空头头寸。

让我们以一个由 35 只股票组成的投资组合为例,使用 5 分钟分辨率进行交易。同样的指标(% Chg),计算公式如下: "收盘价 - 收盘价[1]) / 收盘价"。对于指标值最高的 5 个交易品种,我们建立多头头寸。对于指标值最低的 5 个交易品种,我们建立空头头寸。

交易规模可设置为所有交易品种的固定合约数或投资组合总资本的百分比。

战略发展

投资组合排名信号基础(Portfolio Rank Signal Base)

该信号计算投资组合中所有工具的指定指标值,并使用工具策略指数保存这些值。

用于计算的指标公式和数据序列号由用户设置:
  1. inputs:
  2.         BasedOnData(2),
  3.         Formula( (close - close[1]) / close ),
  4.         TraceOutput(false);
复制代码
我们需要为我们的信号添加一些限制,使其只能用于组合交易;用于计算信号的数据序列应可用于开始计算:
  1. // *** restrictions
  2. once if barstatus(BasedOnData) < 0 then raiseruntimeerror("Portfolio Rank Signal Base needs datastream" + numtostr(BasedOnData, 0));
  3. once if 1 <> getappinfo(aiisportfoliomode) then raiseruntimeerror("Portfolio Rank Signal Base can be applied to MCPortfolio application only.");
  4. // ****************
复制代码
现在,我们将使用公式计算我们的指标,并保存每个品种的值:
  1. BarN = BarNumber of data(BasedOnData);

  2. if BarN > BarN[1] then begin
  3.         R = Formula of data(BasedOnData);
  4.         pmm_set_my_named_num("RankStrategyR", R);
  5. end;
复制代码
要按投资组合资本的百分比而不是固定手数进行交易,每种工具都应返回每份合同的成本:
  1. begin
  2. var: MoneyCostForInvestPerCtrct(0), otential_entry_price(close);
  3. MoneyCostForInvestPerCtrct = pmms_calc_money_cost_for_entry_per_cntrct(potential_entry_price, Portfolio_GetMarginPerContract) + pmms_calc_money_cost_for_entry_per_cntrct(potential_entry_price,Portfolio_GetMaxPotentialLossPerContract);
  4.                
  5.         if 0 > MoneyCostForInvestPerCtrct then raiseruntimeerror( text("Error! Price = ", potential_entry_price:0:6,
  6. "PMargin = ", Portfolio_GetMarginPerContract, "PMaxPLoss = ", Portfolio_GetMarginPerContract) );
  7.                
  8.         // MoneyCostForInvestPerCtrct in currency of the symbol. Convert it to portfolio currency ...
  9. pmm_set_my_named_num("MoneyCostForInvestPerCtrct", pmms_to_portfolio_currency(MoneyCostForInvestPerCtrct));
  10. end;
复制代码
最后,我们将生成多单和空单。经过资金管理信号计算后,只会发送其中的几个(基于策略逻辑):
  1. buy next bar market;
  2. sellshort next bar market;
复制代码
投资组合排名 MM 信号(Portfolio Rank MM Signal)

该信号用于资金管理。它将所有指标值整理成一个列表,并根据列表管理交易工具的开仓头寸。

下面是管理交易规模和开仓工具数量的用户输入:
  1. inputs:
  2.         ContractsNumber(10),
  3.         IgnoreContractsNumberUsePcnt(false),
  4.         PortfolioBalancePercent(1),
  5.         BuyBestN(10),
  6.         SellWorseN(10),
  7.         TraceOutput(false);
复制代码
让我们对该信号进行一些限制:a) 只能在组合交易中使用;b) 组合规模不应超过 10 000 个交易品种;c) 交易品种的数量应与确定入场数量的用户输入一致:
  1. once if 1 <> getappinfo(aiisportfoliomode)

  2. then raiseruntimeerror("Portfolio Rank Money Management Signal can be applied to MCPortfolio application only.");

  3. once if pmms_strategies_count() > 10000

  4. then raiseruntimeerror

  5. ("Portfolio Rank Money Management Signal too much instruments, max value = " + numtostr(100000, 0));

  6. once if pmms_strategies_count() < BuyBestN + SellWorseN

  7. then raiseruntimeerror

  8. ("Portfolio Rank Money Management Signal, please check inputs, BuyBestN + SellWorseN should be less or equal to tradable Instruments number");
复制代码
将投资组合中交易工具的数量保存为变量,并禁止为所有工具开仓:
  1. once begin
  2.         portfolioStrategies = pmms_strategies_count();
  3.         array_setmaxindex(BaseR, portfolioStrategies);
  4.         array_setmaxindex(ContractsForEntry, portfolioStrategies);
  5. end;

  6. pmms_strategies_deny_entries_all;
复制代码
提取每个品种的指标值:
  1. for idx = 0 to portfolioStrategies - 1 begin
  2.         BaseR[idx] = pmms_get_strategy_named_num(idx, "RankStrategyR");
  3. end;
复制代码
策略指数和值存储在数组中,因此我们可以在对所有工具进行排序后,为具有相应指数的工具建仓。

然后,该策略会计算每个交易品种的建仓合约数。然后,按升序对指标值数组进行排序:
  1. for idx = 0 to portfolioStrategies - 1 begin
  2.         Value_Idx[1, idx + 1] = BaseR[idx];
  3.         Value_Idx[2, idx + 1] = idx;

  4.         if IgnoreContractsNumberUsePcnt then begin
  5.                 ContractsForEntry[idx] = pmms_calc_contracts_for_entry(PortfolioBalancePercent, idx);
  6.         end
  7.         else
  8.                 ContractsForEntry[idx] = ContractsNumber;
  9. end;

  10. Sort2DArray(Value_Idx, 2, portfolioStrategies, 1 {from high to low});
复制代码
对于指标值最高的工具,允许在指定的合约数内进行多头交易:
  1. variables: inLong(0), inShort(0);
  2. array: strategyIndexes[](0);

  3. inLong = pmms_strategies_in_long_count(strategyIndexes);
  4. for idx = 1 to BuyBestN - inLong begin
  5.         strIdx = Value_Idx[2, idx];
  6. pmms_strategy_set_entry_contracts(strIdx, ContractsForEntry[strIdx]);
  7.         pmms_strategy_allow_long_entries(strIdx);

  8.         if TraceOutput then
  9.                 print("CurrentBar = ", currentbar:0:0, ".

  10. Allow LONG for symbol ", pmms_strategy_symbol(strIdx), ", Contracts = ", ContractsForEntry[strIdx]);
  11. end;
复制代码
对于指标值最低的金融工具,允许在规定的合约数量内做空:
  1. inShort = pmms_strategies_in_short_count(strategyIndexes);
  2. for idx = portfolioStrategies downto portfolioStrategies - SellWorseN + inShort + 1 begin
  3.         strIdx = Value_Idx[2, idx];
  4. pmms_strategy_set_entry_contracts(strIdx, ContractsForEntry[strIdx]);
  5.         pmms_strategy_allow_short_entries(strIdx);

  6.         if TraceOutput then
  7. print("CurrentBar = ", currentbar:0:0, ". Allow SHORT for symbol ", pmms_strategy_symbol(strIdx), ", Contracts = ", ContractsForEntry[strIdx]);
  8. end;
复制代码
其他工具不按现行计算方法进行交易。

附录

组合信号脚本默认添加到 MultiCharts 和 MultiCharts64 中。

Angelos Diamantis 的原始策略描述:

关于等级策略,这里有一个简短但通用的描述。

例如,AvgReturn= (R1+R2+R3+...+R500)/500;Sdev= AvgReturn 的标准偏差;其中 Ri = 第 i 只股票的日收益率 i=1 至 500,如果我们的范围是标准普尔的 500 只股票,那么根据该指标和适用的数据,例如 Data2= 日线,Data1=5 分钟线,将所有股票从高到低排序。
  1. Vars= BarNo2(0),MyIndicator(0),R(0);
  2. BarNo2= BarNumber of data2;
  3. If BarNo2>BarNo2[1] then Begin
  4. R = (C of data2 - C[1] of data2) / C[1] of data2;
  5. MyIndicator= (R - AvgReturn ) / Sdev
  6. end;

  7. {Retrieve MyIndicator Rank. Rank is from 1 to 500 since our universe is 500 Stocks}
  8. If Rank<=10 then Buy 200 contracts next bar at O; {Go Long the best 10 stocks}
  9. Else If Rank>=490 then SellShort 200 contracts next bar at O; {Go Short the worse 10 stocks}
复制代码
以上是一个典型的股票相对业绩交易案例。MyIndicator 应该是通用的,这意味着用户可以根据自己的意愿更改该排名指标。另一个排名指标示例可能是:
  1. MyIndicator = ADX of data2;
复制代码
然后只允许交易 ADX 最高的股票。

注意:这些信号主要供参考。如果收到的结果不一致,可以根据自己的需要修改信号。

论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
 
期货论坛 - 版权/免责声明   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 Rank Signal Base(signal)
  1. inputs:
  2.         BasedOnData(2),
  3.         Formula( (close - close[1]) / close ),

  4.         TraceOutput(false);


  5. Vars: BarN(0), R(0);


  6. // *** restrictions
  7. once if barstatus(BasedOnData) < 0 then raiseruntimeerror("Portfolio Rank Signal Base needs datastream " + numtostr(BasedOnData, 0));
  8. once if 1 <> getappinfo(aiisportfoliomode) then raiseruntimeerror("Portfolio Rank Signal Base can be applied to MCPortfolio application only.");
  9. // ****************


  10. once pmm_set_global_named_str("RankStrategyApplied", "true");


  11. BarN = BarNumber of data(BasedOnData);

  12. if BarN > BarN[1] then begin
  13.         R = Formula of data(BasedOnData);
  14.         pmm_set_my_named_num("RankStrategyR", R);
  15. end;

  16. if (TraceOutput) then begin
  17.         print("CurrentBar = ", currentbar:0:0, ". Put MyIndicator value = ", R:0:5, " for symbol ", symbolname, ".");
  18. end;


  19. // *** Money management
  20. begin
  21.         var: MoneyCostForInvestPerCtrct(0), potential_entry_price(close);
  22.         MoneyCostForInvestPerCtrct =
  23.                 pmms_calc_money_cost_for_entry_per_cntrct(potential_entry_price, Portfolio_GetMarginPerContract)
  24.                 +
  25.                 pmms_calc_money_cost_for_entry_per_cntrct(potential_entry_price, Portfolio_GetMaxPotentialLossPerContract);
  26.                
  27.         if 0 > MoneyCostForInvestPerCtrct then
  28.                 raiseruntimeerror( text("Error! Price = ", potential_entry_price:0:6, "PMargin = ", Portfolio_GetMarginPerContract, "PMaxPLoss = ",  Portfolio_GetMarginPerContract) );
  29.                
  30.         // MoneyCostForInvestPerCtrct in currency of the symbol. Convert it to portfolio currency ...
  31.         pmm_set_my_named_num("MoneyCostForInvestPerCtrct", pmms_to_portfolio_currency(MoneyCostForInvestPerCtrct));
  32. end;
  33. // ********************

  34. buy next bar market;
  35. sellshort next bar market;
复制代码
Portfolio Rank MM Signal(信号)
  1. inputs:
  2.         ContractsNumber(200),

  3.         IgnoreContractsNumberUsePcnt(false),
  4.         PortfolioBalancePercent(1),

  5.         StdDevLength(14),

  6.         BuyBestN(10),
  7.         SellWorseN(10),

  8.         TraceOutput(false);


  9. variables: AvgReturn(0), SDev(0), portfolioStrategies(0), idx(0), strIdx(0), lots(0);
  10. array: BaseR[](0), ContractsForEntry[](0), Value_Idx[2, 10000](0);


  11. // *** restrictions
  12. once if 1 <> getappinfo(aiisportfoliomode) then raiseruntimeerror("Portfolio Rank Money Management Signal can be applied for MCPortfolio application only.");
  13. //once if "true" <> pmm_get_global_named_str("RankStrategyApplied") then raiseruntimeerror("Portfolio Rank Monem Management Signal can be applied in pair with Portfolio Rank Signal Base only.");
  14. once if pmms_strategies_count() > 10000 then raiseruntimeerror("Portfolio Rank Money Management Signal too much intruments, max value = " + numtostr(100000, 0));
  15. once if pmms_strategies_count() < BuyBestN + SellWorseN then raiseruntimeerror("Portfolio Rank Monem Management Signal, please check inputs, BuyBestN + SellWorseN should be less or equal to tradable Instruments number");
  16. // ****************


  17. once begin
  18.         portfolioStrategies = pmms_strategies_count();
  19.         array_setmaxindex(BaseR, portfolioStrategies);
  20.         array_setmaxindex(ContractsForEntry, portfolioStrategies);
  21. end;

  22. pmms_strategies_deny_entries_all;

  23. AvgReturn = 0;

  24. for idx = 0 to portfolioStrategies - 1 begin
  25.         BaseR[idx] = pmms_get_strategy_named_num(idx, "RankStrategyR");
  26.         AvgReturn += BaseR[idx];
  27. end;

  28. AvgReturn /= portfolioStrategies;
  29. SDev = StandardDev(AvgReturn, StdDevLength, 1);
  30. if (SDev = 0) then SDev = 1;

  31. for idx = 0 to portfolioStrategies - 1 begin
  32.         Value_Idx[1, idx + 1] = ( BaseR[idx] - AvgReturn ) / SDev;
  33.         Value_Idx[2, idx + 1] = idx;

  34.         if IgnoreContractsNumberUsePcnt then begin
  35.                 ContractsForEntry[idx] = pmms_calc_contracts_for_entry(PortfolioBalancePercent, idx);
  36.         end
  37.         else
  38.                 ContractsForEntry[idx] = ContractsNumber;
  39. end;

  40. Sort2DArray(Value_Idx, 2, portfolioStrategies, -1 {from low to high});

  41. variables: inLong(0), inShort(0);
  42. array: strategyIndexes[](0);

  43. inLong = pmms_strategies_in_long_count(strategyIndexes);
  44. for idx = 1 to BuyBestN - inLong begin
  45.         strIdx = Value_Idx[2, idx];
  46.         pmms_strategy_set_entry_contracts(strIdx, ContractsForEntry[strIdx]);
  47.         pmms_strategy_allow_long_entries(strIdx);
  48.         if TraceOutput then
  49.                 print("CurrentBar = ", currentbar:0:0, ". Allow LONG for symbol ", pmms_strategy_symbol(strIdx), ", Contracts = ", ContractsForEntry[strIdx]);
  50. end;

  51. inShort = pmms_strategies_in_short_count(strategyIndexes);
  52. for idx = portfolioStrategies downto portfolioStrategies - SellWorseN + inShort + 1 begin
  53.         strIdx = Value_Idx[2, idx];
  54.         pmms_strategy_set_entry_contracts(strIdx, ContractsForEntry[strIdx]);
  55.         pmms_strategy_allow_short_entries(strIdx);
  56.         if TraceOutput then
  57.                 print("CurrentBar = ", currentbar:0:0, ". Allow SHORT for symbol ", pmms_strategy_symbol(strIdx), ", Contracts = ", ContractsForEntry[strIdx]);
  58. end;
复制代码
如何访问权限为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

返回列表