Create BackTest For LNL System

LearningStock2018

New member
VIP
I current use LNL system and It works for me. I'd like to add Order statement to this indicator to make a strategy and do a backtesting. I want to Buy Arrowup and Sell Arrowdown. I am a new member and have zero coding experience. Would you help me code AddOrder. Thanks in advance. Here is whole script:

Code:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#// [URL]https://www.tradingview.com/v/m0G2Xv7r/[/URL]
#// Created by © L&L Capital
# indicator("LNL Trend System", shorttitle = "LNL Trend System", overlay=true)
# Converted by Sam4Cok@Samer800    - 08/2023
#// Inputs

input TrendMode     = {"Tight", default "Normal", "Loose", "FOMC", "Net"};    # "Trend Mode"
input HtfMode       = {default "Auto", "Manual"};  # "HTF Mode"
input ManualTimeframe = AggregationPeriod.HOUR;    # "HTF Aggregation"
input ShowTrendBars = yes;        # "Show Trend Bars"
input ShowTrend     = yes;        # "Show Trend Line"
input ShowStopLine  = yes;        # "Show Stop Line"
input ShowHtfTrendLine = no;      # "Show HTF Trend Line"
input ShowHtfStopLine = no;       # "Show HTF Stop Line"
input ShowCloud = yes;            # "Show Cloud"
input ShowHtfCloud = no;          # "Show HTF Cloud"

def na = Double.NaN;
def last = isNaN(close);
def net = TrendMode == TrendMode."Net";
#fixnan(data)
script fixnan {
 input src = close;
     def data2;
     def bar = barnumber();
     if bar == 1 then {data2 = 0;} else
     if IsNaN(src) then {data2 = data2[1];} else
      {data2 = src;}
plot valid = data2;
}

#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    input v_ = volume;
    def v = if IsNaN(v_) then 1 else v_;
    def srcV = src * v;
    def srcVol = if IsNaN(srcV) then src else srcV;
    def VWMA = Average(srcVol, len) / Average(v, len);
    plot result = VWMA;
}
DefineGlobalColor("Bullish", CreateColor(39, 194, 46));
DefineGlobalColor("Bearish", CreateColor(255, 82, 82));
DefineGlobalColor("Neutral", CreateColor(67, 70, 81));
#// Trend Bars (DMI Colored Candles)
def tr_ = TrueRange(high, close, low);
def tr  = if IsNaN(tr_) then 0 else tr_;
def nATR = WildersAverage(tr, 14);
def hi = ((high - high[1]) > (low[1] - low)) and ((high - high[1]) > 0);
def lo = ((low[1] - low) > (high - high[1])) and ((low[1] - low) > 0);
def BullishDMI = if hi then (high - high[1]) else 0;
def BearishDMI = if lo then (low[1] - low) else 0;
def DMIUp = 100 * WildersAverage(BullishDMI, 14) / nATR;
def DMIDown = 100 * WildersAverage(BearishDMI, 14) / nATR;
def ADXx = if (DMIUp + DMIDown) > 0 then 100 * AbsValue(DMIUp - DMIDown) / (DMIUp + DMIDown) else na;
def ADX = WildersAverage(ADXx, 14);
def ColorBars = if (DMIUp > DMIDown and ADX > 20) then 1 else
                if (DMIUp < DMIDown and ADX > 20) then -1 else 0;
#ShowTrendBars
AssignPriceColor(if !ShowTrendBars then Color.CURRENT else
                 if ColorBars > 0 then GlobalColor("Bullish") else
                 if ColorBars < 0 then GlobalColor("Bearish") else GlobalColor("Neutral"));
#// Trend System (First Time Frame)
def ema8 =  vwma(close, 8);
def ema13 = vwma(close, 13);
def ema21 = vwma(close, 21);
def ema34 = vwma(close, 34);
def emaup = ema8 > ema13  and ema13 > ema21 and ema21 > ema34;
def emadn = ema8 < ema13  and ema13 < ema21 and ema21 < ema34;
def Trend = ExpAverage(close, 13);
def TrendColor = if emadn and close <= Trend then -1 else
                 if emaup and close >= Trend then 1 else 0;
plot TrendLine = if !ShowTrend then na else Trend; # "Trend", color = TrendColor
TrendLine.SetLineWeight(2);
TrendLine.AssignValueColor(if TrendColor > 0 then GlobalColor("Bullish") else
                           if TrendColor < 0 then GlobalColor("Bearish") else GlobalColor("Neutral"));
def ATRLength;
switch (TrendMode) {
case "Tight" :
    ATRLength = 60;
case "Normal":
    ATRLength = 80;
case "Loose" :
    ATRLength = 100;
case "FOMC"  :
    ATRLength = 120;
case "Net"   :
    ATRLength = 140;
}
def emaTr8 = ExpAverage(tr , 8);
def ATR = (ATRLength / 100) * emaTr8;
def Up = close > (Trend + ATR);
def Down = close < (Trend - ATR);
def T = if Up then 1 else if Down then -1 else T[1];
def StopLineColor =  T == 1;
plot StopLine = if !ShowStopLine then na else
                 if T == 1  then (Trend - ATR) else
                 if T == -1 then (Trend + ATR) else T[1];     # "StopLine"
StopLine.AssignValueColor(if StopLineColor then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLine.SetPaintingStrategy(PaintingStrategy.DASHES);

def ATRA = (ATRLength - 20) / 100 * emaTr8;
def Up11 = close > (Trend + ATRA);
def Down11 = close < (Trend - ATRA);
def T11 = if Up11 then 1 else if Down11 then -1 else T11[1];
def StopLineColor1 = T11 == 1;

plot StopLine2 = if !ShowStopLine then na else
                if T11 == 1 then (Trend - ATRA) else
                if T11 == -1 then (Trend + ATRA) else T11[1]; # "StopLine2"
StopLine2.AssignValueColor(if StopLineColor1 then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLine2.SetPaintingStrategy(PaintingStrategy.DASHES);

def ATRNET = if net then (ATRLength - 40) / 100 * emaTr8 else na;
def UpNET = close > (Trend + ATRNET);
def DownNET = close < (Trend - ATRNET);
def TNET = if UpNET then 1 else if DownNET then -1 else TNET[1];
def StopLineColorNET = TNET == 1;

plot StopLineNET = if !ShowStopLine then na else
                   if TNET == 1 then (Trend - ATRNET) else
                   if TNET == -1 then (Trend + ATRNET) else TNET[1];    # "StopLineNET"
StopLineNET.AssignValueColor(if StopLineColorNET then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLineNET.SetPaintingStrategy(PaintingStrategy.DASHES);

def ATRNET1 = if net then (ATRLength - 60) / 100 * emaTr8 else na;
def UpNET1 = close > (Trend + ATRNET1);
def DownNET1 = close < (Trend - ATRNET1);
def TNET1 = if UpNET1 then 1 else if DownNET1 then -1 else TNET1[1];
def StopLineColorNET1 = TNET1 == 1;

plot StopLineNET1 = if !ShowStopLine then na else
                    if TNET1 == 1 then (Trend - ATRNET1) else
                    if TNET1 == -1 then (Trend + ATRNET1) else TNET1[1]; # "StopLineNET1"
StopLineNET1.AssignValueColor(if StopLineColorNET1 then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLineNET1.SetPaintingStrategy(PaintingStrategy.DASHES);

def ATRNET2 = if net then (ATRLength - 80) / 100 * emaTr8 else na;
def UpNET2 = close > (Trend + ATRNET2);
def DownNET2 = close < (Trend - ATRNET2);
def TNET2 = if UpNET2 then 1 else if DownNET2 then -1 else TNET2[1];
def StopLineColorNET2 = TNET2 == 1;

plot StopLineNET2 = if !ShowStopLine then na else
                    if TNET2 == 1 then (Trend - ATRNET2) else
                    if TNET2 == -1 then (Trend + ATRNET2) else TNET2[1]; # "StopLineNET2"
StopLineNET2.AssignValueColor(if StopLineColorNET2 then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLineNET2.SetPaintingStrategy(PaintingStrategy.DASHES);

#// Higher Time Frame Aggregations

def current = GetAggregationPeriod();
def TimeFrameA =
      if current < AggregationPeriod.FIVE_MIN then AggregationPeriod.FIVE_MIN else
      if current < AggregationPeriod.THIRTY_MIN then AggregationPeriod.THIRTY_MIN else
      if current < AggregationPeriod.FOUR_HOURS then AggregationPeriod.FOUR_HOURS else
      if current < AggregationPeriod.DAY then AggregationPeriod.DAY else
      if current < AggregationPeriod.WEEK then AggregationPeriod.WEEK else
      if current < AggregationPeriod.MONTH then AggregationPeriod.MONTH else
      if current < AggregationPeriod.QUARTER then AggregationPeriod.QUARTER else current;
def TimeFrame;
switch (HTFMode) {
case "Auto" :
    TimeFrame = TimeFrameA;
case "Manual" :
    TimeFrame = ManualTimeframe;
}
def HighTf = high(Period = TimeFrame);
def LowTf  = low(Period = TimeFrame);
def CloseTf = close(Period = TimeFrame);
def volTf   = volume(Period = TimeFrame);
def trTf_   = TrueRange(HighTf, CloseTf, LowTf);
def trTf     = if IsNaN(trTf_) then (HighTf-LowTf) else trTf_;
def ematrTf8 = fixnan(ExpAverage(trTf, 8));
def ATRLength2 = ATRLength;

def ema82  = vwma(CloseTf, 8, volTf);
def ema132 = vwma(CloseTf, 13, volTf);
def ema212 = vwma(CloseTf, 21, volTf);
def ema342 = vwma(CloseTf, 34, volTf);
def emaup2 = ema82 > ema132  and ema132 > ema212 and ema212 > ema342;
def emadn2 = ema82 < ema132  and ema132 < ema212 and ema212 < ema342;

def Trend2 = ExpAverage(CloseTf, 13);
def TrendColor2 = if emadn2 and CloseTf <= Trend2 then -1 else
                  if emaup2 and CloseTf >= Trend2 then 1 else 0;

plot Trend2Line = if !ShowHtfTrendLine then na else Trend2;    # "Trend2"
Trend2Line.SetLineWeight(2);
Trend2Line.AssignValueColor(if TrendColor2 > 0 then GlobalColor("Bullish") else
                            if TrendColor2 < 0 then GlobalColor("Bearish") else GlobalColor("Neutral"));

def ATR2 = (ATRLength2 / 100) * ematrTf8;
def Up2 = CloseTf > (Trend2 + ATR2);
def Down2 = CloseTf < (Trend2 - ATR2);
def T2 = if Up2 then 1 else if Down2 then -1 else T2[1];
def StopLineColor2 = T2 == 1;

plot StopLineTf = if !ShowHtfStopLine then na else
                 if T2 == 1 then (Trend2 - ATR2) else
                 if T2 == -1 then (Trend2 + ATR2) else T2[1];    # "StopLine2"
StopLineTf.AssignValueColor(if StopLineColor2 then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLineTf.SetPaintingStrategy(PaintingStrategy.DASHES);
#ShowStop2

def ATR2A = (ATRLength2 - 20) / 100 * ematrTf8;
def Up2A = CloseTf > (Trend2 + ATR2A);
def Down2A = CloseTf < (Trend2 - ATR2A);
def T2A = if Up2A then 1 else if Down2A[1] then -1 else T2A[1];
def StopLineColor2A = T2A == 1;

plot StopLine2Tf = if !ShowHtfStopLine then na else
                   if T2A == 1 then (Trend2 - ATR2A) else
                   if T2A == -1 then (Trend2 + ATR2A) else T2A[1];    #  "StopLine2"
StopLine2Tf.AssignValueColor(if StopLineColor2A then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLine2Tf.SetPaintingStrategy(PaintingStrategy.DASHES);

def ATR2ANET = if net then (ATRLength2 - 40) / 100 * ematrTf8 else na;
def Up2ANET = CloseTf > (Trend2 + ATR2ANET);
def Down2ANET = CloseTf < (Trend2 - ATR2ANET);
def T2ANET = if Up2ANET then 1 else if Down2ANET[1] then -1 else T2ANET[1];
def StopLineColor2ANET = T2ANET == 1;

plot StopLineNETtf = if !ShowHtfStopLine then na else
                     if T2ANET == 1 then (Trend2 - ATR2ANET) else
                     if T2ANET == -1 then (Trend2 + ATR2ANET) else T2ANET[1];    # StopLineColor2ANET
StopLineNETtf.AssignValueColor(if StopLineColor2ANET then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLineNETtf.SetPaintingStrategy(PaintingStrategy.DASHES);

def ATR2ANET1 = if Net then (ATRLength2 - 60) /100 * ematrTf8 else na;
def Up2ANET1 = CloseTf > (Trend2 + ATR2ANET1);
def Down2ANET1 = CloseTf < (Trend2 - ATR2ANET1);
def T2ANET1 = if Up2ANET1 then 1 else if Down2ANET1[1] then -1 else T2ANET1[1];
def StopLineColor2ANET1 = T2ANET1 == 1;

plot StopLineNETtf1 = if !ShowHtfStopLine then na else
                      if T2ANET1 == 1 then (Trend2-ATR2ANET1) else
                      if T2ANET1 == -1 then (Trend2+ATR2ANET1) else T2ANET1[1];
StopLineNETtf1.AssignValueColor(if StopLineColor2ANET1 then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLineNETtf1.SetPaintingStrategy(PaintingStrategy.DASHES);

def ATR2ANET2 = if Net then (ATRLength2 - 80) /100 * ematrTf8 else na;
def Up2ANET2 = CloseTf > (Trend2 + ATR2ANET2);
def Down2ANET2 = CloseTf < (Trend2 - ATR2ANET2);
def T2ANET2 = if Up2ANET2 then 1 else if Down2ANET2[1] then -1 else T2ANET2[1];
def StopLineColor2ANET2 = T2ANET2 == 1;

plot StopLineNETtf2 = if !ShowHtfStopLine then na else
                      if T2ANET2 == 1 then (Trend2-ATR2ANET2) else
                      if T2ANET2 == -1 then (Trend2+ATR2ANET2) else T2ANET2[1];
StopLineNETtf2.AssignValueColor(if StopLineColor2ANET1 then GlobalColor("Bullish") else GlobalColor("Bearish"));
StopLineNETtf2.SetPaintingStrategy(PaintingStrategy.DASHES);

#// Trend Clouds

def p1 = Trend;
def p2 = if T == 1 then (Trend-ATR) else if T == -1 then (Trend+ATR) else T[1];
AddCloud(if !ShowCloud then na else p1, p2, Color.DARK_GREEN, Color.DARK_RED);

def p3 = Trend2;
def p4 = if T2 == 1 then (Trend2-ATR2) else if T2 == -1 then (Trend2+ATR2) else T2[1];

AddCloud(if !ShowHTFCloud then na else p3 ,p4, Color.DARK_GREEN, Color.DARK_RED);
plot upsignal = if p1 crosses above p2 then p2 else double.NaN ;
upsignal.SetPaintingStrategy(PaintingStrategy.ARROW_up);
upsignal.SetDefaultColor(color.cyan) ;
upsignal.SetLineWeight(3);

plot dnsignal = if p1 crosses below p2 then p2 else double.NaN ;
dnsignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dnsignal.SetDefaultColor(color.magenta) ;
dnsignal.SetLineWeight(3);

# Alerts
Alert(close crosses above StopLineNETtf1, "Price crossing above StopLine1", Alert.Bar, Sound.Chimes);
Alert(close crosses below StopLineNETtf1, "Price crossing below StopLine1", Alert.Bar, Sound.Bell);
Alert(close crosses above StopLineNETtf2, "Price crossing above StopLine2", Alert.Bar, Sound.Chimes);
Alert(close crosses below StopLineNETtf2, "Price crossing below StopLine2", Alert.Bar, Sound.Bell);

#--- END of CODE
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
481 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top