龙听期货论坛's Archiver

C
+
+


 微信: QQ:

龙听 发表于 2024-1-29 15:42

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)

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

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

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

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

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

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

once if pmms_strategies_count() > 10000

then raiseruntimeerror

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

once if pmms_strategies_count() < BuyBestN + SellWorseN

then raiseruntimeerror

("Portfolio Rank Money Management Signal, please check inputs, BuyBestN + SellWorseN should be less or equal to tradable Instruments number");[/code]将投资组合中交易工具的数量保存为变量,并禁止为所有工具开仓:[code]once begin
        portfolioStrategies = pmms_strategies_count();
        array_setmaxindex(BaseR, portfolioStrategies);
        array_setmaxindex(ContractsForEntry, portfolioStrategies);
end;

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

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

        if IgnoreContractsNumberUsePcnt then begin
                ContractsForEntry[idx] = pmms_calc_contracts_for_entry(PortfolioBalancePercent, idx);
        end
        else
                ContractsForEntry[idx] = ContractsNumber;
end;

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

inLong = pmms_strategies_in_long_count(strategyIndexes);
for idx = 1 to BuyBestN - inLong begin
        strIdx = Value_Idx[2, idx];
pmms_strategy_set_entry_contracts(strIdx, ContractsForEntry[strIdx]);
        pmms_strategy_allow_long_entries(strIdx);

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

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

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

附录

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

Angelos Diamantis 的原始策略描述:

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

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

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

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

龙听 发表于 2024-1-29 15:44

Portfolio Rank Signal Base(signal)[code]
inputs:
        BasedOnData(2),
        Formula( (close - close[1]) / close ),

        TraceOutput(false);


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


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


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


BarN = BarNumber of data(BasedOnData);

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

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


// *** Money management
begin
        var: MoneyCostForInvestPerCtrct(0), potential_entry_price(close);
        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);
               
        if 0 > MoneyCostForInvestPerCtrct then
                raiseruntimeerror( text("Error! Price = ", potential_entry_price:0:6, "PMargin = ", Portfolio_GetMarginPerContract, "PMaxPLoss = ",  Portfolio_GetMarginPerContract) );
               
        // MoneyCostForInvestPerCtrct in currency of the symbol. Convert it to portfolio currency ...
        pmm_set_my_named_num("MoneyCostForInvestPerCtrct", pmms_to_portfolio_currency(MoneyCostForInvestPerCtrct));
end;
// ********************

buy next bar market;
sellshort next bar market;

[/code]Portfolio Rank MM Signal(信号)[code]
inputs:
        ContractsNumber(200),

        IgnoreContractsNumberUsePcnt(false),
        PortfolioBalancePercent(1),

        StdDevLength(14),

        BuyBestN(10),
        SellWorseN(10),

        TraceOutput(false);


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


// *** restrictions
once if 1 <> getappinfo(aiisportfoliomode) then raiseruntimeerror("Portfolio Rank Money Management Signal can be applied for MCPortfolio application only.");
//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.");
once if pmms_strategies_count() > 10000 then raiseruntimeerror("Portfolio Rank Money Management Signal too much intruments, max value = " + numtostr(100000, 0));
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");
// ****************


once begin
        portfolioStrategies = pmms_strategies_count();
        array_setmaxindex(BaseR, portfolioStrategies);
        array_setmaxindex(ContractsForEntry, portfolioStrategies);
end;

pmms_strategies_deny_entries_all;

AvgReturn = 0;

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

AvgReturn /= portfolioStrategies;
SDev = StandardDev(AvgReturn, StdDevLength, 1);
if (SDev = 0) then SDev = 1;

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

        if IgnoreContractsNumberUsePcnt then begin
                ContractsForEntry[idx] = pmms_calc_contracts_for_entry(PortfolioBalancePercent, idx);
        end
        else
                ContractsForEntry[idx] = ContractsNumber;
end;

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

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

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

inShort = pmms_strategies_in_short_count(strategyIndexes);
for idx = portfolioStrategies downto portfolioStrategies - SellWorseN + inShort + 1 begin
        strIdx = Value_Idx[2, idx];
        pmms_strategy_set_entry_contracts(strIdx, ContractsForEntry[strIdx]);
        pmms_strategy_allow_short_entries(strIdx);
        if TraceOutput then
                print("CurrentBar = ", currentbar:0:0, ". Allow SHORT for symbol ", pmms_strategy_symbol(strIdx), ", Contracts = ", ContractsForEntry[strIdx]);
end;

[/code]

龙听 发表于 2024-2-1 12:48

为了防止中文表达不到位,下面附上英文原文

龙听 发表于 2024-2-1 12:53

**** Hidden Message *****

页: [1]