Help Needed With Fixed Price Risk/Reward Level SLs & Targets Set At Entry Candle and Strategy Orders
Hello everyone, I've been lurking here for a while and as an amateur coder at best I've benefited so much from the expertise and knowledge of those in this community for which I wanted to first of all say thank you for.
I am currently in the final stages of dev on a multi-strategy system that I plan on posting on this site once I'm finished as my contribution; and therefor am converting a study into a strategy for the first time to efficiently fine tune and finalize the signals and this is where I have run into trouble.
I created this system around R level targets which are dependent on the entry that is signaled and specific conditions that are met. As such in order for the backtest to be effective and accurate I need both the R level(s) and SL that are determined at the entry candle to extend until either the target exit level or the stop loss levels are reached.
The stop losses are measured by the size of the signal candle from the close of that candle or an ATR multiple depending on the signal and specific conditions (not included in the attached code but is in the study version with all of the other signals), position size is automatically calculated based on the maximum loss input in relation to the stop loss price and displayed in the signal bubble.
Issue Example 1 (5/26, 12:25PM):
You can see here that the lines do extend from the R and SL levels set by the entry candle size from entry candle close but they fail to reach far enough for the target line to be reached. The trade then carries over to the next short signal 2 days after..... bad.
So really the issue is just that the lines are inconsistent and I lack the knowledge to make them consistently extend until they are crossed.
Another example, this one the lines extending super far (7/17 3:20PM):
While this specific problem is okay for this signal because it goes off pretty rarely, some go off 2-4 times a day and so them triggering from lines that continue on from a past trade that occurred that day of the same strategy is problematic.
As with any system, what really makes it work is the trade management and risk management and once the problem of extending the lines forward in time has been solved I am also hoping to create orders for my different conditional safety trims, early exits, trims for profit (some strategies exit half at 1R to lock profit and then leave runners that go until certain conditions are met etc) so the final backtesting results I will include when I post the final system here will have results as accurate to the system's performance live as possible.
Link to dev grid with full dev version of system: https://tos.mx/kgVKZGb
Code for the specific signal I have been using to convert the study into a strategy:
Note:
I have not added in Buy To Close and Sell To Close EOD orders yet for trades that do not meet SL or Targets as can be seen, wanted to address the more glaring issues of the SL and Target line extensions first.
Hello everyone, I've been lurking here for a while and as an amateur coder at best I've benefited so much from the expertise and knowledge of those in this community for which I wanted to first of all say thank you for.
I am currently in the final stages of dev on a multi-strategy system that I plan on posting on this site once I'm finished as my contribution; and therefor am converting a study into a strategy for the first time to efficiently fine tune and finalize the signals and this is where I have run into trouble.
I created this system around R level targets which are dependent on the entry that is signaled and specific conditions that are met. As such in order for the backtest to be effective and accurate I need both the R level(s) and SL that are determined at the entry candle to extend until either the target exit level or the stop loss levels are reached.
The stop losses are measured by the size of the signal candle from the close of that candle or an ATR multiple depending on the signal and specific conditions (not included in the attached code but is in the study version with all of the other signals), position size is automatically calculated based on the maximum loss input in relation to the stop loss price and displayed in the signal bubble.
Issue Example 1 (5/26, 12:25PM):

You can see here that the lines do extend from the R and SL levels set by the entry candle size from entry candle close but they fail to reach far enough for the target line to be reached. The trade then carries over to the next short signal 2 days after..... bad.
So really the issue is just that the lines are inconsistent and I lack the knowledge to make them consistently extend until they are crossed.
Another example, this one the lines extending super far (7/17 3:20PM):

While this specific problem is okay for this signal because it goes off pretty rarely, some go off 2-4 times a day and so them triggering from lines that continue on from a past trade that occurred that day of the same strategy is problematic.
As with any system, what really makes it work is the trade management and risk management and once the problem of extending the lines forward in time has been solved I am also hoping to create orders for my different conditional safety trims, early exits, trims for profit (some strategies exit half at 1R to lock profit and then leave runners that go until certain conditions are met etc) so the final backtesting results I will include when I post the final system here will have results as accurate to the system's performance live as possible.
Link to dev grid with full dev version of system: https://tos.mx/kgVKZGb
Code for the specific signal I have been using to convert the study into a strategy:
Code:
input MaxLoss = 1500;
input rtarget = 1.5;
input R_Lane1_Active = yes;
def CS = high - low;
def TickSize = TickSize(GetSymbol());
def TickValue = TickValue(GetSymbol());
def length_rlevel = 50;
###########################____________________ R Lane 1 ____________________###########################
#RSI OB/OS >= 30 , BB Behind Proximity <= 10 , BB Behind Direction >=.6 & <=1.26 , Stoch Change > 1
def length_rlevel = 50;
#______________ Long ______________#
#signal
def l_Rlane1 = if TimeCheck2 and
l_r_lane == 1 and
R_Lane1_Active == yes and
RSI_OB >= 30 and
bb_lp <= 10 and
(bb_ld >= .6 and bb_ld <= 1.26) and
Bullish_Stoch_Change_Valid == 1 then 1 else 0;
plot l_Rlane1_es = if l_Rlane1 == 1 then low else DOUBLE.NAN;
l_Rlane1_es.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
l_Rlane1_es.AssignValueColor(Color.CYAN);
l_Rlane1_es.SetLineWeight(3);
#stop loss
plot l_Rlane1_sl = if l_Rlane1 == 1 then open[-1] - CS else Double.NaN;
l_Rlane1_sl.SetPaintingStrategy(PaintingStrategy.DASHES);
l_Rlane1_sl.AssignValueColor(Color.WHITE);
l_Rlane1_sl.SetLineWeight(1);
# target
plot l_Rlane1_t = if l_Rlane1 == 1 then open[-1] + (CS * rtarget) else Double.NaN;
l_Rlane1_t.SetPaintingStrategy(PaintingStrategy.DASHES);
l_Rlane1_t.AssignValueColor(Color.WHITE);
l_Rlane1_t.SetLineWeight(1);
#bubble
def l_Rlane1_MaxSize = Round(MaxLoss / (((close - l_Rlane1_sl) / TickSize) * TickValue), 1);
AddChartBubble(l_Rlane1, l_Rlane1_sl - (ATR * .75), "RL1 // " + l_Rlane1_MaxSize, GlobalColor("Bullish Bubble"));
# Entry Order
AddOrder(OrderType.BUY_TO_OPEN, l_Rlane1 == 1, open[-1], l_Rlane1_maxsize, Color.CYAN, Color.CYAN, "L Rlane1 // " + rtarget);
def l_Rlane1_count =
if IsNaN(l_Rlane1) and
!IsNaN(l_Rlane1[1]) then 1 else
l_Rlane1_count[1] + 1;
# Target Exit
plot l_Rlane1_t_ext = if IsNaN(l_Rlane1) then GetValue(l_Rlane1_t, l_Rlane1_count) else GetValue(l_Rlane1_t, l_Rlane1_count);
l_Rlane1_t_ext.SetPaintingStrategy(PaintingStrategy.DASHES);
l_Rlane1_t_ext.AssignValueColor(Color.WHITE);
l_Rlane1_t_ext.SetLineWeight(1);
def l_Rlane1_targetmet = if high >= l_Rlane1_t_ext then 1 else 0;
#def l_Rlane1_targetmet2 = if (high >= l_Rlane1_t_ext) then (l_Rlane1_t_ext) else (Highest(high[1], length_rlevel));
AddOrder(OrderType.SELL_TO_CLOSE, l_Rlane1_targetmet, l_Rlane1_t_ext, l_Rlane1_maxsize, Color.MAGENTA, Color.MAGENTA, "L Rlane1 // " + rtarget);
# SL Exit
plot l_Rlane1_sl_ext = if IsNaN(l_Rlane1) then GetValue(l_Rlane1_sl, l_Rlane1_count) else GetValue(l_Rlane1_sl, l_Rlane1_count);
l_Rlane1_sl_ext.SetPaintingStrategy(PaintingStrategy.DASHES);
l_Rlane1_sl_ext.AssignValueColor(Color.WHITE);
l_Rlane1_sl_ext.SetLineWeight(1);
def l_Rlane1_slmet = if low <= l_Rlane1_sl_ext then 1 else 0;
#def l_Rlane1_slmet2 = if (low <= l_Rlane1_sl_ext) then (l_Rlane1_sl_ext) else (Lowest(low[1], length_rlevel));
AddOrder(OrderType.SELL_TO_CLOSE, l_Rlane1_slmet, l_Rlane1_sl_ext, l_Rlane1_maxsize, Color.MAGENTA, Color.MAGENTA, "L Rlane1 // SL");
#______________ Short ______________#
#signal
def s_Rlane1 = if TimeCheck2 and
s_r_lane == 1 and
R_Lane1_Active == yes and
RSI_OS >= 30 and
bb_up <= 10 and
(bb_ud >= .6 and bb_ud <= 1.26) and
Bearish_Stoch_Change_Valid == 1 then 1 else 0;
plot s_Rlane1_es = if s_Rlane1 == 1 then high else DOUBLE.NAN;
s_Rlane1_es.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
s_Rlane1_es.AssignValueColor(Color.CYAN);
s_Rlane1_es.SetLineWeight(3);
#stop loss
plot s_Rlane1_sl = if s_Rlane1 == 1 then open[-1] + CS else Double.NaN;
s_Rlane1_sl.SetPaintingStrategy(PaintingStrategy.DASHES);
s_Rlane1_sl.AssignValueColor(Color.WHITE);
s_Rlane1_sl.SetLineWeight(1);
# target
plot s_Rlane1_t = if s_Rlane1 == 1 then open[-1] - (CS * rtarget) else Double.NaN;
s_Rlane1_t.SetPaintingStrategy(PaintingStrategy.DASHES);
s_Rlane1_t.AssignValueColor(Color.WHITE);
s_Rlane1_t.SetLineWeight(1);
#bubble
def s_Rlane1_maxsize = Round(MaxLoss / (((s_Rlane1_sl - close) / TickSize) * TickValue), 1);
AddChartBubble(s_Rlane1, s_Rlane1_sl + (ATR * .1), "RL1 // " + s_Rlane1_maxsize, GlobalColor("Bearish Bubble"));
# Entry Order
AddOrder(OrderType.Sell_TO_OPEN, s_Rlane1 == 1, open[-1], s_Rlane1_maxsize, Color.CYAN, Color.CYAN, "S Rlane1 // " + rtarget);
def s_Rlane1_count =
if IsNaN(s_Rlane1) and
!IsNaN(s_Rlane1[1]) then 1
else s_Rlane1_count[1] + 1;
# Target Exit
plot s_Rlane1_t_ext = if IsNaN(s_Rlane1) then GetValue(s_Rlane1_t, s_Rlane1_count) else GetValue(s_Rlane1_t, s_Rlane1_count);
s_Rlane1_t_ext.SetPaintingStrategy(PaintingStrategy.DASHES);
s_Rlane1_t_ext.AssignValueColor(Color.WHITE);
s_Rlane1_t_ext.SetLineWeight(1);
def s_Rlane1_targetmet = if low <= s_Rlane1_t_ext then 1 else 0;
#def s_Rlane1_targetmet2 = if (low <= s_Rlane1_t_ext) then (s_Rlane1_t_ext) else (Lowest(low[1], length_rlevel));
AddOrder(OrderType.Buy_TO_CLOSE, s_Rlane1_targetmet, s_Rlane1_t_ext, s_Rlane1_maxsize, Color.MAGENTA, Color.MAGENTA, "S Rlane1 // " + rtarget);
# SL Exit
plot s_Rlane1_sl_ext = if IsNaN(s_Rlane1) then GetValue(s_Rlane1_sl, s_Rlane1_count) else GetValue(s_Rlane1_sl, s_Rlane1_count);
s_Rlane1_sl_ext.SetPaintingStrategy(PaintingStrategy.DASHES);
s_Rlane1_sl_ext.AssignValueColor(Color.WHITE);
s_Rlane1_sl_ext.SetLineWeight(1);
def s_Rlane1_slmet = if high >= s_Rlane1_sl_ext then 1 else 0;
#def s_Rlane1_slmet2 = if (high >= s_Rlane1_sl_ext) then (s_Rlane1_sl_ext) else (Highest(high[1], length_rlevel));
AddOrder(OrderType.Buy_TO_CLOSE, s_Rlane1_slmet, s_Rlane1_sl_ext, s_Rlane1_maxsize, Color.MAGENTA, Color.MAGENTA, "S Rlane1 // SL");
Attachments
Last edited by a moderator: