Scotty B
New member
Hey All,
Probably an easy problem, but I'm trying to backtest a simple strategy that uses a crossover of an MA for entry and set hard targets and stops. The application will be to trade /MES on the 5min for quick 10pt scalps. So, buy/sell signal when the crossover happens and set stops at 5pts away with 10pt target. My problems is that entry signal generates just fine but the positions aren't closing out properly. For instance, I'll get a long entry signal but no sell signal, especially in choppy action. I'm still learning scripting and the addOrder function is probably the source of the problem. Any help would be appreciated. (The Short orders have been commented out for easier testing, but would like those to work as well)
Thanks!
Probably an easy problem, but I'm trying to backtest a simple strategy that uses a crossover of an MA for entry and set hard targets and stops. The application will be to trade /MES on the 5min for quick 10pt scalps. So, buy/sell signal when the crossover happens and set stops at 5pts away with 10pt target. My problems is that entry signal generates just fine but the positions aren't closing out properly. For instance, I'll get a long entry signal but no sell signal, especially in choppy action. I'm still learning scripting and the addOrder function is probably the source of the problem. Any help would be appreciated. (The Short orders have been commented out for easier testing, but would like those to work as well)
Thanks!
Code:
# Strategy for moving average crossover
# with set targets and stop loss
# Inputs
input fastLength = 13;
input slowLength = 21;
input averageType = AverageType.HULL;
input Size = 1;
input offsetType = {percent, default value, tick};
input Stop = 5;
input Target = 10;
plot FastMA = MovingAverage(averageType, close, fastLength);
plot SlowMA = MovingAverage(averageType, close, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
def entryPrice = entryPrice();
# Determine type of stops
def mult;
switch (offsetType) {
case percent:
mult = entryPrice / 100;
case value:
mult = 1;
case tick:
mult = tickSize();
}
# Define Entry Signals
def LongE = FastMA crosses above SlowMA;
def ShortE = FastMA crosses below SlowMA;
# Entry Orders
AddOrder(OrderType.BUY_AUTO, LongE, Close[0], Size, tickColor = GetColor(1), arrowColor = GetColor(1), name = "LE" + Close[0]);
#AddOrder(OrderType.SELL_AUTO, ShortE, Close[0], Size, tickColor = GetColor(2), arrowColor = GetColor(2), name = "SE" + Close[0]);
# Define Exit criteria
def LongTarget = entryPrice + target * mult;
def LongStop = entryPrice - Stop * mult;
def ShortTarget = entryPrice - target * mult;
def ShortStop = entryPrice + Stop * mult;
# Close position at Target or Stop
addOrder(OrderType.SELL_TO_CLOSE, high >= LongTarget, LongTarget, Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "LTarget" + LongTarget);
addOrder(OrderType.SELL_TO_CLOSE, low <= LongStop, LongStop, Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "LStop" + LongStop);
#addOrder(OrderType.SELL_TO_CLOSE, low <= ShortTarget, open[-1], Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "STarget");
#addOrder(OrderType.SELL_TO_CLOSE, high >= ShortStop, open[-1], Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "SStop");