// C:\Dropbox\AmiBroker Formulas\Formulas\_UKB TDash Project\ FileName = "TDashMain104.afl"; GraphXSpace = 10; BlankBars = 10; SetBarsRequired(sbrAll); EnableTextOutput( False ); function PersistentVarRemove( VarName ) { global TDashPath; Fn = TDashPath + VarName + ".pva"; fh = fdelete( Fn ) ; return fh; } function PersistentVarGet( VarName ) { global TDashPath; fh = fopen( TDashPath + VarName + ".pva", "r" ); if ( fh ) { String = fgets( fh ); fclose( fh ); Number = StrToNum( String ); } else Number = Null; return Number; } function PersistentVarSet( VarName, Number ) { global TDashPath; String = NumToStr( Number ); fh = fopen( TDashPath + VarName + ".pva", "w" ); if ( fh ) { fputs( String, fh ); fclose( fh ); } return fh; } function ConvertEntryPriceToPixels() { Price = Nz( StaticVarGet( "~PosMarkerEntryPrice" ) ); //_TRACE( "Price: "+Price ); pxWidth = Status( "pxWidth" ); Miny = Status( "axisminy" ); Maxy = Status( "axismaxy" ); pxchartright = Status( "pxchartright" ); pxchartbottom = Status( "pxchartbottom" ); pxcharttop = Status( "pxcharttop" ); PxChartRange = Status( "pxchartheight" ); pxheight = Status( "pxheight" ); Pricerange = MaxY - MinY; EntryYPixels = ( MaxY - Price ) * PxChartRange / Pricerange + 5; StaticVarSet( "~EntryYPixels", EntryYPixels ); //_TRACE( "EntryYPixels: "+EntryYPixels ); return 0; } function PersistentListGet( VarName ) { global TDashPath; fh = fopen( TDashPath + VarName + ".csv", "r" ); if ( fh ) { String = fgets( fh ); fclose( fh ); } else string = ""; return String; } function GetBRPendingList() { return PersistentListGet( "BRPendingOrders" ); } function ShowPositionMarker() { global BLK; pxWidth = Status( "pxWidth" ); EntryYPixels = Nz( StaticVarGet( "~EntryYPixels" ) ); MArkerColor = Nz( StaticVarGet( "~PosMarkerColor" ) ); GfxSetBkMode( 1 ); // Draw line at Entry price for position GfxSelectPen( MArkerColor, 1, 0 ); GfxMoveTo( 0, EntryYPixels ); GfxLineTo( pxwidth, EntryYPixels ); } function MAinLinkWitTDash( MArkername ) { local xMY; if ( IsNull( StaticVarGet( "~MarkerInit_" + MArkerName ) ) ) // init { Price = PersistentVarGet( "MainChartPrice_" + MarkerName ); if ( Price > 0 ) { Miny = Status( "axisminy" ); Maxy = Status( "axismaxy" ); PxChartRange = Status( "pxchartheight" ); Pricerange = MaxY - MinY; TDashYPixels = ( MaxY - Price ) * PxChartRange / Pricerange + 5; StaticVarSet( "~TDashYPixels_" + MarkerName, TDashYPixels ); StaticVarSet( "~MainChartPrice_" + MarkerName, Price ); StaticVarSet( "~ActiveMarker_" + MarkerName, True ); } StaticVarSet( "~MarkerInit_" + MArkerName, True ); } ActiveMarker = Nz( StaticVarGet( "~ActiveMarker_" + MarkerName ) ); if ( ActiveMarker ) // is marker still active? { pxWidth = Status( "pxWidth" ); Miny = Status( "axisminy" ); Maxy = Status( "axismaxy" ); pxchartright = Status( "pxchartright" ); pxchartbottom = Status( "pxchartbottom" ); pxcharttop = Status( "pxcharttop" ); PxChartRange = Status( "pxchartheight" ); pxheight = Status( "pxheight" ); Pricerange = MaxY - MinY; // TDash Left Button release gets TDash y-pixel value and convert to price if ( Nz( StaticVarGet( "~LeftButtonRelease" ) ) ) { TDashYPixels = Nz( StaticVarGet( "~TDashYPixels_" + MarkerName ) ); // Y pixels from TDash window Price = Maxy - ( Pricerange / PxChartRange * ( TDashYPixels - 5 ) ); StaticVarSet( "~LeftButtonRelease", False ); StaticVarSet( "~MainChartPrice_" + MarkerName, Price ); } else if ( Nz( StaticVarGet( "~NowDragging_" + MarkerName ) ) == 0 ) // TDash marker tracks main window { Price = Nz( StaticVarGet( "~MainChartPrice_" + MarkerName ) ); TDashYPixels = ( MaxY - Price ) * PxChartRange / Pricerange + 5; StaticVarSet( "~TDashYPixels_" + MarkerName, TDashYPixels ); // Y pixels calculated from price in main chart } else if ( Nz( StaticVarGet( "~NowDragging_" + MarkerName ) ) ) // Main window tracks TDash window { TDashYPixels = Nz( StaticVarGet( "~TDashYPixels_" + MarkerName ) ); Price = Maxy - ( Pricerange / PxChartRange * ( TDashYPixels - 5 ) ); StaticVarSet( "~MainChartPrice_" + MarkerName, Price ); // Price for main chart calculated from TDash y pixels } Price = Nz( StaticVarGet( "~MainChartPrice_" + MarkerName ) ); StaticVarSet( "~MainChartPrice_" + MarkerName, Price ); MArkerColor = Nz( StaticVarGet( "~MarkerColor_" + MarkerName ) ); MArkerTextColor = Nz( StaticVarGet( "~MarkerTextColor_" + MarkerName ) ); MArkerPenColor = Nz( StaticVarGet( "~MarkerPenColor_" + MarkerName ) ); GfxSetBkMode( 1 ); GfxSelectPen( MArkerColor, 1, 2 ); if( Nz( StaticVarGet( "~ShowPriceLines" ) ) ) { GfxMoveTo( 0, TDashYPixels ); GfxLineTo( pxwidth, TDashYPixels ); } } } function ShowQBarArrow( Y ) { QBarColor = Nz( StaticVarGet( "~ActionColor" ) ); PCW = Status( "pxchartwidth" ) + 5; GfxSetOverlayMode( 0 ); GfxSelectSolidBrush ( QBarColor ); GfxSelectPen( QBarColor, 1 ); S = 10; X1 = PCW - s; x2 = PCW - 3 * s / 5; x3 = PCW - s / 2; x4 = PCW - 2 * s / 5; x5 = PCW; if ( QBarColor == colorRed ) { Y1 = Y + 3 * s; y2 = Y + 1.5 * s; y3 = Y; GfxPolygon( x1, y2, x3, y1, x5, y2, x4, y2, x4, y3, x2, y3, x2, y2, x1, y2 ); } else if ( QBarColor == colorBrightGreen ) { Y1 = Y - 3 * s; y2 = Y - 1.5 * s; y3 = Y; GfxPolygon( x1, y2, x3, y1, x5, y2, x4, y2, x4, y3, x2, y3, x2, y2, x1, y2 ); } } function DrawQBarPriceLine() { if ( Nz( StaticVarGet( "~QBarVisible" ) ) ) { pxWidth = Status( "pxWidth" ); Miny = Status( "axisminy" ); Maxy = Status( "axismaxy" ); pxchartright = Status( "pxchartright" ); pxchartbottom = Status( "pxchartbottom" ); pxcharttop = Status( "pxcharttop" ); PxChartRange = Status( "pxchartheight" ); pxheight = Status( "pxheight" ); Pricerange = MaxY - MinY; QBarColor = Nz( StaticVarGet( "~QBarColor" ) ); QBarTextColor = Nz( StaticVarGet( "~QBarTextColor" ) ); QBarPenColor = Nz( StaticVarGet( "~QBarPenColor" ) ); QBarColor = Nz( StaticVarGet( "~ActionColor" ) ); QBarTextColor = Nz( StaticVarGet( "~ActionTextColor" ) ); // MKT, BID, and ASK LMTs QBarType = StaticVarGetText( "~QBarType" ); if( QBarType == "MKT" ) { Price = LastValue(C); QBarYPixel = ( MaxY - Price ) * PxChartRange / Pricerange + 5; } else { QBarYPixel = Nz( StaticVarGet( "~QBarYPixel" ) ); // Y pixels from TDash window Price = Maxy - ( Pricerange / PxChartRange * ( QBarYPixel - 5 ) ); } StaticVarSet( "~QBarPriceLine", Price ); GfxSetBkMode( 0 ); GfxSelectPen( QBarColor, 1, 1 ); GfxMoveTo( 0, QBarYPixel ); GfxLineTo( pxwidth, QBarYPixel ); QBarSelection = StaticVarGetText( "~QBarSelection" ); if ( QBarSelection != "" ) { GfxSetBkMode( 2 ); GfxSetBkColor( QBarColor ); GfxSetTextColor( QBarTextColor ); GfxSelectFont( "Lucida Console", FontSize = 10, FontWeight = 700 ); if ( QBarType != "MKT" ) { Y = Nz( QBarYPixel ); Y1 = Y - Fontsize/2; Y2 = Y + Fontsize/2; X1 = Status( "pxchartwidth" ) + 5; X2 = Status( "pxwidth" ); Xw = X2 - X1; // No label needed when trading at MKT GfxSelectSolidBrush( QBarColor ); GfxDrawText( NumToStr( Price, 1.2, False ), pxchartright, QBarYPixel - FontSize, pxwidth, QBarYPixel + Fontsize, 37 ); //GfxPolygon( x1+xw/4, y2, x2-xw/4, y2, (x1+x2)/2, y2+FontSize, x1+xw/4,y2); } //ShowQBarArrow( Nz( QBarYPixel ) ); // text prompt on QBar price line // GfxSetBkMode( 0 ); // GfxSetBkColor( QBarColor ); // GfxSetTextColor( QBarColor ); // GfxSelectFont( "Lucida Console", FontSize = 16, FontWeight = 700 ); // GfxDrawText( QBarSelection, pxchartright - 600, QBarYPixel - FontSize, pxwidth, QBarYPixel + Fontsize, 37 ); } } } TDashPath = StaticVarGetText( "~TDashPath" ); RequestTimedRefresh( 0.1 ); SetChartOptions( 0, chartHideQuoteMarker ); SetOption( "PriceBoundChecking", False); GfxSetOverlayMode( 1 ); //NumBlankBars = Param( "Number Blan bars", 0,0,10,1); //SetChartOptions( 1, chartShowDates, Null, 0, 0, NumBlankBars ); // Test pxWidth = Status( "pxWidth" ); Miny = Status( "axisminy" ); Maxy = Status( "axismaxy" ); pxchartright = Status( "pxchartright" ); pxchartbottom = Status( "pxchartbottom" ); pxcharttop = Status( "pxcharttop" ); PxChartRange = Status( "pxchartheight" ); pxheight = Status( "pxheight" ); Pricerange = MaxY - MinY; OnQBar = Nz( StaticVarGet( "~OnQBar" ) ); LastOpenPrice = LastValue( O ); StaticVarSet( "~MainLastOpenPrice", LastOpenPrice ); LastHighPrice = LastValue( H ); StaticVarSet( "~MainLastHighPrice", LastHighprice ); LastLowPrice = LastValue( L ); StaticVarSet( "~MainLastLowPrice", LastlOWprice ); LastPrice = LastValue( C ); StaticVarSet( "~MainLastPrice", Lastprice ); LastDateTime = LastValue( DateTime() ); StaticVarSet( "~MainLastDT", LastDateTime ); LastpriceYPixels = ( MaxY - LastPrice ) * PxChartRange / Pricerange + 5; StaticVarSet( "~MainLastpriceYPixels", LastpriceYPixels ); StaticVarSet( "~MainLastPrice", Lastprice ); // end, test oly Ticker = Name(); StaticVarSetText( "~TDashTicker", Ticker); if( OnQBar ) DrawQBarPriceLine(); BRPendingList = GetBRPendingList(); T=""; NumPendingOrders = 0; if ( BRPendingList != "" ) { for ( P = 0; ( BROID = StrExtract( BRPendingList, P ) ) != ""; P++ ) { NumPendingOrders++; MAinLinkWitTDash( BROID ); T = T+NumToStr(NumPendingOrders,2.0)+" BROID: "+BROID+" QPx: "+NumToStr(Nz(StaticVarGet( "~TDashYPixels_" + BROID)),3.3,False) +" MQPrice: "+Nz( StaticVarGet( "~MainChartPrice_" + BROID ) ) + " Record: "+StaticVarGetText( "~BROrderRecord_"+BROID )+"\n"; //_TRACE( T ); } StaticVarSet( "~NumPendingOrders", NumPendingOrders ); } _SECTION_BEGIN( "Indicators" ); Plot( C, "", 1, 128 ); Buy=Short=Cover=Sell=0; ShowArrows = Nz( StaticVarGet( "~ShowArrows" ) ); Showequity = Nz( StaticVarGet( "~ShowEquity" ) ); ShowQTY = Nz( StaticVarGet( "~ShowQty" ) ); SignalArray= Nz( Foreign( Ticker + "~SignalArrays", "V", False ) ); Buy = IIf( SignalArray & 1, 1, 0 ); Sell = IIf( SignalArray & 2, 1, 0 ); Short = IIf( SignalArray & 4, 1, 0 ); Cover = IIf( SignalArray & 8, 1, 0 ); BuyPrice = Nz( Foreign( Ticker + "~SignalArrays", "O" ) ); SellPrice = Nz( Foreign( Ticker + "~SignalArrays", "H" ) ); ShortPrice = Nz( Foreign( Ticker + "~SignalArrays", "L" ) ); CoverPrice = Nz( Foreign( Ticker + "~SignalArrays", "C" ) ); TradeQty = Nz(StaticVarGet( "~TradeQty")); TradeQtyArray = Nz( Foreign( Ticker + "~SignalArrays", "I", False ) ); PositionQty = Cum( TradeQtyArray ); // for entire history CurrentPos = LastValue(PositionQty ); StaticVarSet( "~CurrentPosition", CurrentPos ); if ( ShowArrows OR ShowEquity ) { if ( ShowArrows ) { PlotShapes( IIf( Buy, shapeSmallUpTriangle, shapeNone ), 5, 0, BuyPrice, 0 ); PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), 4, 0, SellPrice, 0 ); PlotShapes( IIf( Short, shapeHollowDownTriangle, shapeNone ), 4, 0, ShortPrice, 0 ); PlotShapes( IIf( Cover, shapeHollowUpTriangle, shapeNone ), 5, 0, CoverPrice, 0 ); } if ( ShowEquity ) { Eq = Equity( 0 ); Eqs = ValueWhen( Sell OR Cover, Eq ); Plot( Eqs, "Equity", colorWhite, styleLine | styleOwnScale ); } if ( ShowQty ) { fvb = Status( "firstvisiblebar" ); lvb = Status( "lastvisiblebar" ); for ( b = fvb; b <= Lvb - BlankBars; b++ ) { if( SignalArray[b] != 0 ) { if( TradeQtyArray[b] > 0 ) Color = colorBrightGreen; else Color = colorRed; PlotText( NumToStr( TradeQtyArray[b],1.0,False), b, L[b]-(H[B]-L[B]), color ); } } Color = IIf( PositionQty > 0, colorBrightGreen, IIf( PositionQty < 0, colorRed, colorBlack )); Plot( PositionQty,"",color,styleArea|styleOwnScale,-5*TradeQty,100*TradeQty); } } Delay = Param( "Delay", 1, 0, 100, 1 ); Width = Param( "Width", 2, 0, 10, 0.05 ); Color = ParamColor( "BB Color", colorBlue ); ShowBB = Nz( StaticVarGet( "~ShowBB" ) ); if ( ShowBB ) { BBPeriod = SHowBB*5; BBTop = Ref( BBandTop( C, BBPeriod, Width ), -Delay ); BBBot = Ref( BBandBot( C, BBPeriod, Width ), -Delay ); Plot( BBTop, "BBTop", Color, styleLine|styleNoLabel ); Plot( BBBot, "BBBot", Color, styleLine|styleNoLabel ); } Color = ParamColor( "MA Color", colorBlue ); ShowMA = Nz( StaticVarGet( "~ShowMA" )); if ( ShowMA ) { MAPeriod = SHowMA*5; Plot( MA( C, MAPeriod), "MA", color, styleLine|styleNoLabel ); } _SECTION_END(); PosSign = Nz( StaticVarGet( "~PositionSign" ) ); GoingLongPrice = LastValue( ValueWhen( Ref(PositionQty,-1) == 0 AND PositionQty > 0, BuyPrice )); StaticVarSet( "~GoingLongPrice", GoingLongPrice ); GoingShortPrice = LastValue( ValueWhen( Ref(PositionQty,-1) == 0 AND PositionQty < 0, ShortPrice )); /// ??????????? StaticVarSet( "~GoingShortPrice", GoingShortPrice ); if( PosSign > 0 ) { StaticVarSet( "~PosMarkerEntryPrice", GoingLongPrice ); } else if( PosSign < 0 ) { StaticVarSet( "~PosMarkerEntryPrice", GoingShortPrice ); } // Draw line from entry to right edge of chart if ( PosSign ) { BI = BarIndex(); LBI = LastValue( BI ); GoingLongBI = LastValue( ValueWhen( Ref( PositionQty, -1 ) == 0 AND PositionQty > 0, BI ) ); GoingShortBI = LastValue( ValueWhen( Ref( PositionQty, -1 ) == 0 AND PositionQty < 0, BI ) ); if ( PosSign > 0 ) BarsSinceEntry = LBI - GoingLongBI + 1; //LastValue( BarsSince( Buy OR Short ) ) + 1; else if ( PosSign < 0 ) BarsSinceEntry = LBI - GoingShortBI + 1; lvb = Status( "lastvisiblebar" ); fvb = Status( "firstvisiblebar" ); pxchartwidth = Status( "pxchartwidth" ); BarRange = Lvb - fvb + 1; PxPerbar = pxchartwidth / BarRange; X1 = pxchartRight - ( BarsSinceEntry + BlankBars ) * PxPerbar; Y1 = Y2 = Nz( StaticVarGet( "~EntryYPixels" ) ); Color = Nz( StaticVarGet( "~PosMarkerColor" ) ); GfxSelectPen( Color, 1 ); GfxMoveTo( X1, Y1 ); GfxLineTo( pxWidth, Y2 ); } ConvertEntryPriceToPixels(); //EnableTextOutput( True ); //Title = "\nBR Pending Orders:\n"+T; // TDashMain debug stuff if( ParamToggle( "TDash Debug", "OFF|ON", 0 ) ) { DT=DateTime(); Title = "\n"+ " FirstDate: "+NumToStr(DT[0], formatDateTime)+"\n"+ " Lastfate: "+NumToStr(LastValue(DT),formatDateTime)+"\n"+ " Action: "+NumToStr( SignalArray,1.0,False)+"\n"+ "CurrentPos: "+NumToStr( CurrentPos,1.0,False)+"\n"+ " Buy: "+NumToStr( Buy,1.2,False)+"\n"+ " Sell: "+NumToStr( Sell,1.2,False)+"\n"+ "Short: "+NumToStr( Short,1.2,False)+"\n"+ "Cover: "+NumToStr( Cover,1.2,False)+"\n"+ " Buyprice: "+NumToStr( BuyPrice,1.2,False)+"\n"+ " Sellprice: "+NumToStr( SellPrice,1.2,False)+"\n"+ "Shortprice: "+NumToStr( ShortPrice,1.2,False)+"\n"+ "Coverprice: "+NumToStr( CoverPrice,1.2,False)+"\n"+ " TradeQty: "+NumToStr( CoverPrice,1.2,False)+"\n"+ "\n"+"BlankBars: "+BlankBars; }