- Joined
- 24 October 2005
- Posts
- 1,302
- Reactions
- 834
Thank You.
Just a quick question is there such thing as an Intraday Buy and Sell signal Formula ?.
Yes, the standard edition can backtest down to 1-minute data and the Professional edition down to tick data.
Thank You.
Just a quick question is there such thing as an Intraday Buy and Sell signal Formula ?.
Hi All,
I am looking to learn AFL and I am wondering whats the best way to get started is there any online tutorials on using AFL ?.
I am looking to create a easy Formula for scanning Buy and Sell signals, can you anyone tell me a buy & sell formula explaining what triggers it for a buy and sell ?.
Sorry for such a novice question.
Thanks in advance
There multiple code examples in the AB help file, in the users library, in this thread, in other threads on different websites etc. And as CanOz has already posted there is a Introduction book by H Bandy with simple examples.
SetOption("UseCustomBacktestProc", True );
if( Status("action") == actionPortfolio )
{
// retrieve the interface to portfolio backtester
bo = GetBacktesterObject();
bo.PreProcess();
for( bar = 0; bar < BarCount; bar++ )
{
// this for loop iterates through all trade signals and adjust pos size
for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
{
// if long then $ value else if short then percent of equity
sig.PosSize = IIf( sig.IsLong(), 1000/*position value - $*/, -20/*position value - percent of equity*/ );
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}
CCI0 = CCI(20);
Buy = Cross( CCI0, 100 );
Sell = Cross( 100, CCI0 );
Short = Cross( -100, CCI0 );
Cover = Cross( CCI0, -100 );
BuyPrice = SellPrice = ShortPrice = CoverPrice = O;
SetTradeDelays( 1, 1, 1, 1 );
SetOption("MaxOpenPositions", 1 );
SetOption("InitialEquity", 100000 );
SetOption("FuturesMode", False );
SetOption("UsePrevBarEquityForPosSizing", True);
Position size method?
Code:SetOption("UseCustomBacktestProc", True ); if( Status("action") == actionPortfolio ) { // retrieve the interface to portfolio backtester bo = GetBacktesterObject(); bo.PreProcess(); for( bar = 0; bar < BarCount; bar++ ) { // this for loop iterates through all trade signals and adjust pos size for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) ) { // if long then $ value else if short then percent of equity sig.PosSize = IIf( sig.IsLong(), 1000/*position value - $*/, -20/*position value - percent of equity*/ ); } bo.ProcessTradeSignals( bar ); } bo.PostProcess(); } CCI0 = CCI(20); Buy = Cross( CCI0, 100 ); Sell = Cross( 100, CCI0 ); Short = Cross( -100, CCI0 ); Cover = Cross( CCI0, -100 ); BuyPrice = SellPrice = ShortPrice = CoverPrice = O; SetTradeDelays( 1, 1, 1, 1 ); SetOption("MaxOpenPositions", 1 ); SetOption("InitialEquity", 100000 ); SetOption("FuturesMode", False ); SetOption("UsePrevBarEquityForPosSizing", True);
sig is default key word? No need to declare?
SetOption("UseCustomBacktestProc", True );
if( Status("action") == actionPortfolio )
{
// retrieve the interface to portfolio backtester
bo = GetBacktesterObject();
bo.PreProcess();
for( bar = 0; bar < BarCount; bar++ )
{
// this for loop iterates through all trade signals and adjust pos size
for( crunchor = bo.GetFirstSignal( bar ); crunchor; crunchor = bo.GetNextSignal( bar ) )
{
crunchor.PosSize = IIf( crunchor.IsLong(), 1000/*position value - $*/, -20/*position value - percent of equity*/ );
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}
If I just want to like...whenever buy then just 20% of all cash, but whenever sell 100%, how to do that?
crunchor.PosSize = IIf( crunchor.IsLong(), -20/*position value - percent of equity*/, -100/*position value - percent of equity*/ );
float PosSize
requested position size (positive numbers mean dollar value, negative values mean percent of portfolio equity)
// original by Tomasz Janeczko, edited by trash 2012
wlnum = Param( "Choose Watchlist Number containing your Symbols", 0, 0, 65, 1 );
N_FN = ParamToggle( "Show Name or Fullname in Title", "Name|FullName", 0 );
alignmode = ParamToggle( "Symbol Alignment in Title", "One Row|Multiple Rows", 0 );
if ( N_FN )
Name_ = FullName();
else
Name_ = Name();
if ( alignmode )
align = "\n";
else
align = "";
SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_N( TickerList = CategoryGetSymbols( categoryWatchlist, wlnum ) );
NumBars = 20;
fvb = Status( "firstvisiblebar" );
Plot( 100 * ( C - C[ fvb ] ) / C[ fvb ], Name_, colorBlue );
for ( i = 0; ( symbol = StrExtract( TickerList, i ) ) != ""; i++ )
{
SetForeign( symbol );
fc = Close;
if ( N_FN )
symbol = FullName();
if ( ! IsNull( fc[ 0 ] ) )
{
Plot( 100 * ( fc - fc[ fvb ] ) / fc[ fvb ], align + symbol, colorLightOrange + ( ( 2 * i ) % 15 ), styleLine );
}
RestorePriceArrays();
}
PlotGrid( 0, colorYellow );
_N( Title = Name_ + " - Relative Performance [%]: \n{{VALUES}}" );
@Richard888,
edited Rel. Performance chart
Move symbols to be charted to a watchlist and choose the watchlist in parameter options.
Also added Fullname or Name option and alignment option. If there is a bug let me know.
Code:// original by Tomasz Janeczko, edited by trash 2012 wlnum = Param( "Choose Watchlist Number containing your Symbols", 0, 0, 65, 1 ); N_FN = ParamToggle( "Show Name or Fullname in Title", "Name|FullName", 0 ); alignmode = ParamToggle( "Symbol Alignment in Title", "One Row|Multiple Rows", 0 ); if ( N_FN ) Name_ = FullName(); else Name_ = Name(); if ( alignmode ) align = "\n"; else align = ""; SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle ); _N( TickerList = CategoryGetSymbols( categoryWatchlist, wlnum ) ); NumBars = 20; fvb = Status( "firstvisiblebar" ); Plot( 100 * ( C - C[ fvb ] ) / C[ fvb ], Name_, colorBlue ); for ( i = 0; ( symbol = StrExtract( TickerList, i ) ) != ""; i++ ) { SetForeign( symbol ); fc = Close; if ( N_FN ) symbol = FullName(); if ( ! IsNull( fc[ 0 ] ) ) { Plot( 100 * ( fc - fc[ fvb ] ) / fc[ fvb ], align + symbol, colorLightOrange + ( ( 2 * i ) % 15 ), styleLine ); } RestorePriceArrays(); } PlotGrid( 0, colorYellow ); _N( Title = Name_ + " - Relative Performance [%]: \n{{VALUES}}" );
How to change this AFL codes to
Sell =(SAR() > C) + "only sell if the price is higher than the bought price"?
You will need a stop somewhere. What happens if you buy and the price drops and keeps dropping?
But you could say: Sell = Sellprice>Buyprice;
I tried this before but it doesn't work. My codes above would always have either buy or sell signal. Let's say bar 1 is a buy signal, then 100% buy in, then coming bar 2, bar 3, bar 4 all are raising and there are buy signal for these bars too. Then in bar 5, there is sell signal, but then the buyprice would be the bar 4 buyprice instead of my real buy price in bar 1.
I know there should be stop, I just try to do backtest to see what would happen.
This should work
SetPositionSize( 100, spsPercentOfEquity );
SetBacktestMode( backtestRegular ); //signal-based backtest, redundant signals are removed
BuyPrice = SAR() + 0.01;
SellPrice = SAR() - 0.01;
Buy = Buyprice>=l and Buyprice<=h and cross(C,SAR());
Sell =Sellprice>=l and Sellprice<=h and cross(SAR(), C);
buy = ExRem( buy, sell );
sell = ExRem( sell, buy );
How to change this AFL codes to
Sell =(SAR() > C) + "only sell if the price is higher than the bought price"?
SetPositionSize( 10, spsPercentOfEquity );
acc = 0.02;
accm = 0.2;
sarc = SAR( acc, accm );
Buy = Sell = 0;
Short = Cover = 0;
YourBuyConditions = SARc < C;
YourSellConditions = SARc > C;
priceatbuy = Null;
patba = Null; // Price At Buy Array (for plotting only)
for( i = 0; i < BarCount; i++ )//
{
SellSignal = YourSellConditions[ i ] AND Close[ i ] > priceatbuy;
if( SellSignal AND NOT IsNull( priceatbuy ) )
{
Sell[ i ] = 1;
priceatbuy = Null;
}
BuySignal = YourBuyConditions[ i ];
if( IsNull( priceatbuy ) AND BuySignal )
{
Buy[ i ] = 1;
priceatbuy = BuyPrice[ i ];
}
patba[ i ] = priceatbuy; // to plot you need an ARRAY, not scalar
}
Plot(patba, "priceatbuy", colorGreen, styleLine );
Plot( SARc, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );
I set the SAR to SAR(0.1,1) and apply to index history with around 10000 - 25000 range normally, and there are always having the exact same SAR in the day right after the SAR switch. That is after switching SAR, let's say becoming a raise one, day 1 and day 2 have the same SAR value. I compare the same data to another program SAR with the same data would not have this problem.
Another issue is the default SAR can only set to maximum 1 but nothing more than 1 in SAR(0.1,1) case, like I cannot set to SAR(0.1,2).
///////////////// Custom Backtester/////////////////////
SetCustomBacktestProc("");
if (Status("action") == actionPortfolio)
{
bo = GetBacktesterObject(); // Get backtester object
bo.PreProcess();
dates = DateTime();
bi = BarIndex();
MaxLoss = 0;
HC = 0;
for (i = 0; i < BarCount; i++)
{
for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
{
if (sig.IsEntry() && sig.IsLong())
bo.EnterTrade(i, sig.Symbol, True, sig.Price, sig.PosSize);
}
for (trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos())
{
P = trade.GetPrice(i, "C");// Close at i
[B][COLOR="#FF0000"] HC = Max(P, HC); ////// Highest close - PROBLEM HERE?????????????
[/COLOR][/B] Ni = StaticVarGet("MyAtr"+trade.Symbol);// Atr(20) array
entryBar = LastValue(ValueWhen(trade.EntryDateTime == dates, bi));
EC = trade.GetPrice(entrybar, "C");// Close[entrybar]
EN = Ni[entrybar];// Atr(20) at trade entry
_TRACE(" " + trade.symbol + ", entrybar = " + entrybar + ", date = " + DateTimeToStr(Dates[i]) + ", HC = " + HC + ", P = " + P+ ", N[eb]"+ Ni[entrybar] + ", EC = "
+ EC + ", i = " + i);
if (HC < EC + EN)
MaxLoss = EC - 2*EN;
if (HC >= EC + EN)
MaxLoss = EC - EN;
if (HC >= EC + 2*EN)
MaxLoss = EC;
if (HC >= EC + 3*EN)
MaxLoss = EC + EN;
if (HC >= EC + 4*EN)
MaxLoss = EC + 2*EN;
if(P < MaxLoss)
{
bo.ExitTrade(i, trade.symbol, trade.GetPrice(i, "C"), 1);
MaxLoss = 0;
Ni = 0;
EntryBar = 0;
HC = 0;
EC = 0;
NE = 0;
}
}
bo.HandleStops(i); // Handle programmed stops at this bar
bo.UpdateStats(i, 1); // Update MAE/MFE stats for bar
bo.UpdateStats(i, 2); // Update stats at bar's end
}
bo.PostProcess();
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?