ApplyStop is actually a pretty tricky function to handle especially in a weekly system as it's assuming you are using a conditional order at your broker on a daily/intraday basis.This is exactly the same as the applystop function:
ApplyStop function is designed to be used to simulate
stop orders placed at the exchange or simulated by the brokerage
If using ExitAtStop = 1 the order executes intraday.ExitAtStop = 1 - check High-Low prices and exit intraday on price equal to stop level on the same bar when stop was triggered
ExitAtStop = 2 - check High-Low prices but exit NEXT BAR on regular trade price.
In the case of ExitAtStop = 2 the NEXT BAR is actually indicating the next bar in your actual data. In the case of Norgate Data that would be the next day as Norgate is in a daily format. The ApplyStop will ignore your Weekly/Monthly settings.
It will also ignore any SetTradeDelays be it in the code or settings as ApplyStop is simulating a brokers conditional order.
In a backtest an ApplyStop used in a weekly setting may even seem to be looking at future data.
With ExitAtStop = 2 sell orders that were triggered between Mon-Thu(16/3-19/3 example dates) will be consolidated into the current weekly bar (16-20/3 -> 20/3 weekly bar) as the NEXT BAR is Tue-Fri (17-20/3).
An order triggered on Friday(20/3) will be in next weekly bar (22-26/3 -> 26/3 weekly bar) as Next Monday(22/3) is the NEXT BAR.
The issue is in the first instance where the trades are made in the current weekly bar.
Code:
BuyPrice = Open; //Buy the next day at open
SellPrice = Open; // Sell the next day at open
So a future trigger is selling at Monday's price.
Code:
SetOption( "MaxOpenPositions", PosQty); // Maximum number of open position
So a future trigger is initiating a buy order a week earlier.
Code:
ts1 = 20; // Trailing stop one = set at 20% when the Index Filter is TRUE
ts2 = 10; // Trailing stop two = reduces to 10% when the Index Filter is FALSE
ts = IIf( indexbuyfilter , ts1 , ts2 ); // If the Index Filter is TRUE use (ts1) set at 20%, if the Index Filter is FALSE use (ts2) settings reduce Trail Stop to 10%
ApplyStop( stopTypeTrailing , stopModePercent , ts , exitatstop = 2 );
The first instance uses a loop instead of ApplyStop. This is the best way to go for a weekly system.I would like to do it without the applystop function; as the applystop doesnt spit out trades in explorer.
The second example uses Equity(1) to output all the Sells from ApplyStop but still has all the issues I've stated above.