Hi cudderbean,
Thanks for your reply. From what you suggest, there was still the need to develop an EITHER/OR logic with the creation of the "retrac" variable.
Having said that, after this post, it got my brain juices going and I have made some considerable progress in formulating a stop-loss that varies based on the index filter sentiment. It's not complete and I'd like to ask someone a small favour who could make the last part of the code below work in a way that enables a user to drag the date of the trend on the chart left and right and the stop loss indicator updates dynamically , cause at the moment it is static at 52 bars.
Any further assistance would be greatly appreciated. Please note I have some of my own notes in { } that can be ignored for these purposes.
An example of CDA is attached below. Try it out and let me know if you can see anything I can do to streamline it.
***
[Target=Price; category=Stop Loss];
{Define the date of the trend} TDate:=InputDate("Date of trend", date(2020,01,01));
{Define the stop-loss retracement percentages} Value1:=input("Retracement Bullish %",0.6,0); Value2:=input("Retracement Bearish %",0.9,0);
{Defines the variables for exponential moving average and index filter} expr := Expression("Expression",Close); method := inputma("Method",Exponential); n := input("Time periods",10,1); symb := inputsymbol("Compare to", "XAO");
P:=LoadSymbol(symb,expr);
{Evaluate Bullish sentiment} [color=Lime Green; name=Bullish] SignalAbove:=ref(P,1)>ref(MA(P,n,method),1); {SignalAbove;}
{Evaluate Bearish sentiment}{possibly redundant section?} [color=Red; name=Bearish] SignalBelow:=ref(P,1)<ref(MA(P,n,method),1); {SignalBelow;}
HoldingDays:=BarsSince(OnOrSkipped(TDate));
{Determines the highest prominent high over the last 52 weeks} {NOTE:>>>>> I want to link the number of bars to the HoldingDays variable, not 52 weeks} [color=Blue; linestyle=Dotted ; name=Highest Prominent High] Stop:=HighestSince(1, (lastvalue(barnumber) - barnumber = 52) ,H);
{could this be replaced with HoldingDays variable?} Stop;
[color=Red; linestyle=Horz step; name=Retracement % Stop] ValueB := If(Stop>PREV(undefined) AND ref(P,1)>ref(MA(P,n,method),1),Stop*Value1,Stop*Value2);
{If(Stop<=PREV(undefined),PREV(undefined),Stop*Holdingdays));NOTE:>>>>>
I haven't figured out what to do with the rest of this nested IF
statement as it was inherited from a previous Bullscript file, possibly
redundant now!}
If(ValueB = 0, undefined, ValueB);
|