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
nested for loop of a barIndex
Collapse
X
-
Lets solve one problem at a time. Even if want to use the RMMA code to plot my cma i need to make it work first.
For now my cma is producing a straight horizontal line in the chart.
When using AddPlot(Brushes.Blue, "cma"); do i need to put something in the #region Properties?
Someone at NT told me it was necessary to add base.OnRender(chartControl, chartScale); somewhere in the OnRender.
But my cma is code in OBU. Does it make a difference?
ty
-
Hello frankduc,
If you wanted to make something similar to what the RMMA does, you would need to use an enum just like the RMMA syntax. In the RMMA it uses the enum MultiMA1types to define the options and a variable MaToUse is used with the switch to know what option is selected. The RMMA does not use a double variable in the switch like you did, so to fix what you were trying to do you would have to match what the RMMA did in its syntax.
Enum and Switch are not specific to NinjaScript, these are concepts you can learn more about for C#.
You can also see the following example which uses an enum: https://ninjatrader.com/support/help...ned_parame.htm
The candle stick patterns indicator also uses an enum for selections.
I can't tell you how to use the cma with the switch as I don't really understand what you are trying to do in the syntax you provided, you can instead use the examples you have available to see a working enum and then apply that type of logic to your own custom script how you would like.
Please let me know if I may be of further assistance.
Leave a comment:
-
This is a variable double type inside my indicator.
I could create the cma as an indicator and call it in my new indicator. That would mean to create a Period for the cma.
I was hoping to use switch because i dont need to convert cma into an indicator, cma is like an indicator in the indicator. CMA is a moving average i use for calculation of my indicator.
Like you can see in RMMA it produce several numbers of moving average for the sma, ema, etc.
I just wanted to reproduce that in the moving average of my indicator without having to create a new cma indicator.
Not to mention i need to understand how to apply switch case so i can use it for my other for loops in the indicator i am creating.Attached Files
Leave a comment:
-
Hello frankduc,
I am still not certain what you are trying to do with the switch, as you noted your cma variable will not work with a switch because it returns a double. In what you provided you have used the cma variable in two different ways, one is like an indicator and the other was like a value. Is cma a double type or is this an indicator?
If this is a double type you have other problems in your code in addition to the switch. If this is an indicator, you would need to just call your indicator more than once with the parameters you needed to get more than one value.
Here is an example of calling the same indicator twice to get two different values based on different parameters being input:
Code:double sma1 = SMA(12)[0]; double sma2 = SMA(14)[0];
Code:double cma1 = cma(mybar)[0]; double cma2 = cma(someOtherBar)[0]
Leave a comment:
-
Hi Jesse,
The goal is to create multiple CMA. I dont need them to show up in the chart i just need the result.
Code:for(int barIndex = mybar; barIndex <= ChartBars.ToIndex; barIndex++) { closePrice = Bars.GetClose(barIndex); volumes = Bars.GetVolume(barIndex); clovol = closePrice * volumes; sum1 += clovol++; sum2 += volumes++; cma = sum1 / sum2; }
To produce that i looked to the RMMA. But there is somethings, i can figure out, how to input them into my code to reproduce the idea.
For switch (cma), according to docs.msn the match expression can be pretty much everything except double and cma returns a double.
I am not sure what to do with Values, in principle its link to the value returned by other ma's. In my case the cma is created from a for loop that loop through a certain number of bars in the chart.
All i wanted was to create cma's for all the period in the chart. The way i see it switch case is use to reproduce a same task over and over.
Cant just replace in cmaChartBars.FromIndex by mybar at this stage it wont produce the desired effect. I am not even sure what to do with count++ if its necessary or not.
Ty
Leave a comment:
-
Hello frankduc,
The syntax you provided seems to not be correct, or not enough information was provided. The switch (cma) is likely not correct, what type is cma? Is cma an enum? It looks like you use cma like an indicator shortly after the switch(cma), I cant really tell what you are trying to do there.
If you wanted to make a choice, you would need to create an enum for that and a public property for the enum. Later in your logic, such as the switch you would use the enum property. This is shown in the candlestick patterns indicator (switch (Pattern)).
I look forward to being of further assistance.
Leave a comment:
-
Hello,
It still in the same topic about the RMMA.
I am trying to reproduce the RMMA logic to create multiple cma's.
Code:int count = 0; for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++) { mybar = barIndex; switch (cma) { case cma: Values[count][0] = cma(mybar)[0]; break; } count++; }
[COLOR=rgba(0, 0, 0, 0.87)]NinjaScript File Error Code Line Column[/COLOR][COLOR=rgba(0, 0, 0, 0.87)]SimplyDivVol.cs A switch expression or a case label must be of type bool, char, string, integral, enum or Nullable corresponding CS0151 115 13[/COLOR]
In RMMA (cma) is actually(MaToUse). That part refer toif (State == State.SetDefaults) in MaToUse = MultiMA1types.EMA;
Code:int count = 0; for (int i = LowPeriod; i <= HighPeriod; i += StepAverage) { switch (MaToUse) { case MultiMA1types.DEMA: Values[count][0] = DEMA(i)[0]; break;
TY
Leave a comment:
-
Hello frankduc,
That does not seem to be part of C#, are you omitting other code which you have used in this test? In what you provided the ToConsole method is not present.
Pleas let me know if I may be of further assistance.
Leave a comment:
-
After new test it came up this way. On https://dotnetfiddle.net/it turns ok but translate that into ninjascript is not that obvious, at least for me.
{
double pivot = 1.23;
List<double> numbers = ConstructList();
numbers.ToConsole<double>("Unsorted");
numbers.Sort(Comparer<double>.Create((x, y) => Math.Abs(pivot - x)
< Math.Abs(pivot-y) ? -1 : Math.Abs(pivot - x) > Math.Abs(pivot - y) ? 1 : 0));
numbers.ToConsole<double>("Sorted");
Console.WriteLine("\nDone.");
Console.ReadLine();
}
private static List<double> ConstructList()
{
List<double> result = new List<double>();
result.Add(1.22);
result.Add(1.26);
result.Add(1.33);
result.Add(1.21);
result.Add(1.215);
result.Add(1.225);
return result;
}
https://docs.microsoft.com related to that. Seems related to console writeline.
What's bothering me is the ToConsole part........what is that and what to do with that? i mean i cant see anything in
Any thoughts on that matter?
ThanksLast edited by frankduc; 08-29-2019, 11:44 AM.
Leave a comment:
-
Hello frankduc,
public static void Main(string[] args) , private static List<double> Sort(List<double> numbers, double pivot), public static void is this different class from OnRender? Is it transferable?
You could migrate the code that is inside the Main method to NinjaScript, however I couldn't say where you would put it as I don't know what the goal is with this new code.
I can say that you will not keep the void Main and also anything that is currently static needs to be made non-static. A console application uses static variables if you do not create a class, NinjaScript does not use static variables because you are inside a class.
Code:Console.WriteLine(toString(item)); be replace by Print(toString(item));
Please let me know if I may be of further assistance.
Leave a comment:
-
Someone came up with an interesting solution to my problem.
I tried the code on https://dotnetfiddle.net/ it looks like its doing the job. But i have a few issue related to its conversion to ninjatrader.
namespace ConsoleCS
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
List<double> numbers = ConstructList();
numbers.ToConsole<double>("Unsorted");
List<double> sorted = Sort(numbers, 1.23);
sorted.ToConsole<double>("Sorted");
Console.WriteLine("\nDone.");
Console.ReadLine();
}
private static List<double> Sort(List<double> numbers, double pivot)
{
List<double> result = new List<double>();
while (numbers.Count>0)
{
var minDistance = numbers.Min(n => Math.Abs(pivot - n));
var closest = numbers.First(n => Math.Abs(pivot - n) == minDistance);
numbers.Remove(closest);
result.Add(closest);
}
return result;
}
private static List<double> ConstructList()
{
List<double> result = new List<double>();
result.Add(1.22);
result.Add(1.26);
result.Add(1.33);
result.Add(1.21);
result.Add(1.215);
result.Add(1.225);
return result;
}
}
}
namespace ConsoleCS
{
using System;
using System.Collections.Generic;
public static class EnumerableHelper
{
public static void ToConsole<T>(this IEnumerable<T> enumerable)
{
foreach (T item in enumerable)
{
Console.WriteLine(item);
}
}
public static void ToConsole<T>(this IEnumerable<T> enumerable, string caption)
{
Console.WriteLine(caption);
foreach (T item in enumerable)
{
Console.WriteLine(item);
}
Console.WriteLine("");
}
public static void ToConsole<T>(this IEnumerable<T> enumerable, Func<T, string> toString)
{
foreach (T item in enumerable)
{
Console.WriteLine(toString(item));
}
}
public static void ToConsole<T>(this IEnumerable<T> enumerable, string caption, Func<T, string> toString)
{
Console.WriteLine(caption);
foreach (T item in enumerable)
{
Console.WriteLine(toString(item));
}
Console.WriteLine("");
}
}
}
In NT termspublic static void Main(string[] args) , private static List<double> Sort(List<double> numbers, double pivot), public static void is this different class from OnRender? Is it transferable?
CanConsole.WriteLine(toString(item)); be replace by Print(toString(item));
ToConsole<T> is it link to Console.WriteLine?
thanks
Leave a comment:
-
Hello frankduc,
Is there a method or group method that allow to do this?
I am clueless how to do that. Any hints?
If you are having difficulty with some syntax you have created, I always suggest to try and simplify that syntax. A lambda statement is generally not considered a simplified statement even though it is less syntax. These statements take more foresight to pick the correct use case for that type of syntax over a standard loop or variable. I would suggest experimenting outside of NinjaTrader with C# lambda statements gain a better understanding of the problem you are facing here. It sounds like the syntax you have used is not right for the explained use case, you noted you get one result back but wanted more results, that is not likely the correct syntax for that use case.
I look forward to being of further assistance.
Leave a comment:
-
I was thinking about that this weekend and using RMMA does not solve all the issues, it help for a part of the problem but not all. Lets put that aside for a moment.
I have another question: I did some tests and replace cma by Price (in theory its the same because all cma's will cross some of those prices)
for(int barIndex = index; barIndex <= ChartBars.ToIndex; barIndex++)
{
double Price = Bars.GetClose(barIndex);
if(cma < highPrice && cma > lowPrice)
In this case all the Price between index and ChartBars.ToIndex are listed in the variable Price.
Price gather many close price: 2014, 2015, 2018 .etc
If i replace cma by Price it will process all the prices all together.
I want each value to be calculated one bye one. Like if it was :
if(2014 < highPrice && 2014 > lowPrice)
if(2015 < highPrice && 2015 > lowPrice)
Is there a method or group method that allow to do this?
The problem is that after it pass the if statement it pass throught many formulas and a lambda expression chose one value.
If i have many cma's or replace them by Price like i said it will process all togheter all the prices in Price. Not the biggest problem its just that the lambda at the end is always returning one value when i want it to return a value per price.
So the price will be process one by one and then at the end of the pipeline one answer for each price. So i can use addplot to project a line in the chart. Right now replace cma by Price returning one value because of the lambda.
I am clueless how to do that. Any hints?
thank you
Leave a comment:
-
Hello frankduc,
I am looking where is the variable that will give me the first value at the beginning (of the VWMA) of the chart at the right side and cant find it. You have any clue?
The Beginning of the chart is the Left most bar.
The End of the chart is the Right most bar.
What data are you trying to get in contrast to the descriptions above?
From the description you provided, it sounds like you want the Right most bar or zero BarsAgo.
I look forward to being of further assistance.
Leave a comment:
-
Like i said in previous posts the project started using sampledisplaybarsago as the skeleton of the indicator. I needed the cursorpointX but it cant be used inOnBarUpdate. Now that i did get rid of it, everything could be moved to OnBarUpdate. I am almost done with the project and i was planning to move everything to OnBarUpdate after testing in OnRender that everything is fine.
Your idea to copy the logic of RMMA to OnBarUpdate could be the solution. The VWMA included in the RMMA returns for the first value the same value as the CMA. I only need the first value and i dont need it to be draw in the chart, only the value to send it to my calculations.
So VWMA would replace CMA inif(cma < highPrice && cma > lowPrice)
I only hope the if statement will test each returns brought by the RMMA and not just the last, otherwise i will be missing values at the end of the pipeline.
I have one issue with RMMA, where can we get the last values return by the multiple moving averages in the code?
I cant Print() that part so its not that:case MultiMA1types.VWMA:Values[count][0] = VWMA(i)[0];break;
I am looking where is the variable that will give me the first value at the beginning (of the VWMA) of the chart at the right side and cant find it. You have any clue?
ThanksAttached Files
Leave a comment:
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by FrazMann, Today, 11:21 AM
|
0 responses
1 view
0 likes
|
Last Post
by FrazMann
Today, 11:21 AM
|
||
Started by geddyisodin, Yesterday, 05:20 AM
|
8 responses
51 views
0 likes
|
Last Post Today, 10:45 AM | ||
Started by cmtjoancolmenero, Yesterday, 03:58 PM
|
10 responses
36 views
0 likes
|
Last Post Today, 11:10 AM | ||
Started by DayTradingDEMON, Today, 09:28 AM
|
4 responses
24 views
0 likes
|
Last Post Today, 10:47 AM | ||
Started by George21, Today, 10:07 AM
|
1 response
19 views
0 likes
|
Last Post Today, 10:43 AM |
Leave a comment: