Login Page - Create Account

Support Board


Date/Time: Sun, 28 Apr 2024 22:31:13 +0000



[Programming Help] - Ninja Trader 7 code block converted over to Sierra code

View Count: 4278

[2018-09-09 02:00:08]
User701453 - Posts: 176
I have an ATR trailing stop code block that I need converted over to an ACSIL study.
The code block was originally part of a ninja strat that plotted the stop lines on the chart.

Once its in a Sierra study and I can view the result on a chart with two separate output subgraphs(long, short).

I will then use that study stop values to auto exit trades.

I have the ninja code block available in original C# and VB.Net converted.
Yes, I have tried for days to convert the code myself with no luck.

Does anyone have the time/energy to convert this block.
In VB.Net its about 33 lines, in C# it appears to be a few lines more.

I can provide the code block in a text file if anyone is willing to do this for me.

Thanks in advance.
[2018-09-09 02:36:27]
Xfanman1 - Posts: 320
If it's the Chandelier Stop used in NT I may be able to help you out.

--
Scott
[2018-09-09 03:07:09]
User701453 - Posts: 176
Thanks for the reply Scott,

Unfortunately I don't know what the specific name of stop it is, since its coded directly into the ninja strat.

However the atr stops appear to be the engine of the strat as its entries are taken on the swings.
Closing out a long to go short at close of bar when the short atr line shows up.

atrlength is set at 20 and trendfactor is set at 1.5. Both hard coded in.

Im trying to replicate the ninja strat by pulling out the engine and placing it into a Sierra body.

Ive attached a screen shot of how the stop lines appear on the chart.
I using a demo of ninja 7 and trying to get the atr stop completed before the demo expires.
imagescshot.png / V - Attached On 2018-09-09 03:03:20 UTC - Size: 22.66 KB - 705 views
[2018-09-09 03:28:04]
Xfanman1 - Posts: 320
That’s similar to the Chandelier stop but not identical from what I can tell. If you’re not familiar with it already, look at the Kiwi Trailing Stop in Sierra, it’s an ATR based stop similar to what I’m seeing on your chart.


Scott
[2018-09-09 03:52:29]
User701453 - Posts: 176
Yes, I have tried the Kiwi Trailing Stop.
Spent about 2 hours trying an assortment of input values, it comes close but it just doesn't match up correctly.
When it looked close to correct in one area of the chart.
When scrolling thru the chart, the difference are noticeable in the swings and line distance from chart bars.

Tried all the other stops available within sierra and couldn't get any of them to fit correctly either.
[2018-09-09 03:58:05]
Xfanman1 - Posts: 320
Check the Chandelier Stop in NT and see if you can get that to align with your ATR stop. If you can, I’ll send you the code for the SC version I had done. I’m out for the night but I’ll check this thread in the morning and send it to you if you need it.


Scott
Date Time Of Last Edit: 2018-09-09 10:54:03
[2018-09-09 04:08:55]
User701453 - Posts: 176
I will check the ninja forum to see if I can find it.
I didnt see it in list of indicators in the ninja software.
[2018-09-09 22:39:23]
User701453 - Posts: 176
Scott,
Could you send me the code for your SC version?

Perhaps I can view it and try to figure out how to get this ninja version
ATR stop I have converted to SC.
[2018-09-09 23:38:01]
Xfanman1 - Posts: 320
I've enabled direct messaging for you. Enable mine and I'll direct message it to you.

--
Scott
[2018-09-09 23:51:53]
User701453 - Posts: 176
DM enabled.

Thanks
[2018-09-10 04:41:09]
JoseyWales - Posts: 67
I've converted several NT7 indicators to NT8 in the last few months and originally started programming SC indicators.

I'd be interested in taking a look at your Strategy if you'd be willing to post what files you have.
[2018-09-10 07:33:55]
User701453 - Posts: 176
Hello JoseyWales,
Thanks for the response.

Have you converted entire ninja auto trading strategies or just the indicators?
Have any chart screen shots for side by side comparison of the converted files?
Date Time Of Last Edit: 2018-09-10 07:34:20
[2018-09-11 00:04:01]
User701453 - Posts: 176
I`m still fighting this ninja code block..
Instead of trying to plot the lines.

I decided first to create a SC study that colors the background correctly first.

As the ninja ATR stop code long and short swings match up with the background color changes.
The background color and stop lines are contained within the same ninja code block.

Screenshots of ninja screen and my attempt to clone it to SC chart attached.

Pulling my hair out trying to figure out what I`m doing wrong.
Attachment Deleted.
imageninja chart.PNG / V - Attached On 2018-09-11 00:00:04 UTC - Size: 51.76 KB - 426 views
imagescChart.PNG / V - Attached On 2018-09-11 00:00:13 UTC - Size: 60.19 KB - 512 views
[2018-09-11 00:10:45]
JoseyWales - Posts: 67
Post both your original NT7 code and SC study code and I'll try to convert it.
Can't really tell whats wrong from pictures alone.
Date Time Of Last Edit: 2018-09-11 00:12:13
[2018-09-11 01:09:32]
Xfanman1 - Posts: 320
Looking at your recent images, I just wanted to make sure you realize the Renko bar type you're using with your new SC chart is not the same as your NT Renko chart. You probably changed it intentionally but if not and you need direction on the equivalent SC Renko let me know.

Also, I sent you the .cpp file via direct message.


--
Scott
[2018-09-11 01:33:32]
User701453 - Posts: 176
Thanks Scott for the cpp file.


Yes, the 6-2 renko bars on ninja chart is custom bars, they came with the ninja strat.

The flex renko 6-5-3 in SC was as close match I could get to them.
If you have a setting that is closer, please let me know.
[2018-09-11 01:43:41]
JoseyWales - Posts: 67
Are you able/willing to post all the NT7 code not just a snippet for complete context?

I'd also recommend being cautious using Renko/Range bars on low settings. They look great in hindsight but by the time you think you can act, the market has already moved and may not provide enough Profit Potential to even cover the spread/commission.
Same thing with Heiken Ashi charts. They are deceptively beautiful.
Date Time Of Last Edit: 2018-09-11 01:55:21
[2018-09-11 02:36:56]
Xfanman1 - Posts: 320
It looks to me like your NT Renko bars are Mean Renko or Better Renko bars with an offset of 1/2 the bar length. Look at this link and select the third setting in the Renko bar type drop down window. (I’m on my phone again so can’t confirm with screenshots at the moment)

Renko Bar Charts: New Reversal Bar When Reversal Amount Met Plus 1 Tick and References Prior Close

You want a bar configuration of something like 6-3-9 or 8-4-12. This would mean you’ll have a 6 tick bar with a 3 tick offset and a 12 Tick reversal (you add the 2nd and 3rd numbers for the Reversal).

Make sure you select Renko Bars with wicks and I think that’s it. Play with those settings and
See If they get you closer than you were. I’ll send you the right settings via screenshots tomorrow afternoon if you can’t get the exact Renko look comparing it to NT.


Scott
[2018-09-11 14:26:24]
User701453 - Posts: 176
JoseyWales

What do you think,will you be able to convert the ninja code to SC?
[2018-09-11 14:28:11]
User701453 - Posts: 176
Thanks for the renko bar change idea.
When you get time could you send me the screenshots?
[2018-09-11 15:51:52]
Xfanman1 - Posts: 320
Here is what my Renko 8-4-12 settings look like on the chart. If you want a faster 6 tick bar than you'd use 6-3-9, a slower 12 tick bar, 12-6-18 and so on. Again this is an 8 tick bar with a 4 tick offset and a 16 tick reversal (the 12 + 4 = 16).

The other screen shots show the Chart Setting bar period and style settings to achieve what I've found to be closest to the NT Better Renko or Mean Renko settings. Enter your bar settings and play with it but this should be MUCH closer to your first chart than the SC chart you posted.

If you have any questions feel free to shoot me a DM also.

--
Scott
Date Time Of Last Edit: 2018-09-11 15:56:03
image4.PNG / V - Attached On 2018-09-11 15:47:26 UTC - Size: 198.86 KB - 400 views
imageCompleteRenkoSettings.PNG / V - Attached On 2018-09-11 15:51:46 UTC - Size: 349.14 KB - 371 views
[2018-09-11 16:24:59]
User701453 - Posts: 176
Scott,
Thanks so much for your help, it is very much appreciated..

SC bars look a lot closer now, I will keep fiddling with the settings until it looks like Ninja chart.

I been using the ninja strat with a demo ninja account and placing trades manually by hand to a live account.

Need to get the ninja atr stop and background conversion completed before the ninja demo expires.
Not sure if I would be able to get another demo account of ninja or not when it expires.
[2018-09-12 01:38:17]
JoseyWales - Posts: 67
You may never end up finding a perfect 1 to 1 match between SierraChart and NinjaTrader chart bars. A few reasons I can think of is different platforms may not use the same tick sizes. For example NT uses .00001 for non JPY Forex data, whereas SC uses .00005. Another reason is both SC and NT have their own interpretations of how to calculate Renko bars. If either NT or SC Renko bar logic differs from one another, then one may create a bar where the other does not. The last reason I can think of is NT uses "Break at end of day" and SC "New Bar on Season Start" settings and session times could be different. And the data providers may be different, therefore different raw tick data.

The biggest thing to learn/remember when programming SC ACSIL studies is variables and arrays that aren't a reference are destroyed at the end of the scope. And when AutoLoop = 1 this means it occurs at every update. Data assigned in a SCFloatArray will not be retained, whereas data in a SCFloatArrayRef will be. So the variables carryForward, result, and swing were being reset to 0 every iteration when they should have retained their value from the previous update. SC ACSIL is function like programming compared to NinjaScript's class based programming where a simple field stays in the scope of the class until the class is deleted, not on every update.

SierraChart has Persistent variables that solve this dilemma. If you don't need an entire SCFloatArrayRef and just need a variable to be carried over to the next update, use Persistent variables. It takes an int parameter as a key, so be sure to use different key values for persistent variables of the same type. And don't forget to make the type as a reference (double& name = sc.GetPersistentDouble(0), int& name = sc.GetPersistentInt(0)) so it is assignable. If you forget the reference operator, the code will compile fine but it will be confusing trying to debug until you realize you forgot to make it a reference type.

sc.GetPersistentInt()

This is the code as seen in my attached pictures. Changing the input named Factor changes the output much more than Trend Length. It has another input named "Color Background or Color Bars?" if the user wants to paint the background or bars. If you want to have the stop level as a single subgraph, you can create a new subgraph and assign to it the variable "result" in the bottom if/else blocks like Bullish/Bearish Lines do. The subgraph named Trend assigns -1 for bearish and 1 for bullish conditions. Because it is a subgraph, it can be referenced in an alert condition (ID1.SG3[0] > 0).

Study/Chart Alerts And Scanning

Apparently you can't have a subgraph painting the background and another subgraph painting the bar colors within the same indicator. That's why the user has to choose either or neither, but not both. It might be a SierraChart bug.

Let me know what you think and if there's any improvements I can make for you.


#include "sierrachart.h"

SCSFExport scsf_BG_ColorAndTrailingStop(SCStudyInterfaceRef sc)
{
  SCSubgraphRef BullishLine = sc.Subgraph[0];
  SCSubgraphRef BearishLine = sc.Subgraph[1];
  SCSubgraphRef Trend = sc.Subgraph[2];
  SCSubgraphRef BackgroundColor = sc.Subgraph[3];
  SCSubgraphRef BarColor = sc.Subgraph[4];
  SCSubgraphRef ATR = sc.Subgraph[5];

  SCInputRef TrendLength = sc.Input[0];
  SCInputRef Factor = sc.Input[1];
  SCInputRef ColorType = sc.Input[2];

  if (sc.SetDefaults)
  {
    sc.AutoLoop = 1;
    sc.FreeDLL = 0;
    sc.GraphName = "Color Background";
    sc.GraphRegion = 0;

    // Graphs

    BullishLine.Name = "Bullish Line";
    BullishLine.DrawStyle = DRAWSTYLE_LINE_SKIP_ZEROS;
    BullishLine.PrimaryColor = COLOR_DODGERBLUE;
    BullishLine.LineWidth = 3;

    BearishLine.Name = "Bearish Line";
    BearishLine.DrawStyle = DRAWSTYLE_LINE_SKIP_ZEROS;
    BearishLine.PrimaryColor = COLOR_MAGENTA;
    BearishLine.LineWidth = 3;

    Trend.Name = "Trend";
    Trend.DrawStyle = DRAWSTYLE_IGNORE;
    Trend.SecondaryColorUsed = true;
    Trend.PrimaryColor = COLOR_GREEN;
    Trend.SecondaryColor = COLOR_RED;
    Trend.DrawZeros = false;

    BackgroundColor.Name = "Background Color";
    BackgroundColor.SecondaryColorUsed = true;
    BackgroundColor.PrimaryColor = COLOR_GREEN;
    BackgroundColor.SecondaryColor = COLOR_RED;
    BackgroundColor.DrawZeros = false;

    BarColor.Name = "Bar Color";
    BarColor.DrawStyle = DRAWSTYLE_COLOR_BAR;
    BarColor.PrimaryColor = COLOR_DODGERBLUE;
    BarColor.SecondaryColor = COLOR_RED;
    BarColor.SecondaryColorUsed = true;

    // Inputs

    TrendLength.Name = "Trend Length";
    TrendLength.SetInt(20);

    Factor.Name = "Factor";
    Factor.SetDouble(1.5);

    ColorType.Name = "Color Background or Color Bars?";
    ColorType.SetCustomInputStrings("No;Color Background;Color Bars");
    ColorType.SetCustomInputIndex(0);

    return;
  }

  double& carryForward = sc.GetPersistentDouble(0);
  double& result = sc.GetPersistentDouble(1);
  int& swing = sc.GetPersistentInt(0);

  if (sc.Index < TrendLength.GetInt())
  {
    swing = 1;

    if (ColorType.GetIndex() == 1)
      BackgroundColor.DrawStyle = DRAWSTYLE_BACKGROUND_TRANSPARENT;
    else if (ColorType.GetIndex() == 2)
      BackgroundColor.DrawStyle = DRAWSTYLE_IGNORE;

    return;
  }

  sc.ATR(sc.BaseDataIn, ATR, TrendLength.GetInt(), MOVAVGTYPE_SIMPLE);

  double num = Factor.GetDouble() * ATR[sc.Index];

  if (swing == 1 && sc.Close[sc.Index] <= result)
  {
    swing = -1;
    result = DBL_MAX;
  }
  else if (swing == -1 && sc.Close[sc.Index] >= result)
  {
    swing = 1;
    result = DBL_MIN;
  }
  if (swing == 1)
  {
    if (sc.Low[sc.Index] - carryForward > result)
      result = sc.Low[sc.Index] - carryForward;

    BullishLine[sc.Index] = result;
    Trend[sc.Index] = swing;

    // Color Bullish
    Trend.DataColor[sc.Index] = Trend.PrimaryColor;

    if (ColorType.GetIndex() == 1)
    {
      BackgroundColor[sc.Index] = swing;
      BackgroundColor.DataColor[sc.Index] = BackgroundColor.PrimaryColor;
    }

    else if (ColorType.GetIndex() == 2)
    {
      BarColor[sc.Index] = swing;
      BarColor.DataColor[sc.Index] = BarColor.PrimaryColor;
    }
  }
  else if (swing == -1)
  {
    if (sc.High[sc.Index] + carryForward < result)
      result = sc.High[sc.Index] + carryForward;

    BearishLine[sc.Index] = result;
    Trend[sc.Index] = swing;

    // Color Bearish
    Trend.DataColor[sc.Index] = Trend.SecondaryColor;

    if (ColorType.GetIndex() == 1)
    {
      BackgroundColor[sc.Index] = swing;
      BackgroundColor.DataColor[sc.Index] = BackgroundColor.SecondaryColor;
    }
    else if (ColorType.GetIndex() == 2)
    {
      BarColor[sc.Index] = swing;
      BarColor.DataColor[sc.Index] = BarColor.SecondaryColor;
    }
  }

  carryForward = num;
}

imageSierraChart Color Background - Color Bar.png / V - Attached On 2018-09-12 01:22:06 UTC - Size: 404.92 KB - 460 views
imageSierraChart vs NinjaTrader 8 - Renko Comparison.png / V - Attached On 2018-09-12 01:23:21 UTC - Size: 550.33 KB - 495 views
Attachment Deleted.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account