Convert Volume Profile Support/resistance Code from Tradestation

D

dschapira

New member
Hello, I downloaded this support and resistance study based on volume profile, but its only for tradestation. Could anyone make one for TOS?
Here is the tradestation code if it helps. I am willing to pay for the code.

Code:
int TopVolPivots( 5 ), // The total number of lines to be drawn, with the highest occurrence values. TopVolPivots can be a positive integer up to 20
            int Strength( 3 ), // Strength of pivots
           
            // Define colors for lines
            int Color1( Red ),
            int Color2( Magenta ),
            int Color3( Green ),
            int Color4( Cyan ),
            int Color5( Yellow ),
            int Color6( White ),
            int Color7( White ),
            int Color8( LightGray ),
            int Color9( LightGray ),
            int Color10( LightGray ),
           
            // Define line styles for lines
            int LineStyle1( 0 ),
            int LineStyle2( 0 ),
            int LineStyle3( 0 ),
            int LineStyle4( 0 ),
            int LineStyle5( 0 ),
            int LineStyle6( 0 ),
            int LineStyle7( 0 ),
            int LineStyle8( 0 ),
            int LineStyle9( 0 ),
            int LineStyle10( 0 ),
           
            string FontName( "Ariel" ), // Sets the font of the line labels
            double FontSize( 10 ), // Sets the font size for the line labels
           
            int LineThickness( 1 ), // Defines thickness of lines. LineTickness can be a positive integer up to 6
            int UpdareFrequency( 5000 ), // Determines how often the timer checks for changes in the upper and lower price values. In milliseconds
            bool DrawLabels( True ); // If set to TRUE line labels are drawn.

Vars:        Vector TLVector( Null ), // Stores references for trend lines
            Vector LineLabelVector( Null ), // Stores references for text drawing objects
           
            Vector ColorVector( Null ), // Vector to store color information from user inputs
            Vector LineStyleVector( Null ), // Vector to store line styles from the user inputs
           
            TrendLine Trendline1( Null ),
            TrendLine myTrendLine1( Null ),
            TextLabel LineText( Null ),
           
            DTPoint DTPoint1( Null ), // DateTime point used in the creation of trendlines
            DTPoint DTPoint2( Null ), // DateTime point used in the creation of trendlines
           
            Intrabarpersist bool ScreenSizeChanged( False ),
            intrabarpersist double HighestDispValueLast( 0 ), // Stores highest display value visible
            intrabarpersist double LowestDispValueLast( 0 ),  // Stores lowest display value visible
            IntrabarPersist bool LastBarOnCht( False ), // Set to true if last bar on chart
           
            string Txt(""),
            int DecPlaces( 0 ),
           
            DateTime RightOfChart( Null ), // Stores the datetime of the right of the chart in datetime format
           
            Dictionary PriceVolume( Null ), // Dictionary that stores the entire volume profile for the entire history
            Dictionary PriceVolumeInRange( Null ), // A dictionary that just stores the volume profile for the current price range visible
           
            Vector RankedVolVec( Null ), // A vector containing price levels as strings ranked by volume
           
            double VolVal( 0 ), // Stores volume depending on chart type
           
            bool debug( false ); // Prints the dictionary and vector for debugging

// Method to sort dictionary keys into a vector by volume order with the keys with the highest volume higher in the vector
Method vector DictionaryVolSort( Dictionary Dict2bSortedbyVol )
Vars: string TmpIndex, double TmpVol, Vector Keys, Vector Vols;
Begin
    Keys = Dict2bSortedbyVol.Keys; // Create a vector from the dictionary keys
    Vols = Dict2bSortedbyVol.Values; // Create a vector from the dictionary values
    For Value1 = 0 to Dict2bSortedbyVol.Count - 1
    Begin
        For Value2 = ( Value1 + 1 ) to Dict2bSortedbyVol.Count - 1
        Begin
            If Vols[Value1] astype double < Vols[Value2] astype double then
            Begin
                //Print(" VOL SORT ", Dict2bSortedbyVol.Values[Value1] astype double," should be less than ", Dict2bSortedbyVol.Values[Value2] astype double );
                TmpIndex = Keys[Value1] astype string;
                TmpVol = Vols[Value1] astype double;
               
                Keys[Value1] = Keys[Value2] astype string;
                Vols[Value1] = Vols[Value2] astype double;
               
                Keys[Value2] = TmpIndex;
                Vols[Value2] = TmpVol;
            End;
        End;
    End;
    Return Keys;
End;

// Method to find volume pivots in a dictionary already sorted by price
Method Dictionary VolumePivots( Dictionary FindPivots )
Vars: bool PivotTest, int StrengthCtr, Dictionary TmpDict;
Begin
    TmpDict = New Dictionary;
    // Test each price level (except the number strength at the top as there are no bars above to test and 'FindPivots.Count - Strength - 1' at the bottom)
    For Value1 = Strength to FindPivots.Count - Strength - 1
    Begin
        // Print( "PivotFinder ", Value1, " Strength ", Strength );
        PivotTest = TRUE; // Assume it is a pivot, until proved otherwise
        StrengthCtr = 1; // Reset strength counter
        // First test below pivot
        While PivotTest = TRUE and StrengthCtr <= Strength
        Begin
            If FindPivots.Values[Value1] astype double < FindPivots.Values[Value1 + StrengthCtr] astype double then PivotTest = False
            Else StrengthCtr +=1; // Increment StrengthCtr by 1
        End;
        // If first test was passed now check above the pivot
        StrengthCtr = 1;
        While PivotTest = TRUE and StrengthCtr <= Strength
        Begin
            If FindPivots.Values[Value1] astype double < FindPivots.Values[Value1 - StrengthCtr] astype double then PivotTest = False
            Else StrengthCtr +=1; // Increment StrengthCtr by 1
        End;       
        // If a pivot is found, add to a local dictionary
        If PivotTest = True then TmpDict.Add( FindPivots.Keys[Value1] astype string, FindPivots.Values[Value1] astype double ); // Print("****** ",  FindPivots.Keys[Value1] astype string, FindPivots.Values[Value1] astype double );
        PivotTest = False;
    End;
    Return TmpDict;
End;

// Method to draw initial trend lines and text, otherwise, if already drawn move their positions
Method void TLDictDrawOrRedraw( )
Begin
    If TLVector.Count = 0 then
    Begin
        For Value1 = 0 to TopVolPivots - 1
        Begin
            // Add initial TopVolPivots lines to chart
            DTPoint1 = DTPoint.Create( BarDateTime, 0 );
            Trendline1 = Trendline.Create( DTPoint1, DTPoint1 );
            Trendline1.ExtRight = True;
            Trendline1.ExtLeft = True;
            Trendline1.Persist = True;
            DrawingObjects.Add( Trendline1 );
            TLVector.push_back( Trendline1 );
           
            // Add initial labels to chart
            If DrawLabels then
            Begin
                LineText = TextLabel.Create( DTPoint1,"");
                LineText.VStyle = 0;
                LineText.HStyle = 1;
                LineText.Persist = True;
                LineText.Font = Font.create( FontName, FontSize );
                DrawingObjects.Add( LineText );
                LineLabelVector.push_back( LineText );
            End;
        End;
    End
    Else // If text objects have already been drawn
    Begin
        For Value1 = 0 to TLVector.Count - 1
        Begin
            // Move all existing lines out of the visible area
            DTPoint1 = DTPoint.Create( BarDateTime, 0 );
            myTrendLine1 = TLVector[Value1] astype Trendline;
            myTrendLine1.SetStartPoint( DTPoint1 );
            myTrendLine1.SetEndPoint( DTPoint1 );
            myTrendLine1.Weight = LineThickness;
           
            // Move labels out of the way
            If DrawLabels then
            Begin
                LineText = LineLabelVector[Value1] astype TextLabel;
                LineText.PointValue = DTPoint1;
            End;
        End;
       
        // Now re-position lines based on the sorted dictionary containing the sorted volume pivot levels
        For Value1 = 0 to MinList( TLVector.Count - 1, RankedVolVec.Count - 1 ) // Uses minlist because there may be less than TLVector.Count - 1 in sorted pivot array
        Begin
            RightOfChart = DateTime.FromELDateAndTime( JulianToDate( IntPortion( GetAppInfo( aiRightDispDateTime ) ) ), MinutesToTime( FracPortion( GetAppInfo( aiRightDispDateTime ) ) * 60 * 24));
            DTPoint1 = DTPoint.Create( RightOfChart, StrToNum( RankedVolVec[Value1] astype string ) );
            DTPoint2 = DTPoint.Create( RightOfChart, StrToNum( RankedVolVec[Value1] astype string ) );
           
            // Move trendlines
            myTrendLine1 = TLVector[Value1] astype Trendline;
            myTrendLine1.SetStartPoint( DTPoint1 );
            myTrendLine1.SetEndPoint( DTPoint2 );
           
            // Move the text labels
            If DrawLabels then
            Begin
                LineText = LineLabelVector[Value1] astype TextLabel;
                //LineText.TextString = NumToStr( StrToNum( RankedVolVec[Value1] astype string ), DecPlaces );
                LineText.TextString = RankedVolVec[Value1] astype string;
                LineText.PointValue = DTPoint2;
            End;
           
            // Set color of lines
            If Value1 <= 9 then myTrendline1.Color = Color.FromArgb( 255, getRvalue( ColorVector[Value1] astype int ), getGvalue( ColorVector[Value1] astype int ), getBvalue( ColorVector[Value1] astype int ) )
            // Draw at a darker sahe by altering the alpha value
            Else If Value1 <= 19 then myTrendline1.Color = Color.FromArgb( 150, getRvalue( ColorVector[Value1-10] astype integer ), getGvalue( ColorVector[Value1-10] astype integer ), getBvalue( ColorVector[Value1 - 10] astype integer ) ) ;
            // Set line style
            If Value1 <= 9 then myTrendline1.Style = LineStyleVector[Value1] astype int
            Else If Value1 <= 19 then myTrendline1.Style = LineStyleVector[Value1-10] astype int;
           
            // Set color of text
            If DrawLabels then
            Begin
                If Value1 <= 9 then LineText.Color = Color.FromArgb( 255, getRvalue( ColorVector[Value1] astype int ), getGvalue( ColorVector[Value1] astype int ), getBvalue( ColorVector[Value1] astype int ) )
                Else If Value1 <= 19 then LineText.Color = Color.FromArgb( 150, getRvalue( ColorVector[Value1-10] astype integer ), getGvalue( ColorVector[Value1-10] astype integer ), getBvalue( ColorVector[Value1 - 10] astype integer ) ) ;
            End;           
        End;
    End;
    // Condition1 = VectorPrint( "*", PivotVector );
End;

// Makes sure a screen size change is found even if no price ticks occurring (i.e. if the market is closed)
Method void Timer1_Elapsed_1( elsystem.Object sender, elsystem.TimerElapsedEventArgs args )
begin
   
    ScreenSizeChanged = HighestDispValueLast <> GetAppInfo( aiHighestDispValue ) or LowestDispValueLast <> GetAppInfo( aiLowestDispValue );
    HighestDispValueLast = GetAppInfo( aiHighestDispValue );
    LowestDispValueLast = GetAppInfo( aiLowestDispValue );
   
    // Print(" Highest and Lowest ", D, T," ", GetAppInfo( aiHighestDispValue ):5:5, " HighestDispValueLast ",HighestDispValueLast:5:5," ", GetAppInfo( aiLowestDispValue ):5:5 ," LowestDispValueLast ",LowestDispValueLast:5:5," ScreenSizeChanged ", ScreenSizeChanged );
   
    If ScreenSizeChanged then Redraw();
End;

// Move the labels to the right of the chart
Method void MoveLabels( )
Begin
    // Reposition labels based on the sorted vector containing the sorted pivot levels
    For Value1 = 0 to LineLabelVector.Count - 1
    Begin
        RightOfChart = DateTime.FromELDateAndTime( JulianToDate( IntPortion( GetAppInfo( aiRightDispDateTime ) ) ), MinutesToTime( FracPortion( GetAppInfo( aiRightDispDateTime ) ) * 60 * 24));
        LineText = LineLabelVector[Value1] astype TextLabel;
        DTPoint2 = DTPoint.Create( RightOfChart, Linetext.PointValue.Price astype double );
       
        // Move the text labels to right of chart
        LineText.PointValue = DTPoint2;
    End;
End;

// Method to resample the main volume profile dictionary for prices visible on the screen
Method void Redraw( )
Begin
    ScreenSizeChanged = FALSE;
   
    // Recreate a smaller dictionary as a subset of the PriceVolume dictionary to just contain pivots that are within the screen range
    // PivotVectorInRange.clear( );
    PriceVolumeInRange.Clear( );
   
    For Value1 = 0 to PriceVolume.Count - 1
    Begin
        // If Getsymbolname = "@YM" then Print( File("c:\progr55-1-2data.txt"), D, T, " PivotVectorInRange.Count ", PivotVectorInRange.Count );
        If StrToNum( PriceVolume.Keys[Value1] astype string ) < GetAppInfo( aiHighestDispValue )
        and StrToNum( PriceVolume.Keys[Value1] astype string ) > GetAppInfo( aiLowestDispValue )
        then PriceVolumeInRange.Add( PriceVolume.Keys[Value1] astype string, PriceVolume.Values[Value1] );
    End;
   
    If Debug then
    Begin
        For Value1 = 0 to PriceVolumeInRange.Count - 1
        Begin
            If getSymbolName = "@YM" then Print( Value1," ", D, T, " Key ",PriceVolumeInRange.Keys[Value1] astype string," Vol ", PriceVolumeInRange.Values[Value1] astype double );
        End;
    End;
   
    // Return a dictionary that just contains the key/volume pairs of volume pivots
    PriceVolumeInRange = VolumePivots( PriceVolumeInRange );
   
    If Debug then
    Begin
        For Value1 = 0 to PriceVolumeInRange.Count - 1
        Begin
            If getSymbolName = "@YM" then Print( "PIVOTS BEFORE VOL SORT",Value1," ", D, T, " Key ",PriceVolumeInRange.Keys[Value1] astype string," Vol ", PriceVolumeInRange.Values[Value1] astype double );
        End;
    End;
   
    // Return a vector of the keys (i.e. prices) ranked by volume
    RankedVolVec = DictionaryVolSort( PriceVolumeInRange );
   
    If Debug then
    Begin
        For Value1 = 0 to RankedVolVec.Count - 1
        Begin
            If getSymbolName = "@YM" then Print( "PIVOTS AFTER VOL SORT",Value1," ", D, T, " Key ",RankedVolVec[Value1] astype string," ", Maxbarsback );
        End;
    End;
   
    // Call the TLDictDrawOrRedraw method
    TLDictDrawOrRedraw( );
    //Condition1 = VectorPrint( "IR", PivotVectorInRange );
End;

// Store volume depending on chart type and interval
if BarType >= 2 and BarType < 5 then { not tick/minute data nor an advanced chart type (Kagi, Renko, Kase etc.) } VolVal = Volume
else { if tick/minute data or an advanced chart type;  in the case of minute data, also set the "For volume, use:" field in the Format Symbol dialog to Trade Vol or
Tick Count, as desired;  when using advanced chart types, Ticks returns volume if the chart is built from 1-tick interval data } VolVal = Ticks ;

Once
Begin
    // Raise run time errors for wrongly set inputs
    If TopVolPivots > 20 then RaiseRuntimeError( "The TopVolPivots input must be " +
         "an integer less than or equal to 20." ) ;
    If LineThickness < 0 or LineThickness > 6 then RaiseRuntimeError( "The LineThickness input must be " +
         "an integer between 0 and 6." ) ;

    TLVector = New Vector;
    LineLabelVector = New Vector;
   
    ColorVector = New Vector;
   
    PriceVolume = New Dictionary;
    PriceVolumeInRange = New Dictionary;
    RankedVolVec = New Vector;
   
    // Store colors in the ColorVector
    ColorVector.push_back( Color1 );
    ColorVector.push_back( Color2 );
    ColorVector.push_back( Color3 );
    ColorVector.push_back( Color4 );
    ColorVector.push_back( Color5 );
    ColorVector.push_back( Color6 );
    ColorVector.push_back( Color7 );
    ColorVector.push_back( Color8 );
    ColorVector.push_back( Color9 );
    ColorVector.push_back( Color10 );
   
    LineStyleVector = New Vector;
   
    // Store line styles in the LineStyleVector
    LineStyleVector.push_back( LineStyle1 );
    LineStyleVector.push_back( LineStyle2 );
    LineStyleVector.push_back( LineStyle3 );
    LineStyleVector.push_back( LineStyle4 );
    LineStyleVector.push_back( LineStyle5 );
    LineStyleVector.push_back( LineStyle6 );
    LineStyleVector.push_back( LineStyle7 );
    LineStyleVector.push_back( LineStyle8 );
    LineStyleVector.push_back( LineStyle9 );
    LineStyleVector.push_back( LineStyle10 );
   
    // Determine the number of decimal places required for text drawing
    Txt = NumToStr( FracPortion( MinMove / PriceScale ), 5 );
    If RightStr( Txt, 1 ) <> "0" then DecPlaces = 5;
    If RightStr( Txt, 2 ) = "00" then DecPlaces = 3;
    If RightStr( Txt, 3 ) = "000" or RightStr( Txt, 4 ) = "0000" then DecPlaces = 2;
    //If RightStr( Txt, 4 ) = "0000" then DecPlaces = 1;
    If RightStr( Txt, 5 ) = "00000" then DecPlaces = 0;
   
    ClearPrintLog;
End;

// On last tick of each bar see if new pivot has occurred, if so add to PivotVector
If BarStatus( 1 ) = 2 then
Begin
    // Run redraw on the close of each bar
    If DrawLabels then MoveLabels( );
   
    // At the close of each bar store the volume for the specific price
    // If a volume is already stored for that price, add it to the existing volume, otherwise store it
    If PriceVolume.Contains( NumToStr( C, DecPlaces ) ) then
    PriceVolume.Items[ NumToStr( C, DecPlaces ) ] = PriceVolume.Items[ NumToStr( C, DecPlaces ) ] astype double + VolVal
    Else PriceVolume.Add( NumToStr( C, DecPlaces ), VolVal );
    //If GetSymbolName = "@YM" and C = 15740 then Print(D," ", T, " VolVal ", VolVal );
   
    //If GetSymbolName = "@YM" then Print(D," ", T, " Add vol data ", VolVal, " PriceVolume.Items[ NumToStr( C, DecPlaces ) ] astype double ", PriceVolume.Items[ NumToStr( C, DecPlaces ) ] astype double );
End;

// Run the Redraw() method when first applied to the chart
Once ( GetAppInfo( aiRealTimeCalc ) = 1 or ( D = JulianToDate( LastCalcJDate ) and LastCalcMMTime = 60 * IntPortion( T * 0.01 ) + 100 * FracPortion( T * 0.01 ) ) ) LastBarOnCht = True;

// Do the initial calculation to calculate the levels an draw the horizontal lines
If LastBarOnCht then
Once
Begin
    Redraw( );
    //Print( "LastBarOnCht ", D, T );
End;

// Print(" Highest and Lowest ", D, T," ", GetAppInfo( aiHighestDispValue ):5:5, " ", GetAppInfo( aiLowestDispValue ):5:5 );
 
Last edited by a moderator:

Similar threads

Top