I can Create AFL but Need Strategy

#21
is it so simple ......?

.......NO

as i have 3 afl for hour tf
only 1 need this tf:)
What i stated was a fact that with Amibroker setting custom time for first candle on hourly chart can be done v easily,

It really does not depend on what you have or do not have or what you need or do not need

And since you are smart enough to use 3 systems on Hourly timeframe, you will eventually figure out how to configure what you want.

Cheers
::thumb::
 

Vertigo_1985

Well-Known Member
#22
I attempted to code a strategy for backtesting( 3 bar strategy by Vikas) but wasnt able to code it fully. I am having difficulty in coding it for backtesting purpose. Any help would be really appreciated.

This is the strategy http://www.traderji.com/technical-analysis/83107-three-bar-strategy.html


I want sell signal to be taken at fixed profit % or price hitting trailing low.
Cover signal to be taken at fixed profit % or price hitting trailing high. Please add sell and cover signal to my code and make it appropriate for backtesting.

Below is what i have coded so far:-

// 1st 2 candles Inside bar
Buysignal1 = (Ref(L,-1) >= Ref(L,-2)) AND (Ref(H,-1) <= Ref(H,-2));


// 1st and third candles Bull candle
Buysignal2 = (Ref(O,-2) < Ref(C,-2)) AND ( Open < Close);

//2nd Candle bear candle
Buysignal3 = (Ref(O,-1) > Ref(C,-1));

// Buy when inside bar, 1st and 3rd bar green

Buy = Buysignal1 AND Buysignal2 AND Buysignal3 ;

// SHORT TRADE

//1st 2 candles Inside bar
ShortSignal1 = (Ref(L,-1) >= Ref(L,-2)) AND (Ref(H,-1) <= Ref(H,-2));

// 1st and third candles Bear candle
Shortsignal2 = (Ref(O,-2) > Ref(C,-2)) AND ( Open > Close);

//2nd Candle bull candle
Shortsignal3 = (Ref(O,-1) < Ref(C,-1));

Short = Shortsignal1 AND Shortsignal2 AND Shortsignal3 ;

========================
 
Last edited:

johnnypareek

Well-Known Member
#23
hi sushil

Thanks for your spirit.
just code this simple afl code,please

HTML:
strategy
chart  price as candles,ma5, ma20
display macd,signal,rsi(7) values  in legend-title
buy  condition
Buy if  1) ma(c,5) cross ma(c,20)
2)macd>signal
3)Rsi(7)>55

4)STOPLOSS =ENTRY - 2*ATR(20)  =PLOT AS A LINE
5)TARGET1   ENTRY+2*ATR(20)
   TARGET2 =ENTRY+4*ATR(20)
PLOT targets  AS DASHED LINES
-------------------------------
Once the code comes out we will see

regards

Sorry for delay as kids exams going on and march ending.

In hurry tried to make.
HTML:
/*
http://www.traderji.com/amibroker/84273-i-can-create-afl-but-need-strategy.html
strategy
chart  price as candles,ma5, ma20
display macd,signal,rsi(7) values  in legend-title
buy  condition
Buy if  1) ma(c,5) cross ma(c,20)
2)macd>signal
3)Rsi(7)>55
4)STOPLOSS =ENTRY - 2*ATR(20)  =PLOT AS A LINE
5)TARGET1   ENTRY+2*ATR(20)
   TARGET2 =ENTRY+4*ATR(20)
PLOT targets  AS DASHED LINES

*/
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

Plot(RSI(7),"7 D RSI",4,256+styleOwnScale);
Plot(MACD(),"MACD",4,256+styleOwnScale);
Plot(Signal(),"Signal",4,256+styleOwnScale);
MAfast=MA(C,5);MAslow=MA(C,20);
Buy=Cross(MA(C,5),MA(C,20) )AND MACD()>Signal() AND RSI(7)>55;

j=C;

k = Optimize("K",Param("K",2,0.25,5,0.05),0.25,5,0.05) ;
Per=Optimize("atr",Param("atr",20,3,20,1),3,20,1);
f=ATR(20);

rfsctor = WMA(H-L, Per);

revers = k * rfsctor;

Trend = 1; 
NW[0] = 0; 


for(i = 1; i < BarCount; i++)
{
if(Trend[i-1] == 1) 
{
if(j[i] < NW[i-1]) 
{
Trend[i] = -1; 
NW[i] = j[i] + Revers[i]; 
}
else 
{
Trend[i] = 1;
if((j[i] - Revers[i]) > NW[i-1])
{
NW[i] = j[i] - Revers[i]; 
}
else
{
NW[i] = NW[i-1];
}
} 
}
if(Trend[i-1] == -1) 
{
if(j[i] > NW[i-1]) 
{
Trend[i] = 1; 
NW[i] = j[i] - Revers[i]; 
}
else 
{
Trend[i] = -1;
if((j[i] + Revers[i]) < NW[i-1]) 
{
NW[i] = j[i] + Revers[i]; 
}
else
{
NW[i] = NW[i-1];
}
}
}
}

//===============system================

Plot(NW, "", IIf(Trend == 1, 27, 4), 4);
BuyPrice=C;
ProfitTaker1 = IIf(C>nw,BuyPrice + 2 * ATR(20),Null); 
ProfitTaker2 = IIf(C>nw,BuyPrice + 4 * ATR(20),Null); 
Plot(Ref(ProfitTaker1,-1), "1st Profit areal", 6, 512+4+32);
Plot(Ref(ProfitTaker2,-1), "1st Profit areal", 6, 512+4+32);
Sell=Cross(nw,C);



PlotShapes(Buy * shapeUpArrow,colorGreen, 0,L, Offset=-10); 
PlotShapes(Sell * shapeHollowDownArrow,colorRed, 0,H, Offset=-10);
 
Last edited:
#24
What i stated was a fact that with Amibroker setting custom time for first candle on hourly chart can be done v easily,

It really does not depend on what you have or do not have or what you need or do not need

And since you are smart enough to use 3 systems on Hourly timeframe, you will eventually figure out how to configure what you want.

Cheers
::thumb::
if u know how then write here in details

and to use 3 afl at same time is not means that i m master in ami
 
#25
TimeFrameGetPrice
- retrieve O, H, L, C, V values from other time frame Time Frame functions
(AFL 2.5)


SYNTAX TimeFrameGetPrice( pricefield, interval, shift = 0, mode = expandFirst )
RETURNS ARRAY
FUNCTION The TimeFrameGetPrice - retrieves OHLCV fields from other time frames. This works immediatelly without need to call TimeFrameSet at all.
First parameter - pricefield - is one of the following: "O", "H", "L", "C", "V", "I" (open interest).

Interval is bar interval in seconds. You can use pre-defined interval constants: in1Minute, in5Minute, in15Minute, inHourly, inDaily, inWeekly, inMonthly. Or integer multiples like (3*in1Minute) for 3 minute bars

shift allows to reference past (negative values) and future (positive values) data in higher time frame. For example -1 gives previous bar's data (like in Ref function but this works in higher time frame).

mode - one of available modes:

expandLast - the compressed value is expanded starting from last bar within given period (so for example weekly close/high/low is available on Friday's bar)
expandFirst - the compressed value is expanded starting from first bar within given period (so for example weekly open is available from Monday's bar)
expandPoint - the resulting array gets not empty values only for the last bar within given period (all remaining bars are Null (empty)).
Note these functions work like these 3 nested functions:

TimeFrameExpand( Ref( TimeFrameCompress( array, interval, compress(depending on field used) ), shift ), interval, expandFirst )

therefore, if shift = 0 compressed data may look into the future ( weekly high can be known on monday ). If you want to write a trading system using this function please make sure to reference PAST data by using negative shift value.

The only difference is that TimeFrameGetPrice is 2x faster than nested Expand/Compress.

For more information check Tutorial: Multiple time frame support

EXAMPLE // Example 1. get previous week Open price
TimeFrameGetPrice( "O", inWeekly, -1 )

// Example 2. get weekly Close price 3 weeks ago
TimeFrameGetPrice( "C", inWeekly, -3 )

// Example 3. get weekly High price 2 weeks ago
TimeFrameGetPrice( "H", inWeekly, -2 )

// Example 4. get this week Open price.
TimeFrameGetPrice( "O", inWeekly, 0 )

// Example 5. get previous Day High when working on intraday data
TimeFrameGetPrice( "H", inDaily, -1 )
SEE ALSO TimeFrameSet() function

References:
The TimeFrameGetPrice function is used in the following formulas in AFL on-line library:

Pivots And Prices And Swing Volume
Heatmap V1
Intraday Fibonacii Trend Break System
Intraday Trend Break System
Market Profile
Pivots for Intraday Forex Charts
plot tomorrows pivots on an intraday database
Robert Antony
Vikram's Floor Pivot Intraday System
Woodie's CCI Panel Full Stats
Woodie's Price Panel With Woodie's Pivots
More information:

Updated on-line reference



is it useful
 
#27
TimeFrameSet
- switch price arrays to a different time frame Time Frame functions
(AFL 2.5)


SYNTAX TimeFrameSet( interval)
RETURNS NOTHING
FUNCTION The TimeFrameSet replaces current price/volume arrays: open, high, low, close, volume, openint, avg with time-compressed bars of specified interval once you switched to a different time frame all calculations and built-in indicators operate on selected time frame. To get back to original interval call TimeFrameRestore() function. Before calling TimeFrameSet again in the same formula with different interval you have to restore original time frame first using TimeFrameRestore.
interval defines time frame interval in seconds. So 60 means 1-minute. For the convenience the following interval constants are pre-defined:

in1Minute = 60
in5Minute = 5 * 60
in15Minute = 15 * 60
inHourly = 3600
inDaily = 24 * 3600
inWeekly = 5 * 24 * 3600 + 1 = 432001
inMonthly = 25 * 24 * 3600 + 1 = 2160001
inQuarterly (new in 5.20)
inYearly (new in 5.20)
To get other intervals you can use multiple of pre-defined intervals, for example: ( 3*in1Minute ) gives 3 minute bars. Or you can use 3 * inDaily for 3-day bars.

New in version 4.70 and above: You can also use NEGATIVE values for N-tick charts: -5 fives 5-tick chart. Note that N-tick compression works correct only if you have 1-tick base time interval selected in database settings.

You can also use TimeFrameSet to create N-volume bars as well as Range bars. See TimeFrameMode() function for more details.

VERY IMPORTANT:
inWeekly constant is now 432001 ( 5*inDaily + 1 ) - in previous version it was 432000
inMonthly constant is now 2160001 ( 25*inDaily + 1 ) - in previous version it was 2160000
It is changed because now N-day custom intervals are supported and they will interfere with weekly/monthly.
Note that 5*inDaily is now DIFFERENT than inWeekly. 5*inDaily creates 5-day bars that DO NOT necesarily cover Monday-Friday while inWeekly ALWAYS creates bars that begin on Monday and end on Friday. Also 25*inDaily creates 25-day bars that DO NOT necesarily represent full month, while inMonthly always begins with first day of the month and ends at the last day of the month

Once you switch the time frame using TimeFrameSet , all AFL functions operate on this time frame until you switch back the time frame to original interval using TimeFrameRestore or set to different interval again using TimeFrameSet. It is good idea to ALWAYS call TimeFrameRestore when you are done with processing in other time frames.

When time frame is switched to other than original interval the results of all functions called since TimeFrameSet are time-compressed too. If you want to display them in original time frame you would need to 'expand' them as described later. Variables created and assigned before call to TimeFrameSet() remain in the time frame they were created. This behaviour allows mixing unlimited different time frames in single formula.

Please note that you can only compress data from shorter interval to longer interval. So when working with 1-minute data you can compress to 2, 3, 4, 5, 6, ....N-minute data. But when working with 15 minute data you can not get 1-minute data bars. In a similar way if you have only EOD data you can not access intraday time frames.

For more information check: Tutorial: Multiple time frame support in AFL

EXAMPLE TimeFrameSet( in5Minute ); // switch to 5 minute frame

/* MA now operates on 5 minute data, ma5_13 holds time-compressed 13 bar MA of 5min bars */

ma5_13 = MA( C, 13 );

TimeFrameRestore(); // restore time frame to original

TimeFrameSet( inHourly ); // switch now to hourly

mah_9 = EMA( C, 9 ); // 9 bar moving average from hourly data

TimeFrameRestore(); // restore time frame to original

Plot( Close, "Price", colorWhite, styleCandle );

// plot expanded average

Plot( TimeFrameExpand( ma5_13, in5Minute), "13 bar moving average from 5 min bars", colorRed );
Plot( TimeFrameExpand( mah_9, inHourly), "9 bar moving average from hourly bars", colorRed );


SEE ALSO TimeFrameRestore() function , TimeFrameExpand() function , TimeFrameGetPrice() function

Comments:
Tomasz Janeczko
tj --at-- amibroker.com
2004-06-03 04:35:37 TimeFrameSet(in15Minute);
MA10_15Min=MA(Close,10);
TimeFrameRestore();

Buy=Cross( MA(Close,5), TimeFrameExpand(MA10_15Min, in15Minute) );


References:
The TimeFrameSet function is used in the following formulas in AFL on-line library:

2 Timeframes Candlestick Bar Chart
3TF Candlestick Bar Chart
Automatic Trendlines using multiple timeframes
Bad Tick Trim on 5 sec database
Color Price Bar - Impulse System
Color Price Bars with MACD Histogram Changes
Daily High Low in Advance
EKEKO SAR-MF
Elder Impulse Indicator
Elder Impulse Indicator V2
Elder Triple Screen Trading System
Hull Multiple Moving Averages
IFT of RSI - Multiple TimeFrames
Lagging MA-Xover
Market Breadth Chart-In-Chart
Pivot Point with S/R Trendlines
Price with Woodies Pivots
Rea Time Daily Price Levels
SUPER PIVOT POINTS
Twiggs money flow weekly
More information:

Updated on-line reference



or is it useful
 
#29
ParamTime
- add user user-definable time parameter Exploration / Indicators
(AFL 2.60)


SYNTAX ParamTime( ''Name'', ''Default time'', format = 0 );
RETURNS NUMBER or STRING
FUNCTION Adds a new user-definable time parameter, which will be accessible via Parameters dialog :
right click over chart pane and select "Parameters" or press Ctrl+R allows to change chart parameters - changes are reflected immediatelly.

"name" - defines parameter name that will be displayed in the parameters dialog
"default time" - is a string holding time in any any format: HH:MM:SS, HH:MM, etc.
format - defines return value format, allowable values are:
0 - return value is a NUMBER and holds TimeNum. Ie: 133515 for 13:35:15
1 - return value is a STRING formatted holding time according to your windows regional settings
WARNING: default parameter has to be CONSTANT. This is because these values are cached and are not re-read during subsequent formula evaluations.

EXAMPLE start = ParamTime( "Start Time", "09:30" );

SEE ALSO PARAM() function , PARAMCOLOR() function , ParamDate() function , PARAMSTR() function

References:
The ParamTime function is used in the following formulas in AFL on-line library:

MFE and MAE and plot trades as indicator
More information:

Updated on-line reference



and this is last
 
#30
Hi johnnyBhai

Thanks for the superfast response with correct solution.
I forgot to add one thing.
ON SCREEN LEGEND TO SHOW VALUES OF MACD,SIGNAL,RSI
Short=if ma5 cr belkow ma20 , MACD<SIGNAL,Rsi<45
If the price goes below,in short zone(when ma5 cross below ma20),the targets to be short price -2atr,shortprice-4atr(to print only when short is in progress.
If you can add this,it will be good.
regards



Sorry for delay as kids exams going on and march ending.

In hurry tried to make.
HTML:
/*
http://www.traderji.com/amibroker/84273-i-can-create-afl-but-need-strategy.html
strategy
chart  price as candles,ma5, ma20
display macd,signal,rsi(7) values  in legend-title
buy  condition
Buy if  1) ma(c,5) cross ma(c,20)
2)macd>signal
3)Rsi(7)>55
4)STOPLOSS =ENTRY - 2*ATR(20)  =PLOT AS A LINE
5)TARGET1   ENTRY+2*ATR(20)
   TARGET2 =ENTRY+4*ATR(20)
PLOT targets  AS DASHED LINES

*/
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

Plot(RSI(7),"7 D RSI",4,256+styleOwnScale);
Plot(MACD(),"MACD",4,256+styleOwnScale);
Plot(Signal(),"Signal",4,256+styleOwnScale);
MAfast=MA(C,5);MAslow=MA(C,20);
Buy=Cross(MA(C,5),MA(C,20) )AND MACD()>Signal() AND RSI(7)>55;

j=C;

k = Optimize("K",Param("K",2,0.25,5,0.05),0.25,5,0.05) ;
Per=Optimize("atr",Param("atr",20,3,20,1),3,20,1);
f=ATR(20);

rfsctor = WMA(H-L, Per);

revers = k * rfsctor;

Trend = 1; 
NW[0] = 0; 


for(i = 1; i < BarCount; i++)
{
if(Trend[i-1] == 1) 
{
if(j[i] < NW[i-1]) 
{
Trend[i] = -1; 
NW[i] = j[i] + Revers[i]; 
}
else 
{
Trend[i] = 1;
if((j[i] - Revers[i]) > NW[i-1])
{
NW[i] = j[i] - Revers[i]; 
}
else
{
NW[i] = NW[i-1];
}
} 
}
if(Trend[i-1] == -1) 
{
if(j[i] > NW[i-1]) 
{
Trend[i] = 1; 
NW[i] = j[i] - Revers[i]; 
}
else 
{
Trend[i] = -1;
if((j[i] + Revers[i]) < NW[i-1]) 
{
NW[i] = j[i] + Revers[i]; 
}
else
{
NW[i] = NW[i-1];
}
}
}
}

//===============system================

Plot(NW, "", IIf(Trend == 1, 27, 4), 4);
BuyPrice=C;
ProfitTaker1 = IIf(C>nw,BuyPrice + 2 * ATR(20),Null); 
ProfitTaker2 = IIf(C>nw,BuyPrice + 4 * ATR(20),Null); 
Plot(Ref(ProfitTaker1,-1), "1st Profit areal", 6, 512+4+32);
Plot(Ref(ProfitTaker2,-1), "1st Profit areal", 6, 512+4+32);
Sell=Cross(nw,C);



PlotShapes(Buy * shapeUpArrow,colorGreen, 0,L, Offset=-10); 
PlotShapes(Sell * shapeHollowDownArrow,colorRed, 0,H, Offset=-10);
 

Similar threads