Back

Unexpected backtest behavior: AbsoluteHoldingsCost won't change after calling SetHoldings

I believe my question addressed more to developers of Lean engine. 

I have the following concern. My algo is working on multiple instruments - 25 instances - and each produces signals based on same logic, same indicators. As there many signals from various instruments at various time I thought about how to allocate the available portfolio funds effectively among the securities. And wrote a simple method to manage the available funds. 

1) Every time there is a new signal initiated by one of 25 securities I am trying to SetHolding for 0.3m percent of total portofolio value for that ticker. So I first check if there is enough money to do so by calling:

if( algorithm.Portfolio.MarginRemaining/algorithm.Portfolio.TotalPortfolioValue )  > 0.3 * _initialMarginRequirement

If there is enough maring I buy as much as I intended to:

SetHoldings(symbol, 0.3m).

2) if there is not enough maring available, I am re-setting the non-zero holdings to release the funds: so I go through all such holdings : 

foreach (var s in symbols)   

and call SetHoldings(s, someCalculatedPercentage) inside the loop, where someCalculatedPercentage is some value that will help to free part of funds.

I also count number of shares that have been sold by SetHoldings operation by calling additionally in foreach loop:  algorithm.CalculateOrderQuantity(s, percentage);  and write these values in a log.

3) So here is the problem. The normal behavior is after calling SetHoldings for the symbols in a loop. I can see the amounts of shares being reduced, they do always have a non zero value:

2017-01-17 12:30:00 Qnt: INCY -39 |PTLA -123 |TSRO -48 |SAGE -121 |CLVS -211 |UTHR -14 |

The same way the value of GetMaintenanceMargin (security) , which I also calculate in a loop and write to the log afterwards, where security is the one corresponding to the symbol, alters accordingly, the way like this (was -> became) :

2017-01-17 12:30:00 BP: INCY : 39194 -> 36898 | PTLA : 39607 -> 37990 | TSRO : 37490 -> 33948 | SAGE : 37821 -> 34598 | CLVS : 37473 -> 32281 | UTHR : 37715 -> 36651 | 

Except for two cases that make me stupor:

2017-09-12 16:00:00 Qnt: SRPT -515 |FOLD -1033 |SUPN -336 |ILMN -98 |GILD -224 |BIIB -63 |IMMU -1254 |ARRY -413 |
2017-09-12 16:00:00 BP: SRPT : 58835 -> 58835 | FOLD : 58904 -> 58904 | SUPN : 54723 -> 54723 | ILMN : 55530 -> 55530 | GILD : 56853 -> 56853 | BIIB : 58149 -> 58149 | IMMU : 55808 -> 55808 | ARRY : 54432 -> 54432 | 
 

2018-01-02 16:00:00 Qnt: SAGE -560 |GWPH -128 |ASND 480 |CRSP -1893 |NKTR -399 |IMMU -2596 |UTHR -18 |
2018-01-02 16:00:00 BP: SAGE : 71417 -> 71417 | GWPH : 90895 -> 90895 | ASND : 66972 -> 66972 | CRSP : 99666 -> 99666 | NKTR : 65055 -> 65055 | IMMU : 88890 -> 88890 | UTHR : 73511 -> 73511 | 
 

These two log records made me dig into the source code of the Lean engine for last three days, which I find infinitely useful pursuit, but leaving without a hint on such an unexpected behavior. So I wondering if guys developers could help me to find the roots of my issue, please? 

Thanks in advance!


            

Update Backtest







Here is a code snippet

//list will store symbols for which we have open positions
List<Symbol> symbols = new List<Symbol>();
var quantity = HoldingsCount (ref symbols);
string buyingPowerStr = "BP: ";

foreach (var s in symbols)
{
buyingPowerStr += System.String.Format("{0} : {1} ->", s, (int)GetReservedBuyingPowerForPosition(algorithm.Securities[s]));
algorithm.MarketOrder(s, - 5);
buyingPowerStr += System.String.Format(" {0} | ", (int)GetReservedBuyingPowerForPosition(algorithm.Securities[s]));
}

algorithm.Log(buyingPowerStr);

And in two of all cases I would get results like this:

2017-09-12 16:00:00 BP: SRPT : 67335 -> 67335 | FOLD : 67404 -> 67404 | SUPN : 62624 -> 62624 | ILMN : 63605 -> 63605 | GILD : 65078 -> 65078 | BIIB : 66526 -> 66526 | IMMU : 63861 -> 63861 | ARRY : 62286 -> 62286 | 

Can this be at all possible that after I sell stockts ReservedBuyingPowers remains the same? 

0

hmm does it return the same value after a delay?

sending an order and checking a value after that ...... ??

0

Michael Manus thank you for suggestion. I tried making the algorithm to pause for 7 seconds before writing the derivable value on the days of such behavior. But with no help. 

The algo behaves normally all along except for two cases only when HoldingsCost doesn't change:

Normally:
2017-09-18 12:00:00 BP: NVCR : 46828 -> 43505 | BGNE : 46468 -> 39623 | SRPT : 43007 -> 38863 | LGND : 47654 -> 43441 | FOLD : 47490 -> 42604 | SUPN : 45162 -> 39522 | GILD : 45903 -> 42111 | ILMN : 44015 -> 40521 | BIIB : 46405 -> 43021 | IMMU : 47750 -> 43343 | ARRY : 44938 -> 40058 | 

Wrong:
2018-01-02 16:00:00 Qnt: SAGE -640 |GWPH -146 |ASND 549 |CRSP -2166 |NKTR -457 |IMMU -2969 |UTHR -21 |
2018-01-02 16:00:00 BP: SAGE : 81693 -> 81693 | GWPH : 103977 -> 103977 | ASND : 76632 -> 76632 | CRSP : 114023 -> 114023 | NKTR : 74427 -> 74427 | IMMU : 101694 -> 101694 | UTHR : 84128 -> 84128 | 
 

0

Maybe someone can tell what library files are responsible for the update of  SecurityHolding values after an order execution?? 

0

or i am understanding something wrong and there sould be used: margin remaining or something different.

the value you are searching for which does not change you could get by doing the math for yourself.

try to use Portfolio.MarginRemaining

getbuyingpower,

getmarginremaining

............

the thing is: if i go to ALGO LAB and press on the API tab.

than i try to search for your ReservedBuyingPowers there is no info. nothing found?

use MarginRemaining and others maybe?

0

oh wait its your stuff haha i got lost a little bit because of the logs. hm

but still can you please log MarginRemaining and other stuff from the portfolio class and get around somehow this problem.

0

phu for me it is not easy to see without the log which value does not change. as you have already this logs it would have been cool to see the portfolio outputs after selling/adjusting so everyone can see where the problem is:

                Portfolio["aapl"].AbsoluteHoldingsCost
                Portfolio["aapl"].AbsoluteHoldingsValue
                Portfolio["aapl"].HoldingsCost
                Portfolio["aapl"].HoldingsValue
                Portfolio["aapl"].UnleveredAbsoluteHoldingsCost
                Portfolio["aapl"].UnleveredHoldingsCost

.......

0

  Michael,  I have been using alternitive name for GetMaintenanceMargin() - just in a bottom of a file 

https://github.com/QuantConnect/Lean/blob/master/Common/Securities/SecurityMarginModel.cs#L362

line 362 - there is an alias definition 

public decimal GetReservedBuyingPowerForPosition(Security security)
{
return GetMaintenanceMargin(security);
}

I have been trying to log Portfolio["aapl"].AbsoluteHoldingsCost as you suggested for each security reduced in holdings. but with no success unfrotuantely: 

Still getting no altered values in 12 sept 2017:

2017-09-11 10:00:00 Qnt: FOLD -899 |SUPN -474 |GILD -205 |ILMN -76 |BIIB -61 |IMMU -1275 |ARRY -3054 |
2017-09-11 10:00:00 BP: FOLD : 147363 -> 134795 | SUPN : 147147 -> 125248 | GILD : 146608 -> 130076 | ILMN : 142364 -> 127210 | BIIB : 152703 -> 133052 | IMMU : 142398 -> 127710 | ARRY : 156995 -> 124561 | 


2017-09-12 16:00:00 Qnt: SRPT -589 |FOLD -1182 |SUPN -385 |GILD -256 |ILMN -112 |BIIB -72 |IMMU -1434 |ARRY -473 |
2017-09-12 16:00:00 BP: SRPT : 134628 -> 134628 | FOLD : 134795 -> 134795 | SUPN : 125248 -> 125248 | GILD : 130076 -> 130076 | ILMN : 127210 -> 127210 | BIIB : 133052 -> 133052 | IMMU : 127710 -> 127710 | ARRY : 124561 -> 124561 | 
 

Perhaps the point is how the Portfolio values are updated after the modelled execution in backtest mode? Wondering which Lean library file might be reponsible for this? 

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed