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