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

Can't Identify Green Candle

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

    Can't Identify Green Candle

    New to coding and working on a strategy to identify a hammer doji candle. Whenever i run the code, my variable in "GreenCandle" is always equal to 0 even though the Close is greater than the open. How do i fix this? See below for my code. Any help is greatly appreciate. Thanks

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class DojiStrategy : Strategy
    {
    private double OpenLoc;
    private double CloseLoc;
    private double Pct;
    private double OpenCheck;
    private double CloseCheck;
    private double WickCheck;
    private double GreenCandle;
    private double LowCheck;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "DojiStrategy";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    PctNearTop = 30;
    PT = 1000;
    SL = 1000;
    }
    else if (State == State.Configure)
    {
    SetProfitTarget(CalculationMode.Currency, PT);
    SetStopLoss(CalculationMode.Currency, SL);
    }
    }

    protected override void OnBarUpdate()
    {
    if(CurrentBar < BarsRequiredToTrade)
    return;

    OpenLoc = (Open[0]-Low[0])/(High[0]-Low[0]);
    CloseLoc = (Close[0]-Low[0])/(High[0]-Low[0]);
    Pct = (100-PctNearTop)/100;

    if(OpenLoc>Pct)
    {
    OpenCheck = 1;
    }
    else
    {
    OpenCheck = 0;
    }

    if(CloseCheck>Pct)
    {
    CloseCheck = 1;
    }
    else
    {
    CloseCheck = 0;
    }

    if((High[0]-Close[0])<(Open[0]-Low[0]))
    {
    WickCheck = 1;
    }
    else
    {
    WickCheck = 0;
    }

    if(Close[0]>Open[0])
    {
    GreenCandle = 1;
    }
    else
    {
    GreenCandle = 0;
    }

    if(Low[0]<Low[1])
    {
    LowCheck = 1;
    }
    else
    {
    LowCheck = 0;
    }

    Print(Time[0].ToString() + " OpenLoc: " + OpenLoc);
    Print(Time[0].ToString() + " CloseLoc: " + CloseLoc);
    Print(Time[0].ToString() + " Pct: " + Pct);
    Print(Time[0].ToString() + " OpenCheck: " + OpenCheck);
    Print(Time[0].ToString() + " CloseCheck: " + CloseCheck);
    Print(Time[0].ToString() + " WickCheck: " + CloseCheck);
    Print(Time[0].ToString() + " GreenCandle: " + CloseCheck);
    Print(Time[0].ToString() + " LowCheck: " + CloseCheck);

    Print(Time[0].ToString() + " Open[0]: " + Open[0]);
    Print(Time[0].ToString() + " Close[0]: " + Close[0]);

    if(OpenCheck == 1 && CloseCheck == 1 && WickCheck == 1 && GreenCandle == 1 && LowCheck == 1)
    EnterLong();

    }

    region Properties
    [NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Percentage Near Top", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
    public double PctNearTop
    { get; set; }

    [NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Profit Target", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
    public int PT
    { get; set; }

    [NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Stop Loss", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
    public int SL
    { get; set; }

    #endregion
    }
    }


    #2
    Hello algospoke,

    Thanks for your post.

    To detect a green bar on the chart you could create a condition that checks if the current Close price (Close[0]) is greater than the current Open price (Open[0]).

    The code might look something like this:

    Code:
    if (Close[0] > Open[0])
    {
        Print("The bar is green.");
    }
    I suggest modifying your script so the prints are placed one line above the condition and prints out all the values being used in that condition to see how the condition is evaluating.

    For example:

    Code:
    Print(Time[0] + " Close[0]: " + Close[0] + " > Open[0]: " + Open[0]);
    if(Close[0]>Open[0])
    {
        Print("Close[0] > Open[0] is true.");
        GreenCandle = 1;
    }
    else
    {
        GreenCandle = 0;
    }
    An example of the output would be something like:

    2/18/2024 6:33:00 PM Close[0]: 5021.5 > Open[0]: 5021.25
    Close[0] > Open[0] is true.


    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    ​​​
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Brandon,

      Thanks for the response but I have already tried that and my variable "GreenCandle" is still not changing to a value of 1. Below is my code for the OnBarUpdate and see the writing in red that shows the "if" statement for GreenCandle. Is there something wrong with my "if" statement or maybe its the "if" statements before it that are causing the issue?

      protected override void OnBarUpdate()
      {
      if(CurrentBar < BarsRequiredToTrade)
      return;

      OpenLoc = (Open[0]-Low[0])/(High[0]-Low[0]);
      CloseLoc = (Close[0]-Low[0])/(High[0]-Low[0]);
      Pct = (100-PctNearTop)/100;

      if(OpenLoc>Pct)
      {
      OpenCheck = 1;
      }
      else
      {
      OpenCheck = 0;
      }

      if(CloseCheck>Pct)
      {
      CloseCheck = 1;
      }
      else
      {
      CloseCheck = 0;
      }

      if((High[0]-Close[0])<(Open[0]-Low[0]))
      {
      WickCheck = 1;
      }
      else
      {
      WickCheck = 0;
      }

      if(Close[0]>Open[0])
      {
      GreenCandle = 1;
      }
      else
      {
      GreenCandle = 0;
      }


      if(Low[0]<Low[1])
      {
      LowCheck = 1;
      }
      else
      {
      LowCheck = 0;
      }

      Print(Time[0].ToString() + " OpenLoc: " + OpenLoc);
      Print(Time[0].ToString() + " CloseLoc: " + CloseLoc);
      Print(Time[0].ToString() + " Pct: " + Pct);
      Print(Time[0].ToString() + " OpenCheck: " + OpenCheck);
      Print(Time[0].ToString() + " CloseCheck: " + CloseCheck);
      Print(Time[0].ToString() + " WickCheck: " + CloseCheck);
      Print(Time[0].ToString() + " GreenCandle: " + CloseCheck);
      Print(Time[0].ToString() + " LowCheck: " + CloseCheck);

      Print(Time[0].ToString() + " Open[0]: " + Open[0]);
      Print(Time[0].ToString() + " Close[0]: " + Close[0]);

      if(OpenCheck == 1 && CloseCheck == 1 && WickCheck == 1 && GreenCandle == 1 && LowCheck == 1)
      EnterLong();​

      Comment


        #4
        Hello algospoke,

        Thanks for your notes.

        I do not see anything specifically standing out as incorrect in the code you shared that is highlighted red.

        How are the prints evaluating in your script?

        It seems like instead of printing the GreenCandle variable, you are printing CloseCheck. You need to print out GreenCandle to see how that variable is evaluating. Further, your other prints are also printing out CloseCheck and not the actual value being checked.

        Print(Time[0].ToString() + " CloseCheck: " + CloseCheck);
        Print(Time[0].ToString() + " WickCheck: " + CloseCheck);
        Print(Time[0].ToString() + " GreenCandle: " + CloseCheck);
        Print(Time[0].ToString() + " LowCheck: " + CloseCheck);


        You should make sure to print out the conditions being used to assign a value of 1 to GreenCandle and print out the GreenCandle variable to see how it is evaluating. And, you should print out WickCheck, and LowCheck as well to see how those are evaluating.

        As mentioned in post # 2, I suggest modifying your script so the prints are placed one line above the condition and prints out all the values being used in that condition to see how the condition is evaluating.

        An example of what this print would look like could be seen in post # 2. I have also added the code below demonstrating what the prints would look like.

        This allows you to see how the Close[0] and Open[0] are evaluating and confirms the condition was triggered.

        Code:
        Print(Time[0] + " Close[0]: " + Close[0] + " > Open[0]: " + Open[0]);
        if(Close[0]>Open[0])
        {
            Print("Close[0] > Open[0] is true.");
            GreenCandle = 1;
        }
        else
        {
            GreenCandle = 0;
        }​
        The print to see if the conditions to EnterLong() are true would look something like this.

        Code:
        Print("OpenCheck: " + OpenCheck + " CloseCheck: " + CloseCheck + " WickCheck: " + WickCheck + " GreenCandle: " + GreenCandle + " LowCheck: " + LowCheck);
        if(OpenCheck == 1 && CloseCheck == 1 && WickCheck == 1 && GreenCandle == 1 && LowCheck == 1)
            EnterLong();​​
        The print above would allow you to see how each value in the condition to EnterLong() is behaving. If you see any of those values are not printing out 1, the condition would be false and EnterLong() would not be called.

        See this forum post detailing how to use prints to understand behavior: https://ninjatrader.com/support/foru...121#post791121
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Awesome Brandon. I found my issue. Thanks for your help

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by burtoninlondon, Today, 12:38 AM
          0 responses
          9 views
          0 likes
          Last Post burtoninlondon  
          Started by AaronKoRn, Yesterday, 09:49 PM
          0 responses
          14 views
          0 likes
          Last Post AaronKoRn  
          Started by carnitron, Yesterday, 08:42 PM
          0 responses
          11 views
          0 likes
          Last Post carnitron  
          Started by strategist007, Yesterday, 07:51 PM
          0 responses
          13 views
          0 likes
          Last Post strategist007  
          Started by StockTrader88, 03-06-2021, 08:58 AM
          44 responses
          3,983 views
          3 likes
          Last Post jhudas88  
          Working...
          X