Home › Forums › Trading System Mentor Course Community › AmiBroker Coding and AFL › Multiple trailing stop colours
- This topic is empty.
-
AuthorPosts
-
January 29, 2016 at 1:06 am #101412TrentRothallParticipant
I am trying to plot my trailing stop so when the position has moved into profit the trailing stop colour changes, just for easy visual checks while scrolling through the charts. The following code works on some charts but not others and I’m unsure why. On some charts the trailing stop colour will be red if the position is in profit or not, and on others it will be blue if the position is in profit or not and then on some charts it works as I want it to.
Plot(LStop,”Trailing Stop”,IIf(LStop > PriceAtBuy,ParamColor(“Trail Stop Profit”, colorBlue), ParamColor(“Trail Stop Color”, colorRed)),styleLine);
The image isn’t the best but it shows the issue
January 29, 2016 at 1:22 am #102614Nick RadgeKeymasterIf you can try and decrease the size of the image. It’s gone fuzzy here because its trying to squeeze it lower.
I’m sure Craig will answer your question for you.
January 29, 2016 at 2:51 am #102616Stephen JamesMemberOne way would be to create an array of the PriceAtBuy value, as you did in the Breakeven stop lesson – e.g. BuyLevel
Then in your plot formula use LStop > BuyLevel instead.
January 29, 2016 at 7:25 am #102615TrentRothallParticipantThanks Craig that worked well
January 31, 2016 at 2:49 am #102618TrentRothallParticipantHi Craig
Another question, I don’t want to enter a trade straight after i exit one if my entry conditions are still valid. I believe it creating some whipsaw trades and i want to test it. I have been trying to use the BarsSince() function but i can’t get it working properly. i set up an array then i added it t my trigger bar in the loop but it’s no good. I tried BarsSince(Sell) also BarsSince(LBIT==0)>5; Thanks
Buy = 0;
Sell = 0;
PriceAtBuy = 0;
LBIT = 0;
LStop = Null;
BuyLevel = Null;
//
BStrade = BarsSince(Sell)>5;
//
for(j = 1; j < BarCount; j++)
{
if(PriceAtBuy==0 AND LE[j] AND BSTrade[j] )
{
Buy[j] = True;
BuyPrice[j] = Open[j];
PriceAtBuy = BuyPrice[j];
LStop[j] = Max(TrailStopUp[j-1],TrailStopUp[j]);
LStop[j-1] = TrailStopUp[j-1];
BuyLevel[j] = BuyPrice[j];
LBIT[j] = 1;
…….
…
..January 31, 2016 at 11:25 pm #102620Stephen JamesMemberGood question! I haven’t come up with a solution within looping yet, but for testing purposes for the time being you could create another file and discard the looping. Then BarsSince should work.
February 1, 2016 at 12:21 am #102622TrentRothallParticipantI tried
BStrade = Ref(PriceAtBuy==0,-5);
Doesn’t logic say that should work? to enter a trade PriceAtBuy has to be false 5 bars ago so there is no trade on. That didn’t work either, it still entered a trade straight after an exit
February 1, 2016 at 12:37 am #102623Stephen JamesMemberIf you just want to reference a set bar number ago you could try something like LBIT[j-5] == 0 in the entry condition but that does not cover 2,3 or 4 bars ago etc.
I guess you could extend that to LBIT[j-2]==0 AND LBIT[j-3]==0 etc etc. It may not be ideal but could be enough to at least test your theory for now.
February 1, 2016 at 7:18 pm #102624SaidBitarMemberHi
here is my solution , I tested it and it worked for me
Code:Buy = 0;
Sell = 0;
PriceAtBuy = 0;
LBIT = 0;
LStop = Null;
DaySinceSell = 0;//———————————————————————————
for (j = 1; j < BarCount; j++) { if(PriceAtBuy == 0 AND LE[j] AND DaySinceSell == 0) { Buy[j] = True; BuyPrice[j] = LEPrice[j]; PriceAtBuy = LEPrice[j]; LBIT[j] = 1; LStop[j] = Max(TrailStop[j-1], TrailStop[j]); LSTop[j-1] = TrailStop[j-1]; if(LBIT[j] > 1 AND (Close[j-1] <= LSTop[j-1] OR OnSecondLastBarOfDelistedSecurity[j])) { Sell[j] = True; SellPrice[j] = Open[j]; PriceAtBuy = 0; DaySinceSell = 1; } } else if(PriceAtBuy > 0)
{
LBIT[j] = LBIT[j-1] + 1;if(LBIT[j] > 1)
{
LStop[j] = Max(LStop[j-1], TrailStop[j]);
}if(LBIT[j] > 1 AND (Close[j-1] <= LSTop[j-1] OR OnSecondLastBarOfDelistedSecurity[j])) { Sell[j] = True; SellPrice[j] = Open[j]; PriceAtBuy = 0; DaySinceSell = 1; } } if(DaySinceSell > 0 AND DaySinceSell < 5 ) { DaySinceSell = DaySinceSell + 1; } if(DaySinceSell == 5) { DaySinceSell = 0; } }
the concept is as follows:
you create one counter to count how many days since you had sell signal “DaySinceSell ” the initial value will be zero
the moment you have sell signal you increment it to 1
then there is an if statement that will check if DaySinceSell >= 1 then it will increment it by one again
when the value will reach 5 then the if statement if(DaySinceSell == 5) will become true and the value of DaySinceSell will be set to zero
then this will be one of the conditions for the buyFebruary 1, 2016 at 7:25 pm #102735SaidBitarMemberhere is a snapshot of the system Before adding the lines to the code
here how to looked after
just a small note you see that the first trade was with loss but the second entry was profitable. Anyhow it is good to test to see if there is added value to it or no
February 2, 2016 at 5:07 am #102736TrentRothallParticipantThanks Said – tested it and it doesn’t improve results. i guess the entry conditions are there because they produce profitable trades – miss the proper signals and you can miss out on profitable trades.
-
AuthorPosts
- You must be logged in to reply to this topic.