BUY Stop Order back test

Hi All

I am trying to implement a buy stop order, based on the the stop price being 1.0025 * previous day high

code is here:

#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"
SetBacktestMode(backtestRegular);
OnSecondLastBarOfDelistedSecurity = BarIndex() == (LastValue(BarIndex()) -1) AND !IsNull(GetFnData("DelistingDate"));
OnLastTwoBarsOfDelistedSecurity = BarIndex() >= (LastValue(BarIndex()) -1) AND !IsNull(GetFnData("DelistingDate"));
inindex  = NorgateIndexConstituentTimeSeries ("$spx");
SetTradeDelays(0,1,0,0);


FMA = MA(C,40);
SMA = MA(C,120);


// position size

range = C - ref(llV(l,10),-1) ;
limitpercent  = 1.0025;
buystopprice = Ref( h, -1) * limitpercent;

// risk % of entire equity on single trade
PositionRisk = 1.25;

// position size calculation

PctSize =  PositionRisk * buystopprice / (range +1e-9);

SetPositionSize( Min(pctsize,20), spspercentofequity );

buysignal = Cross(fma,sma) AND NOT OnLastTwoBarsOfDelistedSecurity AND inindex ;


Buy = (Ref(buysignal,-1)) ;
Buy = Buy AND    Cross(High,buystopprice);
BuyPrice = max(  BuystopPrice,l );


Sell = Cross(sma,fma) OR OnLastTwoBarsOfDelistedSecurity;
ApplyStop(0,2,range,1);

when I review the back test it sometimes has buy prices lower than the days low, which should not be possible

any ideas what I am doing wrong?

thanks

A screenshot of a specific example would help.

Also, what have you tried from How do I debug my formula? to identify the fault?

It doesn’t appear that you’re taking opening gaps into account. If the opening price is greater than your Buy Stop price, then you should set BuyPrice to Open. Specifically, I would change this:

BuyPrice = max( BuystopPrice,l );

To this:

BuyPrice = max( BuystopPrice,Open);

Worst case, however, it appears that your BuyPrice should have been equal to the low, not less than the low. Follow @HelixTrader’s advice on how to debug your formula.

thanks

think it was to do with trade delays

I have now tried to develop it further, using a buy signal and no trading delays

I get no trades

when I remove the buy signal and add a trading delay I get trades and signals

is there something peculiar I am doing with the buy signal? do I need to move the cross to the buy signal?

Gapu = GapUp() AND C > O;
Gapd = GapDown() AND C < O;

Buysignal = gapu AND liquid AND inindex
 AND strongbull
 AND bullin AND NOT OnLastTwoBarsOfDelistedSecurity ;

// if Open price is above the stop, then Open for entry
BuyPrice = max(  BuystopPrice,o);
Buy = (Ref(buysignal,-1))  AND Cross(H,buystopprice);

Have you tried using an Exploration to output all the variables that Buy and BuyPrice are dependent on? That will usually reveal where your assumptions differ from the actual data values.

My guess is that you may want to replace

Cross(H,buystopprice)

with

H > buystopprice

However, try the Explore approach, perhaps with

Filter = True

so that you will see output for all bars.

3 Likes

Hey
I think the h>buystopprice has sorted it!
thanks a million

Hi!
I had the same problem and this solution works for me.
What I don't understand is why cross doesn't!

State v Impluse.

3 Likes