I have tried very diligently to find the answer to my question across the forums and tried:
ExRem(),
Filp(),
if( Nz( StaticVarGet("InitializationDone") ) == 0 )
{
StaticVarSet("InitializationDone", 1);
InUnfilteredTrade = False;}
But to date I have not been able to get Ref(InUnfilteredTrade, -1) to == the value from the previous bar. I have not been susessful with Flip() nor ExRem() becuase the second array is not defined only after array1 is true and is not valid if array1 gets a second buy signal because BarsSince() and Hold() stop being accurate on the second signal. Hold() didn't work because the current condition for closing are EITHER # of bars pass OR TimeNum() == ExitTime
I'm using a form of optimization that completely eliminates trades based on custom column values. But the whole trade, for the whole duration it would have had needs to be a No Entry Window otherwise I just get a delayed entry.
Here's the code in it's current version:
// eliminate the ability to make a "late trade" when trying to filter out(completely skip) trades by creating a No Buy Window after the original unfiltered Trade signal is triggered.
InUnfilteredTrade = False; // Initialize the flag variable for the first bar (this will also fill 0/False in every bar unless we change it)
// check if unfiltered Trades are entered and update the flag.
FlagPlotDistance = SelectedValue(Low) * .99; // chart values 1% below the selected bar to avoid chart compression (this may need to be changed based on security type)
Plot(Ref(InUnfilteredTrade, -1)+FlagPlotDistance+20, "InUnfilteredTrade", IIf(InUnfilteredTrade, colorGold, colorDarkOliveGreen), styleDots);
InUnfilteredTrade = IIf(buyTriggerUnfiltered
, True
, Ref(InUnfilteredTrade, -1) // trying to refer to the previous value but it's always reset to FALSE since the end of the code. I have charted it before and after the code and even if true at the end, it's false on the next bar, (or set to false by the variable initialization)
);
BarsSinceUnfilteredEntry = iif(InUnfilteredTrade, BarsSince(InUnfilteredTrade AND NOT Ref(InUnfilteredTrade,-1)), -1); // InUnfilteredTrade has not been calculated yet for current bar, using previous bars' info
UnFilteredEntryPrice = IIf(BarsSinceUnfilteredEntry == 0, Max(Open, brkOutPrice), Null); // In previous code brkOutPrice is calculated based on previous bar already, no need to use Ref(xxx, -1).
// This entry price is for BuyStop at Market calculation Open if there is a gap up, brkOutPrice if High (of current candle) > brkOutPrice
UnFilteredPctStopPrice = IIf(BarsSinceUnfilteredEntry == 0 AND StopPercent > 0, UnFilteredEntryPrice * (1 - StopPercent), Null);
UnFilteredStopDollarPrice = IIf(BarsSinceUnfilteredEntry == 0 AND StopDollar > 0, UnFilteredEntryPrice - StopDollar, Null);
// Add other Stop and/or Profit Target calculations here in the same manner
// check if an unfiltered Trade ended and update the flag.
InUnfilteredTrade = iif(InUnfilteredTrade == True // This logic prevent an entry and immediate exit before other logic to
AND (ExitTime // Stop for End of day, End of trading period (ex. TimeNum() == 160000 for RTH), or other manually set end time
OR (BarsSinceEntry > 0 and BarsSinceUnfilteredEntry >= BarsSinceEntry) // For # of Bars Stop
OR (StopPercent > 0 AND Low < UnFilteredPctStopPrice) // For % stop trigger
OR (StopDollar > 0 AND Low < UnFilteredStopDollarPrice) // For absolute dollar stop trigger
// add other Stop loss or Profit Target calculations here in the same manner
), False, InUnfilteredTrade)
; // in AFL there are no indentation restrictions other than for your own visual useful, the end of a line it a ";". It is in its own line to easily add more conditions
// Post Filters from optimizer
PostFilter =
Ref(ImpVolRank(15 ), -1) < .4118
// add other filters here
;
buyTrigger = IIf(InUnfilteredTrade AND PostFilter, True, False); // tells backtester to buy on this candle
stopTrigger = Iif(NOT InUnfilteredTrade AND Ref(InUnfilteredTrade, -1), True, False); // tells backtester to execute a stop ApplyStop works on backtest trades only and we have manually calculated
// both live and unfiltered stops. This is to plot vs. AmiBroker arrows to make sure code is working as expected.
FlagPlotDistance = SelectedValue(Low) * .99; // chart values 1% below the selected bar to avoid chart compression (this may need to be changed based on security type)
Plot( C, "Price", colorDefault, styleCandle );
Plot( FlagPlotDistance-5, "FlagPlotDistance", colorDefault, styleDashed );
Plot(InUnfilteredTrade+FlagPlotDistance, "InUnfilteredTrade", IIf(InUnfilteredTrade, colorGold, colorDarkOliveGreen), styleDots);
Plot(BarsSinceUnfilteredEntry+FlagPlotDistance-5, "BarsSinceUnfilteredEntry", colorLightYellow , styleDots);
Plot(UnFilteredEntryPrice, "UnFilteredEntryPrice", IIf(InUnfilteredTrade, colorBrightGreen, colorDarkOliveGreen), styleDots);
Plot(UnFilteredPctStopPrice, "UnFilteredPctStopPrice", IIf(InUnfilteredTrade>0, colorLightOrange, colorDarkOliveGreen), styleDots);
Plot(UnFilteredStopDollarPrice, "UnFilteredStopDollarPrice", IIf(UnFilteredStopDollarPrice, colorLightYellow, colorDarkYellow), styleDots);
Plot(PostFilter+FlagPlotDistance-1, "PostFilter", IIf(PostFilter, colorLightBlue, colorTeal), styleDots);
Plot(buyTriggerUnfiltered+FlagPlotDistance+10, "buyTriggerUnfiltered", IIf(buyTriggerUnfiltered, colorLightBlue, colorTeal), styleDots);