Back

Pine editor build in function code to convert C #

Hi all,

I am new here, I have a code that I wrote in pine editor (trandingview) for a part of my trailing stop algo. I used build in fuction (nz) but I dont know how to use it here so Is there anyone can help me to convert this portion of code to C# ? 

 
AF2 = input(defval=1,title="Percent",minval=0.1)/100
SL2 = Trail1*AF2 // Stop Loss
Trail2 = 0.0
Trail2 := iff(Trail1>nz(Trail2[1],0) and Trail1[1]>nz(Trail2[1],0),max(nz(Trail2[1],0),Trail1-SL2),iff(Trail1<nz(Trail2[1],0) and Trail1[1]<nz(Trail2[1],0),min(nz(Trail2[1],0),Trail1+SL2),iff(Trail1>nz(Trail2[1],0),Trail1-SL2,Trail1+SL2)))
 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Yusuf,

In this situation, the nz function returns 0 if the previous `Trail2` value is na. We can translate this to C# by first creating a RollingWindow for `Trail1` and `Trail2`.

public RollingWindow<float> Trail1 = new RollingWindow<float>(2);
public RollingWindow<float> Trail2 = new RollingWindow<float>(2);

We ensure these RollingWindows never return na when trying to access their last elements by adding a 0 to each one at the beginning of the program.

Trail1.Add(0);
Trail2.Add(0);

Then we can replace the nested if statement that determines the newest Trail2 value with

Trail1.Add(random.Next(0, 1000));

if (Trail1[0] > Trail2[0] && Trail1[1] > Trail2[0])
Trail2.Add(Math.Max(Trail2[0], Trail1[0] - SL2));
else if (Trail1[0] < Trail2[0] && Trail1[1] < Trail2[0])
Trail2.Add(Math.Min(Trail2[0], Trail1[0] - SL2));
else if (Trail1[0] > Trail2[0])
Trail2.Add(Trail1[0] - SL2);
else
Trail2.Add(Trail1[0] + SL2);

See the attached backtest for the full code files. I also recommend reviewing our documentation and completing some bootcamp lessons to learn our API.

Best,
Derek Melchin

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed