Dynamic Stops

Questions about MultiCharts and user contributed studies.
tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Dynamic Stops

Postby tony » 26 Jun 2013

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 -

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 27 Jun 2013

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.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Dynamic Stops

Postby tony » 27 Jun 2013

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 -

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 27 Jun 2013

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?
No. This is the attribute for the whole script.
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?
[IntraBarOrderGeneration = true] is required again for this.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Dynamic Stops

Postby tony » 27 Jun 2013

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.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 27 Jun 2013

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?
Yes, of course..

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Dynamic Stops

Postby tony » 27 Jun 2013

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)

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Dynamic Stops

Postby tony » 28 Jun 2013

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.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Dynamic Stops

Postby tony » 28 Jun 2013

I stand corrected, I think it is working properly. I had a few things going on and was causing failure elsewhere.

maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

Re: Dynamic Stops

Postby maxmax68 » 30 Jun 2013

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

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 01 Jul 2013

Can I have an IFF statement for breakeven = true and one for breakeven = false.
Yes, it should be possible if you create correct conditions.
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 ?
Hello Massimo,

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.

maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

Re: Dynamic Stops

Postby maxmax68 » 01 Jul 2013

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

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Dynamic Stops

Postby tony » 01 Jul 2013

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

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.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 103 times

Re: Dynamic Stops

Postby janus » 01 Jul 2013

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

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.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 02 Jul 2013

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.
You are correct, in MC with current implementation it works this way (let's say IOG is ON):
  1. Tick 1 -> Script is calculated -> Conditions of order generation are met -> Order is placed.
  2. Tick 2 (assume the order is pending) -> Script is calculated -> Conditions of order generation are not met any longer-> The order is cancelled.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 103 times

Re: Dynamic Stops

Postby janus » 02 Jul 2013

You are correct, in MC with current implementation it works this way (let's say IOG is ON):
  1. Tick 1 -> Script is calculated -> Conditions of order generation are met -> Order is placed.
  2. Tick 2 (assume the order is pending) -> Script is calculated -> Conditions of order generation are not met any longer-> The order is cancelled.
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?

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 02 Jul 2013

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.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 103 times

Re: Dynamic Stops

Postby janus » 02 Jul 2013

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.
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?

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.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 02 Jul 2013

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. 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".
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.
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.
The IOG modes you mentioned are for limiting the same order generation within 1 bar.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 103 times

Re: Dynamic Stops

Postby janus » 02 Jul 2013

I see. I made the wrong assumption we were discussing any type of order rather than say limit orders. I should have read the original post more carefully.

maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

Re: Dynamic Stops

Postby maxmax68 » 02 Jul 2013

You are correct, in MC with current implementation it works this way (let's say IOG is ON):
  1. Tick 1 -> Script is calculated -> Conditions of order generation are met -> Order is placed.
  2. Tick 2 (assume the order is pending) -> Script is calculated -> Conditions of order generation are not met any longer-> The order is cancelled.
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?
Thanks
Massimo

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 03 Jul 2013

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?
Yes, correct.
This for only Stop-Limit Orders. And what about Market Orders?

Code: Select all

If condition1 then buy next bar market;
Let's say, you have only the above line for order generation in script (no other orders should be generated, only 1 "buy" line and some logic, a simple example). This feature allows you to control the time to cancel market orders. Even if the option is not enabled, any market order is cancelled in 40 seconds automatically, it only gives you the opportunity to control this timeout. Let's say it is unchecked, so you have default 40 seconds before the order is cancelled.

2 possible scenarios to demonstrate the logic (IOG is ON):
  1. 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.
  2. 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.
To summarize: market orders can be cancelled either in default 40 seconds (the option is not checked), or in specified time period (the option is checked and custom time interval is specified).

maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

Re: Dynamic Stops

Postby maxmax68 » 03 Jul 2013

  1. 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.
  2. 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.
Ok, thanks Andrew. I'm slowly seeing the light.
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

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 103 times

Re: Dynamic Stops

Postby janus » 03 Jul 2013

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.

User avatar
Andrew MultiCharts
Posts: 1587
Joined: 11 Oct 2011
Has thanked: 931 times
Been thanked: 559 times

Re: Dynamic Stops

Postby Andrew MultiCharts » 05 Jul 2013

So now I understand that market orders, are generated only once (?) when the condition1 is met.
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.
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.
Correct.

altoncrooks
Posts: 14
Joined: 28 Jan 2022
Has thanked: 6 times

Re: Dynamic Stops

Postby altoncrooks » 11 Mar 2022

Do you have any sample script with Dynamic stops that I can use to create my own script?


Return to “MultiCharts”