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
Increment or Counter in the Strategy Builder?
Collapse
X
-
Increment or Counter in the Strategy Builder?
Is there a way to increment an int in the strategy builder? When I go to set an int equal to itself there's no way to add 1 to it as well. Just wondering if this is possible in the wizard.Tags: None
-
Hello Plzwork123,
Thanks for your post.
No, there is no means to increment a variable in the Strategy builder.
You could accomplish this by using a custom series. A custom series will provide a "slot" for every bar. You can assign values to the custom series. To work as a counter you would have to have one set that assigns the previous bar value of the series to the current bar value of the series to keep the latest count available.
I've attached an example "Event counter" that counts events as they occur. In this case the event counted is when a fast moving average crosses above a slow.
Set#1 - Is only used once, when the first bar of the data series is loaded, set the EventCounter to a value of 0 (zero)
Set#2 - On every OnBarUpdate, assign the previous(bar) value of the counter to the current (bar) counter (NOTE: this need to happen before the event set(s) occur)
Set#3 - Is the example crossabove. The key here is that the current counter is assigned the value of the previous counter + 1
Set#4 - Is an example of printing out the counter to New>Ninjascript output window.
The strategy will put the moving averages on the chart, will highlight the background of a cross and will display text to show the event number. See example:
EventCounterExample.zipLast edited by NinjaTrader_ChelseaB; 07-13-2022, 07:35 AM.Paul H.NinjaTrader Customer Service
-
Paul thanks for the reply.
That is very close to what I had in mind but it looks like you are not able to access custom series variables in a condition. I need to be able to read the value of a counter in a condition to check if a cross event has happened in 26 bars or more. The normal CrossBelow/Above function checks 26 bars or less. I am not sure how I can do this in the strategy builder (or if its even possible).
Comment
-
-
Originally posted by NinjaTrader_PaulH View PostHello Plzwork123,
Thanks for your post.
No, there is no means to increment a variable in the Strategy builder.
You could accomplish this by using a custom series. A custom series will provide a "slot" for every bar. You can assign values to the custom series. To work as a counter you would have to have one set that assigns the previous bar value of the series to the current bar value of the series to keep the latest count available.
I've attached an example "Event counter" that counts events as they occur. In this case the event counted is when a fast moving average crosses above a slow.
Set#1 - Is only used once, when the first bar of the data series is loaded, set the EventCounter to a value of 0 (zero)
Set#2 - On every OnBarUpdate, assign the previous(bar) value of the counter to the current (bar) counter (NOTE: this need to happen before the event set(s) occur)
Set#3 - Is the example crossabove. The key here is that the current counter is assigned the value of the previous counter + 1
Set#4 - Is an example of printing out the counter to New>Ninjascript output window.
The strategy will put the moving averages on the chart, will highlight the background of a cross and will display text to show the event number. See example:
[ATTACH]n1099188[/ATTACH]
Comment
-
Hello WattMan, you can try this:
protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
{
if (position.MarketPosition == MarketPosition.Flat)
{
if (SystemPerformance.AllTrades.Count >= 1)
{
Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1] ;
double res = lastRTTrade.ProfitCurrency * lastRTTrade.Quantity;
if(res < 0)
stop += 1;
if(res > 0)
target += 1;
}
}
}
- Likes 1
Comment
-
Originally posted by mcosta72 View PostHello WattMan, you can try this:
protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
{
if (position.MarketPosition == MarketPosition.Flat)
{
if (SystemPerformance.AllTrades.Count >= 1)
{
Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1] ;
double res = lastRTTrade.ProfitCurrency * lastRTTrade.Quantity;
if(res < 0)
stop += 1;
if(res > 0)
target += 1;
}
}
}
Comment
-
Hello WattMan,
Thanks for your post.
To be clear you would not be able to access system performance while using the Strategy Builder and would have to unlock the script. As this thread is about "Increment or Counter in the Strategy Builder?" it would be best if you have further questions to create a new thread, thanks for your understanding and help to keep the forums organized.
As member mcosta72 pointed out you can access SystemPerformance values.
These could be simply accessed in OnBarUpdate(). Here are links to the various sections of information that you can access:
Paul H.NinjaTrader Customer Service
- Likes 1
Comment
-
Originally posted by NinjaTrader_PaulH View PostHello Plzwork123,
Thanks for your post.
No, there is no means to increment a variable in the Strategy builder.
You could accomplish this by using a custom series. A custom series will provide a "slot" for every bar. You can assign values to the custom series. To work as a counter you would have to have one set that assigns the previous bar value of the series to the current bar value of the series to keep the latest count available.
I've attached an example "Event counter" that counts events as they occur. In this case the event counted is when a fast moving average crosses above a slow.
Set#1 - Is only used once, when the first bar of the data series is loaded, set the EventCounter to a value of 0 (zero)
Set#2 - On every OnBarUpdate, assign the previous(bar) value of the counter to the current (bar) counter (NOTE: this need to happen before the event set(s) occur)
Set#3 - Is the example crossabove. The key here is that the current counter is assigned the value of the previous counter + 1
Set#4 - Is an example of printing out the counter to New>Ninjascript output window.
The strategy will put the moving averages on the chart, will highlight the background of a cross and will display text to show the event number. See example:
[ATTACH]n1099188[/ATTACH]
Hey Paul. It seems the link for the example is missing.
Thanks.
Comment
-
Hello Trader17,
Thanks for your reply.
Yes, it is possible to e-mail but you really need to be able to download the file. There are now two links in post#2. We use google chrome but you should be able to use any browser to download the files.
I would encourage you to try again on both links in post #2 and use different a browser.
If after that you cannot download, please write into ScriptingSupport[at]NinjaTrader[dot]com (Replace [at] with @ and [dot] with .)
Mark the E-mail Atten Paul, Ticket # 2550336 and include a link to this thread in the e-mail.Paul H.NinjaTrader Customer Service
Comment
-
Hello Forum,
The posted EventCounterExample works great. I've created a specific counter for my needs however I need a way to limit the print statement to report only when the counter value updates instead of printing on every bar. Could someone please post the syntax. I can't find a reference to it anywhere. Tried creating a new Custom Series and recording the bar's Date/Time to it, however the data format does not allow it. Same result when trying to create a Date/Time Variable to record the bar date/time when the counter updates.
The only issue is the massive Print Statement. Again, only the timestamp for the bar on which the counter increases by one (1) is required.
For the example let's put "EventCounter" on a 15sec chart & start counting from 8:31:00 AM on Jan 3, 2023. You can see the counter increment increasing by one (1) when an event occurs. The counter remains static until the next event.
1/3/2023 8:31:00 AM 0
1/3/2023 8:31:15 AM 1
1/3/2023 8:31:30 AM 2
1/3/2023 8:31:45 AM 3
1/3/2023 8:32:00 AM 3
1/3/2023 8:32:15 AM 3
1/3/2023 8:32:30 AM 3
prints a line representing each 15 sec. bar
~~~
...and we're still at a count of "3" after 24 hours and ~21,600 output lines later.....
1/4/2023 8:30:45 AM 3
1/4/2023 8:31:00 AM 3
1/4/2023 8:31:15 AM 4
1/4/2023 8:31:30 AM 4
1/4/2023 8:31:45 AM 5
1/4/2023 8:32:00 AM 5
1/4/2023 8:32:15 AM 5
~~~
This is an example of the desired NinjaScript Output.
Count increases by one and only prints the bar date/time where the conditions were true.
1/3/2023 8:31:00 AM 0
1/3/2023 8:31:15 AM 1
1/3/2023 8:31:30 AM 2
1/3/2023 8:31:45 AM 3
1/4/2023 8:31:15 AM 4
1/4/2023 8:31:45 AM 5
There is a help guide Methods discussion which includes "Method With No Return Type" (pasted below) however this non-programmer can't get the strategy to compile after adding it to the NinjaScript.
// This method prints out the data and time
private void PrintDateTime()
{
Print(DateTime.Now.ToString());
}
Thanks in advance.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by jxs_xrj, 01-12-2020, 09:49 AM
|
5 responses
3,289 views
1 like
|
Last Post
by jgualdronc
Today, 10:47 AM
|
||
Started by Touch-Ups, Today, 10:36 AM
|
0 responses
5 views
0 likes
|
Last Post
by Touch-Ups
Today, 10:36 AM
|
||
Started by geddyisodin, 04-25-2024, 05:20 AM
|
8 responses
61 views
0 likes
|
Last Post Yesterday, 10:45 AM | ||
Can someone turn this thinkscript into a ninjascript. 5PM CST OHL and Previous Day
by Option Whisperer
Started by Option Whisperer, Today, 09:55 AM
|
0 responses
5 views
0 likes
|
Last Post Today, 09:55 AM | ||
Started by halgo_boulder, 04-20-2024, 08:44 AM
|
2 responses
24 views
0 likes
|
Last Post Today, 09:41 AM |
Comment