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:	27
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 MatthewLesko, 05-21-2021, 09:37 PM
    4 responses
    164 views
    0 likes
    Last Post Mahmad1323  
    Started by samish18, Today, 03:02 PM
    3 responses
    15 views
    0 likes
    Last Post bltdavid  
    Started by Jakub, Today, 11:06 AM
    1 response
    16 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by samish18, Today, 03:50 AM
    10 responses
    19 views
    0 likes
    Last Post samish18  
    Started by Mestor, 06-03-2018, 10:09 AM
    16 responses
    2,872 views
    0 likes
    Last Post myaha1000  
    Working...
    X