simplify adjusting stoploss and take profit

Gilgameash_02

New member
need some help.
is there a simpler way to write this code?
and is the exit on events portion correct?


Rich (BB code):
#SHOULD CLOSE ON FRIDAYS, END OF DAY IF ENABLED.
#SHOULD CLOSE ON EVENTS BY DEFAULT
#SHOULD SELF ADJUST PROFIT TARGETS AND STOPLOSS ACCORDINGLY.
#def=HMA IS NOT THE HULL MOVING AVERAGE

input Close_EndofDay =no;
input Close_friday =yes;

def aggregationPeriod = getAggregationPeriod();


input ll=1;

def HMA=wildersSmoothing(ohlc4, length = ll);

input MIN_PROFIT=0.2;
def TIME = if SecondsTillTime(1555) == 0 and SecondsFromTime(1555) == 0 then 1 else 0;
def usetimefilter = yes;
def timeopen = 0930;
def timeclose = 1555;
def close_all = if usetimefilter == yes then if SecondsFromTime(timeopen) >= 0 and SecondsTillTime(timeclose) >= 0 then 1 else 0 else 1;
def trading_time = !TIME and close_all;



#should exit 1(?) bars before the trading day is over for most timeframes.

def timeFrame = GetAggregationPeriod();
def EOD = if timeFrame ==
AggregationPeriod.MIN and SecondsTillTime(1557) == 0 then 1 else if timeFrame ==
AggregationPeriod.TWO_MIN and SecondsTillTime(1556) == 0 then 1 else if timeFrame ==
AggregationPeriod.THREE_MIN and SecondsTillTime(1554) == 0 then 1 else if timeFrame ==
AggregationPeriod.FOUR_MIN and SecondsTillTime(1552) == 0 then 1 else if timeFrame ==
AggregationPeriod.FIVE_MIN and SecondsTillTime(1550) == 0 then 1 else if timeFrame ==
AggregationPeriod.TEN_MIN and SecondsTillTime(1540) == 0 then 1 else if timeFrame ==
AggregationPeriod.FIFTEEN_MIN and SecondsTillTime(1530) == 0 then 1 else if timeFrame ==
AggregationPeriod.TWENTY_MIN and SecondsTillTime(1530) == 0 then 1 else if timeFrame ==
AggregationPeriod.THIRTY_MIN and SecondsTillTime(1500) == 0 then 1 else if timeFrame ==
AggregationPeriod.HOUR and SecondsTillTime(1430) == 0 then 1 else if timeFrame ==
AggregationPeriod.TWO_HOURS and SecondsTillTime(1330) == 0 then 1 else 0;
def isTime= EOD;
addOrder(OrderType.SELL_TO_CLOSE, EOD and Close_EndofDay, tickColor = Color.CYAN, arrowColor = Color.CYAN, name = "SELL TO CLOSE End of Day");

addOrder(OrderType.BUY_TO_CLOSE,EOD and Close_EndofDay, tickColor = Color.CYAN, arrowColor = Color.CYAN, name = "BUY TO CLOSE End of Day");




#exit on Fridays, can this be written simpler? not sure if this even works correctly. 
def isFriday = GetDayOfWeek(GetYYYYMMDD()) == 5; # Friday 5 in thinkscript
def allFridays =
GetDayOfWeek(GetYYYYMMDD()[1]) == 5 or GetDayOfWeek(GetYYYYMMDD()[2]) == 5 or GetDayOfWeek(GetYYYYMMDD()[3]) == 5 or GetDayOfWeek(GetYYYYMMDD()[4]) == 5 or GetDayOfWeek(GetYYYYMMDD()[5]) == 5 or GetDayOfWeek(GetYYYYMMDD()[6]) == 5 or GetDayOfWeek(GetYYYYMMDD()[7]) == 5 or GetDayOfWeek(GetYYYYMMDD()[8]) == 5 or GetDayOfWeek(GetYYYYMMDD()[9]) == 5 or GetDayOfWeek(GetYYYYMMDD()[10]) == 5 or GetDayOfWeek(GetYYYYMMDD()[11]) == 5 or GetDayOfWeek(GetYYYYMMDD()[12]) == 5 or GetDayOfWeek(GetYYYYMMDD()[13]) == 5 or GetDayOfWeek(GetYYYYMMDD()[14]) == 5 or GetDayOfWeek(GetYYYYMMDD()[15]) == 5 or GetDayOfWeek(GetYYYYMMDD()[16]) == 5 or GetDayOfWeek(GetYYYYMMDD()[17]) == 5 or GetDayOfWeek(GetYYYYMMDD()[18]) == 5 or GetDayOfWeek(GetYYYYMMDD()[19]) == 5 or GetDayOfWeek(GetYYYYMMDD()[20]) == 5 or GetDayOfWeek(GetYYYYMMDD()[21]) == 5 or GetDayOfWeek(GetYYYYMMDD()[22]) == 5 or GetDayOfWeek(GetYYYYMMDD()[23]) == 5 or GetDayOfWeek(GetYYYYMMDD()[24]) == 5 or GetDayOfWeek(GetYYYYMMDD()[25]) == 5 or GetDayOfWeek(GetYYYYMMDD()[26]) == 5 or GetDayOfWeek(GetYYYYMMDD()[27]) == 5 or GetDayOfWeek(GetYYYYMMDD()[28]) == 5 or GetDayOfWeek(GetYYYYMMDD()[29]) == 5 or GetDayOfWeek(GetYYYYMMDD()[30]) == 5 or GetDayOfWeek(GetYYYYMMDD()[31]) == 5;
 # Checks previous days for Friday
def v = if isFriday and istime then 1 else 0;

addOrder(OrderType.SELL_TO_CLOSE,v and Close_friday, tickColor = Color.PLUM, arrowColor = Color.PLUM, name = "SELL TO CLOSE its Friday");

AddOrder(type = OrderType.BUY_TO_CLOSE,  v and Close_friday, arrowColor = Color.PLUM, name = "BUY TO CLOSE its Friday", tickColor = Color.PLUM);




#TEST EXIT 2 BARS BEFORE EVENTS

DEF VV=if ((HasEarnings()[-2]== 1) or (hasConferenceCall()[-2]== 1) ) then 1 else 0;

def hasEarningsWithin5Bars = if VV THEN  1 else 0;

DEF isEarningsWithin5Bars = if hasEarningsWithin5Bars > 0 then 1 else 0;
addOrder(OrderType.SELL_TO_CLOSE,  isEarningsWithin5Bars, tickColor = Color.BLUE, arrowColor = Color.BLUE, name = "SELL TO CLOSE EVENT PENDING?");

addOrder(OrderType.BUY_TO_CLOSE, isEarningsWithin5Bars, tickColor = Color.BLUE, arrowColor = Color.BLUE, name = "BUY TO CLOSE EVENT PENDING?");







###self adjusting Longs

def ep=entryPrice();
def    level1     = ep +(min_profit*    1    );
def    level2     = ep +(min_profit*    2    );
def    level3     = ep +(min_profit*    3    );
def    level4     = ep +(min_profit*    4    );
def    level5     = ep +(min_profit*    5    );
def    level6     = ep +(min_profit*    6    );
def    level7     = ep +(min_profit*    7    );
def    level8     = ep +(min_profit*    8    );
def    level9     = ep +(min_profit*    9    );
def    level10     = ep +(min_profit*    10    );
def    level11     = ep +(min_profit*    11    );
def    level12     = ep +(min_profit*    12    );
def    level13     = ep +(min_profit*    13    );
def    level14     = ep +(min_profit*    14    );
def    level15     = ep +(min_profit*    15    );
def    level16     = ep +(min_profit*    16    );
def    level17     = ep +(min_profit*    17    );
def    level18     = ep +(min_profit*    18    );
def    level19     = ep +(min_profit*    19    );
def    level20     = ep +(min_profit*    20    );
def    level21     = ep +(min_profit*    21    );
def    level22     = ep +(min_profit*    22    );
def    level23     = ep +(min_profit*    23    );
def    level24     = ep +(min_profit*    24    );
def    level25     = ep +(min_profit*    25    );
def    level26     = ep +(min_profit*    26    );
def    level27     = ep +(min_profit*    27    );
def    level28     = ep +(min_profit*    28    );
def    level29     = ep +(min_profit*    29    );
def    level30     = ep +(min_profit*    30    );
def    level31     = ep +(min_profit*    31    );
def    level32     = ep +(min_profit*    32    );
def    level33     = ep +(min_profit*    33    );
def    level34     = ep +(min_profit*    34    );
def    level35     = ep +(min_profit*    35    );
def    level36     = ep +(min_profit*    36    );
def    level37     = ep +(min_profit*    37    );
def    level38     = ep +(min_profit*    38    );
def    level39     = ep +(min_profit*    39    );
def    level40     = ep +(min_profit*    40    );
def    level41     = ep +(min_profit*    41    );
def    level42     = ep +(min_profit*    42    );
def    level43     = ep +(min_profit*    43    );
def    level44     = ep +(min_profit*    44    );
def    level45     = ep +(min_profit*    45    );
def    level46     = ep +(min_profit*    46    );
def    level47     = ep +(min_profit*    47    );
def    level48     = ep +(min_profit*    48    );
def    level49     = ep +(min_profit*    49    );
def    level50     = ep +(min_profit*    50    );
def    level51     = ep +(min_profit*    51    );
def    level52     = ep +(min_profit*    52    );
def    level53     = ep +(min_profit*    53    );
def    level54     = ep +(min_profit*    54    );
def    level55     = ep +(min_profit*    55    );
def    level56     = ep +(min_profit*    56    );
def    level57     = ep +(min_profit*    57    );
def    level58     = ep +(min_profit*    58    );
def    level59     = ep +(min_profit*    59    );
def    level60     = ep +(min_profit*    60    );
def    level61     = ep +(min_profit*    61    );
def    level62     = ep +(min_profit*    62    );
def    level63     = ep +(min_profit*    63    );
def    level64     = ep +(min_profit*    64    );
def    level65     = ep +(min_profit*    65    );
def    level66     = ep +(min_profit*    66    );
def    level67     = ep +(min_profit*    67    );
def    level68     = ep +(min_profit*    68    );
def    level69     = ep +(min_profit*    69    );
def    level70     = ep +(min_profit*    70    );
def    level71     = ep +(min_profit*    71    );
def    level72     = ep +(min_profit*    72    );
def    level73     = ep +(min_profit*    73    );
def    level74     = ep +(min_profit*    74    );
def    level75     = ep +(min_profit*    75    );
def    level76     = ep +(min_profit*    76    );
def    level77     = ep +(min_profit*    77    );
def    level78     = ep +(min_profit*    78    );
def    level79     = ep +(min_profit*    79    );
def    level80     = ep +(min_profit*    80    );
def    level81     = ep +(min_profit*    81    );
def    level82     = ep +(min_profit*    82    );
def    level83     = ep +(min_profit*    83    );
def    level84     = ep +(min_profit*    84    );
def    level85     = ep +(min_profit*    85    );
def    level86     = ep +(min_profit*    86    );
def    level87     = ep +(min_profit*    87    );
def    level88     = ep +(min_profit*    88    );
def    level89     = ep +(min_profit*    89    );
def    level90     = ep +(min_profit*    90    );
def    level91     = ep +(min_profit*    91    );
def    level92     = ep +(min_profit*    92    );
def    level93     = ep +(min_profit*    93    );
def    level94     = ep +(min_profit*    94    );
def    level95     = ep +(min_profit*    95    );
def    level96     = ep +(min_profit*    96    );
def    level97     = ep +(min_profit*    97    );
def    level98     = ep +(min_profit*    98    );
def    level99     = ep +(min_profit*    99    );
def    level100     = ep +(min_profit*    100    );

#cross below longs

def crossbelowAnyLevel =
(HMA[1] > HMA and HMA crosses below     level1    )  or
(HMA[1] > HMA and HMA crosses below     level2    )  or
(HMA[1] > HMA and HMA crosses below     level3    )  or
(HMA[1] > HMA and HMA crosses below     level4    )  or
(HMA[1] > HMA and HMA crosses below     level5    )  or
(HMA[1] > HMA and HMA crosses below     level6    )  or
(HMA[1] > HMA and HMA crosses below     level7    )  or
(HMA[1] > HMA and HMA crosses below     level8    )  or
(HMA[1] > HMA and HMA crosses below     level9    )  or
(HMA[1] > HMA and HMA crosses below     level10    )  or
(HMA[1] > HMA and HMA crosses below     level11    )  or
(HMA[1] > HMA and HMA crosses below     level12    )  or
(HMA[1] > HMA and HMA crosses below     level13    )  or
(HMA[1] > HMA and HMA crosses below     level14    )  or
(HMA[1] > HMA and HMA crosses below     level15    )  or
(HMA[1] > HMA and HMA crosses below     level16    )  or
(HMA[1] > HMA and HMA crosses below     level17    )  or
(HMA[1] > HMA and HMA crosses below     level18    )  or
(HMA[1] > HMA and HMA crosses below     level19    )  or
(HMA[1] > HMA and HMA crosses below     level20    )  or
(HMA[1] > HMA and HMA crosses below     level21    )  or
(HMA[1] > HMA and HMA crosses below     level22    )  or
(HMA[1] > HMA and HMA crosses below     level23    )  or
(HMA[1] > HMA and HMA crosses below     level24    )  or
(HMA[1] > HMA and HMA crosses below     level25    )  or
(HMA[1] > HMA and HMA crosses below     level26    )  or
(HMA[1] > HMA and HMA crosses below     level27    )  or
(HMA[1] > HMA and HMA crosses below     level28    )  or
(HMA[1] > HMA and HMA crosses below     level29    )  or
(HMA[1] > HMA and HMA crosses below     level30    )  or
(HMA[1] > HMA and HMA crosses below     level31    )  or
(HMA[1] > HMA and HMA crosses below     level32    )  or
(HMA[1] > HMA and HMA crosses below     level33    )  or
(HMA[1] > HMA and HMA crosses below     level34    )  or
(HMA[1] > HMA and HMA crosses below     level35    )  or
(HMA[1] > HMA and HMA crosses below     level36    )  or
(HMA[1] > HMA and HMA crosses below     level37    )  or
(HMA[1] > HMA and HMA crosses below     level38    )  or
(HMA[1] > HMA and HMA crosses below     level39    )  or
(HMA[1] > HMA and HMA crosses below     level40    )  or
(HMA[1] > HMA and HMA crosses below     level41    )  or
(HMA[1] > HMA and HMA crosses below     level42    )  or
(HMA[1] > HMA and HMA crosses below     level43    )  or
(HMA[1] > HMA and HMA crosses below     level44    )  or
(HMA[1] > HMA and HMA crosses below     level45    )  or
(HMA[1] > HMA and HMA crosses below     level46    )  or
(HMA[1] > HMA and HMA crosses below     level47    )  or
(HMA[1] > HMA and HMA crosses below     level48    )  or
(HMA[1] > HMA and HMA crosses below     level49    )  or
(HMA[1] > HMA and HMA crosses below     level50    )  or
(HMA[1] > HMA and HMA crosses below     level51    )  or
(HMA[1] > HMA and HMA crosses below     level52    )  or
(HMA[1] > HMA and HMA crosses below     level53    )  or
(HMA[1] > HMA and HMA crosses below     level54    )  or
(HMA[1] > HMA and HMA crosses below     level55    )  or
(HMA[1] > HMA and HMA crosses below     level56    )  or
(HMA[1] > HMA and HMA crosses below     level57    )  or
(HMA[1] > HMA and HMA crosses below     level58    )  or
(HMA[1] > HMA and HMA crosses below     level59    )  or
(HMA[1] > HMA and HMA crosses below     level60    )  or
(HMA[1] > HMA and HMA crosses below     level61    )  or
(HMA[1] > HMA and HMA crosses below     level62    )  or
(HMA[1] > HMA and HMA crosses below     level63    )  or
(HMA[1] > HMA and HMA crosses below     level64    )  or
(HMA[1] > HMA and HMA crosses below     level65    )  or
(HMA[1] > HMA and HMA crosses below     level66    )  or
(HMA[1] > HMA and HMA crosses below     level67    )  or
(HMA[1] > HMA and HMA crosses below     level68    )  or
(HMA[1] > HMA and HMA crosses below     level69    )  or
(HMA[1] > HMA and HMA crosses below     level70    )  or
(HMA[1] > HMA and HMA crosses below     level71    )  or
(HMA[1] > HMA and HMA crosses below     level72    )  or
(HMA[1] > HMA and HMA crosses below     level73    )  or
(HMA[1] > HMA and HMA crosses below     level74    )  or
(HMA[1] > HMA and HMA crosses below     level75    )  or
(HMA[1] > HMA and HMA crosses below     level76    )  or
(HMA[1] > HMA and HMA crosses below     level77    )  or
(HMA[1] > HMA and HMA crosses below     level78    )  or
(HMA[1] > HMA and HMA crosses below     level79    )  or
(HMA[1] > HMA and HMA crosses below     level80    )  or
(HMA[1] > HMA and HMA crosses below     level81    )  or
(HMA[1] > HMA and HMA crosses below     level82    )  or
(HMA[1] > HMA and HMA crosses below     level83    )  or
(HMA[1] > HMA and HMA crosses below     level84    )  or
(HMA[1] > HMA and HMA crosses below     level85    )  or
(HMA[1] > HMA and HMA crosses below     level86    )  or
(HMA[1] > HMA and HMA crosses below     level87    )  or
(HMA[1] > HMA and HMA crosses below     level88    )  or
(HMA[1] > HMA and HMA crosses below     level89    )  or
(HMA[1] > HMA and HMA crosses below     level90    )  or
(HMA[1] > HMA and HMA crosses below     level91    )  or
(HMA[1] > HMA and HMA crosses below     level92    )  or
(HMA[1] > HMA and HMA crosses below     level93    )  or
(HMA[1] > HMA and HMA crosses below     level94    )  or
(HMA[1] > HMA and HMA crosses below     level95    )  or
(HMA[1] > HMA and HMA crosses below     level96    )  or
(HMA[1] > HMA and HMA crosses below     level97    )  or
(HMA[1] > HMA and HMA crosses below     level98    )  or
(HMA[1] > HMA and HMA crosses below     level99    )  or
(HMA[1] > HMA and HMA crosses below     level100    )  ;




#self adjusting Shorts
def    xlevel1     = ep -(min_profit*    1    );
def    xlevel2     = ep -(min_profit*    2    );
def    xlevel3     = ep -(min_profit*    3    );
def    xlevel4     = ep -(min_profit*    4    );
def    xlevel5     = ep -(min_profit*    5    );
def    xlevel6     = ep -(min_profit*    6    );
def    xlevel7     = ep -(min_profit*    7    );
def    xlevel8     = ep -(min_profit*    8    );
def    xlevel9     = ep -(min_profit*    9    );
def    xlevel10     = ep -(min_profit*    10    );
def    xlevel11     = ep -(min_profit*    11    );
def    xlevel12     = ep -(min_profit*    12    );
def    xlevel13     = ep -(min_profit*    13    );
def    xlevel14     = ep -(min_profit*    14    );
def    xlevel15     = ep -(min_profit*    15    );
def    xlevel16     = ep -(min_profit*    16    );
def    xlevel17     = ep -(min_profit*    17    );
def    xlevel18     = ep -(min_profit*    18    );
def    xlevel19     = ep -(min_profit*    19    );
def    xlevel20     = ep -(min_profit*    20    );
def    xlevel21     = ep -(min_profit*    21    );
def    xlevel22     = ep -(min_profit*    22    );
def    xlevel23     = ep -(min_profit*    23    );
def    xlevel24     = ep -(min_profit*    24    );
def    xlevel25     = ep -(min_profit*    25    );
def    xlevel26     = ep -(min_profit*    26    );
def    xlevel27     = ep -(min_profit*    27    );
def    xlevel28     = ep -(min_profit*    28    );
def    xlevel29     = ep -(min_profit*    29    );
def    xlevel30     = ep -(min_profit*    30    );
def    xlevel31     = ep -(min_profit*    31    );
def    xlevel32     = ep -(min_profit*    32    );
def    xlevel33     = ep -(min_profit*    33    );
def    xlevel34     = ep -(min_profit*    34    );
def    xlevel35     = ep -(min_profit*    35    );
def    xlevel36     = ep -(min_profit*    36    );
def    xlevel37     = ep -(min_profit*    37    );
def    xlevel38     = ep -(min_profit*    38    );
def    xlevel39     = ep -(min_profit*    39    );
def    xlevel40     = ep -(min_profit*    40    );
def    xlevel41     = ep -(min_profit*    41    );
def    xlevel42     = ep -(min_profit*    42    );
def    xlevel43     = ep -(min_profit*    43    );
def    xlevel44     = ep -(min_profit*    44    );
def    xlevel45     = ep -(min_profit*    45    );
def    xlevel46     = ep -(min_profit*    46    );
def    xlevel47     = ep -(min_profit*    47    );
def    xlevel48     = ep -(min_profit*    48    );
def    xlevel49     = ep -(min_profit*    49    );
def    xlevel50     = ep -(min_profit*    50    );
def    xlevel51     = ep -(min_profit*    51    );
def    xlevel52     = ep -(min_profit*    52    );
def    xlevel53     = ep -(min_profit*    53    );
def    xlevel54     = ep -(min_profit*    54    );
def    xlevel55     = ep -(min_profit*    55    );
def    xlevel56     = ep -(min_profit*    56    );
def    xlevel57     = ep -(min_profit*    57    );
def    xlevel58     = ep -(min_profit*    58    );
def    xlevel59     = ep -(min_profit*    59    );
def    xlevel60     = ep -(min_profit*    60    );
def    xlevel61     = ep -(min_profit*    61    );
def    xlevel62     = ep -(min_profit*    62    );
def    xlevel63     = ep -(min_profit*    63    );
def    xlevel64     = ep -(min_profit*    64    );
def    xlevel65     = ep -(min_profit*    65    );
def    xlevel66     = ep -(min_profit*    66    );
def    xlevel67     = ep -(min_profit*    67    );
def    xlevel68     = ep -(min_profit*    68    );
def    xlevel69     = ep -(min_profit*    69    );
def    xlevel70     = ep -(min_profit*    70    );
def    xlevel71     = ep -(min_profit*    71    );
def    xlevel72     = ep -(min_profit*    72    );
def    xlevel73     = ep -(min_profit*    73    );
def    xlevel74     = ep -(min_profit*    74    );
def    xlevel75     = ep -(min_profit*    75    );
def    xlevel76     = ep -(min_profit*    76    );
def    xlevel77     = ep -(min_profit*    77    );
def    xlevel78     = ep -(min_profit*    78    );
def    xlevel79     = ep -(min_profit*    79    );
def    xlevel80     = ep -(min_profit*    80    );
def    xlevel81     = ep -(min_profit*    81    );
def    xlevel82     = ep -(min_profit*    82    );
def    xlevel83     = ep -(min_profit*    83    );
def    xlevel84     = ep -(min_profit*    84    );
def    xlevel85     = ep -(min_profit*    85    );
def    xlevel86     = ep -(min_profit*    86    );
def    xlevel87     = ep -(min_profit*    87    );
def    xlevel88     = ep -(min_profit*    88    );
def    xlevel89     = ep -(min_profit*    89    );
def    xlevel90     = ep -(min_profit*    90    );
def    xlevel91     = ep -(min_profit*    91    );
def    xlevel92     = ep -(min_profit*    92    );
def    xlevel93     = ep -(min_profit*    93    );
def    xlevel94     = ep -(min_profit*    94    );
def    xlevel95     = ep -(min_profit*    95    );
def    xlevel96     = ep -(min_profit*    96    );
def    xlevel97     = ep -(min_profit*    97    );
def    xlevel98     = ep -(min_profit*    98    );
def    xlevel99     = ep -(min_profit*    99    );
def    xlevel100     = ep -(min_profit*    100    );




#crossing above any level shorts

def crossAboveAnyXLevel =
(HMA >    xlevel1      and HMA[1] <     xlevel1      ) or
(HMA >    xlevel2      and HMA[1] <     xlevel2     ) or
(HMA >    xlevel3      and HMA[1] <     xlevel3     ) or
(HMA >    xlevel4      and HMA[1] <     xlevel4     ) or
(HMA >    xlevel5      and HMA[1] <     xlevel5     ) or
(HMA >    xlevel6      and HMA[1] <     xlevel6     ) or
(HMA >    xlevel7      and HMA[1] <     xlevel7     ) or
(HMA >    xlevel8      and HMA[1] <     xlevel8     ) or
(HMA >    xlevel9      and HMA[1] <     xlevel9     ) or
(HMA >    xlevel10      and HMA[1] <     xlevel10     ) or
(HMA >    xlevel11      and HMA[1] <     xlevel11     ) or
(HMA >    xlevel12      and HMA[1] <     xlevel12     ) or
(HMA >    xlevel13      and HMA[1] <     xlevel13     ) or
(HMA >    xlevel14      and HMA[1] <     xlevel14     ) or
(HMA >    xlevel15      and HMA[1] <     xlevel15     ) or
(HMA >    xlevel16      and HMA[1] <     xlevel16     ) or
(HMA >    xlevel17      and HMA[1] <     xlevel17     ) or
(HMA >    xlevel18      and HMA[1] <     xlevel18     ) or
(HMA >    xlevel19      and HMA[1] <     xlevel19     ) or
(HMA >    xlevel20      and HMA[1] <     xlevel20     ) or
(HMA >    xlevel21      and HMA[1] <     xlevel21     ) or
(HMA >    xlevel22      and HMA[1] <     xlevel22     ) or
(HMA >    xlevel23      and HMA[1] <     xlevel23     ) or
(HMA >    xlevel24      and HMA[1] <     xlevel24     ) or
(HMA >    xlevel25      and HMA[1] <     xlevel25     ) or
(HMA >    xlevel26      and HMA[1] <     xlevel26     ) or
(HMA >    xlevel27      and HMA[1] <     xlevel27     ) or
(HMA >    xlevel28      and HMA[1] <     xlevel28     ) or
(HMA >    xlevel29      and HMA[1] <     xlevel29     ) or
(HMA >    xlevel30      and HMA[1] <     xlevel30     ) or
(HMA >    xlevel31      and HMA[1] <     xlevel31     ) or
(HMA >    xlevel32      and HMA[1] <     xlevel32     ) or
(HMA >    xlevel33      and HMA[1] <     xlevel33     ) or
(HMA >    xlevel34      and HMA[1] <     xlevel34     ) or
(HMA >    xlevel35      and HMA[1] <     xlevel35     ) or
(HMA >    xlevel36      and HMA[1] <     xlevel36     ) or
(HMA >    xlevel37      and HMA[1] <     xlevel37     ) or
(HMA >    xlevel38      and HMA[1] <     xlevel38     ) or
(HMA >    xlevel39      and HMA[1] <     xlevel39     ) or
(HMA >    xlevel40      and HMA[1] <     xlevel40     ) or
(HMA >    xlevel41      and HMA[1] <     xlevel41     ) or
(HMA >    xlevel42      and HMA[1] <     xlevel42     ) or
(HMA >    xlevel43      and HMA[1] <     xlevel43     ) or
(HMA >    xlevel44      and HMA[1] <     xlevel44     ) or
(HMA >    xlevel45      and HMA[1] <     xlevel45     ) or
(HMA >    xlevel46      and HMA[1] <     xlevel46     ) or
(HMA >    xlevel47      and HMA[1] <     xlevel47     ) or
(HMA >    xlevel48      and HMA[1] <     xlevel48     ) or
(HMA >    xlevel49      and HMA[1] <     xlevel49     ) or
(HMA >    xlevel50      and HMA[1] <     xlevel50     ) or
(HMA >    xlevel51      and HMA[1] <     xlevel51     ) or
(HMA >    xlevel52      and HMA[1] <     xlevel52     ) or
(HMA >    xlevel53      and HMA[1] <     xlevel53     ) or
(HMA >    xlevel54      and HMA[1] <     xlevel54     ) or
(HMA >    xlevel55      and HMA[1] <     xlevel55     ) or
(HMA >    xlevel56      and HMA[1] <     xlevel56     ) or
(HMA >    xlevel57      and HMA[1] <     xlevel57     ) or
(HMA >    xlevel58      and HMA[1] <     xlevel58     ) or
(HMA >    xlevel59      and HMA[1] <     xlevel59     ) or
(HMA >    xlevel60      and HMA[1] <     xlevel60     ) or
(HMA >    xlevel61      and HMA[1] <     xlevel61     ) or
(HMA >    xlevel62      and HMA[1] <     xlevel62     ) or
(HMA >    xlevel63      and HMA[1] <     xlevel63     ) or
(HMA >    xlevel64      and HMA[1] <     xlevel64     ) or
(HMA >    xlevel65      and HMA[1] <     xlevel65     ) or
(HMA >    xlevel66      and HMA[1] <     xlevel66     ) or
(HMA >    xlevel67      and HMA[1] <     xlevel67     ) or
(HMA >    xlevel68      and HMA[1] <     xlevel68     ) or
(HMA >    xlevel69      and HMA[1] <     xlevel69     ) or
(HMA >    xlevel70      and HMA[1] <     xlevel70     ) or
(HMA >    xlevel71      and HMA[1] <     xlevel71     ) or
(HMA >    xlevel72      and HMA[1] <     xlevel72     ) or
(HMA >    xlevel73      and HMA[1] <     xlevel73     ) or
(HMA >    xlevel74      and HMA[1] <     xlevel74     ) or
(HMA >    xlevel75      and HMA[1] <     xlevel75     ) or
(HMA >    xlevel76      and HMA[1] <     xlevel76     ) or
(HMA >    xlevel77      and HMA[1] <     xlevel77     ) or
(HMA >    xlevel78      and HMA[1] <    xlevel78     ) or
(HMA >    xlevel79      and HMA[1] <     xlevel79     ) or
(HMA >    xlevel80      and HMA[1] <     xlevel80     ) or
(HMA >    xlevel81      and HMA[1] <     xlevel81     ) or
(HMA >    xlevel82      and HMA[1] <     xlevel82     ) or
(HMA >    xlevel83      and HMA[1] <     xlevel83     ) or
(HMA >    xlevel84      and HMA[1] <     xlevel84     ) or
(HMA >    xlevel85      and HMA[1] <    xlevel85     ) or
(HMA >    xlevel86      and HMA[1] <     xlevel86     ) or
(HMA >    xlevel87      and HMA[1] <     xlevel87     ) or
(HMA >    xlevel88      and HMA[1] <     xlevel88     ) or
(HMA >    xlevel89      and HMA[1] <     xlevel89     ) or
(HMA >    xlevel90      and HMA[1] <     xlevel90     ) or
(HMA >    xlevel91      and HMA[1] <     xlevel91     ) or
(HMA >    xlevel92      and HMA[1] <     xlevel92     ) or
(HMA >    xlevel93      and HMA[1] <     xlevel93     ) or
(HMA >    xlevel94      and HMA[1] <     xlevel94     ) or
(HMA >    xlevel95      and HMA[1] <     xlevel95     ) or
(HMA >    xlevel96      and HMA[1] <     xlevel96     ) or
(HMA >    xlevel97      and HMA[1] <     xlevel97     ) or
(HMA >    xlevel98      and HMA[1] <     xlevel98     ) or
(HMA >    xlevel99      and HMA[1] <     xlevel99     ) or
(HMA >    xlevel100      and HMA[1] <     xlevel100    );

def short_exit_signal = if HMA > HMA[1] and crossAboveAnyXLevel and (close<ep) then 1 else 0;

def long_exit_signal = if HMA < HMA[1] and crossbelowAnyLevel and (close>ep) then 1 else 0;
 

#profit targets
addOrder(OrderType.sell_TO_CLOSE,long_exit_signal and trading_time, tickColor = Color.green, arrowColor = Color.green, name = "SELL TO CLOSE Long profit");
addOrder(OrderType.buy_TO_CLOSE, short_exit_signal and trading_time, tickColor = Color.green, arrowColor = Color.green, name = "BUY TO CLOSE short profit");



#stoplosses.


def short_loss= if HMA crosses above level5 then 1 else 0;
def short_loss2= if HMA crosses above level10  then 1 else 0;

def long_loss= if HMA crosses below xlevel5  then 1 else 0;
def long_loss2= if HMA crosses below xlevel10 then 1 else 0;

addOrder(OrderType.sell_TO_CLOSE,long_loss and trading_time, tickColor = Color.RED, arrowColor = Color.RED, name = "SELL TO CLOSE long_loss 1");
addOrder(OrderType.sell_TO_CLOSE,long_loss2 and trading_time, tickColor = Color.RED, arrowColor = Color.RED, name = "SELL TO CLOSE long_loss 2");




addOrder(OrderType.buy_TO_CLOSE, short_loss and trading_time, tickColor = Color.RED, arrowColor = Color.RED, name = "BUY TO CLOSE short_loss 1");

addOrder(OrderType.buy_TO_CLOSE, short_loss2 and trading_time, tickColor = Color.RED, arrowColor = Color.RED, name = "BUY TO CLOSE short_loss 2");
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
339 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top