Problem with Stop loss

I’ ve got a problem with applying stop loss. I’ll explain on the example. My buy signal, lets say, is hammer candle. I buy some stocks and my target is crossing bollinger top. I want to apply stop 2*ATR(20) under the buyprice. Unfortunatelly sometimes before I sell (target price) another hammer appears and my SL changes its value. How can I solve this problem?

@kubadrozdz it sounds like you have coded your rules incorrectly. But without seeing your code, how can someone point out the mistake? I could take a guess that you should read up on ExRem() but that is just a guess.

@quantboy it is my code, what is wrong with it?:

SetOption( "MaxOpenPositions", 20 );
SetTradeDelays( 1, 0, 0, 0 );
RoundLotSize = 1;
BuyPrice = O;
VanTharpSize = ( (   1 / ATR(20) ) * BuyPrice ); 
SetPositionSize( VanTharpSize, spsPercentOfEquity );

O1 = Ref(O,-1);
O2 = Ref(O,-2);
H1 = Ref(H,-1);
H2 = Ref(H,-2);
L1 = Ref(L,-1);
L2 = Ref(L,-2);
C1 = Ref(C,-1);
C2 = Ref(C,-2);

optma = Optimize( "MA", 20, 1, 100, 1 );
optwid = Optimize( "Wid", 2, 0.5, 4, 0.1 );
optatr = Optimize( "ATR", 2, 0, 3, 1 );

eng = ((O1>C1) AND (C>O) AND (C>= O1) AND (C1>= O) AND ((C-O)>(O1-C1)));
ham = (((H-L)>3*(O-C)) AND ((C-L)/(.001+H-L)>0.6) AND ((O-L)/(.001+H-L)>0.6));
pie = ((C1<O1) AND (((O1+C1)/2)<C) AND (O<C) AND (O<C1) AND (C<O1) AND ((C-O)/(.001+(H-L))>0.6));
inv = (((H-L)>3*(O-C)) AND ((H-C)/(.001+H-L)>0.6) AND ((H-O)/(.001+H-L)>0.6));

bolli = BBandTop( C, optma, optwid );
mov = MA( C, optma );

index = Foreign( "WIG", "C", True );
indexma = MA( index, 150 );
indexfilter = index > indexma;


sig1 = eng AND (L <= mov OR L1 <= Ref( mov, -1 )) AND indexfilter AND C > mov;
sig2 = ham AND (L <= mov OR L1 <= Ref( mov, -1 )) AND indexfilter AND C > mov;
sig3 = pie AND (L <= mov OR L1 <= Ref( mov, -1 )) AND indexfilter AND C > mov;
sig4 = inv AND (L <= mov OR L1 <= Ref( mov, -1 )) AND indexfilter AND C > mov;


Buy = sig1 OR sig2 OR sig3 OR sig4;
Sell = C >= bolli;
ApplyStop( stopTypeLoss, stopModePoint, optatr * ValueWhen( Buy, ref( ATR(20), 1 ), 1 ));


Plot( bolli, "bolli", colorGreen, styleLine, 0, 0, 0, 0, 1 );
Plot( mov, "mov", colorWhite, styleLine, 0, 0, 0, 0, 1 );
Plot( ValueWhen( Buy, Ref( O, 1 ), 1 ) - optatr * ValueWhen( Buy, ref( ATR(20), 1 ), 1 ), "loss", colorRed, styleLine, 0, 0, 0, 0, 1 );

Moderator comment: I have edited your post because you have posted code without using the CODE button.

Good grief, again no code blocks. It is as if users on this forum have never been on the internet or have never “copy and paste” anything off the internet into a word processor or spreadsheet or Amibroker before.

@kubadrozdz use proper code blocks. To get possible answers to your questions, copy your afl from your Amibroker afl editor and paste into the forum using proper code blocks. This way if someone wants to help you they can copy it into their afl editor and debug your code. Otherwise the act of copying and then pasting will generate many formatting errors.

Learn how to use the forum correctly, it will at least give you a chance at getting help. Using the forum incorrectly decreases the chance that anyone will try to help.

Good luck

1 Like

For the reason mentioned by @quantboy if someone wanted to help you and check your code in his AmiBroker he would have to change 18 wrong quotation marks first.

Przechwytywanie

Copy and paste your code to the AFL Editor and see for yourself.

Not obeying the rules of this forum is disrespecting other users. If someone expects free help, he/she should spend at least a few minutes to learn how to compose the post properly.

2 Likes