• The price of VIP membership is increasing to $199 beginning June 1. Click here to lock in the current rate of $149.

Long trade works but not short trade

danwilks12

New member
Basically title. Both codes are the same except for the appropriate variables changed (I think) to make the 2nd code a short trade. Is there any glaringly obvious problem here? 1st code is the working long trade and second is the not working short trade. Pic is the long trade being opened and closed properly (opened on -2 deviations and closed on the middleline) and then when the condition to open the short trade is hit (price is >= +1 deviation), nothing happens. I'm also running this on the SPY which should be shortable? Any help is greatly appreciated, thanks.

LONG CODE:

#Inputs

input tradesize = 1;
input price = close;
input deviations = 2.0;
input fullRange = Yes;
input length = 21;

#Define

def regression;
def stdDeviation;
if (fullRange) {
regression = InertiaAll(price);
stdDeviation = StDevAll(price);
} else {
regression = InertiaAll(price, length);
stdDeviation = StDevAll(price, length);
}

def UpperLine = regression + deviations * stdDeviation;
def MiddleLine = regression;
def LowerLine = regression - deviations * stdDeviation;

#Signal

def Condition1 = if
price[1] <= LowerLine
then 1 else Double.NaN;

#Orders

AddOrder(OrderType.BUY_TO_OPEN, Condition1, open, tradesize, Color.CYAN, Color.CYAN);

#Alert
Alert(Condition1, "Long Trade Placed", Alert.ONCE, Sound.NoSound);

SHORT CODE:

#Inputs

input tradesize = 1;
input price = close;
input deviations = 1.0;
input fullRange = Yes;
input length = 21;

#Define

def regression;
def stdDeviation;
if (fullRange) {
regression = InertiaAll(price);
stdDeviation = StDevAll(price);
} else {
regression = InertiaAll(price, length);
stdDeviation = StDevAll(price, length);
}

def UpperLine = regression + deviations * stdDeviation;
def MiddleLine = regression;
def LowerLine = regression - deviations * stdDeviation;

#Signal

def Condition1 = if
price[1] >= Upperline
then 1 else Double.NaN;

#Orders

AddOrder(OrderType.SELL_TO_OPEN, Condition1, open, tradesize, Color.CYAN, Color.CYAN);

#Alert
Alert(Condition1, "Short Trade Placed", Alert.ONCE, Sound.NoSound);

g2GVlBr.png
 
Last edited:

Similar threads

Top