Search Results for

    Show / Hide Table of Contents

    The StopLossTakeProfitAnnotationCreationModifier

    The StopLossTakeProfitAnnotationCreationModifier is a chart modifier that allows for interactive creation of StopLossTakeProfitAnnotation instances using either a 2-tap or a single-drag workflow.

    Overview

    When this modifier is enabled, users can place the 2 required anchor points - the opposite corners of the price zone - in one of two ways:

    • Tap to place: Each tap confirms the position of the next anchor point. A "rubber-band" dashed line provides a preview of the segment between the last confirmed point and the current touch position.
    • Single drag: If the finger moves more than 10 pixels before the first touch-up, the gesture is classified as a drag - both anchor points are placed from the start and end of that drag, and the annotation is finalized on touch-up.

    Once both points are placed, the annotation is finalized, the rubber-band preview is removed, and the modifier automatically resets to create the next one.

    Note

    The zone is automatically colored as a take-profit or a stop-loss band depending on whether the second point ends up above or below the first. See the StopLossTakeProfitAnnotation article for details.

    Properties

    The following properties can be used to configure the StopLossTakeProfitAnnotationCreationModifier:

    Property Description
    xAxisId Sets the ID of the X-Axis that the created annotations should be measured against.
    yAxisId Sets the ID of the Y-Axis that the created annotations should be measured against.
    rubberBandStroke Sets the pen style for the preview line shown during creation. Must not be null.

    The following read-only properties allow to inspect the creation progress, e.g. from a custom subclass:

    Property Description
    currentAnnotation The annotation currently being created, or null if no creation is in progress.
    currentPointIndex The index of the next anchor point to be placed - 0 for the start, 1 for the end.
    rubberBandAnnotation The rubber-band preview annotation, or null if it is not currently shown.

    Event Listeners

    You can receive a notification when an annotation is fully created by setting an OnAnnotationCreatedListener:

    • setAnnotationCreationListener(OnAnnotationCreatedListener listener): Passes the newly created annotation instance to the listener.

    Using StopLossTakeProfitAnnotationCreationModifier

    The following code demonstrates how to add and configure a StopLossTakeProfitAnnotationCreationModifier:

    • Java
    • Kotlin
    // Create a StopLossTakeProfitAnnotationCreationModifier
    final StopLossTakeProfitAnnotationCreationModifier stopLossTakeProfitCreationModifier = new StopLossTakeProfitAnnotationCreationModifier();
    
    // Optional: Customize the rubber-band preview line shown while placing the points
    stopLossTakeProfitCreationModifier.setRubberBandStroke(new SolidPenStyle(Color.WHITE, true, 1.5f, new float[]{5, 5}));
    
    // Optional: Set a listener to be notified when an annotation is created
    stopLossTakeProfitCreationModifier.setAnnotationCreationListener(newAnnotation -> {
        // Configure the newly created annotation if needed
        newAnnotation.setIsEditable(true);
    });
    
    // Add the modifier to the surface
    surface.getChartModifiers().add(stopLossTakeProfitCreationModifier);
    
    // Create a StopLossTakeProfitAnnotationCreationModifier
    val stopLossTakeProfitCreationModifier = StopLossTakeProfitAnnotationCreationModifier().apply {
        // Optional: Customize the rubber-band preview line shown while placing the points
        rubberBandStroke = SolidPenStyle(Color.WHITE, true, 1.5f, floatArrayOf(5f, 5f))
    
        // Optional: Set a listener to be notified when an annotation is created
        setAnnotationCreationListener { newAnnotation ->
            // Configure the newly created annotation if needed
            newAnnotation.setIsEditable(true)
        }
    }
    
    // Add the modifier to the surface
    surface.chartModifiers.add(stopLossTakeProfitCreationModifier)
    

    Customizing the Modifier

    The StopLossTakeProfitAnnotationCreationModifier is designed for extensibility. You can customize its behavior by extending it and overriding its protected methods:

    Method Description
    createAnnotation(Context) Replace the annotation type, or apply custom default styling to every created annotation.
    createRubberBandAnnotation(Context) Replace the rubber-band preview annotation type.
    configureRubberBandAnnotation(IAnnotation) Configure the rubber-band annotation after creation.
    updateRubberBandEndPoint(PointF) Change how the rubber-band tracks the finger.
    removeRubberBandAnnotation() Clean up the rubber-band annotation.
    onAnnotationCreated(IAnnotation) React to annotation completion.
    • Java
    • Kotlin
    // Define a custom StopLossTakeProfitAnnotationCreationModifier
    private static class CustomStopLossTakeProfitAnnotationCreationModifier extends StopLossTakeProfitAnnotationCreationModifier {
        @Override
        protected void configureRubberBandAnnotation(IAnnotation annotation) {
            super.configureRubberBandAnnotation(annotation);
    
            // Customize the rubber-band styling
            if (annotation instanceof LineAnnotation) {
                ((LineAnnotation) annotation).setStroke(new SolidPenStyle(Color.YELLOW, true, 2f, new float[]{10, 5}));
            }
        }
    
        @Override
        protected void onAnnotationCreated(IAnnotation newAnnotation) {
            super.onAnnotationCreated(newAnnotation);
    
            // Custom logic after annotation is fully created
            newAnnotation.setIsEditable(true);
        }
    }
    
    void useCustomStopLossTakeProfitAnnotationCreationModifier(@NonNull SciChartSurface surface) {
        // Use the custom modifier
        final CustomStopLossTakeProfitAnnotationCreationModifier customModifier = new CustomStopLossTakeProfitAnnotationCreationModifier();
    
        surface.getChartModifiers().add(customModifier);
    }
    
    // Define a custom StopLossTakeProfitAnnotationCreationModifier
    private class CustomStopLossTakeProfitAnnotationCreationModifier : StopLossTakeProfitAnnotationCreationModifier() {
        override fun configureRubberBandAnnotation(annotation: IAnnotation) {
            super.configureRubberBandAnnotation(annotation)
    
            // Customize the rubber-band styling
            if (annotation is LineAnnotation) {
                annotation.stroke = SolidPenStyle(Color.YELLOW, true, 2f, floatArrayOf(10f, 5f))
            }
        }
    
        override fun onAnnotationCreated(newAnnotation: IAnnotation) {
            super.onAnnotationCreated(newAnnotation)
    
            // Custom logic after annotation is fully created
            newAnnotation.setIsEditable(true)
        }
    }
    
    fun useCustomStopLossTakeProfitAnnotationCreationModifier(surface: SciChartSurface) {
        // Use the custom modifier
        val customModifier = CustomStopLossTakeProfitAnnotationCreationModifier()
    
        surface.chartModifiers.add(customModifier)
    }
    
    Note

    To learn more about other Annotation Types, available out of the box in SciChart, please find the comprehensive list in the Annotation APIs article.

    Back to top © 2011-2025 SciChart. All rights reserved. | sitemap.xml