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.
GenerateProfitTarget() and GenerateStopLoss() lifetime
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: GenerateProfitTarget() and GenerateStopLoss() lifetime
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.
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.
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: GenerateProfitTarget() and GenerateStopLoss() lifetime
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:
If they are under condition-the orders are generated once the condition is met (either intrabar or on bar close):
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)
Code: Select all
if(StrategyInfo.MarketPosition != 0)
{
GenerateStopLoss(amount);
}
Re: GenerateProfitTarget() and GenerateStopLoss() lifetime
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 :
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 :
Thank you.
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);
}
}
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);
}
}
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: GenerateProfitTarget() and GenerateStopLoss() lifetime
Yes, but the code treats the limit and GenerateProfitTarget commands differently.So one way or the other (priced order or generate profit), there will be a limit order at the broker.
The orders are valid while they are sent by the strategy.The question still remains the same as it is stated in the title. How long will the order stay active.
That is correct.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 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:What I'd expect is, that the PT/SL orders wold stay active until one of them is filled.
Code: Select all
protected override void CalcBar()
{
if (Bars.CurrentBar == 10) {
buyorder.Send();
GenerateStopLoss(amount);
GenerateProfitTarget(amount);
}
}
You just need to make the special orders outside a condition to achieve your goal: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
Code: Select all
protected override void CalcBar()
{
if (Bars.CurrentBar == 10) {
buyorder.Send();
}
GenerateStopLoss(amount);
GenerateProfitTarget(amount);
}
Re: GenerateProfitTarget() and GenerateStopLoss() lifetime
Henry,
In your post #4 above,
In your post #4 above,
Did you mean "Special Orders" are valid at broker while they are sent by the strategy... ?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.
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: GenerateProfitTarget() and GenerateStopLoss() lifetime
"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.Did you mean "Special Orders" are valid at broker while they are sent by the strategy... ?