I have been fleshing out a system and decided I'd like to run a strategy against it to remove my bias. I have been slowly adding code to it and have run in to a failure that I just can't seem to figure out.
The general idea is that the order entry criteria is true (Signal_Dn2) I want to short at that price (entryPrice). I need to capture that price as a variable so that I can use that value as a trade exit (exitPrice = entryPrice - points). I plot that number on the chart for verification... which it does (getout). The problem with the whole thing is that it doesn't "close" the trade. I have tried as much stuff as I can for the exit criteria (btcCondition) and the BTC order. Please teach me what I'm doing wrong here! Thanks in advance.
The general idea is that the order entry criteria is true (Signal_Dn2) I want to short at that price (entryPrice). I need to capture that price as a variable so that I can use that value as a trade exit (exitPrice = entryPrice - points). I plot that number on the chart for verification... which it does (getout). The problem with the whole thing is that it doesn't "close" the trade. I have tried as much stuff as I can for the exit criteria (btcCondition) and the BTC order. Please teach me what I'm doing wrong here! Thanks in advance.
Code:
input start1 = 1900;
input end1 = 2359;
input start2 = 0001;
input end2 = 1500;
input contracts = 1;
input points = 2;
input losslimit = 2;
input price = close;
input length = 20;
input displace = 0;
def HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
def isUptrend = HMA > HMA[1];
def withinSession1 = secondsTillTime(end1) >= 0 and secondsFromTime(start1) >= 0;
def withinSession2 = secondsTillTime(end2) >= 0 and secondsFromTime(start2) >= 0;
def ResetCondition1 = secondsFromTime(start1) == 0;
def gap = reference VWAP()."UpperBand" - reference VWAP()."LowerBand" > 5;
def up = close is less than reference VWAP()."LowerBand" and gap;
def down = close is greater than reference VWAP()."UpperBand" and gap;
def Hold_Up_Value = if ResetCondition1 then 0 else if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if ResetCondition1 then 0 else if down then 1 else if !up then Hold_Dn_Value[1] else 0;
plot Signal_Dn2 = if withinSession2 and down and down[1] == 0 and Hold_Dn_Value[1] == 0 and gap then low else Double.NaN;
Signal_Dn2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Signal_Dn2.SetDefaultColor(Color.DOWNTICK);
Signal_Dn2.SetLineWeight(3);
def entryPrice = if Signal_Dn2 then close else Double.NaN;
def exitPrice = entryPrice - points;
def lossPrice = entryPrice + losslimit;
plot getout = if Signal_Dn2 then exitPrice else Double.NaN;
getout.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
def btcCondition = close < exitPrice;
addOrder(OrderType.SELL_TO_OPEN, Signal_Dn2, close, contracts);
addOrder(OrderType.BUY_TO_CLOSE, btcCondition, exitPrice, contracts);
# Resetting holding register to nothing after closing the trade
def resetRegister = if IsNaN(close) then Double.NaN else resetRegister[1];