Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

TWAP for 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();

10 comments

1. rahuldnayak9

how to trade

2. tradinology

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,,,,

3. bambanghk

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);
4. tradinology

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

5. bambanghk

OK Thank you Mr Tradinology
Please anyone can help ?

6. tradinology

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();
7. bambanghk

That’s work Mr Tradinology
Thank you, Big Thank You

8. tradinology

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

9. kaydypham

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

10. diepthevinh2002

big thanks! very transparent indicator. Helpful!!!

Leave Comment

Please login here to leave a comment.

Back