Changes

Jump to navigation Jump to search

Portfolio Trader Strategy Examples

6,058 bytes added, 13:47, 29 September 2014
no edit summary
Strategies for Short entry are processed likewise.
== Spread Trading Strategy ==
 
=== Strategy Description ===
 
Spread trading is a type of trading where instruments, divided into pairs, trade in opposite directions. This type of trading occurs when a Long Position is opened for one instrument, while another is opened simultaneously in the opposite direction (Short). Both of these positions open and close synchronously.
 
Here is an example. A portfolio has two pairs of instruments: QQQ vs SPY and KO vs PEP.
 
The strategy will enter into position when the spread deviation exceeds a Standard Deviation value for the last 20 bars. The Second Pair of Instruments enters synchronously into a position opposite the Main Instruments (First Pair).
 
=== Strategy Development ===
 
==== Portfolio_SpreadTradingSystem.Master Signal ====
 
This signal is calculated based on an instrument’s data series. It contains opening and closing logic positions:
 
<syntaxhighlight>inputs: Ratio(c / c data2), Length(10), PercentOfEquity(10);
var: AvgRatio(0), StdDevRatio(0);
var: intrabarpersist cur_pos(0);
var: Contracts_(0);
Contracts_ = Portfolio_Equity * PercentOfEquity / 100;
if 1 < currentbar then begin
if AvgRatio + StdDevRatio < Ratio then begin// short data1, long data2
if -1 <> cur_pos then begin
sellshort Contracts_ contracts this bar at c;
cur_pos = -1;
end;
end else if AvgRatio - StdDevRatio > Ratio then begin// buy data1, short data2
if 1 <> cur_pos then begin
buy Contracts_ contracts this bar at c;
cur_pos = 1;
end;
end else begin
cur_pos = 0;
sell this bar c;
buytocover this bar c;
end;
end;
AvgRatio = XAverage(Ratio, Length);
StdDevRatio = StdDev(Ratio, Length);</syntaxhighlight>
 
Other calculations require the strategy to be applied to a portfolio of symbols, so we need to check and see if that’s the case:
 
<syntaxhighlight>if 1 = getappinfo(aiisportfoliomode) then begin
// code
end;</syntaxhighlight>
 
For the basic strategy, we need to return the strategy index of the second instrument and check if it has been applied:
 
<syntaxhighlight>var: slave_idx(pmms_strategies_get_by_symbol_name(symbolname data2));
once if 0 > slave_idx then
raiseruntimeerror(text("specified slave trader on instrument ", doublequote, symbolname data2, doublequote, " not found"));</syntaxhighlight>
 
To synchronize the capital invested into positions for both instruments, we need to send the price of the current position of the main instrument to the pair strategy:
 
<syntaxhighlight>value22 = absvalue(cur_pos*Contracts_) * c * bigpointvalue;
if 0 < value22 then
value22 = pmms_to_portfolio_currency(value22);
pmms_set_strategy_named_num(slave_idx, "MPMoney", -cur_pos * value22);</syntaxhighlight>
 
==== Portfolio_SpreadTradingSystem.Slave Signal ====
 
This signal '''Portfolio_SpreadTradingSystem.Slave Signal''' is calculated for the second instrument of the pair. It monitors all entries and exits generated by the previous signal '''Portfolio_SpreadTradingSystem.Master Signal''' for the main instrument of the pair and trades in the opposite direction. Firstly, all synchronization is done when '''MPMoney''' variable returned by master strategy changes.
 
<syntaxhighlight>value1 = pmms_from_portfolio_currency( pmm_get_my_named_num("MPMoney") );</syntaxhighlight>
 
We extract this variable and convert it from portfolio currency into instrument currency. Then, based on its value, we calculate the number of contracts for potential entry positions:
 
<syntaxhighlight>value33 = c;
if marketposition <> 0 then
value33 = entryprice;
master_mp = IntPortion( value1 / ( value33 * bigpointvalue) );</syntaxhighlight>
 
The instrument’s current position:
 
<syntaxhighlight>my_mp = currentcontracts*marketposition;</syntaxhighlight>
 
Now we will check to see if its position is unsynchronized. If that’s the case, then we will synchronize it with the main strategy:
 
<syntaxhighlight>if sign(my_mp) <> sign(master_mp) then begin
...
end;</syntaxhighlight>
 
We’ll check if the main instrument’s position has closed:
 
<syntaxhighlight>if 0 = value1 then begin // need to close position
if my_mp > 0 then
sell all contracts this bar c
else
buytocover all contracts this bar c;
#return;
end;</syntaxhighlight>
 
If it has closed, we’ll close the position for the second instrument as well. If the main instrument has an open position, then we will determine the position’s direction for the second instrument:
 
<syntaxhighlight>if 0 < value1 then begin // we must buy
if 0 < value1 then begin // we must buy</syntaxhighlight>
 
Value1 > 0 means that to synchronize the positions we should buy. There can be two cases:
 
# The current flat or short position should change to long, i.e., the master strategy has reversed its position or has entered a long position from the flat state.
# The current position is already long which means that the first instrument partially closed its short position, signifying that we need to partially close the second instrument’s position.
 
<syntaxhighlight>if Sign(master_mp) <> Sign(my_mp) then
buy absvalue(master_mp) contracts this bar c
else
buytocover value1 contracts this bar c;</syntaxhighlight>
 
In the opposite case:
 
<syntaxhighlight>end else begin
if Sign(master_mp) <> Sign(my_mp) then
sell short absvalue(master_mp) contracts this bar c
else
sell absvalue(value1) contracts this bar c;
end;</syntaxhighlight>
 
Value1 < 0 means that we need to sell to synchronize the positions; there also can be two cases:
 
# The current flat or long position should change to short, i.e., the master strategy has reversed its position or has entered a short position from the flat state.
# The current position is already short which means that the first instrument partially closed its long position, signifying that we need to partially close the second instrument’s position.
 
=== Appendix ===
 
Portfolio signals scripts are added to MultiCharts and MultiCharts64 by default.
 
== Rank Strategy ==
 
This strategy can be considered a modification of the '''[[#Rotation Strategy|Rotation Strategy]]'''.
 
This strategy was suggested by '''Angelos Diamantis'''.

Navigation menu