Modifying Heikin-Ashi, adding stops
Author: Eugene
Creation Date: 12/11/2020 6:11 AM
profile picture

pestocat2

#1
Eugene,
Yes, this is directly related to the color change. In addition to DataSeries containing the data to change color, I also need a couple more items to bring back to display code area. In the For loop code I have created Heikin-Ashi candlesticks. Now I need to display them.

code for the Heikin-Ashi
CODE:
Please log in to see this code.


At the top of the For loop code I have this
CODE:
Please log in to see this code.

Then to plot code
CODE:
Please log in to see this code.


The code at the top of the For loop seems like a problem. It would seem that I'm allowed only one data set to return to the display area. I can't add another line for the Heikin-Ashi item. I could use some clarification.
Thank you

profile picture

superticker

#2
There maybe a correction:
CODE:
Please log in to see this code.

I don't understand why you split the code up in your post such that it won't compile. Of course, it's great you simplify the code (showing only the salient parts that have issues), but at a minimum, it's best if you post a code segment that actually compiles. We can fix code problems, but we can only fix code that compiles on our machines. Like the code you posted in Post# 1, I don't see any glaring errors (other than the fact it's not complete enough to compile).

UPDATE: I just ran your code and it works fine except for a technical issue. The number of elements in the barsha object must be identical to those of the Bars object for it to "synchronize" with the Bars object and plot, so you just need to add the line below before the FOR loop:
CODE:
Please log in to see this code.

The entire code--which works--is below. You're good to go!
CODE:
Please log in to see this code.

The stop array and the resultHA DataSeries never seem to be used.
profile picture

pestocat2

#3
superticker,
Thank you for your assistance. The problem I'm having is in the declarations at the top of the For loop area. I already have one series result that goes to the display area. But I need a couple more series to push to the display area. This series noted above is a symbol series and that declaration is tricky. It's not a DataSeries and that is where I'm having the problem. It's a symbol series.
Take Care
profile picture

superticker

#4
QUOTE:
This series noted above is a symbol series and that declaration is tricky. It's not a DataSeries and that is where I'm having the problem. It's a symbol series.

What's the problem you're actually trying to solve? I'm confused.
profile picture

pestocat2

#5
The Symbol series is Heikin-Ashi candlesticks. The Heikin-Ashi candlesticks are the basis for my strategy. Coming out of the For loop is the DateSeries that I displayed in the chart above. I also want to move the Heikin-Ashi candles too, plus another couple charts. If I can get the candlesticks out of the For loop along with chart series, then the last charts should be easy.
profile picture

superticker

#6
QUOTE:
The Symbol series is Heikin-Ashi candlesticks.
So "Symbol series" is the barsha Bars object? Is this correct?

QUOTE:
Coming out of the For loop is the DateSeries that I displayed in the chart above.
And how is the resultHA DataSeries actually computed? Or is it not a DataSeries?

QUOTE:
I also want to move the Heikin-Ashi candles too.
That's already accomplished and plotted in Post# 2.

QUOTE:
If I can get the candlesticks out of the For loop along with chart series,
That's what Post# 2 is doing. There's not a problem moving these data structures out of the FOR loop provided they are declared prior to the FOR loop, which Post# 2 does.

If Post# 2 is missing something, can you try adding it, posting it, then we can see why it's not working?

---
Looking at the code again, there "might" be some problems with how the Heikin-Ashi calculation results are applied to the harsha.Add(...) call. I'm not familiar with this calculation. But some of the calculated results may not be applied right to the
CODE:
Please log in to see this code.
call. You might want to double check that.
profile picture

superticker

#7
I rewrote the code independently using https://www.investopedia.com/terms/h/heikinashi.asp as the calculation reference. Below is the running code. There shouldn't be any problems computing other DataSeries in the FOR loop and accessing their results outside the loop as well provided that you declare them before the FOR loop.
CODE:
Please log in to see this code.
profile picture

pestocat2

#8
In order to get back SeriesData from the For loop code I had to separate the Display code from the For loop code. This works for SeriesData. I have shown that. But also in the For loop code I also generate Heikin-Ashi candlesticks and need to get that series back to the display and show on the chart. My problem is that I can't figure out what the code should be to separate the Display code from the For loop code. for the DataSeries code I used
CODE:
Please log in to see this code.

But the series for the candlestick data is not DataSeries. The candlestick code is a Bars object. I can't figure out what the Bars code to separate the 2 sections should look like. Also a more basic question, is it possible to get more than one DataSeries or Bars object out of the For loop to the Display section.
Thank you
profile picture

superticker

#9
QUOTE:
But the series for the candlestick data is not DataSeries. The candlestick code is a Bars object. I can't figure out what the Bars code to separate the 2 sections should look like.
Post# 7 demonstrates how the barsha Bars object is [1] declared before the FOR loop, [2] assembled within the FOR loop, and then [3] charted outside the FOR loop. And this approach will work with any data structure (whether its a Bars object or not).

Clearly you don't understand. My recommendation is to post all your code which compiles, then explain why you're unhappy with it. Short of doing that, I don't think we are going to get anywhere. But if you just follow the three steps [1-3] in the above paragraph for each separate data structure you're building, it should work just fine.

QUOTE:
... is it possible to get more than one DataSeries or Bars object out of the For loop to the Display section.
You can get an infinite number of data structures--of all types--through the program by following steps [1-3]. That's simply how the "scoping" rules of C# work. Honestly, I can't be any clearer. Perhaps someone else can chime in and explain the C# scoping rules better.
profile picture

pestocat2

#10
Superticker,
I must have the Heiken-Ashi code in the trading For loop because I use data from the candlesticks for trade signals. Right now it is working in the Trading For loop, I'm getting the correct trades. The problem is that my Trading For loop isolated by a line of code that separates the Display area from the For Trading code. The reason for this is to be able to get a DataSeries from the For loop out to the display area. I don't want show the compile code as I have over 800 lines of code. Cone showed me how to get a DataSeries back to the Display area. Here is few lines before and after where I separate the code sections.
CODE:
Please log in to see this code.


The lines
"
}
DataSeries GLSeries(Bars bars)
{
"
are the lines that separates the Trading For loop and the display section. If I didn't do this I would not be able to get a DataSeries information such that I can change the color of the chart depending on the data value. I hope this makes sense. I don't know what this code section in the Trading For is called, it an object or what is it.
Thank you for all your work.

I'm getting the impression of getting Heikin-Ashi candlesticks generated in the For loop is not possible, I think only DataSeries data can be return to the display area. I will have to generate another set of HA candlesticks in the Display area and go with that. So, one hidden and one displayed. It's complicated.

profile picture

pestocat2

#11
Superticker,
I have attached the screen I'm trying get back to. Notice the 4th chart down from the top and indicator is colored gray, that's because I don't have access to indicator value to change the indicator color. Notice that the Heikih-Ashi candlesticks come out nice.
profile picture

superticker

#12
QUOTE:
I must have the Heiken-Ashi code in the trading For loop because I use data from the candlesticks for trade signals.
No, no, no! Do not build Bars objects or DataSeries objects in the trading FOR loop. Use a separate FOR loop (before the trading FOR loop) if you must, but don't declare or build complex data structures of any type in the trading FOR loop. This cannot work. You can pass the preassembled Heiken-Ashi Bars object into the trading FOR loop for testing purposes, but create it before entering the trading FOR loop.

QUOTE:
I'm getting the impression of getting Heikin-Ashi candlesticks generated in the [trading] For loop is not possible,...
That's correct. In fact creating any large data structure in the trading FOR loop is bad. That's why WL indicators (which have their own internal FOR loop) are called prior to entering the trading FOR loop. Now you can pass those WL indicator structures--once they have been created--into the trading FOR loop for testing purposes. And that's what you should be doing.

Finish all data structures and calls to any plot functions (which create internal structures of their own) before entering the trading FOR loop. This is very important! There are garbage collection considerations--let alone C# scoping rules--that won't allow you to organize your code the way you're trying to. So quite trying to do the impossible. Restructure your code.

In addition, declaring data structures (and that includes calls to Plot routines, which create data structures of their own) in the trading FOR loop is incredibility inefficient for garbage collection reasons.

---
Once you get your code restructured so the trading FOR loop is only doing testing on existing data structures (and not creating them), you'll be amazed how much faster it executes!
profile picture

pestocat2

#13
Maybe I used the wrong words when I say that the Heikin-Ashi candlestick routine is in the Trading For loop, The HA routine is in the Trading For section and comes before the for loop starts just after the declarations. My strategy requires candlesticks be generated before the loop starts as the strategy needs the data. My strategy works very fast. Once I enter a stock, the back-test is instant and this is looking back 6 months with 60 minute bars. It is very quick. In order to see the indicators I want in my strategy I do several checks in the actual For loop code. I do boolean checks on WL indicators and now have added the gain loss for any stock plotted with time. After some more experiments, it looks as if I can only return one DataSeries using the keyword "return".
More information to follow and thank you for your attention and work,
Take Care,
Gary
profile picture

superticker

#14
QUOTE:
After some more experiments, it looks as if I can only return one DataSeries using the keyword "return".
Well, in "modern" programming languages, you typically want to design your code so there's only one output structure. In other words, you want to avoid "side effects" (latent bugs) on the input parameter list by not letting the routine being called modify them. This is by design.

Now you can override these rules (and the really thick version of your C# manual discusses how), but you're treading more dangerously when you do. This is why WL indicators, which each have their own internal FOR loop to build their DataSeries, return only one DataSeries object and nothing more. So if you want to use three different indicators, you call three different WL indicators (each with different internal FOR loop) to do this. There are software engineering reasons for this design to reduce side effects.

Now you can create your own "class" that returns a single datatype object that encapsulates several other output objects that "belong" to that class. And that's what a DataSeries is. Now for its own protection (to prevent side effects), that DataSeries class will let you modify only some elements of a DataSeries (like prices), but it won't let you modify salient internal state variables inside the DataSeries object. That's intentional to prevent your strategy from corrupting the integrity of that DataSeries object.

So what you're really trying to tell us is that you want to create your own C# class that encapsulates several DataSeries objects. That computer science stuff is beyond the WL forum, but the thick version of your C# manual should have a chapter about encapsulation. My only comment is to design each class you create as reusable with a "specific service function", and that's what a DataSeries or ChartPane object is. They each have specific service functions.
profile picture

pestocat2

#15
Hi Superticker,
I'm almost there. I have the Heikin-Ashi candlesticks displayed now. As before I generate this in the Display section and display it. I also have another copy of this that is in the For loop section and not the actual for loop. I use that to develop the Buy/sell signals. I still want to bring back 2 more DataSeries that show the Buy/Sell Signals. I have had communication with Eugene and he suggested to use Tuple to return more than one DataSeries. I'm still working on that, I can't figure out where all the code in the procedure or the Display section. See attached figure for the latest screen. I have a C# book coming tomorrow!
Take Care,

profile picture

superticker

#16
QUOTE:
... suggested to use a Tuple to return more than one DataSeries.
Well, a Tuple could return multiple DataSeries.

I still think approaching it like WL indicators and making a separate function call for each DataSeries (like you do with WL indicators) gives you the most flexibility. That's partly why WL indicators are designed that way. What if a future strategy doesn't need the same three DataSeries your current strategy is using?

Of all the C# classes (including WL indicators) I've written for WL, none of them return multiple DataSeries to the strategy, although some create a second DataSeries or array that's a field variable "internal" to the class object itself. You can use a C# property to get access to those internal field variables.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).