Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Draw.Arrowup/ArrowDown Error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Draw.Arrowup/ArrowDown Error

    Hey guys, novice here I thought I would start with a basic project of creating a RSI Divergence indicator, but I just cannot get past this Draw Error, It feels like I am doing it right but I have no doubt I am missing something obvious.

    Thanks in Advance.

    Click image for larger version

Name:	image.png
Views:	28
Size:	23.5 KB
ID:	1307410


    Here is my code:

    using System;
    using System.Windows.Media;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.NinjaScript.Strategies;

    public class MyRSIDivergenceIndicator : Indicator
    {
    private RSI rsiIndicator;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Detects bullish and bearish RSI divergences.";
    Name = "MyRSIDivergenceIndicator";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    AddPlot(Brushes.Orange, "RSIDivergencePlot");
    }
    else if (State == State.DataLoaded)
    {
    rsiIndicator = RSI(13, 3);
    }
    }
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 50) return; // Ensure there are enough bars to perform the analysis

    // Use Draw.ArrowUp to indicate bullish divergences
    if (IsBullishDivergence())
    {
    Draw.ArrowUp(this, "BullDiv" + CurrentBar, 0, Low[0] - (2 * TickSize), Brushes.Green);
    }

    // Use Draw.ArrowDown to indicate bearish divergences
    if (IsBearishDivergence())
    {
    Draw.ArrowDown(this, "BearDiv" + CurrentBar, 0, High[0] + (2 * TickSize), Brushes.Red);
    }
    }

    private bool IsBullishDivergence()
    {
    int lookBackPeriod = 50; // Maximum look-back period
    int minimumLookBack = 5; // Minimum look-back period

    double lowestLowPrice = double.MaxValue;
    double lowestRSI = double.MaxValue;
    int lowestLowIndex = -1;

    // Search for the lowest low in price and RSI within the last 50 bars
    for (int i = minimumLookBack; i <= lookBackPeriod; i++)
    {
    if (Low[i] < lowestLowPrice)
    {
    lowestLowPrice = Low[i];
    lowestRSI = rsiIndicator[i];
    lowestLowIndex = i;
    }
    }

    // Check for bullish divergence within the last 5 to 50 bars
    if (CurrentBar - lowestLowIndex <= lookBackPeriod && CurrentBar - lowestLowIndex >= minimumLookBack)
    {
    if (Low[0] < lowestLowPrice && rsiIndicator[0] > lowestRSI)
    {
    return true; // Bullish divergence found
    }
    }

    return false;
    }

    private bool IsBearishDivergence()
    {
    int lookBackPeriod = 50; // Maximum look-back period
    int minimumLookBack = 5; // Minimum look-back period

    double highestHighPrice = double.MinValue;
    double highestRSI = double.MinValue;
    int highestHighIndex = -1;

    // Search for the highest high in price and RSI within the last 50 bars
    for (int i = minimumLookBack; i <= lookBackPeriod; i++)
    {
    if (High[i] > highestHighPrice)
    {
    highestHighPrice = High[i];
    highestRSI = rsiIndicator[i];
    highestHighIndex = i;
    }
    }

    // Check for bearish divergence within the last 5 to 50 bars
    if (CurrentBar - highestHighIndex <= lookBackPeriod && CurrentBar - highestHighIndex >= minimumLookBack)
    {
    if (High[0] > highestHighPrice && rsiIndicator[0] < highestRSI)
    {
    return true; // Bearish divergence found
    }
    }

    return false;
    }
    }​

    #2
    Hello binzel,

    May I confirm you created this script with the NinjaScript Editor by right-clicking the Indicators folder and selecting New Indicator?

    Have you modified the using statements that are added by default?

    (Try creating a new script and copying all of the using statements at the top to your script)
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by xfactscout, 07-22-2024, 09:01 PM
    1 response
    23 views
    0 likes
    Last Post xfactscout  
    Started by Chrisnasdaq, 08-26-2023, 08:37 PM
    4 responses
    1,402 views
    0 likes
    Last Post pierodicarlo  
    Started by GillRymhes, 06-23-2022, 09:16 AM
    13 responses
    744 views
    5 likes
    Last Post pierodicarlo  
    Started by aligator, 06-22-2022, 01:10 PM
    13 responses
    1,821 views
    0 likes
    Last Post pierodicarlo  
    Started by sumobull, Today, 06:48 PM
    0 responses
    10 views
    0 likes
    Last Post sumobull  
    Working...
    X