General Strategy Question


New member
Hey guys, trying to backtest some different strategies and wondering if you guys can help give me some direction because i am struggling...I'm using certain boolean conditions to define a buy/sell setup to open a trade. When the condition is true for a bar, how do I get and hold values from the bar right before the condition triggers? As an example, if X happens on bar 5, then I would like to use the low value of bar 4 as a stop, and then take the difference from entryprice to define a target. I know this is a vague example, so here is one I am working on for supertrend. Basically, I look for the price to flip color (above/below supertrend) and then if the following bar retraces a certain percentage of the "flip" bar's range (around 50%) initiate a long/short. I would then use the low/high of the bar before the "priceflip" as a stop and use that as a target multiple.

Below is an excerpt (not complete code) from the strategy i described above. Keep in mind, this script already has ST defined I just didn't think I needed to include in this post. Also, i have the bool conditions as plots instead of def's to help me while I debug

input fib = .25;   #retrace percentage for bar following flip
input mult = 3;  #target multiple
plot D1 = HL2 crosses above ST;  #priceflip (condition 1)

def range = high - low;
plot D2 = D1 and low[-1] <= high - (fib * (range));  # price flip day 1 and day 2 retrace below fib percentage to take long (condition 2)

AddOrder(OrderType.BUY_TO_OPEN, D2, price = high - (fib * range), name = "LE");

def entry = EntryPrice();
plot stopline=if entry!=0 then low[1] else 0; #the low of bar right before flip. This is what I can't figure out because it carries the low onto following bars instead of being static
def target= entry+(mult*(entry-stopline));

AddOrder(OrderType.SELL_TO_CLOSE, low crosses below stopline, price = stopline, name = "LES");
AddOrder(OrderType.SELL_TO_CLOSE, high crosses above target, price = target, name = "LEX");

##End Code

This is pretty simple but I am not a coding expert. Thanks in advance for any insight!

Similar threads