NexusFi: Find Your Edge


Home Menu

 





Limit nr of positions in portfolio!


Discussion in MultiCharts

Updated
    1. trending_up 2,738 views
    2. thumb_up 4 thanks given
    3. group 2 followers
    1. forum 8 posts
    2. attach_file 0 attachments




 
Search this Thread

Limit nr of positions in portfolio!

  #1 (permalink)
 Gabriel123 
Stockholm
 
Experience: Intermediate
Platform: ProRealTime
Trading: DAX
Posts: 40 since Jul 2015
Thanks Given: 6
Thanks Received: 5

Hi!

I am looking for a very simple Money Management script that limits the number of open positions simultaneously in a portfolio to max 10.

This is for a swing trading portfolio of stocks, meaning the resolution is daily bars, not intraday.

Let's just say the strategy code is:
_________________

If RSI(c,2) < 5 AND C > Average(C,200)
then buy next bar at open;

If RSI(c,2) > 65
then sell next bar at open;
_________________

What would the money management signal be and what additions need to be done in the strategy signal?

Thanks in advance!

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
About a successful futures trader who didn´t know anyth …
Psychology and Money Management
Cheap historycal L1 data for stocks
Stocks and ETFs
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
What broker to use for trading palladium futures
Commodities
 
  #2 (permalink)
 Gabriel123 
Stockholm
 
Experience: Intermediate
Platform: ProRealTime
Trading: DAX
Posts: 40 since Jul 2015
Thanks Given: 6
Thanks Received: 5

I read this post:
But it doesn't work at all for me.

I have also tested to include this code in my strategy signal as a condition:
_____________
Portfolio_CurrentEntries < 10
_____________

Unfortunately when several signals appear at the same day the strategy applies them all (if it is less than 10 signals in market the day before) - which means that in reality it can be a lot more positions than 10 iin the market simultaneously.

Started this thread Reply With Quote
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629



Gabriel123 View Post
Unfortunately when several signals appear at the same day the strategy applies them all (if it is less than 10 signals in market the day before) - which means that in reality it can be a lot more positions than 10 iin the market simultaneously.

Gabriel123,

you will likely have to evaluate the number of signals that want to enter at the current date within the Money Management signal and block those that you don't want to get filled. Otherwise there wouldn't be a way to stop the entries anymore.
Something along the line of the below should work, but it will require some programming:
1. When the entry conditions in your signal are true, send a value (+1 for example) to the money management signal.
2. If the conditions are not true, send a value of 0 for example.
3. In the money management signal you'd block all entries first. Then you
can loop over all symbols in the portfolio and check how many individual signals want to enter.

One thing to decide here for you would be how to handle situations where more signals want to enter than the number of open positions you want to allow i.e. do you deal with that on a first come first serve basis or do you want to apply a ranking. If it's a ranking, the ranking would have to be computed in the individual signal and the result send to the money management, too.

4. Depending on the above method you choose, you'd either allow the entries for the symbols until you allowed number of positions is reached. If you apply a ranking you'd gather the ranking values for all symbols that want to enter in step 3.. Once you have all, sort them and let only the top (or bottom or what ever your ranking is) X symbols pass. Where X is the number of positions that can still be filled i.e. Max allowed positions - current open positions.

When I say send to the money management this involves the special portfolio reserved words that start with pmm_ and pmms_ and the help files provide a good start on these. You might also want to explore the three build in money management signals together with the PDF that explains them in depth. This will help a lot.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #4 (permalink)
 Gabriel123 
Stockholm
 
Experience: Intermediate
Platform: ProRealTime
Trading: DAX
Posts: 40 since Jul 2015
Thanks Given: 6
Thanks Received: 5

Thanks a lot for your reply, ABCTG.

I have been able to come up with a signal base and MM signal that limits the number of position simultaneously to 1. But not to 10. Most of the help I got from this thread:

Signal base:
 
Code
                            
input:arg(true);

If 
RSI(c,2) < AND Average(C,200then begin

if (arg truethen pmm_set_my_named_num("test",1);

buy next bar at open;

end;

If 
RSI(c,2) > 65 then begin

if (arg truethen pmm_set_my_named_num("test",0);

sell next bar at open;

end
MM signal:
 
Code
                            
var:idx(0),count(0);

array:
strategyIndexes[](0);

count 0;

    
pmms_strategies_deny_entries_all;

for 
idx 0 to pmms_strategies_count -1 begin

    
if ( pmms_get_strategy_named_num(idx,"test") = and count 0then begin

    count 
1;

    
pmms_strategy_allow_entries(idx);

    
pmms_set_strategy_named_num(idx,"test",0);

    
end;

end;

if 
pmms_strategies_in_positions_count(strategyIndexes) >= 1 then

pmms_strategies_deny_entries_all


Started this thread Reply With Quote
  #5 (permalink)
 Gabriel123 
Stockholm
 
Experience: Intermediate
Platform: ProRealTime
Trading: DAX
Posts: 40 since Jul 2015
Thanks Given: 6
Thanks Received: 5

The issue is how to get the variable to change - so that it adds "1" to the "pmm_set_my_named_num" everytime a new position enters the market and at the same time subtracts "1" from the "pmm_set_my_named_num" everytime a position closes.

How do I do that?

Thanks in advance.

Started this thread Reply With Quote
Thanked by:
  #6 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

Gabriel123,

you are welcome. By the way you can show your appreciation for a post on futures.io by clicking the "Thanks" button next to it.

As a general tip and in case you don't do that already, I'd suggest to start commenting your code. This will make your life a lot simpler down the road. Imagine going back to a code you wrote month or years ago - without comments it will take you so much longer to find your way around again. This can also help you finding errors in your code, when your comment says this part should do xyz and when you compare the code you find that it does in fact uvw.

I don't understand why you'd want to add or subtract values to a variable that you send with pmm_set... - in my opinion it would be less error prone to only have the signal code change this value and have the money management signal read those values only.

Why did you add this code snippet, that blocks all further entries when you are in at least one position?
 
Code
if pmms_strategies_in_positions_count(strategyIndexes) >= 1 then

pmms_strategies_deny_entries_all;

From your MM code it appears you might want to add the following:

1. Before you loop over all signals, check how many are in positions already. Your code already contains the array
for that, but it seems you strapped this part. You can look into the three build in Portfolio Trader examples in Multicharts, at least one of them demonstrates how to do that.

2. Now you can compute how many positions you have and arrive at a number of how many you still want to allow.

3. You will most likely need two loops here. During the first loop check the symbols that want to enter (you already do that) and check if they are already in a position (you'd need to add that part). If they want to enter and are not yet in a position, save the strategy index number in an array (which you need to create, and clear each time before you start the loop).

4. In you second array you need to loop through the array that holds the symbols that want to enter and only allow the number of positions that are still free (which is what you computed under 2.). If there are less symbols to enter in the array than allowed positions, there would be no need to filter them. If there are more symbols than "free spots" you need to come up with code to handle this situation i.e. only allow X out of the total number that wants to enter - for example either allow them on a first come, first served base or apply a ranking so you can decide which to prefer.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #7 (permalink)
 Gabriel123 
Stockholm
 
Experience: Intermediate
Platform: ProRealTime
Trading: DAX
Posts: 40 since Jul 2015
Thanks Given: 6
Thanks Received: 5

Thanks for your response ABCTG.

This was not simple.

This is my MM code at the moment:

 
Code
                            
var:
idx(0),
count(0);

array:
strategyIndexes[](0);

pmms_strategies_deny_entries_all;

variablesinLong(0);
arraysstrategiesLong[](-1);
inLong pmms_strategies_in_long_count(strategiesLong);

count 0;

    
pmms_strategies_deny_entries_all;

for 
idx 0 to pmms_strategies_count -1 begin

    
if ( pmms_get_strategy_named_num(idx,"test") = and count 0then begin

    count 
1;

    
pmms_strategy_allow_entries(idx);

    
pmms_set_strategy_named_num(idx,"test",0);

    
end;

end;

if 
pmms_strategies_in_positions_count(strategyIndexes) >= 10 then

pmms_strategies_deny_entries_all


Started this thread Reply With Quote
  #8 (permalink)
 Gabriel123 
Stockholm
 
Experience: Intermediate
Platform: ProRealTime
Trading: DAX
Posts: 40 since Jul 2015
Thanks Given: 6
Thanks Received: 5


ABCTG View Post
Gabriel123,
1. Before you loop over all signals, check how many are in positions already. Your code already contains the array
for that, but it seems you strapped this part. You can look into the three build in Portfolio Trader examples in Multicharts, at least one of them demonstrates how to do that.

The part I have added in the code since last post should do this I think.
I have checked throught all three example MM codes in Multicharts but I still have a hard time trying to create code for these steps:


ABCTG View Post
Gabriel123,
2. Now you can compute how many positions you have and arrive at a number of how many you still want to allow.

3. You will most likely need two loops here. During the first loop check the symbols that want to enter (you already do that) and check if they are already in a position (you'd need to add that part). If they want to enter and are not yet in a position, save the strategy index number in an array (which you need to create, and clear each time before you start the loop).

4. In you second array you need to loop through the array that holds the symbols that want to enter and only allow the number of positions that are still free (which is what you computed under 2.). If there are less symbols to enter in the array than allowed positions, there would be no need to filter them. If there are more symbols than "free spots" you need to come up with code to handle this situation i.e. only allow X out of the total number that wants to enter - for example either allow them on a first come, first served base or apply a ranking so you can decide which to prefer.


Started this thread Reply With Quote
  #9 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

Gabriel123,

the task you are trying to accomplish is definitely a bit more complex and depending on your level of programming experience can give you "a hard time" for sure.
You are dealing with "advanced" concepts in Multicharts here and someone without a solid foundation of the basics (speaking in general terms here and not geared towards
you, as I don't know your programming experience in Multicharts) might run into problems here easily.

Regarding the points, you already have the most part for 2. covered (although only for long positions).
 
Code
inLong = pmms_strategies_in_long_count(strategiesLong);
Doesn't the variable inLong hold the number of long positions you are in at the moment?

If that is the case and as you know you want to enter 10 positions at max (which I'd suggest to make an input), you should be able to compute how many spots are still left.

Now you can start with 3.. Create the array that holds the strategy index numbers for the symbols to enter and build
the first loop as described:

Quoting 
3. During the first loop check the symbols that want to enter (you already do that) and check if they are already in a position (you'd need to add that part). If they want to enter and are not yet in a position, save the strategy index number in an array (which you need to create, and clear each time before you start the loop).

Regards,

ABCTG

Follow me on Twitter Reply With Quote




Last Updated on August 4, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts