Entry & Exit in Same Candle - AFL Programming - Am PDF
Entry & Exit in Same Candle - AFL Programming - Am PDF
We understand that you may be in a hurry, but before you post pretty please do the following:
Read How to use this site - really it is essential to read at least 3 top points
Search the forum (use magnifying glass in upper right corner) to find already existing solutions first
Check AmiQuote FAQ - 99% of all AmiQuote questions are answered already there and any new
AmiQuote questions should go there.
Make sure you are using up-to-date version of the software. Your very first step should be updating your
I am trying price action based strategy, where I want to exit in the same candle if my exit price matches. But
none of my trades are exiting in the same candle even though price hit the target in the same candle, its
exiting in the next candle when the price hit the target or exiting at open, if open is above target.
Not sure if I am missing any function.
Buy = H>Prev_Day_High;
BuyPrice = Prev_Day_High;
Sell = H>(BuyPrice + Prev_Day_High*0.001);
Please refer to Allow same bar exit section of System test settings window guide. Search the forum too
(this is discussed several times). Also refrain from using ExRem() function while same bar exit is "on".
Limitation of Exrem or Flip functions: It has nothing to do with the platform (in this cas…
Please refer to Allow same bar exit section of System test settings window guide. Search the forum too
(this is discussed several times).
Also refrain from using ExRem() function while same bar exit is "on".
Limitation of Exrem or Flip functions: It has nothing to do with the platform (in this case AB) but more to do
with the logic of your strategy. As per definition, Exrem( ARRAY1, ARRAY2 ) removes excessive signals;
returns 1 on the first occurrence of "true" signal in Array1 then returns 0 until Array2 is true even if
there are "true" signals in Array1. Now, what if by virtue of the strategy used, both Array1 and Array2
becomes true at the same time on a single bar? The below code can be used to control such specific
situation otherwise Exrem function is well-off - depending on the strategy.
Copy
//Array Initialization
Buy = Sell = Short = Cover = Null;
LongFlag = ShortFlag = 0; //Simple Flag arrays to identify whether in a Lo
//Short Positions
if( _Short[ i ] AND ShortFlag == 0 )
{
Short[ i ] = 1;
ShortFlag = 1; //To record that we are in Short position
}
if( _Buy[ i ] AND ShortFlag == 1 )
{
Cover[ i ] = 1; //Covering the Short position
ShortFlag = 0; //Reseting ShortFlag back to False, to denote tha
}
}
//Plotting
Plot( C, "", colorDefault, styleBar | styleThick );
Plot( HigherHighs, "Highs", colorDarkGreen );
Plot( LowerLows, "Lows", colorBrown );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -
PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0,
Controlled Trailing Stop-Loss (TSL): It is necessary to ensure that TSL always goes up for Long positions
and goes down for Short positions or remains constant to the previous value. "Percentage or Points" based
TSLs does not account market Volatility, so it is not dynamic. "Standard Deviation" based TSL (for e.g. Kase
DevStop ) or "ATR" based TSLs are ideal. For demo using ATR based TSL here:
Copy
//Array Initialization
Buy = Sell = Short = Cover = TSL = Null;
LongFlag = ShortFlag = 0; //Simple flags
//Short Positions
if( _Short[ i ] AND ShortFlag == 0 )
{
Short[ i ] = 1;
ShortFlag = 1; //To record that we are in Short position
}
if( ShortFlag )
{
if( TSL[ i - 1 ] < L[ i ] + _ATR[ i ] * MultATR )
TSL[ i ] = TSL[ i - 1 ];
else
TSL[ i ] = L[ i ] + _ATR[ i ] * MultATR;
}
//Plotting
Plot( C, "", colorDefault, styleBar | styleThick );
Plot( HigherHighs, "Highs", colorDarkGreen, styleDashed | styleNoRescale )
Plot( LowerLows, "Lows", colorBrown, styleDashed | styleNoRescale );
yogi:
Purely circumstantial - depends on the strategy! If the system demands explicit segregation, then TINA than
looping.
To keep signals you have to have them IN STATE form as opposed to Signal (Pulse) form.
Your Buy is NOT in pulse form and you can’t use BarsSince then.
Secondly, you are doing other mistakes such as using = (assignment) instead of comparison (==).
These are basic mistakes that you need to get rid of otherwise all formulas will be wrong.
https://www.amibroker.com/guide/a_mistakes.html
Thirdly if you are using loop, you don’t need ExRem or BarsSince or anything like that
So if you don't want same bar entry-exit and exit-entry you need to
Turn OFF / Remove that option / line from the code
It was written a number of times already: remove ExRems. They are not needed. ExRem() and Flip()
logically are working like set-reset latch flip-flop device . So obviously the same time "set" and "reset" is
not allowed. If you don't understand how ExRems work you should follow advice given here: How do I
debug my formula? and specifically this http://www.amibroker.com/kb/2014/09/29/debugging-
techniques-part-1-exploration/
yogi Nov '19
if (isUpTrend[i])
lowerBand[i] = Max(lowerBand[i], lowerBand[i-1]);
else
upperBand[i] = Min(upperBand[i], upperBand[i-1]);
}
st = SuperTrend(10,3);
///////////////////////////////////////////////////////////////////////////////
BASIC UPPERBAND = (HIGH + LOW) / 2 + Multiplier * ATR
BASIC LOWERBAND = (HIGH + LOW) / 2 - Multiplier * ATR
FINAL UPPERBAND = IF( (Current BASICUPPERBAND < Previous FINAL UPPERBAND) and
FINAL LOWERBAND = IF( (Current BASIC LOWERBAND > Previous FINAL LOWERBAND) and
SUPERTREND = IF(Current Close <= Current FINAL UPPERBAND ) THEN Current FINAL U
///////////////////////////////////////////////////////////////////////////////
_AT = ATR(10);
_Mul = 3;
BUB = ((High + Low)/2) + (_Mul * _AT);
BLB = ((High + Low)/2) - (_Mul * _AT);
FUB = IIf(BUB <= P_FUB OR P_C >= P_FUB, BUB, P_FUB);
Please guide.
@yogi ,
See license-verified-badge .
Also, the SuperTrend function that you have posted was written by me. So first, you should either ask
permission or at least give credit of some sort when you're referencing someone else's work. Second, I
resorted to a loop because you can't write the SuperTrend logic using only array functions. The reason is
that the upper and lower band values get modified as you iterate through the array, and they feed back into
the isUpTrend array.