# Portfolio Strategy on Equity

Questions about MultiCharts and user contributed studies. al68
Posts: 11
Joined: 25 Mar 2016

### Portfolio Strategy on Equity

Hi, I’m trying to develop a Portfolio Management Strategy which evaluates the Equity curve of the underlying TradingSystem and allows its orders only when the Equity curve is over its average.
So if the Equity > its Avg then trade, if not don’t trade. Of course I’m talking about using a virtual Equity curve i.e. the Equity curve the system would generate if it was allowed to operate always.
The strategy is applied on 1 instrument only(for the moment).
These my first attempts (calc 1 to 6) which don't work properly.
Any suggestion?

Code: Select all

```Inputs: Calc(1),PortfolioPctStop(1); Vars: Eq(0),EqAvg(0); var: need_convert(false), equity(0); Eq=NetProfit{+OpenPositionProfit}{+InitalCapital}; EqAvg=Average(Eq,20); If Calc=1 and BarNumber>20 and MP=0 then begin If Eq>=EqAvg and Eq<EqAvg then pmms_strategies_pause_all(){pmms_strategy_pause(idx)}; If Eq<EqAvg and Eq>=EqAvg then pmms_strategies_resume_all(){pmms_strategy_resume(idx)}; End; If Calc=2 and BarNumber>20 then begin If Eq>=EqAvg and Eq<EqAvg then begin pmms_strategies_pause_all(){pmms_strategy_pause(idx)}; pmms_strategy_close_position(0); End; If Eq<EqAvg and Eq>=EqAvg then pmms_strategies_resume_all(){pmms_strategy_resume(idx)}; End; If Calc=3 and BarNumber>20 then begin {pmm_set_my_named_num(var_name, val) ?set the numerical value ?val?for var_name variable} If Eq>=EqAvg and Eq<EqAvg then begin pmms_strategy_pause(0); pmms_strategy_close_position(0); End; If Eq<EqAvg and Eq>=EqAvg then pmms_strategy_resume(0); End; If Calc=4 then begin equity = Portfolio_Equity; if (SymbolCurrencyCode <> portfolio_CurrencyCode) then equity = convert_currency( datetime, portfolio_CurrencyCode, SymbolCurrencyCode, equity); setstopposition; setstoploss(0.01 * equity); End; If Calc=5 then begin equity = Portfolio_Equity; SetStopPosition; SetStopLoss((PortfolioPctStop/100) * equity) ; End; If Calc=6 then begin equity = NetProfit; SetStopPosition; SetStopLoss((PortfolioPctStop/100) * equity) ; End;```

TJ
Posts: 7158
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2042 times

### Re: Portfolio Strategy on Equity

Please describe what do you mean by "don't work properly."

See post #4
[FAQ] How to get help
viewtopic.php?f=16&t=11713 al68
Posts: 11
Joined: 25 Mar 2016

### Re: Portfolio Strategy on Equity

I forgot: i use MultiCharts 9.1 build 12410 (the latest) tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 80 times
Contact:

### Re: Portfolio Strategy on Equity

You have "calc" as an input not a variable, so right there it seems like this is not automated but something you would be manually turning on or off. You also have barnumber > 20 in most / all of your conditional statements. It will likely always be > 20 unless you are trying to reset barnumber. al68
Posts: 11
Joined: 25 Mar 2016

### Re: Portfolio Strategy on Equity

As I wrote in my comments these are my attempts (1 to 6) of writing a PortfolioStrategy i.e. six different ways of calculating what I want , but none of them manage to switch off and on (pause an resume) the TradingSystem (which is not included) as the Equity line crosses under and over its Average.
So the 6 calculations are just 6 different ways of coding the same thing. al68
Posts: 11
Joined: 25 Mar 2016

### Re: Portfolio Strategy on Equity

My idea consisted of using a TradingSystem with an imperfect equity line (as shown in the image below) and add an EquityManagement strategy which pauses the TradingSystem when the equity line crosses under its average, and resumes the TradingSystem when the equity line crosses over its average.
I wanted the EquityManagement to be applied directly to the single instrument (as shown in the enclosed image).
I coded six (6) different ways of calculating the same "pause and resume" but none of them managed to modify the equity line i.e. to make it more regular.
Happy Easter to everybody
P.S. hope I will manage to post the image...
Attachments EquityMngmnt4.jpg (236.81 KiB) Viewed 1382 times

TJ
Posts: 7158
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2042 times

### Re: Portfolio Strategy on Equity

My idea consisted of using a TradingSystem with an imperfect equity line (as shown in the image below) and add an EquityManagement strategy which pauses the TradingSystem when the equity line crosses under its average, and resumes the TradingSystem when the equity line crosses over its average.
I wanted the EquityManagement to be applied directly to the single instrument (as shown in the enclosed image).
I coded six (6) different ways of calculating the same "pause and resume" but none of them managed to modify the equity line i.e. to make it more regular.
Happy Easter to everybody
P.S. hope I will manage to post the image...
Do you know the value of Eq and EqAvg at the end of each bar? al68
Posts: 11
Joined: 25 Mar 2016

### Re: Portfolio Strategy on Equity

I'm afraid the Eq value is wrong.
I should use the "I_ClosedEquity" function.
I will let you know. al68
Posts: 11
Joined: 25 Mar 2016

### Re: Portfolio Strategy on Equity

Calculations 1 and 2 of the strategy below seem to work, but I've had no time to check the results, yet.

Code: Select all

```Inputs: Calc(1),PortfolioPctStop(1); Vars: Eq(0),EqAvg(0); var: need_convert(false), equity(0); inputs: StopLossPcntsOfPortfolio(0.1), ProfitTargetPcntsOfPortfolio(0.6); variable: value(0); vars: eqaverage(0); Eq=I_ClosedEquity{I_ClosedEquity}{NetProfit}{+OpenPositionProfit}{+InitalCapital}; EqAvg=Average(Eq,10); If Calc=1 and BarNumber>20 and MP=0 then begin If Eq>=EqAvg and Eq<EqAvg then pmms_strategies_pause_all(){pmms_strategy_pause(idx)}; If Eq<EqAvg and Eq>=EqAvg then pmms_strategies_resume_all(){pmms_strategy_resume(idx)}; End; If Calc=2 and BarNumber>20 then begin If Eq>=EqAvg and Eq<EqAvg then begin pmms_strategies_pause_all(){pmms_strategy_pause(idx)}; pmms_strategy_close_position(0); End; If Eq<EqAvg and Eq>=EqAvg then pmms_strategies_resume_all(){pmms_strategy_resume(idx)}; End; ```
The first chart (Equity0charts) shows the EquityLine of the system without any EquityManagement.
The second chart (Equity1chart) shows the EquityLine of the system + EquityManagement (calculation2 precisely).
As anybody can see, there's a difference, even if it's not a huge difference.
Now I'm gonna try to undestand if the instruction "pmms_strategies_pause_all()" closes the open positions or just pauses the TradingSystem an leaves the position open...
Attachments Equity0chart.jpg (164.56 KiB) Viewed 1375 times al68
Posts: 11
Joined: 25 Mar 2016

### Re: Portfolio Strategy on Equity

Second chart
Attachments Equity2chart.jpg (160.97 KiB) Viewed 1380 times Equity2chart.jpg (160.97 KiB) Viewed 1380 times al68
Posts: 11
Joined: 25 Mar 2016

### Re: Portfolio Strategy on Equity

This is my new code but, as you can see from the enclosed image, it doesn't allow the system to trade more than 20 trades.
Any suggestion ?

Code: Select all

``` Vars: Eq(0),EqAvg(0),EqJ(0); var: need_convert(false), equity(0); Vars: maxeq(0),mineq(0),numWins(0),numLosses(0),numTrades(0); Eq=I_ClosedEquity; If Eq<>Eq then begin Value20=Value19; Value19=Value18; Value18=Value17; Value17=Value16; Value16=Value15; Value15=Value14; Value14=Value13; Value13=Value12; Value12=Value11; Value11=Value10; Value10=Value9; Value9=Value8; Value8=Value7; Value7=Value6; Value6=Value5; Value5=Value4; Value4=Value3; Value3=Value2; Value2=Value1; Value1=Eq; If Value20<>0 then begin EqAvg=AvgList(Value1,Value2,Value3,Value4,Value5,Value6,Value7,Value8,Value9,Value10, Value11,Value12,Value13,Value14,Value15,Value16,Value17,Value18,Value19,Value20); MinEq=MinList({Value1,}Value2,Value3,Value4,Value5,Value6,Value7,Value8,Value9,Value10, Value11,Value12,Value13,Value14,Value15,Value16,Value17,Value18,Value19,Value20); MaxEq=MinList({Value1,}Value2,Value3,Value4,Value5,Value6,Value7,Value8,Value9,Value10, Value11,Value12,Value13,Value14,Value15,Value16,Value17,Value18,Value19,Value20); numTrades=numTrades+1; If Eq>Eq then numWins=numWins+1; If Eq<Eq then numLosses=numLosses+1; End; End; If Eq<EqAvg and Value20<>0 then begin pmms_strategy_pause(0); pmms_strategy_close_position(0); End; If Eq>EqAvg then pmms_strategy_resume(0); ```
Attachments NewCalculation.jpg (198.23 KiB) Viewed 1378 times

TJ
Posts: 7158
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 988 times
Been thanked: 2042 times

### Re: Portfolio Strategy on Equity

This is my new code but, as you can see from the enclosed image, it doesn't allow the system to trade more than 20 trades.
Any suggestion ?

Code: Select all

```:: If Eq<EqAvg and Value20<>0 then begin pmms_strategy_pause(0); pmms_strategy_close_position(0); End; :: ```

how do you get Eq to become larger than EqAvg again? al68
Posts: 11
Joined: 25 Mar 2016

### Re: Portfolio Strategy on Equity

Correct !
But the Portfolio strategies (with pmms functions) shouldn't work as a firewall which allow or not the orders to be executed? If they're firewalls the strategy should carry on generating orders and so calculating the equity.
Maybe should I put the code in the "Money Management Signal" window ?
If I do so, the Portfolio strategy doesn't affect the total results at all (i.e. it's like not applicating any Portfolio strategy).
Attachments PortfolioStrategy.jpg (256.72 KiB) Viewed 1377 times

fbertram
Posts: 166
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 36 times
Been thanked: 73 times
Contact:

### Re: Portfolio Strategy on Equity

What you need, is an 'observer', which allows you to continue monitoring the trading performance. There are multiple solutions to this:

1) you write a bunch of code, so that your strategy knows what the trades are it would have taken... if the money-management signal had allowed entries. This is pretty painful.

2) you don't completely shut off the disabled strategies, but keep them trading at 1 share each. Now you can use a bit of code like this to observe trading performance:

Code: Select all

``` variables: Performance (0), intrabarpersist LastPositionsAgo (1), thePos (0); while(LastPositionsAgo < MaxPositionsAgo) begin thePos = MaxPositionsAgo - LastPositionsAgo; LastPositionsAgo += 1; Performance += ExitPrice(thePos) - EntryPrice(thePos) - 2 * commission; end; ```
Now, as you don't want these 1 share trades executed, you can disable that piece of code when you reach LastBarOnChart.

There are a few more thoughts here:
http://www.bertram-family.com/felix/pro ... ghost.html

Cheers, Felix