Home › Forums › Trading System Mentor Course Community › AmiBroker Coding and AFL › Emergency Exit
- This topic is empty.
-
AuthorPosts
-
November 24, 2016 at 6:29 am #101513JulianCohenParticipant
I’m looking to write an emergency exit for my MR systems. I want to exit a trade after n number of lows have been made after an entry.
I could use exit after n bars in the looping but this is a bit of a blunt instrument and I want to make it a little more precise.
Can I use a function in the looping?
November 24, 2016 at 8:38 am #105849SaidBitarMemberi did not test it since i am not next to Amibroker but you can try one thing like this
in the exit part you can add
OR Sum(L[j-1] < L[j-2], LBIT[j-1]) == LBIT[j-1]
Sell[j] = true;
SellPrice[j] = whatever;this is checking if the previous Low was lower than the low before it and the LBIT is used here to start counting only for the duration that you are in the trade. if for example you are 4 days in the trade and everyday you have lower low then the answer of Sum(L[j-1] < L[j-2], LBIT[j-1]) is 4 which is the same as the LBIT[j-1]
November 24, 2016 at 8:51 am #105853LeeDanelloParticipantWere you thinking this?
Code:LE = Ref(BuySetUp,-1) AND L <= Ref(BuyLim,-1) AND NOT OnLastTwoBarsOfDelistedSecurity; ConsecutiveDownDays = LLV(L < Ref(L, -1),3); ConsecutiveDownDaysSinceBuy = LE AND ref(ConsecutiveDownDays,-1); if( LBIT[j] > 1 AND ConsecutiveDownDaysSinceBuy[j] OR OnSecondLastBarOfDelistedSecurity[j])3 lower lows since buy signal occurs
November 24, 2016 at 9:41 am #105854JulianCohenParticipantThanks guys
I’ll try them tomorrow. Appreciate the help
November 25, 2016 at 3:50 am #105850JulianCohenParticipantThanks for the help guys.
Maurice I couldn’t make your idea work, but I applied Said’s idea and came up with this looping
Code://=================================================================================
//Looping
//=================================================================================
Buy = 0;
Sell = 0;
PriceAtBuy = 0;
LBIT = 0;
LLIT = 0;
FlagExit = 0;
FlagExit2 = 0;
//=================================================================================
for (j = 1; j < BarCount; j++) { if (PriceAtBuy==0 AND LE[j]) { Buy[j] = True; PriceAtBuy = LEPrice[j]; BuyPrice[j] = LEPrice[j]; LBIT[j] = 1; } else if (PriceAtBuy > 0)
{
LBIT[j] = LBIT[j-1]+1;if (L[j-1] < L[j-2]) { LLIT[j] = LLIT[j-1]+1; } if (LBIT[j] == ExitBars) { FlagExit[j] = True; } if (LLIT[j] == EmergencyExit) { FlagExit2[j] = True; } if (LBIT[j] > ExitBars)
{
Sell[j] = True;
SellPrice[j] = Open[j];
PriceAtBuy = 0;
}
if (LLIT[j] > EmergencyExit )
{
Sell[j] = True;
SellPrice[j] = Open[j];
PriceAtBuy = 0;
}
if (LBIT[j]>1 AND LEx[j])
{
Sell[j] = True;
SellPrice[j] = Open[j];
PriceAtBuy = 0;
}if (LBIT[j] >= 1 AND NTEx[j])
{
Sell[j] = True;
SellPrice[j] = C[j];
LPriceAtBuy = 0;
}
}
}November 25, 2016 at 8:09 am #105855SaidBitarMemberJulian Cohen wrote:Thanks for the help guys.Maurice I couldn’t make your idea work, but I applied Said’s idea and came up with this looping
Code://=================================================================================
//Looping
//=================================================================================
Buy = 0;
Sell = 0;
PriceAtBuy = 0;
LBIT = 0;
LLIT = 0;
FlagExit = 0;
FlagExit2 = 0;
//=================================================================================
for (j = 1; j < BarCount; j++) { if (PriceAtBuy==0 AND LE[j]) { Buy[j] = True; PriceAtBuy = LEPrice[j]; BuyPrice[j] = LEPrice[j]; LBIT[j] = 1; } else if (PriceAtBuy > 0)
{
LBIT[j] = LBIT[j-1]+1;if (L[j-1] < L[j-2]) { LLIT[j] = LLIT[j-1]+1; } if (LBIT[j] == ExitBars) { FlagExit[j] = True; } if (LLIT[j] == EmergencyExit) { FlagExit2[j] = True; } if (LBIT[j] > ExitBars)
{
Sell[j] = True;
SellPrice[j] = Open[j];
PriceAtBuy = 0;
}
if (LLIT[j] > EmergencyExit )
{
Sell[j] = True;
SellPrice[j] = Open[j];
PriceAtBuy = 0;
}
if (LBIT[j]>1 AND LEx[j])
{
Sell[j] = True;
SellPrice[j] = Open[j];
PriceAtBuy = 0;
}if (LBIT[j] >= 1 AND NTEx[j])
{
Sell[j] = True;
SellPrice[j] = C[j];
LPriceAtBuy = 0;
}
}
}Julian I think you need to modify your loop
Code:if (L[j-1] < L[j-2]) { LLIT[j] = LLIT[j-1]+1; }here you are checking directly for lower lows but you need to add one more condition to be sure that you are already in the trade when you started measuring
Code:if (L[j-1] < L[j-2] AND LBIT[j] > 2)
{
LLIT[j] = LLIT[j-1]+1;
}when you add the condition for LBIT[j] > 2 you are making sure that L[j-2] is not reading the days that are before the entry day
November 25, 2016 at 10:56 am #105858JulianCohenParticipantThanks a lot Said. I bloody hate looping
-
AuthorPosts
- You must be logged in to reply to this topic.