Difference between revisions of "4.6.10 Advanced. Portfolio"

From MultiCharts
Jump to navigation Jump to search
 
Line 15: Line 15:
  
 
The above example will write the Symbol, resolution, bar and bar time at which the signal is calculated.
 
The above example will write the Symbol, resolution, bar and bar time at which the signal is calculated.
Now, let’s apply several instruments with different resolutions in the portfolio in Data1 column, for example: MSFIT 1,2,3,4 days, then, calculate the portfolio and look in Output Power Language .NET:
+
Now, let’s apply several instruments with different resolutions in the portfolio in Data1 column, for example: MSFIT 1,2,3,4 days, then, calculate the portfolio and look in Output PowerLanguage .NET:
  
 
<syntaxhighlight>
 
<syntaxhighlight>

Latest revision as of 15:49, 4 May 2017

In Portfolio Trader, unlike trading from the chart, several strategies can be calculated simultaneously, each one with its own tool set and its own signal set. Thus, the calculation of all the strategies upon all the data series in them is synchronized.

True portfolio backtesting means that strategies are calculated across all price series, one bar at a time. For example, if you have one minute bars for five symbols, the strategy will first calculate on the first minute of each symbol, then the second, and so on.

All of the signals, even those where additional data series are used, are calculated upon the calculation rules at several DataStreams. The orders are filled at the opening of the next bar on the Data1, the same as in the charts.

Let’s consider the following example and create a simple signal:

public class pf_test : SignalObject {
	public pf_test(object _ctx):base(_ctx){}
	protected override void CalcBar(){
		Output.WriteLine("Symbol:{0} Resolution:{1} CurrentBar:{2} Date:{3}", Bars.Request.Symbol, Bars.Request.Resolution.Size, Bars.CurrentBar, Bars.Time.Value);	}
}

The above example will write the Symbol, resolution, bar and bar time at which the signal is calculated. Now, let’s apply several instruments with different resolutions in the portfolio in Data1 column, for example: MSFIT 1,2,3,4 days, then, calculate the portfolio and look in Output PowerLanguage .NET:

Symbol:MSFT Resolution:1 CurrentBar:1 Date:06.02.2013 16:00:00
Symbol:MSFT Resolution:1 CurrentBar:2 Date:07.02.2013 16:00:00
Symbol:MSFT Resolution:1 CurrentBar:3 Date:08.02.2013 16:00:00
Symbol:MSFT Resolution:2 CurrentBar:1 Date:08.02.2013 16:00:00
Symbol:MSFT Resolution:3 CurrentBar:1 Date:08.02.2013 16:00:00
Symbol:MSFT Resolution:4 CurrentBar:1 Date:08.02.2013 16:00:00
Symbol:MSFT Resolution:1 CurrentBar:4 Date:11.02.2013 16:00:00
Symbol:MSFT Resolution:1 CurrentBar:5 Date:12.02.2013 16:00:00
Symbol:MSFT Resolution:2 CurrentBar:2 Date:12.02.2013 16:00:00
Symbol:MSFT Resolution:1 CurrentBar:6 Date:13.02.2013 16:00:00
Symbol:MSFT Resolution:3 CurrentBar:2 Date:13.02.2013 16:00:00
Symbol:MSFT Resolution:2 CurrentBar:3 Date:14.02.2013 16:00:00
Symbol:MSFT Resolution:4 CurrentBar:2 Date:14.02.2013 16:00:00

As we can see, the strategy on the symbol with lower resolution is counted more often than on other symbols, as a result all of the data series are calculated in sequence, from left to right and from the top to the bottom in chronological order.

Portfolio.png

Additionally, in portfolio the new number of default entry contracts calculation algorithm appeared: Percent of Equity: Equity = Portfolio Initial Capital + Portfolio Net Profit – Portfolio Invested Capital. The amount for the entry is calculated as following: DollarsPerEntry = (PercentOfEquity / 100)* Equity; The number of contracts for the entry EntryContracts = DollarsPerEntry / (EntryPrice*BigPointValue).

In the portfolio, unlike the strategy calculation on the chart, there are additional limits for the maximum number of contracts. This can be set in the Portfolio Properties tab:

Portfolio_settings_sdf.png

Initial Portfolio Capital $ - the initial capital for the whole portfolio common for all strategies in the portfolio.
Exposure % - the amount that we can risk in the whole portfolio.
Max % of Capital at Risk per Position % - the amount that we can risk upon separate positions in the portfolio.
Margin per Contract % - the amount in percent from the investment capital that will be frozen as a warranty provision.
Potential Loss per Contract – the amount in percent or money per contract from the investment capital that will be frozen for the potential loss. This parameter can be set individually for the trade symbol straight from the script and can be changed dynamically upon the calculation:

Example:
protected override void CalcBar(){
 // strategy logic 
 Portfolio.SetMaxPotentialLossPerContract(Bars.High[0] - Bars.Low[0]);

The parameter of the SetMaxPotentialLossPerContract function can be expressed either in money or in percent depending upon how it was set for the portfolio in the settings of the Portfolio Settings tab. Please note that this parameter, once set in the SetMaxPotentialLossPerContract function, will be applied to the following calculation of this symbol until it is changed again in the SetMaxPotentialLossPerContract function. If the function is not called at all, then the Potential Loss per Contract set in the Portfolio Settings is used.

In the same interface, IPortfolioPerformance, and many other useful properties and functions can be found. With the help of these properties and functions you can learn the current condition of the whole portfolio and evaluate risks from the script, CalcMaxPotentialLossForEntry (int side, int contracts, double price).

Example:

We have 2 symbols in the portfolio. InitialCapital = $100, 000
Exposure = 50%
MaxCapitalRiskPerPosition = 30%
Margin = 10%
PotentialLoss = $1 per contract
Commission =$10 per order. Slippage =$0.

At the first symbol (BigPointValue = 10) the position was opened with 2 orders:

  • Buy 100 contracts at $26.34. Position$ = $26.34*100*10 = $26.340 .00. Let’s count the amount of money to be frozen for margin provision and potential loss: FreezeMargin = $26,340.00 * (10%/100) = $2,634.00; FreezePotLoss = $1* 100 = $100
  • Buy 200 contracts for $24.12 Position$ = $24.12*200*10 = $48,240.00 Frozen: FreezeMargin = $48,240.00 * (10%/100) =$4,824.00; FreezePotLoss = $1.00 * 200 = $200.00

In total, the buying power decreased by: $2,634.00 + $100.00 + $4,824.00+ $200+ $1.00 (Commission entry1) + $1 (Commission entry2) = $7.760.00

At the second symbol (BigPointValue = 1) the position was opened with 1 order:

  • SellShort 100 contracts for $112.40. Position$ = $112.40*100*1 = $11,240.00Frozen: FreezeMargin = $11,240.00* (10%/100) = $1,124.00; FreezePotLoss =$ 1.00* 100 = $100.00

In total, the buying power is decreased more by: $1.124.00+ $100.00 + $1.00 (Commission) = $1,225.00

Altogether, the buying power is decreased by: PortfolioFreeze = $7,760 .00+ $1,225.00= $8,985.00

The capital at the moment of calculation = InitialCapital+NetProfit+OpentProfit = $100, 000.00 + $400.00 - $100.00= $100, 300.00.
Because part of the money is frozen for the warranty provision and for the potential loss and considering the maximum risk (Exposure %), the available funds in the portfolio (buying power) are: $100 ,300.00*(50/100) - PortfolioFreeze = $100 ,300.00*0.5 - $8, 985.00= $41, 165.00
After the calculation of all the strategy signals in the portfolio, for each strategy (the line in the portfolio) there is its own orders collection (which is the same as the strategy calculation on the chart). Then, for each strategy where there are entries the priority is calculated (expression), which can be specified in the signal through the PortfolioEntriesPriority property.

Example:
protected override void CalcBar(){
	PortfolioEntriesPriority = new Lambda<double>(Math.Abs(Bars.Close - Bars.Close[1])/Bars.Close[1]);
}

If the expression is not set the priority for the strategy is calculated as 0 and the strategies are sorted according to their priority. That is, if two strategies have the same priority, the strategy that is higher in the list will be calculated first and the number of contracts for the entry orders (Buy, SellShort) is calculated according to the Money Management parameters and the current portfolio condition:


For example, on the first symbol, we calculated PortfolioEntriesPriority = 0,023; on the second, PortfolioEntriesPriority = 0,027 It means that, at the beginning the entries from the second signal are calculated as follows: On the second symbol, we try to reverse from the short position, 100 contracts, into the long position for 300 contracts, using the order buy 300 at $110.50:

As the order is a reverse, the short position will be completely closed at 100 contracts and the long position will be opened at 300 contracts.

The previous position will be closed at the price of $110.50 at 100 contracts. But at the order execution the commission charged was $1.00.

So, potentially at Equity in the portfolio, the value will change at the commission of $1.00.

The frozen funds for Short position will be released and for the long position entry we will have: ($100, 300.00 - $1.00)*(30%/100) = $30,089.70 For every bought contract will be frozen:

For warranty provision: $110.5 0* BigPointValue = $110.50 *1 = $110.50

For potential loss: $1.

Overall, for each contract at the price of $110.50 the following will be frozen: $110.50+1.00$ = $111.50.

Maximum number of contracts that can be purchase at $110.50: $30,089.70 / $111.50 = 269 contracts.

So, the entry will be cut from 300 contracts to 269 and the final order will be executed at 100+269 = 369 contracts, instead of 400.

Additionally, in case of that order execution, for the warranty provision and potential loss will be frozen: Position$ = $110.50*269 *1 = $29,724.50 Frozen: FreezeMargin = $29,724.5 0* (10%/100) =$29,72.45; FreezePotLoss = $1.00* 269.00 = $269.00 and commission charged $1.00.

Totally, in case of this order execution, on the second symbol, we will risk: $2972.45+$269.00 + $1.00= $3242.45. The maximum risk is 50%(Exposure%) from that amount for the portfolio: $100, 300.00 * (50%/100) = $50, 150.00;

From it, $7,760.00 are already frozen from the first symbol and on the second symbol $3242.45 will be frozen.

Available capital for the portfolio: $50,150.00 - $7,760.00 - $3,242.45 = $39, 147.55

Now, the entries on the first symbol will be calculated: The maximum capital for this symbol according to the settings is: MaxCapitalRiskPerPosition = 30%:

$100, 300.00 * (30/100)- $7,760 .00= $22, 330.00 For every bought contract will be frozen: For warranty provision: $25.00 * BigPointValue = $25.00 *10 = $250.00 For the potential loss: $1.00

Also, the commission charged at the order execution is $1.00. For each contract at the price of $250.00 will be frozen - $250.00+$1.00 = $251.00. We can buy more at price the $25.00, considering the limit per position: (min($39,147.55, $22,330.00)-$1.00(commission))/$251.00 = $22, 329.00 / $251.00 = 88 contracts. The final order, instead of buy for 100 contracts, will be buy for 88 contracts. If there are no funds available for the entry then this entry will not be executed at the current calculation. The funds can become available in case of partial or complete position(s) close of the portfolio.Trading with borrowed funds in cannot be executed in the portfolio.