Click here to Login

                                                   Advanced Money Management


2011-01-18 12:37:00


I have a question regarding the events OnEndPeriod / OnNewPosition / OnClosePosition and in what order they are managed for the same bar.
Suppose we use a daily timeframe, so 1 bar = 1 day.

I use a Money Management script that incorporate buy/sell rules (so I don't use the simulator buy/sell rules).

An example would be :
- OnEndPeriod : assume that I detect that a stop-loss has been breached during the day for 1 long position (that is : stoploss > low). The stoploss has been set and stored in pos.Var1 on previous bar.
Then I will submit a sell order at the stop loss price (or at open price if it is below the stop price) with the following limit order : Functions.SellPosition(pos.Symbol,Orders.LimitOrder(stopprice));
Question #1 : will this order be executed on the current bar ? I suppose no, because we are on the OnEndPeriod event. Then, it means that I will receive the OnClosePosition for this sell order ont the next bar ?

In fact what I would really like to do is, instead of managing a stop loss sell on the bar where these stop losses are breached, I would like to set a stop loss on previous bar(s) and when they are hit that they generate an OnClosePosition on the appropriate bar.
Question # 2 : I have not found any function to set stop loss for the money manager. Are ther any ? If yes where can I find them ?

Question #3 : more generally, could you describe in what order the events OnEndPeriod / OnNewPosition / OnClosePosition are generated for a given bar. I suppose that OnNewPosition and OnClosePosition are generated for the same bar as many times as there are buys and sells and that AFTER, OnEndPeriod is generated. Is that correct ?

Question #4 : When are Portfolio object data (like the one returned by "GetOpenPositions" or "GetTotalEquity") adjusted regardin buy and sell ? On the appropriate OnNewPosition / OnClosePosition events ?
That is, if I submit a sell order, the Portfolio object will be adjusted before, on, or after the related OnClosePosition ? I suppose after the OnClosePosition script has exited.

Thank you in advance for your response as I am trying to undestand the basics about these events.

2011-01-19 05:02:43



1- The order will be executed on the current bar.
This is equivalent to: Orders.LimitOrder(stopprice, 0, 5)
In the next release you will be able to use Orders.LimitOrder(stopprice, -1, 5) to execute the order on the current bar.
You will not receive the "OnClosePosition" event because this event is fired only when an order is closed using the simulator sell rule. Same thing for "OnNewPosition".

2- What do you mean by a stop loss for the money manager?

3- Right. The order is: OnClosePosition -> OnNewPosition -> OnEndPeriod

4- They are not updated after OnNewPosition/OnClosePosition, because these functions create orders. They don't add positions to the portfolio.
They are updated at the end of each bar.

You can use for example "Orders.GetPendingBuyOrders()" to get pending buy orders.

2011-01-19 08:29:36



Thank you so much for the quick reply !
In fact I am trying to see if it will be possible for me to use the different strategies I am currently working on with Quantshare.
I am mostly interested in risk management of trades, that is exit points, scaling in/out and stop loss.
So I am investigating the money manager script capabilities.

What I want to do is, at evey bar, scale in /out and update the stop loss individually for all open positions for the next bar according to a per trade and portfolio risk management algorithm that will be implemented in the OnEndPeriod event.
So I want to use the OnEndPeriod Event to generate orders (buy/sell and stops) for open positions for next bar.

That is why I asked if there was a special function to set next bar stop loss for open positions.

The only way I have found so far is to submit a sell order with a limitorder type for the next bar at the stop loss price. But I think it will give me wrong results instead.
For example :
- suppose I am long a security and I put a stop loss order for next bar
- I will use Functions.SellPosition(pos.Symbol,Orders.LimitOrder(stopprice)) to set the stop loss for next bar (is it correct?)
- on next bar, suppose the low remains above ma stop loss price.
- normally, that means that my stop would not been hit and the sell will not be executed. Can you confirm this ? As I am not sure that the order I use (above) will generate this correct result, becausse if I submit a sell order with a price that is BELOW the low, I think that it will instead be executed and I don't want to !

2011-01-19 12:15:16


Yes, there is a way to update the stop value of an open position. You must get a reference to a position and then update the variable "StopSettings".

Example: (N-Bar stop must be enabled in "Update a trading system" form)

MMPosition[] positions = Portfolio.GetAllPositions();
for(int i=0;i<positions.Length;i++)
MMPosition pos = positions[i];
pos.StopSettings.NBarsStop = 10;
if(pos.Performance > 5 && pos.BarsSinceEntry > 5)
pos.StopSettings.NBarsStop = 20;

- If you submit a sell limit order below current price, then yes, the order will be executed.

2011-01-19 15:38:30


Thanks again.
I have not found any information regarding the variables StopSettings on this site or the user guide.
Could you explain what means "20" in pos.StopSettings.NBarsStop = 20 ?

In fact I would like to set a price value for next bar stop and not a % or something else.
Would the variable "StopSettings.StopLoss"do this ? If yes, could you please provide an example of how it is used ?

I really appreciate you help.

2011-01-20 04:44:35


Best Answer
For NBarsStop, 20 represents the number of bars.
For StopLoss, 20 will represent 20$ or 20% stop depending on whether you have specified "Percent" or "Point" in Stop Settings in "Update a trading system".

StopSettings.StopLoss = 2; -> Create a stop at Entry Price - 2.

If you want to create a stop at price X then use the following formula:
pos.StopSettings.StopLoss = pos.EntryPrice - X;

2011-01-20 06:43:08


Thanks a lot !
This should work for what I want to do.

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
Long strategy created with the money management tool
Withdraw a Fixed Amount Every Month - Money Management
Correlation Filter Money Management Strategy
Investing in stocks using the Kelly criterion money management st...
Return per Bar Stop - Money Management Strategy

How-to Lessons
How to calculate the average of a time series using the money man...
How to debug a trading system using the money management tool
How to download Portfolio123 data into QuantShare
How to dynamically change the number of positions in a portfolio?
How to add a metric in the trading system simulation report

Related Forum Threads
example of advanced money management tool Stop Order
Change exit type in Advanced Money Management
Add Money Management Settings to Reports
Get the Previous N bar Date/DateTime in Money Management script
Backtesting: Using lookback money management feature

Blog Posts
Basic trading system implemented using the money management tool
Create a trading strategy using the money management tool - Part ...
Create a trading strategy using the money management tool - Part ...
Several money management strategies in a trading system
Advanced Tactical Asset Allocation Strategies

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

Copyright 2021
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.