# Looking for help learning Loops

Hello All,

I wanted to reach out to ask if some of you could help point me towards resources to learn how to use loops? I don't have much experience with them besides pasting from other sources and would like to learn. I didn't know where best to start.

For reference my goal is to learn how to make an exit or stop that only increases in value, but has two components. When a filter is positive I'd like to use a lower stop, and when negative a higher stop, but I don't want it to revert to a lower stop if the filter value changes. If there is a better way to do this than loops I'd be happy if you'd point me towards something else to research instead.

I'm not great with the coding and still trying to get it work, but here's what I'm looking to do conceptually:

Filter would be binary 1 or 0
Initial stop when filter is positive would be H*.6
Trailing stop when filter is negative would be H*.9
And I'd like it to only increase in value so if the filter flipped from negative back to positive without an exit I'd like to keep the higher stop.

I haven't had any success doing this with ApplyStop which is why I'm hoping to learn Loops and would appreciate any help.

Hi ab-ami,

Thank you very much for the reply!

That was helpful in that it lets me know I don't need to learn how to use loops, but I still haven't solved my goal.

I have the two variables I need in arrays (initial stop, trailing stop) as well as a third variable which contains the one I want currently (active stop). What I'm still struggling with is how to keep that variable from decreasing.

I've tried using Highest, Max, and Ref to make it work but am still not successful. Is there a way to keep a variable from decreasing?

``````//Regime Filter
FilterMALength = 10;//Optimize ( "filterMALength", 20, 10, 400, 10);
Index = Foreign("\$SP", "C", True);
FilterMA = MA (Index, FilterMALength);
IndexClose = Foreign("SP", "C", True);
FilterPass = IIf (FilterMA <= Foreign("\$SP", "C", True), 1, 0);

//Entry
Cond1=FilterPass;

RateOfChange=ROC (C,20);
Cond3=IIf (RateOfChange >= 10, 1,0);

//Exit
//Inital Stop
InitialStop=H*0.6;

//Trailing Stop
TrailingStop=H*0.9;

ActiveStop=Iif(FilterPass*H*.9 > H*.6, InitialStop,TrailingStop);

// unsuccessful attempts
OldStop=Iif(FilterPass*HHV(C,-1)*.9 > HHV(C,-1)*.6, InitialStop,TrailingStop);
LastWeek=TimeFrameGetPrice("H",inWeekly,-1);
ThisWeek=H;
OldInitialStop=LastWeek*0.6;
OldTrailingStop=LastWeek*0.9;
OldActiveStop=Iif(FilterPass*LastWeek*.9 > LastWeek*.6, OldInitialStop,OldTrailingStop);
Try1=Iif(FilterPass*TimeFrameGetPrice("H",inWeekly,-1)*.9 > TimeFrameGetPrice("H",inWeekly,-1)*.6, OldInitialStop,OldTrailingStop);
GreaterStop=OldActiveStop-ActiveStop;
highervar=Highest(activestop);
Currentstop=ActiveStop+GreaterStop;
Ratchetstop=Highest(ActiveStop);
RatchetInitial=Highest(InitialStop);
RatchetTrailing=Highest(TrailingStop);
maxstop=Max(activestop,oldactivestop);
MaxInitialStop=Max(initialStop,OldInitialStop);
HighestMaxInitialStop=Highest(MaxInitialStop);
RefInitialStop=Ref(InitialStop,-1);
MaxRefInitialStop=Max(InitialStop,RefInitialStop);
RefActiveStop=Ref(ActiveStop,-1);
MaxRefActiveStop=Max(ActiveStop,RefActiveStop);
RefMaxRefActiveStop=Ref(MaxRefActiveStop,-1);
MaxRefMaxRefActiveStop=Max(RefMaxRefActiveStop,MaxRefActiveStop);
MaxActiveStop=Max(ActiveStop,MaxRefActiveStop);
Whatif=Max(ActiveStop,Ref(ActiveStop,-1));
FrozenStop =ApplyStop(stopTypeTrailing, stopModePercent, 15, True, True );

//Sell = InitialStop OR TrailingStop;
//Sell = Cross (C, ActiveStop);
Sell = (C<=ActiveStop);

// use Explore to create a table
Filter = 1;
//AddColumn (Cond1, "Cond1 Regime Filter", 1.0);
//AddColumn (RateOfChange, "Rate of Change", 1.0);
//AddColumn (Cond3, "Cond3 Rate of Change", 1.0);
//AddColumn (OldInitialStop, "Old Initial Stop", 1.0);
//AddColumn (OldTrailingStop, " OldTrailing Stop", 1.0);
AddColumn (OldActiveStop, "Old Active Stop", 1.0);
``````

Here is a picture of the explore output with the table. You can see I highlighted where my active stop first decreases in value. I'm just not certain what logic or command I need to use to keep it from decreasing as time passes.

I was able to achieve my goal with HighestSince

This let me return the highest value of activestop since the buy signal which is what I was trying to do.

Thank you for all of the help!

Be careful with that approach, as it will give you the highest value since your most recent Buy signal, which is not always the same as the Buy signal that caused you to enter the trade.

Yes, it is always better to use ApplyStop for stops.

Thank you all very much for the help!

I don't consider myself good at coding, but by just being able to use the explore tables to see what the arrays do I can get done what I need.

I'm not quite good enough to figure out how to make ApplyStop work in this situation, but this exit seems to satisfy what I'm looking for. I've been quite pleased to have been able to create several new trading strategies to play with and try this exit with some of them.

A follow up question here if someone could help.

Perhaps I was unaware of this, but it seems it is possible to use 2 different ApplyStops and either one can trigger an exit? So it is possible to have an ApplyStop for a max loss as well as for trailing?

I've been using explore to try and figure out what my code does, but I'm not aware of how to do that with applystop (since both have the same name).

``````ApplyStop( stopTypeLoss, StopModePoint, 5*ATR(20), True );
ApplyStop(stopTypeTrailing,stopModePercent,25,True);
``````

You can have 4 stops working at the same time (one of each kind): stopTypeLoss, stopTypeTrailing, stopTypeProfit, stopTypeNBar

All is documented in the MANUAL which is must -read
https://www.amibroker.com/f?applystop

Thank you for the quick reply, Tomasz! It wasn't clear to me that you could have one of each kind for ApplyStop so I appreciate you clarifying.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.