Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Converting Tradingview to Ninjascript, can someone help me?

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

    #16
    The strategy is from Steven primo: we need to have 5 closes above the upper Bollinger band, then we look for the most recent swing high, the swing high can be after 5 or more candles for example, we enter long when we break it if we didn’t close again inside the bands, cycle rest when we close inside the band and we look again for 5 closes and swing high break again

    Comment


      #17
      Ok. I guess I'm not understanding the logic of what you consider a swing high. In the first picture, you mention that the 5th candle does not have a swing. A few candles before it there was a doji swing high, are you not counting this because it was already broken by that fifth candle? You also pointed out that a later candle was a valid swing, does this mean that original 5th candle was somehow not a valid swing? Or it was, but because a new one popped up, you're just counting that one now?

      Comment


        #18
        Yes the 5th candle was a swing high but we didn’t broke it, so we are looking for a formation of an another swing, the new one formed was lower than the previous one, so we look to break the new one.

        Comment


          #19
          Also noticing from your screenshot examples, I had not taken into account if looking back to find a swing high, if that swing high was already broken.
          if (Position.MarketPosition == MarketPosition.Long)
          {
          for (int i = 1; i <5; i++)
          {
          if (High[i] > High[i-1] && High[i] > High[i+1] && High[i] > HighestBar(High, i)
          swingHigh = High[i] + TickOffset * TickSize; // Where default TickOffset = 1;
          break;
          }
          }​​

          Comment


            #20
            Ok, and you are looking to enterLong in this case if price breaks that price, assuming it does not for a lower swing high in the meantime, or closes within the bands again.

            Comment


              #21
              The cycle is: 5 closes above the upper band, if it closes inside the band, the cycle reset.
              5 closes without reset, we start looking for the most recent swing, if we break above it , we enter long
              and the opposite for short

              Comment


                #22
                If this goes on for 10 candles without fulfilling the requirements, are you just continuing to look at the most recent 5 closes for each new bar?

                Comment


                  #23
                  No only once per cycle

                  Comment


                    #24
                    Ok I see. So it would take the recent swing high within those 5 candles and set the level to be broken and simply wait for that level to be broken or for price to close the bands, invalidating that cycle?

                    Comment


                      #25
                       

                      Comment


                        #26
                        Ok. I think I just wasn't explaining myself properly, but you are continuing to look for recent swings after the initial 5 candles if they pop up. At least until the trade setup is invalidated or a trade is taken.

                        So the code I provided for the long set up would continue to look for swings using the most recent 5 candles.
                        //Logic to look for swingHighs
                        if (Position.MarketPosition == MarketPosition.Long)
                        {
                        for (int i = 1; i <= 5; i++)
                        {
                        if (High[i] > High[i-1] && High[i] >= High[i+1] && High[i] >= HighestBar(High, i))
                        swingHigh = High[i] + TickOffset * TickSize; // Where default TickOffset = 1;
                        break;
                        }
                        }​​​

                        You could use a boolean to validate the trade set up, so when there are 5 closes above the band, for a long, LongTradeIsValid = true;

                        You can then check to see if the trade setup is invalidated with
                        if (Position.MarketPosition == MarketPosition.Long && LongTradeIsValid)
                        {
                        if (Close[0] <= band)
                        LongTradeIsValid = false;
                        }

                        To enter a trade, you would look for the swing High that was assigned
                        if (LongTradeIsValid && Close[0] >= swingHigh)
                        {
                        EnterLong();
                        //LongTradeIsValid = false;
                        }

                        You could change the LongTradeIsValid to false if you only want to enter once per trade.

                        Does that sound about right to you?
                        Last edited by rockmanx00; 02-17-2025, 03:34 PM. Reason: Fixed code logic

                        Comment


                          #27
                          After achieving 5 consecutive closes, this serves as the initial condition to begin identifying a potential swing. However, it's important to note that we won't always rely solely on these 5 closes. The key requirement is for a swing high to be broken; before a reset of the counter. This means that if a candle closes inside the bands or on the opposite side, the count resets, and we must start over.

                          Comment


                            #28
                            It seems we are on the same page. While the code I provided may not be the full code, as far as serving the purposes of finding your swing highs, in a long, validating and invalidating trade ideas and enter the trades, the code should work. Keep in mind, as I am not using the bands myself, I have not attempted to code it in a way that I can test and debug it, so there might be errors.

                            However, there are a few discrepancies in your stop loss logic based off of your examples. For example, in the second and third examples, you state that the stop loss should be at a certain point a few bars behind, but if you look in between, there is a swing there that you could have used as your swing high. For the third example, you set your stop loss very low, when the bar before it would have been the most recent swing low, as the candle that did the breaking had a higher low than the prior candle. It is possible to code it in a way that looks for the swing used for your stop loss after a certain number of candles, so it really depends on the logic for that portion of the code.

                            Also, in the first example, there was the swing that needed to be broken. After that there was an inside bar, followed by a red candle that touched the band. I can't really tell, but it looks like the two highs for those candles are equal. In my logic for the code, I made it so when you look for a swing, the bar in question needs to be higher than the one before it. For the one AFTER, I have made the requirement as equal or lower. I believe in one of your examples you actually used the same logic. It's probably not as important for the swing that needs to be broken, but for your stop loss, depending on price action and your logic for placing that, it is possible that skipping those situation would put your stop loss very far from entry, giving you a lot of risk, and a bigger distance for price to cover to hit your take profit. Again, it's possible, it just depends on what you want your strategy to do.

                            Are you planning on having this stop loss and target fixed? Do you move to breakeven at all? If you don't, it should be really easy to determine the tick size of your risk after getting filled, and then setting your stop loss and target accordingly.

                            Keep in mind, since you seem to already have to logic to determine if there was 5 closes outside of the bands, I have not included that in the code I have provided. That would be the condition that makes LongTradeIsValid set to true.
                            Last edited by rockmanx00; 02-18-2025, 09:25 AM.

                            Comment


                              #29
                              Thank you for your response and the help you are providing me. These are just examples, but the logic is sound. We enter on the breakout of a swing high, and we place the stop one tick below the last swing low. For risk, it is calculated in dollars on the MNQ (Micro E-mini Nasdaq futures), meaning the strategy will calculate the number of contracts to buy or sell. Everything will depend on the risk value we want to take. This is why, even if the stop is far, it will adjust the number of contracts and round the number up if it is more than 0.5 to a higher number of contracts, and to fewer contracts if it is less than 0.5.

                              Comment


                                #30
                                here is the indicator:
                                Attached Files

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by clarocque1, Today, 06:18 PM
                                0 responses
                                2 views
                                0 likes
                                Last Post clarocque1  
                                Started by clarocque1, Today, 06:17 PM
                                0 responses
                                2 views
                                0 likes
                                Last Post clarocque1  
                                Started by clarocque1, 03-23-2025, 08:17 AM
                                4 responses
                                26 views
                                0 likes
                                Last Post clarocque1  
                                Started by Artorias, Today, 05:22 PM
                                0 responses
                                9 views
                                0 likes
                                Last Post Artorias  
                                Started by several, Today, 07:07 AM
                                8 responses
                                26 views
                                0 likes
                                Last Post several
                                by several
                                 
                                Working...
                                X