Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Converting Tradingview to Ninjascript, can someone help me?
Collapse
X
-
That would have been from post #26 if I remember correctly.
The part about breakouts is this part in particular:
swingHigh is set earlier in the code and if the LongTrade is still valid, it will enter once price has reached or has broken that particular level.
if (LongTradeIsValid && Close[0] >= swingHigh)
{
EnterLong();
//LongTradeIsValid = false;
}
Also, I noticed in when you originally posted your code that you have it set to Calculation.OnBarClose. I would change it to OnPriceChange or OnEachTick, if you haven't already done so. If you have it set to OnBarClose, OnBarUpdate won't update again until the close of the bar, so it won't enter at the break of the level, it will enter on the close only if the close is higher than the swingHigh..In most cases, it will slip past the point you wanted to enter unnecessarily.Last edited by rockmanx00; 02-18-2025, 07:17 PM.
Leave a comment:
-
Ok. Just making sure that the logic itself was fine. As long as the logic matches what you are intending for it, and that is all a part of your strategy, then it should be alright. From there, you just need to double check the code to make sure it works as intended, then keep testing it in various situations to ensure the code is still doing everything as intended, and fix any bugs/ logic issues that come up. Was there anything else that you are having issues with?
Leave a comment:
-
-
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.
Leave a comment:
-
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.
Leave a comment:
-
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.
Leave a comment:
-
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?
Leave a comment:
-
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?
Leave a comment:
-
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?
Leave a comment:
-
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
Leave a comment:
-
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.
Leave a comment:
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by hunghnguyen2016, Today, 08:00 PM
|
0 responses
6 views
0 likes
|
Last Post
![]() |
||
Started by cbentrikin, Today, 03:49 PM
|
0 responses
11 views
0 likes
|
Last Post
![]()
by cbentrikin
Today, 03:49 PM
|
||
Started by MiCe1999, 04-14-2025, 06:54 PM
|
7 responses
75 views
0 likes
|
Last Post
![]()
by b.j.d
Today, 03:45 PM
|
||
Started by NISNOS69, Today, 02:20 PM
|
0 responses
18 views
0 likes
|
Last Post
![]()
by NISNOS69
Today, 02:20 PM
|
||
Started by hunter7, Today, 01:09 PM
|
0 responses
26 views
0 likes
|
Last Post
![]()
by hunter7
Today, 01:09 PM
|
Leave a comment: