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!
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
NinjaTrader
Skip next trade after winning trade
Collapse
X
-
Skip next trade after winning trade
Hello. I am in the process of backtesting a strategy and I would like to find out if there is a code snippet to skip the next trade after a winning trade but take all trades after losing trades. I have tried adapting the sample strategy 'SampleTradeObjects' but could not get it to do what I described above. Is there a way to do it without using looping i.e. a single line of code in an IF statement that would skip the trade after a win?Tags: None
-
Skip next trade after winning trade
Hello homey,
Thanks for writing in to our Support team.
To check if the last trade was a loser, we will have to save the last trade to a variable and pull the profit it made and save it to a variable. Then, we can set a bool to true or false which will cause us to skip the next trade after a winning trade or take all trades after losing trades.
For example:
Code:protected override void Initialize() { bool canTrade = true; } protected override void OnBarUpdate() { // Check to make sure there is at least one trade in the collection if (Performance.AllTrades.Count > 0) { Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; double lastProfit = lastTrade.profitCurrency; // if the last trade was a winner, set canTrade to false - change to < to check if the last trade was a loser and set canTrade to true if(lastProfit > 0) canTrade = false; // order entry logic if (your conditions && canTrade == true) // enter trades } }
http://ninjatrader.com/support/helpG...collection.htm
Please let me know if I may be of any further assistance.Alan S.NinjaTrader Customer Service
-
Originally posted by NinjaTrader_AlanS View PostHello homey,
Thanks for writing in to our Support team.
To check if the last trade was a loser, we will have to save the last trade to a variable and pull the profit it made and save it to a variable. Then, we can set a bool to true or false which will cause us to skip the next trade after a winning trade or take all trades after losing trades.
For example:
Code:protected override void Initialize() { bool canTrade = true; } protected override void OnBarUpdate() { // Check to make sure there is at least one trade in the collection if (Performance.AllTrades.Count > 0) { Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; double lastProfit = lastTrade.profitCurrency; // if the last trade was a winner, set canTrade to false - change to < to check if the last trade was a loser and set canTrade to true if(lastProfit > 0) canTrade = false; // order entry logic if (your conditions && canTrade == true) // enter trades } }
http://ninjatrader.com/support/helpG...collection.htm
Please let me know if I may be of any further assistance.
Comment
-
Hello homey,
Thanks for your reply.
There is no order entry logic in the code sample that I provided. You will need to include your order entry logic after the if(your conditions && canTrade == true) check and include your conditions in that check in place of 'your conditions'. You will also want to ensure you are creating another check to set canTrade == true after a losing trade so that trades are being placed.
You can find more information on all the different order methods in our help guide here:
http://ninjatrader.com/support/helpG...er_methods.htm
Please let me know if I may be of any further assistance.Alan S.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_AlanS View PostHello homey,
Thanks for your reply.
There is no order entry logic in the code sample that I provided. You will need to include your order entry logic after the if(your conditions && canTrade == true) check and include your conditions in that check in place of 'your conditions'. You will also want to ensure you are creating another check to set canTrade == true after a losing trade so that trades are being placed.
You can find more information on all the different order methods in our help guide here:
http://ninjatrader.com/support/helpG...er_methods.htm
Please let me know if I may be of any further assistance.
I have another question. It seems that the code will skip the next trade after a winner; but what happens from that point on is that it skips all following trades. What I am after is to skip 1 trade after a winner and take the next trade and then again always skip 1 trade after the next winner.
I would appreciate your comments.
Comment
-
Hello homey,
Thanks for your reply.
This code sample was meant to be taken as an example to demonstrate what you are trying to achieve - in the code sample I mention that you will need to change the condition to check if(lastProfit < 0) and set canTrade to true when this occurs. If you do not include this in your code then you will get exactly the behavior you are describing as in the sample I provided I only wrote the if(lastProfit > 0) check. You must set the bool to true so that when you are checking if it is true later in your code, the condition can be met.
Please let me know if I may be of any further assistance.Alan S.NinjaTrader Customer Service
Comment
-
Hi Alan S.
The strategy seems to be doing what it is meant to for the most part. Thanks for your help so far.
I have encountered another problem and I will try to describe it. The strategy is a long only strategy that uses a couple of indicators. It skips or takes 1 trade when the initial entry conditions are met using the indicators - so that is not a problem; however, while that initial position is open, subsequent entries are ignored when a 2nd, 3rd, or 4th entry condition is active when using the indicators. When a 'grouping' of such valid entries take place together, I would like to treat all 4 possible entries as a 'single' result and then move on to the next trade, and depending on whether the last result was a net winner/loser, I would skip/enter long.
I have looked at the difference between using 'All entries' and 'Unique entries' but toggling between either of them gives the same result. Could you point me in the right direction or suggest any relevant code snippets that might help? Cheers.
Comment
-
Hi Alan P.
Yes I have uniquely named my entries "long1", "long2" etc. And I also set EntriesPerDirection and EntriesHandling.UniqueEntries in the Initialize section. What happens is that all entries take place on the initial bar when the initial entry conditions are met, but subsequent entries on different bars are ignored. Was there anything else that I may have missed?
Is there a way to compare 2 groups of trades in the case when there is more than 1 valid entry condition (as in my previous post)? And also, is it possible to compare the net profit or loss for each group of trades to determine if the next trade OR group of trades should be skipped or taken?
Thanks. Look forward to your input
Comment
-
Hello homey,
You have EntriesHandling.UniqueEntries, what is your Entries Per Direction set to?
When you are referring to comparing groups of trades, are you looking to do this from within the strategy? If so you could see the following example; otherwise could you please provide more information so I can best assist?
I look forward to your reply.Alan P.NinjaTrader Customer Service
Comment
-
Hi Alan P.
I have tried setting Entries per direction at 1 and also 5, however, the result is not what I expected. I am looking to get the strategy to enter on each signal as per the indicator.
Thanks for sending the link for the Sample PnL strategy. I have had a look at it but it is not quite what I'm after. When comparing groups of trades, I would look to do this from within the strategy.
What I want to achieve is to skip the next trade after a winning trade and take the next trade after a losing trade. I have run some tests for serial correlation and believe my strategy exhibits this property. Why I need to compare groups of trades at times is that there can be a few open trades that exit at the same time. So for the purposes of determining if the last 'trade' was a winner or a loser, I want to evaluate the collective net profit or loss on that result to signal whether I would enter or stay out of the next signal.
Hope I'm describing what I'm after with clarity. Cheers.
Comment
-
Hello homey,
You could use the following reference sample for an example of how you could write logic to take or not take action based off whether or not the last trade was a winner or loser.
Regarding why subsequent trades are not taken, could you please send an email to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and attach the log and trace files for the day in subject which you can find in My Documents>NinjaTrader7>Log and My Documents>NinjaTrader7/Trace folders.
I look forward to receiving your email.Last edited by NinjaTrader_AlanP; 06-08-2017, 10:04 AM.Alan P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by ETFVoyageur, Today, 07:55 PM
|
0 responses
1 view
0 likes
|
Last Post
by ETFVoyageur
Today, 07:55 PM
|
||
Started by janio973, Today, 07:24 PM
|
1 response
6 views
0 likes
|
Last Post Today, 07:35 PM | ||
Started by aligator, 01-06-2022, 12:14 PM
|
4 responses
240 views
0 likes
|
Last Post
by john_44573
Today, 06:11 PM
|
||
Started by reynoldsn, Today, 05:56 PM
|
0 responses
12 views
0 likes
|
Last Post
by reynoldsn
Today, 05:56 PM
|
||
Started by bortz, 11-06-2023, 08:04 AM
|
51 responses
1,993 views
0 likes
|
Last Post
by aligator
Today, 05:50 PM
|
Comment