Home › Forums › Trading System Mentor Course Community › AmiBroker Coding and AFL › AFL Template Question
- This topic is empty.
-
AuthorPosts
-
May 8, 2016 at 7:12 am #101489JulianCohenParticipant
In the Templates for looping given in the course, there is a template for exit and trail stop
Code:LE = //Long Entry
LEPrice = //Entry Price
//———————————————————————————LEx = //Long Exit
//trailing stop
LIS = //Long Initial Stop
LTS = //Long Trail StopIn this instance what is LIS referring to? I can see the LEx for the exit stop and LTS for the trailing stop but what is LIS used for?
May 8, 2016 at 7:31 am #103708TrentRothallParticipantI think it’s for a tighter initial stop than the trailing stop. Example. buy and have a 10% initial stop but follow the price with a 15% trailing stop. I think..
May 8, 2016 at 8:06 am #103709SaidBitarMemberInitial Stop will be used for positions size
regarding position size you can have many ways such as Fixed % of Portfolio Equity another way is Fixed Fractional Risk %
for Fixed Fractional Risk % suppose you want to risk 1 or 2% per position so you need to know the value of the initial stop so that you can estimate your risk and find the number of shares.
May 8, 2016 at 8:52 am #103710JulianCohenParticipantOK thanks. I’m trying to write a system with an initial stop and an exit but I’m struggling with the looping. I’ll keep at it. Thanks for the help guys
May 8, 2016 at 9:10 am #103779JulianCohenParticipantWith this code my trailing stop is always either 0 or 1 and I can’t figure out why. What painfully obvious thing am I missing?
Code://=================================================================================
//Entry and Exit Signals
//=================================================================================Cond1 = L < Ref(L,-1); Cond2 = BullDB > ClsSt;
Cond3 = H < SMA; Cond4 = C > SMA;
Cond5 = OptFilt AND IndexUp AND UniverseFilter AND HDBFilter; //All the optional filtersOnLastTwoBarsOfDelistedSecurity = BarIndex() >= (LastValue(BarIndex()) -1) AND !IsNull(GetFnData(“DelistingDate”));
OnSecondLastBarOfDelistedSecurity = BarIndex() == (LastValue(BarIndex()) -1) AND !IsNull(GetFnData(“DelistingDate”));BuySetUp = Cond1 AND Cond2 AND Cond3 AND Cond5;
Buy = Ref(BuySetUp,-1);
BuyStop = H + 0.01;
SellStop = L – 0.01;LE = Ref(BuySetUp,-1) AND H >=Ref(BuyStop,-1) AND NOT OnLastTwoBarsOfDelistedSecurity;// TRIGGER BAR Entry is all conditions fulfilled and above the BuyStop
LEPrice = Max(Open,Ref(BuyStop,-1)); //Entry PriceLEx = C > SMA; //Long Exit
LIS = Ref(BuySetUp,-1) AND L <=Ref(SellStop,-1) AND NOT OnLastTwoBarsOfDelistedSecurity; //Initial stop a tic below SetUpBar //-------------------------------------------------------------------------- //MCS code part 2 if(MCP) LE = LE && Random()*100>=MCP; // Identifier MUST match the Identifier in the trigger bar line above
//————————————————————————–//=================================================================================
//Looping
//=================================================================================
Buy = 0;
Sell = 0;
PriceAtBuy = 0;
LBIT = 0;
LStop = Null;
//=================================================================================
for (j = 1; j < BarCount; j++) { if (PriceAtBuy==0 AND LE[j]) { Buy[j] = True; PriceAtBuy = LEPrice[j]; BuyPrice[j] = LEPrice[j]; LStop[j] = LIS[j-1]; LStop[j-1] = LIS[j-1]; LBIT[j] = 1; if (LBIT[j]>1 AND L[j] <= LStop[j-1]) { Sell[j] = True; SellPrice[j] = LStop[j-1]; PriceAtBuy = 0; } } else if (PriceAtBuy > 0)
{
LBIT[j] = LBIT[j-1]+1;if (LBIT[j]>1)
{
LStop[j] = LStop[j-1];
}if (LBIT[j]>1 AND L[j] <= LStop[j-1]) { Sell[j] = True; SellPrice[j] = LStop[j-1]; PriceAtBuy = 0; } if (LBIT[j]>1 AND LEx[j]) //Exit on SMA
{
Sell[j] = True;
SellPrice[j] = Max(Open[j],SMA[j-1]);
PriceAtBuy = 0;
}
}
}May 8, 2016 at 10:35 am #103781TrentRothallParticipantOne thing i noticed with your line
LEx = C > MA;
you might need
LExSetup = C > MA;
LEx = ref(LExSetup,-1);other wise you will be exiting on the open of the bar that closes above a MA causing a error
May 8, 2016 at 10:43 am #103786JulianCohenParticipantThanks Trent…doesn’t this code in the looping cover that occurrence?
if (LBIT[j]>1 AND LEx[j]) //Exit on SMA
{
Sell[j] = True;
SellPrice[j] = Max(Open[j],SMA[j-1]);
PriceAtBuy = 0;It appears to be working correctly at first glance but I got so hung up with trying to work out the initial stop I haven’t had a good look at that yet.
May 8, 2016 at 10:45 am #103787JulianCohenParticipantYou’re right…Appreciate it.
May 8, 2016 at 10:51 am #103788TrentRothallParticipantNo worries, i use a similar exit so i noticed it. you could use
LBIT[j] >1 and LEx[j-1]
but the other way you can use LExSetup for plotting/ explorationsMay 8, 2016 at 11:01 am #103789TrentRothallParticipantCode:BuySetUp = Cond1 AND Cond2 AND Cond3 AND Cond5;
Buy = Ref(BuySetUp,-1);
BuyStop = H + 0.01;
SellStop = L – 0.01;LE = Ref(BuySetUp,-1) AND H >=Ref(BuyStop,-1) AND NOT OnLastTwoBarsOfDelistedSecurity;// TRIGGER BAR Entry is all conditions fulfilled and above the BuyStop
LEPrice = Max(Open,Ref(BuyStop,-1)); //Entry PriceLEx = C > SMA; //Long Exit
LIS = SellStop; //Initial stop a tic below SetUpBar TRY CHANGING TO THIS
//————————————————————————–
//MCS code part 2
if(MCP)
LE = LE && Random()*100>=MCP; // Identifier MUST match the Identifier in the trigger bar line above
//————————————————————————–//=================================================================================
//Looping
//=================================================================================
Buy = 0;
Sell = 0;
PriceAtBuy = 0;
LBIT = 0;
LStop = Null;
//=================================================================================
for (j = 1; j < BarCount; j++) { if (PriceAtBuy==0 AND LE[j]) { Buy[j] = True; PriceAtBuy = LEPrice[j]; BuyPrice[j] = LEPrice[j]; LStop[j] = LIS[j-1]; // 1 LStop[j-1] = LIS[j-1]; //2 LBIT[j] = 1;Try changing that LIS line, i think that part is incorrect in your section.
In the loop on the line maarked 1 you are getting the initial stop to 1 tick below the setup bar which is correct, then line marked 2 is holding it at that price. I think that might work any way… :S
May 8, 2016 at 11:10 am #103792SaidBitarMemberif you want to make trailing stop then it should be like this
Code:LStop[j] = Max(LIS[j-1],LIS[j]);
LStop[j-1] = LIS[j-1];May 8, 2016 at 11:15 am #103793JulianCohenParticipantTrent…It makes sense but it didn’t work haha…Thanks anyway. It’s given me another 1000 things to try
May 8, 2016 at 11:18 am #103794JulianCohenParticipantI want the stop to stay in one place.
I will buy one tic over the top of the set up bar and create a stop loss 1 tic below the set up bar. That S/L will stay in place until hit or the C>SMA exit works.
That’s what I am trying to do…
May 8, 2016 at 11:19 am #103795TrentRothallParticipantHa Craig will help when he is around i’m sure!
May 8, 2016 at 11:20 am #103796SaidBitarMemberOK
now it makes sense
here what you need to do
you do not define your initial stop as array i mean no need to have [j] for it because it is fixed
-
AuthorPosts
- You must be logged in to reply to this topic.