When should SetStopLoss be placed?

Questions about MultiCharts and user contributed studies.
User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

When should SetStopLoss be placed?

Postby lingwuchung » 29 Dec 2020

Hi,

I thought I should execute it right after an order is made, so that the Multicharts or broker will trigger the stop loss once the stoploss price is matched.

Here is my previous code which works ok:

Code: Select all

if MarketPosition <> 0 then begin SetStopPosition; SetStopLoss(StopLossAmt); end;
however when I set the code like this, where 29052 is where the stoploss should occur:

Code: Select all

if MarketPosition <> 0 and ((BarNumber+MaxBarsBack) < 29052) then begin SetStopPosition; SetStopLoss(StopLossAmt); end;
then I find that the stoploss will not be executed. So it seems that I need to call SetStopLoss it at every bar and it can only evaluate with information of that bar? Isn't that not efficient? Is it possible to place a stoploss order to the broker, so that it will be executed even MC is not running?

Thanks!

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: When should SetStopLoss be placed?

Postby TJ » 29 Dec 2020


User avatar
syswizard
Posts: 295
Joined: 15 Dec 2012
Has thanked: 16 times
Been thanked: 28 times

Re: When should SetStopLoss be placed?

Postby syswizard » 29 Dec 2020

Check this out....right from tech support: Lots of rule to be aware of .....

When the special order is not under an IF statement- it is evaluated intra-bar (even if IOG is off).
The special order will be placed and remain active while position is open automatically without additional programming.

When the special order is under an IF statement- it is evaluated on bar close (if IOG is off) and intra-bar (if IOG is on). The special order will be placed once the IF statement is met and the position is open.

The order will be active only while the IF statement is met and the position is open.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 30 Dec 2020

Check this out....right from tech support: Lots of rule to be aware of .....

When the special order is not under an IF statement- it is evaluated intra-bar (even if IOG is off).
The special order will be placed and remain active while position is open automatically without additional programming.

When the special order is under an IF statement- it is evaluated on bar close (if IOG is off) and intra-bar (if IOG is on). The special order will be placed once the IF statement is met and the position is open.

The order will be active only while the IF statement is met and the position is open.
Hi, what do you mean "under an IF statement" (I have IOG off though)? Both of my code are under IF statement, just that I notice that the stop loss order won't be effective unless it is called at bar 29052 (the current bar where SL should occur). However in real world, we would like the order to be effective earlier.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 30 Dec 2020

See post #5
viewtopic.php?t=10811
Don't quite understand - "To avoid logical errors, DO NOT place these keywords inside a condition". I do try to apply exit on the entire strategy.

If not inside a condition, does it mean that the SetStopLoss call has to be appeared at the top level of the code - meaning it will be called at every bar/tick? Isn't this ineffective and will MC trigger this to Broker at every bar/tick too? I wonder if broker will block me by generating so many duplicating calls.

In the code I provided above, I did place SetStopLoss inside a IF condition. It did work but only at the latest/current bar - seems like only at this point that MC knows the stoploss has reached and take action.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: When should SetStopLoss be placed?

Postby TJ » 30 Dec 2020

Hi,

I thought I should execute it right after an order is made, so that the Multicharts or broker will trigger the stop loss once the stoploss price is matched.

Here is my previous code which works ok:

Code: Select all

if MarketPosition <> 0 then begin SetStopPosition; SetStopLoss(StopLossAmt); end;
however when I set the code like this, where 29052 is where the stoploss should occur:

Code: Select all

if MarketPosition <> 0 and ((BarNumber+MaxBarsBack) < 29052) then begin SetStopPosition; SetStopLoss(StopLossAmt); end;
then I find that the stoploss will not be executed. So it seems that I need to call SetStopLoss it at every bar and it can only evaluate with information of that bar? Isn't that not efficient? Is it possible to place a stoploss order to the broker, so that it will be executed even MC is not running?

Thanks!
You are describing a logical error.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: When should SetStopLoss be placed?

Postby TJ » 30 Dec 2020

See post #5
viewtopic.php?t=10811
Don't quite understand - "To avoid logical errors, DO NOT place these keywords inside a condition". I do try to apply exit on the entire strategy.

If not inside a condition, does it mean that the SetStopLoss call has to be appeared at the top level of the code - meaning it will be called at every bar/tick? Isn't this ineffective and will MC trigger this to Broker at every bar/tick too? I wonder if broker will block me by generating so many duplicating calls.

In the code I provided above, I did place SetStopLoss inside a IF condition. It did work but only at the latest/current bar - seems like only at this point that MC knows the stoploss has reached and take action.
Please go to the post I provided,
go to the end of the post,
see the link on "Order types supported by various brokers:"

Some brokers support stop orders, others are emulated and held by MultiCharts in your computer.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: When should SetStopLoss be placed?

Postby TJ » 30 Dec 2020

[snip] . . .

If not inside a condition, does it mean that the SetStopLoss call has to be appeared at the top level of the code - meaning it will be called at every bar/tick? Isn't this ineffective and will MC trigger this to Broker at every bar/tick too? I wonder if broker will block me by generating so many duplicating calls.

[snip] . . .
If there is no change to your price, MultiCharts will not withdraw and repost the order.

User avatar
syswizard
Posts: 295
Joined: 15 Dec 2012
Has thanked: 16 times
Been thanked: 28 times

Re: When should SetStopLoss be placed?

Postby syswizard » 30 Dec 2020

If there is no change to your price, MultiCharts will not withdraw and repost the order.
Agreed for SetStopLoss. However, what about SetPercentTrailing(Profit,Percentage) ?
It seems in that case, once the profit target is hit, then MC must issue cancel and replace orders as the Profit increases.
Of course, if Profit decreases, no change is necessary to the original stop order issued.
Lots to consider here. To me, these built-in order types have always been a "black box" of sorts.....and I don't use them for that reason.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 30 Dec 2020

See post #5
viewtopic.php?t=10811
Don't quite understand - "To avoid logical errors, DO NOT place these keywords inside a condition". I do try to apply exit on the entire strategy.

If not inside a condition, does it mean that the SetStopLoss call has to be appeared at the top level of the code - meaning it will be called at every bar/tick? Isn't this ineffective and will MC trigger this to Broker at every bar/tick too? I wonder if broker will block me by generating so many duplicating calls.

In the code I provided above, I did place SetStopLoss inside a IF condition. It did work but only at the latest/current bar - seems like only at this point that MC knows the stoploss has reached and take action.
Please go to the post I provided,
go to the end of the post,
see the link on "Order types supported by various brokers:"

Some brokers support stop orders, others are emulated and held by MultiCharts in your computer.
Thanks! I am using IB and so all are supported at broker side. But I am just doing local backtesting/playback only now. I will verify the broker later.

But about my puzzle around "logical error". I do place my OCO inside a IF as you can see above (and another bigger "if within trading hours"). Does OCO not work all the time or just randomly inside a condition?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: When should SetStopLoss be placed?

Postby TJ » 30 Dec 2020

Thanks! I am using IB and so all are supported at broker side. But I am just doing local backtesting/playback only now. I will verify the broker later.

But about my puzzle around "logical error". I do place my OCO inside a IF as you can see above (and another bigger "if within trading hours"). Does OCO not work all the time or just randomly inside a condition?
If you put a code inside a condition,
it does not matter if the code is a variable assignment or a stop loss,
that piece of code will only execute IF the condition is met.

For a stop loss, it is a Global order, it is only in effect if there is an open position, there is really no reason to put it in a condition.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: When should SetStopLoss be placed?

Postby TJ » 30 Dec 2020

Hi,

I thought I should execute it right after an order is made, so that the Multicharts or broker will trigger the stop loss once the stoploss price is matched.

Here is my previous code which works ok:

Code: Select all

if MarketPosition <> 0 then begin SetStopPosition; SetStopLoss(StopLossAmt); end;
[sniped] . . .

This line of code is redundant,
because Global Exits are in effect ONLY if you have an open position.

Code: Select all

if MarketPosition <> 0 then begin

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 31 Dec 2020

Hi,

I thought I should execute it right after an order is made, so that the Multicharts or broker will trigger the stop loss once the stoploss price is matched.

Here is my previous code which works ok:

Code: Select all

if MarketPosition <> 0 then begin SetStopPosition; SetStopLoss(StopLossAmt); end;
however when I set the code like this, where 29052 is where the stoploss should occur:

Code: Select all

if MarketPosition <> 0 and ((BarNumber+MaxBarsBack) < 29052) then begin SetStopPosition; SetStopLoss(StopLossAmt); end;
then I find that the stoploss will not be executed. So it seems that I need to call SetStopLoss it at every bar and it can only evaluate with information of that bar? Isn't that not efficient? Is it possible to place a stoploss order to the broker, so that it will be executed even MC is not running?

Thanks!
You are describing a logical error.
Only if the stoploss order was not placed to the market until the bar stoploss price is reached.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 31 Dec 2020

[snip] . . .

If not inside a condition, does it mean that the SetStopLoss call has to be appeared at the top level of the code - meaning it will be called at every bar/tick? Isn't this ineffective and will MC trigger this to Broker at every bar/tick too? I wonder if broker will block me by generating so many duplicating calls.

[snip] . . .
If there is no change to your price, MultiCharts will not withdraw and repost the order.
OK. This answers one of my puzzles. At least I shouldn't worry about calling SetStopLoss (with the same price) more than necessary or even at every bar.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 31 Dec 2020

Thanks! I am using IB and so all are supported at broker side. But I am just doing local backtesting/playback only now. I will verify the broker later.

But about my puzzle around "logical error". I do place my OCO inside a IF as you can see above (and another bigger "if within trading hours"). Does OCO not work all the time or just randomly inside a condition?
If you put a code inside a condition,
it does not matter if the code is a variable assignment or a stop loss,
that piece of code will only execute IF the condition is met.

For a stop loss, it is a Global order, it is only in effect if there is an open position, there is really no reason to put it in a condition.
Hi TJ,

I know what a IF condition is. My IF condition above is not just about MP but other conditions too. I made it simple to use “BarNumber” for illustration. My original intention is to use the past pivot point at the point of the order to determine the stoploss price. As pivot points will move, I need to call it right after the order is placed, not at every current bar or the deduced SL price will got changed.

Based all your replies so far and my testing today, my guess is that:
1. In real-time mode, setstoploss() does trigger to broker immediately within the same bar/tick iteration, no matter you call it under a condition or not. The stoploss exit will be fulfilled by the broker.
2. In backtest/playback, the stop loss exit will be evaluated by a call to setstoploss() (as an internal emulator of the broker). So if I skip it at the bar where the stop loss should be fullfilled, no stop loss will be filled. That's why in your FAQ article you said "To avoid logical errors, DO NOT place these keywords inside a condition", to avoid this kind of situation.

Any comment? In fact the problem I am facing now is with backtesting.

And Happy New Year to you and your loved ones!!!

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 31 Dec 2020

Check this out....right from tech support: Lots of rule to be aware of .....

When the special order is not under an IF statement- it is evaluated intra-bar (even if IOG is off).
The special order will be placed and remain active while position is open automatically without additional programming.

When the special order is under an IF statement- it is evaluated on bar close (if IOG is off) and intra-bar (if IOG is on). The special order will be placed once the IF statement is met and the position is open.

The order will be active only while the IF statement is met and the position is open.
The problem I am facing is not stop loss order not able to be placed, but it was not exit when the SL price is met during backtest. It seems that I need to call SetStopLoss only at the bar when SL price is met - if that's the case I dont need to call SetStopLoss, for I could just place a BuyToCover.

User avatar
syswizard
Posts: 295
Joined: 15 Dec 2012
Has thanked: 16 times
Been thanked: 28 times

Re: When should SetStopLoss be placed?

Postby syswizard » 31 Dec 2020

So you are saying that the SetStoploss is working in real time, but not in back-test mode ?
You should post your code again.
And make sure you show your IntrabarOrderGeneration(IOG) statement.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: When should SetStopLoss be placed?

Postby TJ » 31 Dec 2020


Hi TJ,
I know what a IF condition is. My IF condition above is not just about MP but other conditions too.

. . .
I am not criticizing your intention, nor questioning your ability to code.

I am merely stating the mode of operation -- If your condition is not triggered, that is because the condition has not been met.

ie it is a logical error.


ps. you can use the papertrading account to do a real time test.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 01 Jan 2021

So you are saying that the SetStoploss is working in real time, but not in back-test mode ?
You should post your code again.
And make sure you show your IntrabarOrderGeneration(IOG) statement.
Yes. My code look like this:

Code: Select all

mp = MarketPosition; if mp[0] <> 0 and mp[1] <> mp[0] then begin // new position detected stopLossPrice = price of last pivot point; SetStopPosition; SetStopLoss_pt(PriceScale / MinMove * AbsValue(OpenEntryPrice(0) - stopLossPrice))); end; If (order condition met) begin buy/sell 100 shares next bar at open; end;
"and mp[1] <> mp[0]" is the new condition I have added that makes the stoploss doesn't work. It has been working fine with just "mp[0] <> 0" although it means it will be called in every bar (my worry answered by TJ). Basically I am trying to set the stopLossPrice only when the order was made, because the pivot point will keep moving going forward and so I shouldn't set it later. I have been calling SetStopLoss() before the buy/sell becasue I notice that MarketPosition only returns new value in next bar.

I debuged and traced the reason is that the stopLossPrice has to be called at the bar where the stoploss price has reached. And because at that time, the MP is not changing and mp[1] = mp[0], thus the logic won't work according to my original intention.

Again, I am referring to backtesting only.

What do you mean "IOG statement"? I don't have IOG enabled in the signal properties.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 01 Jan 2021


ps. you can use the papertrading account to do a real time test.
Yes, I did a real test test yesterday, hence my deduction:

1. In real-time mode, setstoploss() does trigger to broker immediately within the same bar/tick iteration, no matter you call it under a condition or not. The stoploss exit will be fulfilled by the broker.
2. In backtest/playback, the stop loss exit will be evaluated by a call to setstoploss() (as an internal emulator of the broker). So if I skip it at the bar where the stop loss should be fullfilled, no stop loss will be filled. That's why in your FAQ article you said "To avoid logical errors, DO NOT place these keywords inside a condition", to avoid this kind of situation.

Otherwise if a call to setStopLoss() is effective (i.e. will exit accordingly later) as soon as it is being called, there shouldn't be a worry about logical error that it will be skipped later.

Please correct if I am wrong. Thanks.

User avatar
syswizard
Posts: 295
Joined: 15 Dec 2012
Has thanked: 16 times
Been thanked: 28 times

Re: When should SetStopLoss be placed?

Postby syswizard » 01 Jan 2021

Please correct if I am wrong. Thanks.
Dude, you're not just wrong, you're stupid.
We keep asking what is your IOG setting (IntrabarOrderGeneration) ..with no response ?

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 01 Jan 2021

Please correct if I am wrong. Thanks.
Dude, you're not just wrong, you're stupid.
We keep asking what is your IOG setting (IntrabarOrderGeneration) ..with no response ?
I said "I don't have IOG enabled in the signal properties". Let me capture some screen shots in next post.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 01 Jan 2021

So you are saying that the SetStoploss is working in real time, but not in back-test mode ?
You should post your code again.
And make sure you show your IntrabarOrderGeneration(IOG) statement.
Yes. My code look like this:

Code: Select all

mp = MarketPosition; if mp[0] <> 0 and mp[1] <> mp[0] then begin // new position detected stopLossPrice = price of last pivot point; SetStopPosition; SetStopLoss_pt(PriceScale / MinMove * AbsValue(OpenEntryPrice(0) - stopLossPrice))); end; If (order condition met) begin buy/sell 100 shares next bar at open; end;
"and mp[1] <> mp[0]" is the new condition I have added that makes the stoploss doesn't work. It has been working fine with just "mp[0] <> 0" although it means it will be called in every bar (my worry answered by TJ). Basically I am trying to set the stopLossPrice only when the order was made, because the pivot point will keep moving going forward and so I shouldn't set it later. I have been calling SetStopLoss() before the buy/sell becasue I notice that MarketPosition only returns new value in next bar.

I debuged and traced the reason is that the stopLossPrice has to be called at the bar where the stoploss price has reached. And because at that time, the MP is not changing and mp[1] = mp[0], thus the logic won't work according to my original intention.

Again, I am referring to backtesting only.

What do you mean "IOG statement"? I don't have IOG enabled in the signal properties.
If my code is like this, stop loss will be triggered (see attached image):

Code: Select all

if (BarNumber+MaxBarsBack) < 12063 and MarketPosition <> 0 then begin SetStopPosition; SetStopLoss(StopLoss); end;
If my code is like this, stop loss will NOT be triggered (see attached image):

Code: Select all

if (BarNumber+MaxBarsBack) < 12062 and MarketPosition <> 0 then begin SetStopPosition; SetStopLoss(StopLoss); end;
So the only difference is just the bar number. This means the SetStopLoss called soon after the "Buy" wasn't really effective, until the one at bar 12062.

I have tried to enable IOG but the result is the same.
Attachments
IOG.gif
IOG settings
(23.14 KiB) Not downloaded yet
12062.gif
(BarNumber+MaxBarsBack) < 12062
(39.66 KiB) Not downloaded yet
12063.gif
(BarNumber+MaxBarsBack) < 12063
(40.75 KiB) Not downloaded yet

User avatar
syswizard
Posts: 295
Joined: 15 Dec 2012
Has thanked: 16 times
Been thanked: 28 times

Re: When should SetStopLoss be placed?

Postby syswizard » 06 Jan 2021

Well, well, well....based on your feedback, it seems as though conditional execution of SetStopLoss is not really supported.
Also, you insist on using MarketPosition WHEN IN FACT YOU DON'T NEED IT.
SetStopLoss and the other internal stops "know" when there is a marketposition <> 0.

User avatar
lingwuchung
Posts: 50
Joined: 03 Feb 2014
Has thanked: 6 times

Re: When should SetStopLoss be placed?

Postby lingwuchung » 09 Jan 2021

Well, well, well....based on your feedback, it seems as though conditional execution of SetStopLoss is not really supported.
Also, you insist on using MarketPosition WHEN IN FACT YOU DON'T NEED IT.
SetStopLoss and the other internal stops "know" when there is a marketposition <> 0.
I think it’s not just an issue of conditional execution but a constraint/bug in backtesting. Just not happen in live trading.

I will continue to add the simple MP <> 0 condition, to remind myself the logical thinking. Anyway, I have implemented my own stoploss logic instead of riding on setstoploss black box logic as u suggested in your first post. Thanks.


Return to “MultiCharts”