Multiple strategies on one chart

Questions about MultiCharts and user contributed studies.
AAY
Posts: 56
Joined: 29 Nov 2013
Has thanked: 9 times
Been thanked: 30 times

Multiple strategies on one chart

Postby AAY » 29 Jun 2016

Hello. I'm trying to reduce the number of charts I use for autotrading, by running several strategies on one chart. Here I describe the problems I found, and workarounds for them. This may be useful for someone.

Some of workarounds are really awkward. I understand there must be easier ways to achieve the same. If someone more experienced can point me to more proper solutions for these problems, I'd appreciate it a lot.

I want trading systems to behave on one chart exactly the same way they would have behaved on different charts. When you run several trading signals on one chart, their entries and exits work together. I understand that signals are meant to work in this way. But all my signals are complete strategies with entries and exits, and I do not want their orders to mix.

For example, you run strategy 1 with a big stop, and strategy 2 with a very short stop. If you run them together and strategy 1 enters position, it will be stopped out by a short stop from strategy 2. Workaround: name your entries, and make exit to work only on named position for the same strategy. It works even if entry names are the same for different strategies. Example:

Code: Select all

Input: price_B(1000), price_S(1100);
Buy ("Entry A") next bar price_B limit;
Sell entry ("Entry A") next bar price_S limit;
If you run several copies of this strategy on one chart, they would place trades indepedently, like if they were running on different charts.

Unfortunately, it works only for limit and stop exit orders. Market orders do not work this way for some reason. Market entry order opens position only for one strategy instance, even if several instances must place entry orders. Here is the workaround I finally implemented. Instead of using a market order, I'm using a limit order with some offset from close of the previous bar. But if I backtest with limit orders like this, another weirdness will appear which I'm not going to describe here. I want to backtest with market orders. There is a way to run different code in backtesting and live trading:

Code: Select all

if GetAppInfo(aiStrategyAuto)=0 then begin
Buy next bar open;
end else begin
Buy next bar close+100 limit;
end;
Finally, the problem with entryprice. Entryprice refers to the price of the initial entry into the position. If your strategy is the only strategy on a chart, and it has only one entry, there is no problem. But when you combine several strategies on one chart, and they all have open opsitions, entryprice is the same for all instances, and your strategies together behave differently from when running independently.

Here is the really awkward workaround I managed to create:

Name your entries with dynamic order names, for example:
buy (getstrategyname + " Buy A")

To get the real entry price for a particular strategy instance, I use this code:

Code: Select all

var: numOfTrades(0), entryPriceBuy(0), entryPriceSell(0);

if marketposition<>0 then begin
numOfTrades=PosTradeCount(0);
for value1=0 to (numOfTrades) begin
if PosTradeEntryName(0,value1)=getstrategyname + " Buy A" then entryPriceBuy=PosTradeEntryPrice(0,value1);
if PosTradeEntryName(0,value1)=getstrategyname + " Sell A" then entryPriceSell=PosTradeEntryPrice(0,value1);
end;
end;
It gets the total number of trades, and searches the number of trade with the name you use in this strategy, then gets the real entry price for this trade.

In short, I found 3 problems with trading multiple strategies on one chart.

1. Mixed entries/exits, one strategy can affect positions in another — Name your entries and exits.
2. Only one strategy places a market order — Avoid market orders in live trading, use limits with offset instead. Branch code with "if GetAppInfo(aiStrategyAuto)=1" if needed.
3. entryprice is the same for all strategies — you can get real entryprice for this strategy instance using the code I provided.
Last edited by AAY on 30 Jun 2016, edited 2 times in total.

Zheka
Posts: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

Re: Multiple strategies on one chart

Postby Zheka » 29 Jun 2016

Thanks a lot for your message!

I have long wondered why there is no "unmanaged" order placement mode in MC whereby "Buy 500" would really mean "Buy 500", not "establish a long position of 5000"..

What would happen if one of the strategies signals "buy 500" and another - later - "sell short 500"??

I might be wrong but my checks showed that "sell short" will basically establish a net short position, even though it should be net 0 (if traded from 2 different charts).

Is there a way to make this work correctly?
Last edited by Zheka on 30 Jun 2016, edited 1 time in total.

AAY
Posts: 56
Joined: 29 Nov 2013
Has thanked: 9 times
Been thanked: 30 times

Re: Multiple strategies on one chart

Postby AAY » 30 Jun 2016

What would happen if one of the strategies signals "buy 500" and another - later - "sell short 500"??

I might be wrong but my checks showed that "sell short" will basically establish a net short position, even though it should be net 0 (if traded from 0 different charts).

Is there a way to make this work correctly?
I did not test it thoroughly. I group similar strategies on one chart, they never trade in different directions.

I know that stop-and-reverse strategies do not work as expected: in addition to your stop, entry order in reverse position is twice the size. For example, your position is 1 long, if stops (position 0 now) and reverses (position -2 now). I worked around this by splitting the strategy to long-only and short-only, and running them on two different charts.

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

Re: Multiple strategies on one chart

Postby janus » 02 Jul 2016

Thanks a lot for your message!

I have long wondered why there is no "unmanaged" order placement mode in MC whereby "Buy 500" would really mean "Buy 500", not "establish a long position of 5000"..

What would happen if one of the strategies signals "buy 500" and another - later - "sell short 500"??

I might be wrong but my checks showed that "sell short" will basically establish a net short position, even though it should be net 0 (if traded from 2 different charts).

Is there a way to make this work correctly?
It all comes down to understanding the "rules of engagement" for MC trading. Some trading platforms do it differently but there is really no "right" or "wrong" way - just different approaches to the same end. So for MC if you are already long 500 contracts and your next signal says to cover those longs then use:

sell all contracts next bar at market;

At least that how it would work for a single strategy. As for multiple ones on different charts - I'm not sure but I think it would work the same. I would hope so to avoid confusion.

User avatar
Smoky
Posts: 507
Joined: 03 Dec 2010
Location: Thailand
Has thanked: 97 times
Been thanked: 115 times

Re: Multiple strategies on one chart

Postby Smoky » 02 Jul 2016

I know that stop-and-reverse strategies do not work as expected: in addition to your stop, entry order in reverse position is twice the size. For example, your position is 1 long, if stops (position 0 now) and reverses (position -2 now). I worked around this by splitting the strategy to long-only and short-only, and running them on two different charts.
Hi AAY,

stop and reverse works but you mus add one test:

always long = > marketposition <> 0 sellshort NbSharesLong + 1 (1 is minimun could be NbSharesShort)

stop hit => marketposition = 0 sellshort NbSharesShort

works fine for me ;-)

Zheka
Posts: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

Re: Multiple strategies on one chart

Postby Zheka » 02 Jul 2016

Guys:-)

Appreciate your comments but they are all about somewhat different situations.

My question related to the following set-up:

- One chart, 2 signals applied to it, signals generate opposite orders (one after another)

The problem is that the opposite-side order generated by the 2nd strategy will REVERSE the entire position (i.e. order quantity will equal to closing position of strategy1+opening position for strategy2). While the desired behavior is that it just buys/sells only the quantity specified in strategy 2.

In NT and some other platforms there is an "unmanaged" mode of placing orders, where Buy 500 means exactly that, not "establish a net position of +500".

@MC,

Is there a way around or shall I create a PM for that?

User avatar
fbertram
Posts: 166
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 36 times
Been thanked: 76 times
Contact:

Re: Multiple strategies on one chart

Postby fbertram » 02 Jul 2016

Hi Zheka,

there are ways to do what you are looking for:
* change your signals to not directly submit orders, but to set a global variable that holds the number of shares each signal would like to hold
* have one additional signal, which nets all the global variables and submits trades to make CurrentShares match the calculated net value

Regarding the global variables shared by the strategies there are at least 2 options:
(1) use of Global Variables: https://www.multicharts.com/trading-sof ... _Variables
(2) use Portfolio Trader and the pmm_set_my_named_num

Regarding the additional required strategy to submit the trades, you will find most of what you need here: viewtopic.php?f=5&t=49635

Hope this helps,
Cheers

Felix

Zheka
Posts: 223
Joined: 13 Jan 2016
Has thanked: 8 times
Been thanked: 53 times

Re: Multiple strategies on one chart

Postby Zheka » 04 Jul 2016

Thank you, Felix!

I will try it the way you suggested.


Return to “MultiCharts”