Skip to main content

TWAP for Amibroker (AFL)

tradinology over 9 years ago Amibroker (AFL)

  • Rating:
    3 / 5 (Votes 2)
  • Tags:
    amibroker

Hi all, this is an attempt to do twap indicator. Please correct me if I’m wrong,,,

Indicator / Formula

Copy & Paste Friendly
_SECTION_BEGIN("TWAP");
/*
The TWAP for a stock is calculated by Averaging OHLC in each bar then averaging the whole previous bars
Jarrah
*/


Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
TodayClose = Sum(C,Bars_so_far_today);
TodayHigh = Sum(H,Bars_so_far_today);
TodayLow = Sum(L,Bars_so_far_today);
TodayOpen = Sum(O,Bars_so_far_today);
TWAP = (TodayClose + TodayHigh + TodayLow + TodayOpen)/4 ;
IIf (BarIndex() >= StartBar, ATWAP = TWAP / Bars_so_far_today,0);
Plot (ATWAP,"TWAP",colorYellow, styleThick);
_SECTION_END();

11 comments

over 9 years ago

this indicator for learning purpose please don’t use it as a trading indicator,the time weighted average price. is theaverage of OHLC prices for all previous bars to indicate a resistance of intraday trading. its just an attempt to start i welcome your comment,,,,

over 9 years ago

Hallo Mr Tradinology
First of all I would like to Thank You for your TWAP AFL. It’s a good idea.
When I create the line TWAP_5 and line TWAP_10, how to add an arrow signal to the intersection of the two lines.
I’ve tried several times but failed.
Thanks in advance Mr Tradinology

_SECTION_BEGIN("TWAP 5");
Bars_so_far_today = 5 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
TodayClose = Sum(C,Bars_so_far_today);
TodayHigh = Sum(H,Bars_so_far_today);
TodayLow = Sum(L,Bars_so_far_today);
TodayOpen = Sum(O,Bars_so_far_today);
TWAP_5 = (TodayClose + TodayHigh + TodayLow + TodayOpen)/4 ;
IIf (BarIndex() >= StartBar, ATWAP_5 = TWAP_5 / Bars_so_far_today,0);
ATWAP_5_Color = IIf(ATWAP_5 >= Ref(ATWAP_5,-1),colorYellow,colorBrown);
Plot(ATWAP_5,"",ATWAP_5_Color, styleline+styleThick|styleNoLabel,Null, Null, 0, 1, 2);
_SECTION_END();

_SECTION_BEGIN("TWAP 10");
Bars_so_far_today = 10 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
TodayClose = Sum(C,Bars_so_far_today);
TodayHigh = Sum(H,Bars_so_far_today);
TodayLow = Sum(L,Bars_so_far_today);
TodayOpen = Sum(O,Bars_so_far_today);
TWAP_10 = (TodayClose + TodayHigh + TodayLow + TodayOpen)/4 ;
IIf (BarIndex() >= StartBar, ATWAP_10 = TWAP_10 / Bars_so_far_today,0);
ATWAP_10_Color = IIf(ATWAP_10 >= Ref(ATWAP_10,-1),colorYellow,colorBrown);
Plot(ATWAP_10,"",ATWAP_10_Color, styleline+styleThick|styleNoLabel,Null, Null, 0, 1, 2);
_SECTION_END();

BUY_5_10 = Cross(TWAP_5,TWAP_10);
SELL_5_10 = Cross(TWAP_10,TWAP_5);
PlotShapes(IIf(BUY_5_10, shapeUpArrow, shapeNone),colorCustom12, 0,L, Offset=-20);               
PlotShapes(IIf(SELL_5_10, shapeDownArrow, shapeNone),colorCustom11, 0,H, Offset=20);
over 9 years ago

Dear bambanghk, unfortunately I’m new with afl. but i hope our friends here can contribute for solution,

over 9 years ago

Hello bambanghk,
the problem is in

BUY_5_10 = Cross(TWAP_5,TWAP_10);
SELL_5_10 = Cross(TWAP_10,TWAP_5);

you should be using the arrays ATWAP_10 and ATWAP_5
the below code worked for me :

_SECTION_BEGIN("TWAP 5");
Bars_so_far_today5 = 5 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
TodayClose = Sum(C,Bars_so_far_today5);
TodayHigh = Sum(H,Bars_so_far_today5);
TodayLow = Sum(L,Bars_so_far_today5);
TodayOpen = Sum(O,Bars_so_far_today5);
TWAP_5 = (TodayClose + TodayHigh + TodayLow + TodayOpen)/4 ;
IIf (BarIndex() >= StartBar, ATWAP_5 = TWAP_5 / Bars_so_far_today5,0);
ATWAP_5_Color = IIf(ATWAP_5 >= Ref(ATWAP_5,-1),colorBlue,colorBrown);
Plot(ATWAP_5,"",ATWAP_5_Color, styleline+styleThick|styleNoLabel,Null, Null, 0, 1, 2);

Bars_so_far_today10 = 10 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 090000, BarIndex());
TodayClose = Sum(C,Bars_so_far_today10);
TodayHigh = Sum(H,Bars_so_far_today10);
TodayLow = Sum(L,Bars_so_far_today10);
TodayOpen = Sum(O,Bars_so_far_today10);
TWAP_10 = (TodayClose + TodayHigh + TodayLow + TodayOpen)/4 ;
IIf (BarIndex() >= StartBar, ATWAP_10 = TWAP_10 / Bars_so_far_today10,0);
ATWAP_10_Color = IIf(ATWAP_10 >= Ref(ATWAP_10,-1),colorYellow,colorBrown);
Plot(ATWAP_10,"",ATWAP_10_Color, styleline+styleThick|styleNoLabel,Null, Null, 0, 1, 2);

BUY_5_10 = Cross(ATWAP_5,ATWAP_10);
SELL_5_10 = Cross(ATWAP_10,ATWAP_5);
PlotShapes(shapeUpArrow*BUY_5_10, colorGreen, 0, L, -20 );
PlotShapes(shapeDownArrow*SELL_5_10, colorOrange, 0, H, -40 );

_SECTION_END();
over 9 years ago

You’re most welcome, if you have any idea that can help make this indicator more useful pls share so everyone can learn something new,,,

thanks

over 9 years ago

It’s a filter code in (explone) in AmiBroker based in the indicator Ichimoku Kinko Hyo chart of the full range of items as shown in the table. filtering based on the chart and the Ichimoku signals are as follows;

strong buy signal; tenkansen cut prices or Kijun sen from the bottom up position above the clouds cut kumo, the direction from the bottom up. tenkan Kijun above the clouds from below kumo. Price cloud kumo break upward.
Weak buy signal; price cut tenkan sen sen or Kijun upward position kumo cloud bottom cutter, cut up Kijun sen sen Tankan bottom kumo cloud.
All the signals occurred in the context of cloud kumo average signals are buy and sell even.
Strong sell signal: price cut Tankan sen sen or Kijun from above down to the lower cutter cloud kumo
Weak sell signal; Tankan cut prices or Kijun sen sen from above down to the blades above the cloud kumo

11. 42gr
6 months ago

Try the following.
I’ve tested it on EOD data. The buy Sell signals work well.

I’ve added parameters to allow tuning the indicator:

_SECTION_BEGIN("TWAP 4/22 (EOD) - Indicator Pane");
	// Time Weighted Average like series
	// Test on IWM 
	// GR 19/12/2025 

	// =====================
	// Pane options (indicator pane)
	// =====================
	SetChartOptions( 0, chartShowDates | chartShowArrows | chartLogarithmic | chartWrapTitle );
	SetChartBkGradientFill( colorWhite, colorLavender );
	GraphXSpace = 5;

	// =====================
	// Params
	// =====================
	Lookback1 = Param("Fast Lookback", 4, 2, 60, 1);
	Lookback2 = Param("Slow Lookback 2", 22, 2, 120, 1);

	// =====================
	// EOD "TWAP-like" series:
	// average OHLC each day, then moving average over N days
	// =====================
	typ = (O + H + L + C) / 4;

	ATWAP_5  = MA( typ, Lookback1 );
	ATWAP_10 = MA( typ, Lookback2 );

	// Avoid early-bar signals before MAs are valid
	valid = BarIndex() >= Max( Lookback1, Lookback2 );

	// =====================
	// Plot the two TWAP lines (no price plot)
	// =====================
	c5  = IIf( ATWAP_5  >= Ref(ATWAP_5,  -1), colorBlue,  colorBrown );
	c10 = IIf( ATWAP_10 >= Ref(ATWAP_10, -1), colorGreen, colorRed );

	Plot( ATWAP_5,  "TWAP " + Lookback1, c5,  styleLine | styleThick );
	Plot( ATWAP_10, "TWAP " + Lookback2, c10, styleLine | styleThick );

	// =====================
	// Signals
	// =====================
	Buy  = valid AND Cross( ATWAP_5,  ATWAP_10 );
	Sell = valid AND Cross( ATWAP_10, ATWAP_5 );

	// Optional de-duplication (uncomment if desired)
	// Buy  = ExRem( Buy, Sell );
	// Sell = ExRem( Sell, Buy );

	// =====================
	// Shapes in indicator pane
	// Use y-position = line value so arrows sit on the TWAP lines
	// =====================
	PlotShapes( Buy  * shapeUpArrow,   colorGreen, 0, ATWAP_5,  -12 );  // plotted in current pane [web:15]
	PlotShapes( Sell * shapeDownArrow, colorRed,   0, ATWAP_5,  -12 );  // plotted in current pane [web:15]

_SECTION_END();





 



Leave Comment

Please login here to leave a comment.