Backtesting AddOrder Fills

dannyboi11

New member
VIP
Relevant facts:
1. Variation of Black Dog set up trading /ES.
2. Price target strategy, no exit indicator.

Code:
Code:
#SES = Standard Entry Signal

#__________INPUTS__________
input tradesize = 2;
input price = close;
input pricetype = PriceType.LAST;
input Hprice = high;
input Lprice = low;
input Hlength = 50;
input Llength = 50;
input Hdisplace = 0;
input Ldisplace = 0;
input tickgaingoal = 10;
input ticklossstop = -5;

#__________DEFINE__________
#Change Aggregation Period to 5 minutes (SES)
input agg5min = AggregationPeriod.FIVE_MIN;
def data5min = close(period = agg5min);

#2 EMAs of SES (5min) (SES)
def SESfastEMA = ExpAverage(data5min, 3);
def SESslowEMA = ExpAverage(data5min, 50);

#SES UP for EMA3 crossing ABOVE EMA50
def UpCrossSES = if SESfastEMA > SESslowEMA and SESfastEMA[1] < SESslowEMA then 1 else 0;

#SES Down for EMA3 crossing BELOW EMA50
def DnCrossSES = if SESfastEMA < SESslowEMA and SESfastEMA[1] > SESslowEMA then 1 else 0; 

#Change Aggregation Period to 20 minutes (BlackDog)
input agg20min = AggregationPeriod.TWENTY_MIN;
def data20min = close(period = agg20min);

#2 EMAs of Black Dogs (20min)
def BDfastEMA = ExpAverage(data20min, 20);
def BDslowEMA = ExpAverage(data20min, 100);

#Black Dog UP for EMA20 crossing ABOVE EMA100
def UpCrossBD = if BDfastEMA > BDslowEMA and BDfastEMA[1] < BDslowEMA then 1 else 0;

#Black Dog DN for EMA20 crossing BELOW EMA100
def DnCrossBD = if BDfastEMA < BDslowEMA and BDfastEMA[1] > BDslowEMA then 1 else 0;

#Adjust time to only trade during open market hours
input usetimefilter = Yes;
input rthopen = 0701;
input rthclose = 1455;
def RTH = if usetimefilter == yes then if SecondsFromTime(rthopen) >= 0 and
               SecondsTillTime(rthclose) >= 0
            then 1
            else 0 else 1;

#__________PLOTS__________
#EMA of HIGHS
plot HAvg = MovAvgExponential(Hprice[-Hdisplace], Hlength);

HAvg.SetDefaultColor(Color.WHITE);
HAvg.SetLineWeight(1);
HAvg.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
HAvg.SetStyle(Curve.FIRM);
HAvg.HideBubble();
HAvg.HideTitle();

HAvg.AssignValueColor(if HAvg < HAvg[1] then CreateColor(255, 204, 204) else (if HAvg == HAvg[1] then CreateColor(0, 255, 0) else CreateColor(0, 255, 0)));

#EMA of LOWS
plot LAvg = MovAvgExponential(Lprice[-Ldisplace], Llength);

LAvg.SetDefaultColor(Color.WHITE);
LAvg.SetLineWeight(1);
LAvg.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
LAvg.SetStyle(Curve.FIRM);
LAvg.HideBubble();
LAvg.HideTitle();

LAvg.AssignValueColor(if LAvg < LAvg[1] then CreateColor(255, 204, 204) else (if LAvg == LAvg[1] then CreateColor(0, 255, 0) else CreateColor(0, 255, 0)));

#SES UP for EMA3 crossing ABOVE EMA50
plot SESup = if UpCrossSES then high else Double.NaN;

SESup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SESup.SetLineWeight(5);
SESup.SetDefaultColor(CreateColor(7, 237, 253));
SESup.HideBubble();
SESup.HideTitle();

#SES Down for EMA3 crossing BELOW EMA50
plot SESdn = if DnCrossSES then low else Double.NaN;

SESdn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SESdn.SetLineWeight(5);
SESdn.SetDefaultColor(CreateColor(255, 0, 127));
SESdn.HideBubble();
SESdn.HideTitle();

#Black Dog UP for EMA20 crossing ABOVE EMA100
plot BDup = if UpCrossBD then high else Double.NaN;

BDup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BDup.SetLineWeight(5);
BDup.SetDefaultColor(Color.WHITE);
BDup.HideBubble();
BDup.HideTitle();

#Black Dog DN for EMA20 crossing BELOW EMA100
plot BDdn = if DnCrossBD then low else Double.NaN;

BDdn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BDdn.SetLineWeight(5);
BDdn.SetDefaultColor(Color.WHITE);
BDdn.HideBubble();
BDdn.HideTitle();

#__________ORDERS__________
#Long
def tickchange = close - Entryprice();
def longbuySignal = UpCrossSES;
def exitGood = tickchange > tickgaingoal;
def exitBad = tickchange < ticklossstop;

#Short

#__________SALES__________
#Long
AddOrder(OrderType.BUY_TO_OPEN, longbuySignal, Hprice + 0.5, tradesize, Color.WHITE);
AddOrder(OrderType.SELL_TO_CLOSE, exitGood, EntryPrice() + 10);
AddOrder(OrderType.Sell_TO_CLOSE, exitBad, EntryPrice () - 5);


#Short


#__________LABELS__________
def StateUp = 1;
def StateDn = 2;
def LastXState;

if UpCrossBD {
    LastXState = StateUp;

} else if DnCrossBD {

    LastXState = StateDn;

} else {

    LastXState = LastXState[1];

}
AddLabel(1, "Last BD Cross Direction = " + if LastXState == StateUp then "UP" else if LastXState == StateDn then "DOWN" else "NOP",

    if LastXState == StateUp then CreateColor(0, 255, 0) else if LastXState == StateDn then CreateColor(255, 204, 204) else Color.YELLOW);

#__________ALERTS__________
Alert(UpCrossSES, "SES Buy Signal", Alert.BAR, Sound.Ring);
Alert(DnCrossSES, "SES Sell Signal", Alert.BAR, Sound.Bell);

Questions:
1. How do I modify the script to only enter trades if the fill criteria is satisfied (e.g. candle closes above 50 High EMA, enter when price climbs 2 ticks above high of the candle that closed above 50 High EMA). Right now the "order" is being filled 2 ticks above the high of the close candle, regardless on if it is filled in the next or following candles. I assume I would also have to add a time period or # of following candles to be considered before the unfilled "order" is cancelled.

2. How do I modify the script to close trades when it hits my price target? It appears as though its closing one if not more candles further than when I would have been filled to exit the trade.
 

rad14733

Well-known member
VIP
@dannyboi11 Why are you including aggregation into your trading strategy rather than just using chart timeframe data...??? That may well be at least part of the problem you are experiencing... Don't try to overthink and over-complicate backtesting strategies because they are already quirky enough all by themselves... You will never get the exact results you are expecting and that is one reason why I have given up on trying to prove my trade logic using them... I keep track of my backtesting on paper or using a spreadsheet instead... I've wasted far too much time and effort trying to force backtesting code into submission...
 

Similar threads

Top