Click here to Login

                                                   dynamic position sizing based on stocks selected by the buy rules


Andrew Thomas
2020-07-03 16:49:17


In a Trading System Strategy I would like to be able to set the number of positions to the number of selected equities based on the 'buy' rules. For example, if on a particular day the 'buy' rules select 5 stocks, I would like the number of positions to be 5 with all $s sent to those 5 stocks equally, if on another day the 'buy' rules select 38 stocks, I would like the number of positions to be set to 38 with all $s sent to those 38 positions equally. It seems like SetSimPosSize() or SetSimSetting(_NbPositions,) are the best choices for control of this variable, but they seem to require fore knowledge of how you want to divide up your investment either by %, shares, $ per position. Is there a way to sum the number of stocks matching the buy rules such that I could pass that variable to the SetSimPosSize function?


2020-07-05 12:18:23


When backtesting QS language formula, the engine generates buy and sell orders and doesn't know how many buy or sell signals there is for each trading bar. To do what you want, you need to create a custom money management script that will catch the "OnNewPosition" event and adjust the size of each position depending on the number of buy signals for that bar.

Andrew Thomas
2020-07-05 13:51:13


Thanks for the quick response, I ended up using code from the "Custom Rebalancing" script. The following in the "OnEndPeriod" event seems to work at giving a dynamic number of positions with maximum % investment:

// Update new signals position
_TradingOrder[] orders = Orders.GetPendingBuyOrders();
double cash = Portfolio.GetAvailableCash("long");
double amount = cash / orders.Length;
for(int i=0;i<orders.Length;i++)
TimeSeries close = Data.GetPriceSeries(orders[i].Symbol, "close");
int size = (int)Math.Floor(amount / close[0]);
orders[i].NbShares = size;

Let me know if the above is bonkers.


2020-07-06 12:28:15


Yes this is correct. Assuming you sell all positions on each rebalance, the new rebalance will buy equal amount for all available buy signals.

No more messages


No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"


Trading Items
Trading Strategy based on the Number of Buy Signals
Trading Rules based on the Relative Strength Index - RSI Indicato...
The Best trading rules for the ADX technical indicator
Trading System Based on the CloseHL Percent Rank Oscillator
Trading System based on the Previous Week SPY Return

How-to Lessons
How to create a volatility-based Stop - Dynamic stop based on the...
How to quickly select stocks based on the last value of a databas...
How to create trading rules based on Put and Call volume data
How to quickly download the most recent EOD data for your stocks
How to plot the ratio of two stocks

Related Forum Threads
Enhanced Dynamic Position Sizing money mgmt - some issue
NaN error in Dynamic Position Sizing MM object
Dynamic Position Sizing
Simulation/Back testing - Default position sizing method
Dynamic Position

Blog Posts
Buy the best/top rated stocks or how to create powerful rank base...
How to create buy and sell trading rules based on News Data
Dynamic Position Sizing in your Trading System
Trading System: Buy Stocks based on their Sharpe Ratio Rank
Trading System: Buy stocks with the highest Sharpe ratio

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2023
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.