Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Buying in green candle

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

    Buying in green candle

    Hello,

    I want to make a strategy that always buys when the candle is green,
    Here is my code, it compiles fine but, when I enabled it into the chart, it doesn't do anything.​
    Code:
     protected override void OnBarUpdate()
            {
                if (Close[0]>Open[0])
                 {
                    EnterLong(Convert.ToInt32(DefaultQuantity), "");
                }
            }
    ​
    Thanks

    #2
    Hello kiro1000,

    How did you test the strategy when you tried it?

    That is a fairly basic condition that should be able to become true. You can additionally add a Print or Drawing object into the condition to check if became true in your test.

    Code:
    if (Close[0]>Open[0])
    {
        Print("Inside condition, condition was true");
        EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }
    JesseNinjaTrader Customer Service

    Comment


      #3

      Hello Jesse,

      Thank you for your message and guidance.

      Upon retesting, I've come to realize that the strategy does indeed work. However, it doesn't execute trades on every green candle; instead, it buys at the end of the day close and sells at the end of the day close.
      Click image for larger version

Name:	Buy in green candle.png
Views:	112
Size:	51.1 KB
ID:	1279321
      Attached Files

      Comment


        #4
        Hello kiro1000,

        You would need to exit the trade so it can continue placing trades. Are you trying to just enter a lot of positions but not exit them? If so you need to increase the entries per direction to match how many total entries in that direction you wanted to allow.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          Thank you for your advice.

          I'll make sure to exit the trade to allow for further trades. I was indeed aiming to enter multiple positions without exiting them. I'm trying to solve it with a stoploss but I got the error message :
          'BuyGreenCandle' already defines a member called 'OnStateChange' with the same parameter types

          Here my code

          Code:
          protected override void OnBarUpdate()
                       {
                if (Close[0]>Open[0])
                  {
                          EnterLong(Convert.ToInt32(DefaultQuantity), "");
                  }
                        }
          
                       protected override void OnStateChange()
          {
              if (State == State.Configure)
              {
                      SetStopLoss(CalculationMode.Pips, 50);
              }
          
          }    ​


          Appreciate your assistance!

          Comment


            #6
            Hello kiro1000,

            A script can only have one method called OnStateChange. You need to move that code into the existing OnStateChange that the script has. You can add additional conditions like

            Code:
            if (State == State.Configure)
            {
            SetStopLoss(CalculationMode.Pips, 50);
            }


            inside the original OnStateChange in the script.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello Jesse,

              Thank you for the guidance, I've successfully followed your instructions, and now it works! I'll consolidate the code by integrating the additional conditions with only one method called OnStateChange, Here the code :

              Code:
              protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Enter the description for your new custom Strategy here.";
                              Name                                        = "GreenCandle";
                              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;
                          }
                          else if (State == State.Configure)
                          {
                              SetStopLoss(CalculationMode.Pips, 50);        
                              SetProfitTarget(CalculationMode.Pips, 50);                
                          }
              
                      }
                            protected override void OnBarUpdate()
                           {
                      if (Close[0]>Open[0])
                      {
                              EnterLong(Convert.ToInt32(DefaultQuantity), "");
                          }
                            }


              Your assistance was invaluable; thank you for helping resolve this!​

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by RosySmit, Today, 01:35 AM
              0 responses
              3 views
              0 likes
              Last Post RosySmit  
              Started by gmacjapan, Today, 01:27 AM
              0 responses
              2 views
              0 likes
              Last Post gmacjapan  
              Started by WHICKED, 06-27-2023, 12:07 PM
              95 responses
              1,147 views
              1 like
              Last Post diegocml  
              Started by gmacjapan, Yesterday, 11:47 PM
              0 responses
              7 views
              0 likes
              Last Post gmacjapan  
              Started by gregorious13, Yesterday, 11:43 PM
              0 responses
              5 views
              0 likes
              Last Post gregorious13  
              Working...
              X