GenerateProfitTarget() and GenerateStopLoss() lifetime

Questions about MultiCharts .NET and user contributed studies.
mk42
Posts: 6
Joined: 13 Jun 2013

GenerateProfitTarget() and GenerateStopLoss() lifetime

Postby mk42 » 18 Jun 2013

Hi,

in my strategy I am trying to utilize GenerateProfitTarget() and GenerateStopLoss() methods. For some time I could not find out, why things were not working for me. But when I lowered the SL/PT amount low enough I found out that the generated limit orders seem to be only worth on the same bar where the entry happened so the Profit/StopLoss got activated only if it was reached on the 1st bar.

Is that correct? Can this behavior somehow be affected? My preferred behavior would be that the orders are active unless the original position has been closed by some other means (another market/limit order) or that a day passed. Can that be achieved?

Thanks.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: GenerateProfitTarget() and GenerateStopLoss() lifetime

Postby Henry MultiСharts » 18 Jun 2013

Hello mk42,

Please check the following posts that answer your question: #1, #2.

mk42
Posts: 6
Joined: 13 Jun 2013

Re: GenerateProfitTarget() and GenerateStopLoss() lifetime

Postby mk42 » 18 Jun 2013

Hi,

I read through both links and neither answers the question. Maybe I do not understand the methods properly. My understanding is I call GenerateProfitTarget() every time I need a new limit order (or set of limit orders) to be sent in order to achieve the profit target.

Is the limit order only valid for 1 bar? Do I need to call GenerateProfitTarget() every bar while I am in position? And each time new order will be send to the broker?

Thanks.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: GenerateProfitTarget() and GenerateStopLoss() lifetime

Postby Henry MultiСharts » 19 Jun 2013

First of all you need to understand the difference between OrderCreator.Limit/Stop and GenerateProfitTarget/StopLoss. Limit/Stop are price orders while GenerateProfitTarget/StopLoss are special orders.
Price orders are valid at broker while they are sent by the strategy. That means the conditions for order generation should be met in your code on each bar close or each tick (if IOG=ON) calculation while you need to keep your orders active.

If GenerateProfitTarget()/StopLoss() methods are not under condition - they are automatically sent intra bar on the next tick after the code knows there is an open position:

Code: Select all

buyorder.Send()
GenerateStopLoss(amount)
If they are under condition-the orders are generated once the condition is met (either intrabar or on bar close):

Code: Select all

if(StrategyInfo.MarketPosition != 0)
{
GenerateStopLoss(amount);
}

mk42
Posts: 6
Joined: 13 Jun 2013

Re: GenerateProfitTarget() and GenerateStopLoss() lifetime

Postby mk42 » 20 Jun 2013

Hi,

still you are explaining how to affect when the orders are sent. I am asking how to affect how long they stay active. When reading the documentation you linked here, it clearly says that GenerateProfitTarget() generates a Limit order on the price which comes from the calculation of the amount given to the method. So one way or the other (priced order or generate profit), there will be a limit order at the broker. The question still remains the same as it is stated in the title. How long will the order stay active.

Consider example :

Code: Select all

protected override void CalcBar()
{
if (Bars.CurrentBar == 10) {
buyorder.Send();
GenerateStopLoss(amount);
GenerateProfitTarget(amount);
}
}
This sample strategy will at 1 point of time enter the long position with 1 contract at a market price. And generate two sell orders, one limit and one stop to achieve the ProfitTarget and StopLoss, right?

What I'd expect is, that the PT/SL orders wold stay active until one of them is filled.
However the behavior which I am observing now is that these orders would get filled only if the price would reach PT/SL in the 1st bar where the position was entered. If the SL/PT price is not reached in that 1st bar the position stays opened indefinitely.

The only way how to achieve the proper PT/SL, that is to have orders active on each bar when position is active I had to execute the special order methods on each bar after the position is opened which I do not think is is right, because it means every time new orders are sent to the broker for the same prices. Like in the code below :

Code: Select all

protected override void CalcBar()
{
if (Bars.CurrentBar == 10) {
buyorder.Send();
}
if (StrategyInfo.MarketPosition != 0) {
GenerateStopLoss(amount);
GenerateProfitTarget(amount);
}
}
Thank you.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: GenerateProfitTarget() and GenerateStopLoss() lifetime

Postby Henry MultiСharts » 24 Jun 2013

So one way or the other (priced order or generate profit), there will be a limit order at the broker.
Yes, but the code treats the limit and GenerateProfitTarget commands differently.
The question still remains the same as it is stated in the title. How long will the order stay active.
The orders are valid while they are sent by the strategy.
This sample strategy will at 1 point of time enter the long position with 1 contract at a market price. And generate two sell orders, one limit and one stop to achieve the ProfitTarget and StopLoss, right?
That is correct.
What I'd expect is, that the PT/SL orders wold stay active until one of them is filled.
That is incorrect. Special orders are under condition, i.e. the orders are generated (while they are generated they are valid at broker) only once, when the condition is met. Special orders (GenerateStopLoss, GenerateProfitTarget) from this example are valid only when Bars.CurrentBar == 10:

Code: Select all

protected override void CalcBar()
{
if (Bars.CurrentBar == 10) {
buyorder.Send();
GenerateStopLoss(amount);
GenerateProfitTarget(amount);
}
}
The only way how to achieve the proper PT/SL, that is to have orders active on each bar when position is active I had to execute the special order methods on each bar after the position is opened
You just need to make the special orders outside a condition to achieve your goal:

Code: Select all

protected override void CalcBar()
{
if (Bars.CurrentBar == 10) {
buyorder.Send();
}
GenerateStopLoss(amount);
GenerateProfitTarget(amount);
}
These orders will stay valid while there is an open position until one of them gets filled.

User avatar
orad
Posts: 121
Joined: 14 Nov 2012
Has thanked: 50 times
Been thanked: 20 times

Re: GenerateProfitTarget() and GenerateStopLoss() lifetime

Postby orad » 23 Jan 2015

Henry,

In your post #4 above,
Price orders are valid at broker while they are sent by the strategy. That means the conditions for order generation should be met in your code on each bar close or each tick (if IOG=ON) calculation while you need to keep your orders active.
Did you mean "Special Orders" are valid at broker while they are sent by the strategy... ?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: GenerateProfitTarget() and GenerateStopLoss() lifetime

Postby Henry MultiСharts » 30 Jan 2015

Did you mean "Special Orders" are valid at broker while they are sent by the strategy... ?
"Special Orders" are regular price orders that have a certain logic behind them. All price orders (non market) including "Special Orders" have the same order life - they are valid at broker while they are sent by the strategy.


Return to “MultiCharts .NET”