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