Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

issue with ninjascript! cant short

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

    issue with ninjascript! cant short

    Hello everyone

    Im a newbie programmer but have been able to put together a very simple strategy. My only concern is that it enters "long" positions correctly and exits "long" positions correctly.

    It just doesn't perform any of the "short"entries.

    Here is my strategy and I have uploaded an image of where a short should have been entered.

    In red, you will see that when price crosses below a moving average enveloppe AND the price is below the longer moving average (in this case 60 week MA) then enter short.


    // Condition set 1
    if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1)
    && Close[0] > LongSMA)

    {
    EnterLong(DefaultQuantity, "enter long");
    }
    // Condition set 2
    if (CrossBelow(Close, MAEnvelopes(1.5, 3, 20).Lower, 1)
    && Close[0] < LongSMA)

    {
    EnterShort(DefaultQuantity, "enter short");
    }

    // Closes the position created by entry condition 1
    if (CrossBelow(Close, MAEnvelopes(1.5, 3, 20).Lower, 1))
    ExitLong("enter long");



    // Closes the position created by entry condition 2
    if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1))
    ExitShort("enter short");
    Attached Files

    #2
    Originally posted by antonio_zeus View Post
    Hello everyone

    Im a newbie programmer but have been able to put together a very simple strategy. My only concern is that it enters "long" positions correctly and exits "long" positions correctly.

    It just doesn't perform any of the "short"entries.

    Here is my strategy and I have uploaded an image of where a short should have been entered.

    In red, you will see that when price crosses below a moving average enveloppe AND the price is below the longer moving average (in this case 60 week MA) then enter short.


    // Condition set 1
    if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1)
    && Close[0] > LongSMA)

    {
    EnterLong(DefaultQuantity, "enter long");
    }
    // Condition set 2
    if (CrossBelow(Close, MAEnvelopes(1.5, 3, 20).Lower, 1)
    && Close[0] < LongSMA)

    {
    EnterShort(DefaultQuantity, "enter short");
    }

    // Closes the position created by entry condition 1
    if (CrossBelow(Close, MAEnvelopes(1.5, 3, 20).Lower, 1))
    ExitLong("enter long");



    // Closes the position created by entry condition 2
    if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1))
    ExitShort("enter short");
    Any errors in your log?
    What is your CalculateOnBarClose setting?

    Comment


      #3
      Hello antonio_zeus,

      Thank you for your post.

      In addition to the items koganam mentioned here (check the Log tab for errors and what is the CalculateOnBarClose setting set to) please try commenting out the entire Exit conditions and actions for both long and short and advise if the strategy begins taking short positions.

      I look forward to your response.

      Comment


        #4
        hey guys

        thanks for the responses.. and here are some replies

        1. My calculate on bar close is true.

        CalculateOnBarClose = true;

        2. I do not see any erros and when I clikc "compile" it makes a noise and shows no errors whatsoever.

        I know this because when I did have errors, it opened a new window below my strategy showing the errors.

        How can I comment out the entire Exit conditions and actions for both long and short.....

        I thought my script was complete enough... or are my newb skills showing here.

        TY

        Comment


          #5
          Thanks Antonio, so you're basically looking for a reversal strategy at those enveloper levels?

          Then you would not need the exit condition at all, since the Enter() methods would reverse for you.

          Any different outcome with those conditions used in your OnBarUpdate()? I have simply commented out the separate exits you had in...you can achieve that with a // before the code line.

          // Condition set 1
          if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1)
          && Close[0] > LongSMA)

          {
          EnterLong(DefaultQuantity, "enter long");
          }
          // Condition set 2
          if (CrossBelow(Close, MAEnvelopes(1.5, 3, 20).Lower, 1)
          && Close[0] < LongSMA)

          {
          EnterShort(DefaultQuantity, "enter short");
          }

          // Closes the position created by entry condition 1
          //if (CrossBelow(Close, MAEnvelopes(1.5, 3, 20).Lower, 1))
          //ExitLong("enter long");



          // Closes the position created by entry condition 2
          //if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1))
          //ExitShort("enter short");

          Comment


            #6
            Bertrand!

            thanks for the response. One issue though, my "reversal" doesnt occur unless the closing price is above or below my moving average.

            if you notice my conditions, they include ".. && Close[0] > LongSMA)"

            therefore a reversal wouldnt occur automatically...

            Comment


              #7
              also, one more thing

              it seems that my condition doesnt work. Trades are being initiated on the long side, even though price is below my long SMA.

              Seems like my strategy only takes long positions, and doesnt follow the additional rule added to my condition.

              Is my script correct?

              // Condition set 1
              if (CrossAbove(Close, MAEnvelopes(1.5, 3, 20).Upper, 1)
              && Close[0] > LongSMA)

              {
              EnterLong(DefaultQuantity, "enter long");
              }
              // Condition set 2
              if (CrossBelow(Close, MAEnvelopes(1.5, 3, 20).Lower, 1)
              && Close[0] < LongSMA)

              Comment


                #8
                Antonio, so the cyan line on the chart would be the LongSMA in both cases? Could it be for the longs / short a different period would be used, so this would not be set as you expect / intend?

                For the reversal, it looks to me as if you would need a separate condion then - if you're in a short and Close < SMA + your envelope breakout to the downside, then EnterShort() and not only the ExitLong() call.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                671 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                379 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                111 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                575 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                582 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X