Dynamic Stops
Dynamic Stops
Within my script I base all long and short entries on the bar close. But on exits (both stop and limit orders) I want to use intrabar calculations, not closing.
Where it may get a bit tricky is the stop and limit orders are dynamic similar to if you used two moving averages where one was a limit order and the other a stop. If one MA for example is pierced "intrabar" then the position would stop out.
So I don't know if I can actually place stop and limit orders (server side) as they continually update with each tick intrabar. It would as a result require these orders to continually be cancelled and placed again. I suspect the exchange would get a bit annoyed. So the first question I have is am I wrong in my assumption, is there a way to put a limit and stop order out that updates continually (this is different from a trailing stop).
If my assumption above is correct then I assume the work around is to have market orders trigger in place of limit or stops when these levels are crossed. So then the question becomes can I have a script that utilizes closing prices for entry and intrabar (IOG) for exits?
I do hope this makes sense. Thank You -
Where it may get a bit tricky is the stop and limit orders are dynamic similar to if you used two moving averages where one was a limit order and the other a stop. If one MA for example is pierced "intrabar" then the position would stop out.
So I don't know if I can actually place stop and limit orders (server side) as they continually update with each tick intrabar. It would as a result require these orders to continually be cancelled and placed again. I suspect the exchange would get a bit annoyed. So the first question I have is am I wrong in my assumption, is there a way to put a limit and stop order out that updates continually (this is different from a trailing stop).
If my assumption above is correct then I assume the work around is to have market orders trigger in place of limit or stops when these levels are crossed. So then the question becomes can I have a script that utilizes closing prices for entry and intrabar (IOG) for exits?
I do hope this makes sense. Thank You -
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
Hello Tony,
The best solution for you would be to create separate signal scripts and use [IntrabarOrderGeneration=on] at the beginning of the scripts to make them work intrabar and recalculate with every new tick in real-time, what would not affect you main strategy (that is another signal script, which calculates on closes only).
As for placing stops and limits directly to exchange through broker, it depends on the broker itself. Not all brokers in MC supports stop and limit orders natively, so in some cases the orders are help within MC and are released when required price level is reached by means of another order type. This article should cover all cases.
Some brokers support order modification, the others do not. For the ones, which don't support modification, old orders are cancelled, the new orders are placed.
By the way, your idea to track price level in MC and then send market order (if you don't want to bother your broker and exchange by constant order modifications, cancellations, replacement) is a way to go, but you should take into account possible slippage.
The best solution for you would be to create separate signal scripts and use [IntrabarOrderGeneration=on] at the beginning of the scripts to make them work intrabar and recalculate with every new tick in real-time, what would not affect you main strategy (that is another signal script, which calculates on closes only).
As for placing stops and limits directly to exchange through broker, it depends on the broker itself. Not all brokers in MC supports stop and limit orders natively, so in some cases the orders are help within MC and are released when required price level is reached by means of another order type. This article should cover all cases.
Some brokers support order modification, the others do not. For the ones, which don't support modification, old orders are cancelled, the new orders are placed.
By the way, your idea to track price level in MC and then send market order (if you don't want to bother your broker and exchange by constant order modifications, cancellations, replacement) is a way to go, but you should take into account possible slippage.
Re: Dynamic Stops
Thanks Andrew. Two follow up questions
1 - within the same script can I have a section bracketed with [IntraBarOrderGeneration = true] and then [IntraBarOrderGeneration = false] and within that section have the logic for position exits?
2 - if I go the route of sending market orders and NOT using limit and stop orders, is there a way within the script I currently have to have logic that when price crosses below or above a set variable that a market order is generated on the next tick versus at the bar close?
Thank You -
1 - within the same script can I have a section bracketed with [IntraBarOrderGeneration = true] and then [IntraBarOrderGeneration = false] and within that section have the logic for position exits?
2 - if I go the route of sending market orders and NOT using limit and stop orders, is there a way within the script I currently have to have logic that when price crosses below or above a set variable that a market order is generated on the next tick versus at the bar close?
Thank You -
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
No. This is the attribute for the whole script.1 - within the same script can I have a section bracketed with [IntraBarOrderGeneration = true] and then [IntraBarOrderGeneration = false] and within that section have the logic for position exits?
[IntraBarOrderGeneration = true] is required again for this.2 - if I go the route of sending market orders and NOT using limit and stop orders, is there a way within the script I currently have to have logic that when price crosses below or above a set variable that a market order is generated on the next tick versus at the bar close?
Re: Dynamic Stops
Thanks for the help Andrew. Just wrote another script for stops so I have one for entries based on close and one for exits based on IOG and it works like a charm. I'm adding this post partly for anyone following along and having the same question.
I assume the answer to this final question is yes. If a variable is used in two separate scripts both of which are run on the same chart, do the variables need to be declared and assigned within each script?
It would seem that using multiple scripts can make the process of optimization more difficult if more than one script uses the same input(s). But if the answer to the question above is no, then it would not impact the optimization.
I assume the answer to this final question is yes. If a variable is used in two separate scripts both of which are run on the same chart, do the variables need to be declared and assigned within each script?
It would seem that using multiple scripts can make the process of optimization more difficult if more than one script uses the same input(s). But if the answer to the question above is no, then it would not impact the optimization.
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
Yes, of course..I assume the answer to this final question is yes. If a variable is used in two separate scripts both of which are run on the same chart, do the variables need to be declared and assigned within each script?
Re: Dynamic Stops
I seem to have a lot of questions here but hopefully not too simple that I'm wasting time. As part of the "exit script" I wrote, I wanted to have the stops to be one of two levels along with a price target.
For example if I were long and assume var3 is lower than var5 what I wanted is to use var3 if price never pushes higher and starts to pullback. But if price moves higher and above var5 but never hits the price target, then var5, not var3 becomes the stop.
To do that I wrote the following conditional statement
IFF (price crosses above var5, var5, var3)
I assume that is the proper way to achieve what I want. But what if I also want the ability to optimize and see if breakeven or var3 (which is a loss) is better. Can I have an IFF statement for breakeven = true and one for breakeven = false
Is it easier for me to just backtest with first using this in the script where var3 does not = entryprice
IFF(price crosses above var5, var5, entryprice)
and then back test with the above replaced with this
IFF(price crosses above var5, var5, var3)
For example if I were long and assume var3 is lower than var5 what I wanted is to use var3 if price never pushes higher and starts to pullback. But if price moves higher and above var5 but never hits the price target, then var5, not var3 becomes the stop.
To do that I wrote the following conditional statement
IFF (price crosses above var5, var5, var3)
I assume that is the proper way to achieve what I want. But what if I also want the ability to optimize and see if breakeven or var3 (which is a loss) is better. Can I have an IFF statement for breakeven = true and one for breakeven = false
Is it easier for me to just backtest with first using this in the script where var3 does not = entryprice
IFF(price crosses above var5, var5, entryprice)
and then back test with the above replaced with this
IFF(price crosses above var5, var5, var3)
Re: Dynamic Stops
This line of code - IFF (price crosses above var5, var5, var3)
does not seem to be the way to accomplish having two stops orders conditional on the first being true or false.
As soon as I trigger long, I am also triggering a stop per the above IFF statement at var3 even though var3 is below where it is stopping out.
does not seem to be the way to accomplish having two stops orders conditional on the first being true or false.
As soon as I trigger long, I am also triggering a stop per the above IFF statement at var3 even though var3 is below where it is stopping out.
Re: Dynamic Stops
I stand corrected, I think it is working properly. I had a few things going on and was causing failure elsewhere.
Re: Dynamic Stops
Hi Tony and Andrew,
what about using Barstatus=2 in IOG mode?
Probably this way it is possible to create only one strategy that uses close tick for entries and IOG ticks for exits.
What do you think about this solution ?
Greetings
Massimo
what about using Barstatus=2 in IOG mode?
Probably this way it is possible to create only one strategy that uses close tick for entries and IOG ticks for exits.
What do you think about this solution ?
Greetings
Massimo
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
Yes, it should be possible if you create correct conditions.Can I have an IFF statement for breakeven = true and one for breakeven = false.
Hello Massimo,Hi Tony and Andrew,
what about using Barstatus=2 in IOG mode?
Probably this way it is possible to create only one strategy that uses close tick for entries and IOG ticks for exits.
What do you think about this solution ?
In this case the order that is sent on open of next bar, will be cancelled on next tick of that next bar because the condition is not met any more = cancel the order.
Re: Dynamic Stops
Hi Andrew,
excuse me if I bother you, but for me it's very difficult to understand how MC submits strategy orders.
To explain:
I used a platform that on every incoming tick looped the code, and if the condition was met fired the order on the market.
i.e. if condition is true buy x shares
Well, on the next ticks, the order remains on the market regardless of the condition that generated it.
In addition, each subsequent tick if it meets the condition as true generates a new order, so that each tick can fire an order on market.
In MC in IOG mode it is not so, if I well understand.
Please can you explain me how orders works in IOG mode in MC?
Many thanks.
Massimo
excuse me if I bother you, but for me it's very difficult to understand how MC submits strategy orders.
To explain:
I used a platform that on every incoming tick looped the code, and if the condition was met fired the order on the market.
i.e. if condition is true buy x shares
Well, on the next ticks, the order remains on the market regardless of the condition that generated it.
In addition, each subsequent tick if it meets the condition as true generates a new order, so that each tick can fire an order on market.
In MC in IOG mode it is not so, if I well understand.
Please can you explain me how orders works in IOG mode in MC?
Many thanks.
Massimo
Re: Dynamic Stops
Let me see if I can add some value now that I benefited from help here. MC calculates by default at bar close meaning it will not calculate intrabar and only go through the calculations upon close of the bar. So if you are using a 10 min bar every 10 min when the bar closes, the calculations are done. So only one time per bar.Hi Andrew,
excuse me if I bother you, but for me it's very difficult to understand how MC submits strategy orders.
To explain:
I used a platform that on every incoming tick looped the code, and if the condition was met fired the order on the market.
i.e. if condition is true buy x shares
Well, on the next ticks, the order remains on the market regardless of the condition that generated it.
In addition, each subsequent tick if it meets the condition as true generates a new order, so that each tick can fire an order on market.
In MC in IOG mode it is not so, if I well understand.
Please can you explain me how orders works in IOG mode in MC?
Many thanks.
Massimo
If you want to calculate every tick then you need to tell MC to calculate based on intrabar so that every tick causes the script to essentially run through the calculations again. So in the case of IOG, bar close is literally every tick.
Maybe not the most eloquent statement of how it works but hopefully makes the point.
Re: Dynamic Stops
I recognize that issue but I use that technique in a strategy with IOG on by testing barstatus = 2. I get around the issue you highlighted by submitting the order once (when barstatus = 2) and remembering what the new order was on subsequent ticks. I then check to see if the position count according to the broker is correct. If after a certain amount of ticks it's not, I flag a failure and halt all trading (typically due to a problem at the broker). Works fine, at least on 8.0.In this case the order that is sent on open of next bar, will be cancelled on next tick of that next bar because the condition is not met any more = cancel the order.
Having said all this, I'm now using a very basic strategy to submit orders (IOG still on, plus RecalcLastBarAfter(1), and using the same logic as above except I now submit a new order once only whenever barstatus >= 0) and I compute all my rules using indicators (with update on each tick). They broadcast long/short/cover/etc. signals to the strategy via global variables. I do this because I find it messy and impossible to do everything I need all in one study (eg, inability to plot from strategies, the technique of using multiple data series with different time-frames in the one study complicates the code too much and has other issues I won't go into here, etc.). So now I prefer to keep things separate to make the coding simpler, easier and more "object orientated". For example, one of my trading systems uses over 6 separate charts (some with different timeframes) to broadcast preliminary signals to a main indicator chart, which in turn collates and processes all the information and if there's a signal it will broadcast an order to the strategy for submission to the broker on the next tick.
As a result of all this my coding of my complex system is so much clearer. It's much like the difference between writing "spaghetti code" and code in a properly structured style. The downside is I can't perform backtesting, but I can live with that as I now perform a lot of "forward testing" instead (and found it better anyway for my scalp and day trading but that's a another story). Perhaps a cool feature in a future MC release is to be able to use this technique in back-testing, similar to what we can do now with play-back using multiple chart windows. I know such backtesting will slow down a lot but with faster and faster PCs I can see the day soon, if not already with high end PCs, that such an issue will no longer be relevant.
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
You are correct, in MC with current implementation it works this way (let's say IOG is ON):I used a platform that on every incoming tick looped the code, and if the condition was met fired the order on the market.
i.e. if condition is true buy x shares
Well, on the next ticks, the order remains on the market regardless of the condition that generated it.
In addition, each subsequent tick if it meets the condition as true generates a new order, so that each tick can fire an order on market.
In MC in IOG mode it is not so, if I well understand.
- Tick 1 -> Script is calculated -> Conditions of order generation are met -> Order is placed.
- Tick 2 (assume the order is pending) -> Script is calculated -> Conditions of order generation are not met any longer-> The order is cancelled.
Re: Dynamic Stops
I don't understand that. What is doing the cancellation? In my studies I submit an order once only, unless it's a limit or stop order. Once a normal order is submitted it may not be filled (ie, pending) for several ticks. The study can't be doing anything to cancel it. Are you saying this has changed since 8.0 and now one has to continue sending a normal entry type of order even after it's filled?You are correct, in MC with current implementation it works this way (let's say IOG is ON):
- Tick 1 -> Script is calculated -> Conditions of order generation are met -> Order is placed.
- Tick 2 (assume the order is pending) -> Script is calculated -> Conditions of order generation are not met any longer-> The order is cancelled.
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
The behavior has been always the same: if an order was sent but the conditions for its generation are not met any longer, the order is cancelled by MultiCharts. If you want your order to stay active at broker till it is filled, you should adjust the script to make the conditions for order generation always true once they are met. If you don't do this, the script will cancel the order as soon as the conditions are false.
Re: Dynamic Stops
To make sure I understand what you are saying, if I want to execute the following statement:The behavior has been always the same: if an order was sent but the conditions for its generation are not met any longer, the order is cancelled by MultiCharts. If you want your order to stay active at broker till it is filled, you should adjust the script to make the conditions for order generation always true once they are met. If you don't do this, the script will cancel the order as soon as the conditions are false.
buy 2 contracts next bar at market;
I need to do this on every update tick and bar until I want a different order to be submitted?
I believe it depends on various settings, such as if IOG is on or not, and if on which of the following is set:
"Limit each order command in this signal to one entry and one exit per bar"
"Limit this signal to one entry and one exit per bar"
"Allow unlimited entries and exist per bar"
and any position limits set in the Strategy Properties. In other words, it's not that simple.
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
No. Market order is separate case. We have been talking about price orders (stop, limit, stop-limit). If you had had "buy 2 contracts next bar at X limit;" in your example, then the answer would have been "Yes".To make sure I understand what you are saying, if I want to execute the following statement:
buy 2 contracts next bar at market;
I need to do this on every update tick and bar until I want a different order to be submitted?
No, Sir. it is that simple: if a price order was sent and the conditions for its generation are not met any longer, the order that was sent is cancelled by MultiCharts.I believe it depends on various settings, such as if IOG is on or not, and if on which of the following is set:
"Limit each order command in this signal to one entry and one exit per bar"
"Limit this signal to one entry and one exit per bar"
"Allow unlimited entries and exist per bar"
and any position limits set in the Strategy Properties. In other words, it's not that simple.
The IOG modes you mentioned are for limiting the same order generation within 1 bar.
Re: Dynamic Stops
Let's assume Tick 2, Tick 3, Tick ... (first order pending) and Conditions of order generation are met again.You are correct, in MC with current implementation it works this way (let's say IOG is ON):
- Tick 1 -> Script is calculated -> Conditions of order generation are met -> Order is placed.
- Tick 2 (assume the order is pending) -> Script is calculated -> Conditions of order generation are not met any longer-> The order is cancelled.
If I well understand no more orders will be added, and first order will stay active. Right ?
This for only Stop-Limit Orders. And what about Market Orders?
Thanks
Massimo
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
Yes, correct.Let's assume Tick 2, Tick 3, Tick ... (first order pending) and Conditions of order generation are met again.
If I well understand no more orders will be added, and first order will stay active. Right?
This for only Stop-Limit Orders. And what about Market Orders?
Code: Select all
If condition1 then buy next bar market;
2 possible scenarios to demonstrate the logic (IOG is ON):
- Tick 1 = condition1 is met -> market order is generated.
Tick 2 = the market order is sent, condition1 is still met, the 40 seconds countdown is started.
Tick 3 (within 40 seconds) = market order is pending (for whatever reason), condition1 is still met, new 40 seconds countdown is started.
There is no tick 4 in next 40 seconds = market order is canceled. - Tick 1 = condition1 is met -> market order is generated.
Tick 2 = the market order is sent, condition1 is NOT met anymore, the 40 seconds countdown is started.
Tick 3 (within 40 seconds) = market order is pending (for whatever reason), condition1 is NOT met, new 40 seconds countdown is started.
There is no tick 4 in next 40 seconds = market order is canceled.
Re: Dynamic Stops
Ok, thanks Andrew. I'm slowly seeing the light.
- Tick 1 = condition1 is met -> market order is generated. Tick 2 = the market order is sent, condition1 is still met, the 40 seconds countdown is started. Tick 3 (within 40 seconds) = market order is pending (for whatever reason), condition1 is still met, new 40 seconds countdown is started. There is no tick 4 in next 40 seconds = market order is canceled.
- Tick 1 = condition1 is met -> market order is generated. Tick 2 = the market order is sent, condition1 is NOT met anymore, the 40 seconds countdown is started. Tick 3 (within 40 seconds) = market order is pending (for whatever reason), condition1 is NOT met, new 40 seconds countdown is started. There is no tick 4 in next 40 seconds = market order is canceled.
So now I understand that market orders, are generated only once (?) when the condition1 is met. And, when generated, they are not cancelled if condition1 is no more met (differently from Limit-Stop orders that would be cancelled), but they are cancelled only when no tick occurs in next 40 seconds.
Regards
Massimo
Re: Dynamic Stops
This is one of the many features I like about MC; the flexibility to alter the way orders are handled. It takes a while to understand it well enough but it's well worth the effort. The method chosen is subtle and can have a critical change to the way a rule-based trading system behaves. The right settings (and there are other settings that might also need to be included) depend on whether you are an scalper, day trader, swing trader, position trader or investor, as well as other things. I’ve been thinking of writing up a detailed document explaining all this to speed up the learning curve for novices. When I get some time I like to do it, unless someone else has already done so.
- Andrew MultiCharts
- Posts: 1587
- Joined: 11 Oct 2011
- Has thanked: 931 times
- Been thanked: 559 times
Re: Dynamic Stops
Technically not, they may be generated on every calculation, but if the order is the same, nothing happens, expect for the resetting default 40 seconds countdown timer.So now I understand that market orders, are generated only once (?) when the condition1 is met.
Correct.And, when generated, they are not cancelled if condition1 is no more met (differently from Limit-Stop orders that would be cancelled), but they are cancelled only when no tick occurs in next 40 seconds.
-
- Posts: 14
- Joined: 28 Jan 2022
- Has thanked: 6 times
Re: Dynamic Stops
Do you have any sample script with Dynamic stops that I can use to create my own script?