_Forex_Nn_Ind_dimicr

Download script
_Forex_Nn_Ind_dimicr

This script is a sophisticated technical indicator designed to analyze market trends by combining traditional price action metrics with an artificial intelligence model known as a Neural Network. It processes historical price data to generate two distinct visual signals on your chart, helping to identify potential momentum or trend shifts.

Core Logic Overview

The tool operates in three primary stages:

1. Price Action Normalization (NOC) The script first looks at the "Center of Low Volume" (CLV) logic. It examines the relationship between the current price and the high/low range of a specific number of preceding candles. By calculating where the current price sits relative to this range, it determines the immediate "strength" of the move. This data is then smoothed out using an Exponential Moving Average (EMA), which removes minor market noise and creates a cleaner trend line for the next stage.

2. The Neural Network Processing (NN) This is the "brain" of the script. It takes the smoothed price data from the first stage and feeds it into a multi-layered artificial neural network.

  • Input Layer: It gathers a series of data points (17 inputs) based on recent price behavior.
  • Hidden Layers: The data passes through two hidden layers of "neurons." These layers use pre-defined weights (mathematical constants) to identify complex patterns that are not visible to the naked eye.
  • Activation: It uses a "Sigmoid" function, which is a common mathematical method in AI to determine the probability of an outcome—essentially deciding how strongly the data leans toward one direction or another.

3. Final Output and Visualization The final result of the neural network's calculation is mapped onto your chart as a line. The logic produces a value between 0 and 1:

  • Buy/Sell Levels: The script features two specific horizontal threshold levels (the Buy Level and Sell Level).
  • Trend Identification: When the neural network’s output crosses these specific thresholds, it signals a shift in market sentiment. Because the model is "trained" via fixed weights embedded in the code, it consistently interprets price patterns based on those internal rules to provide a consistent visual signal of trend direction.

Summary for the User

In simple terms, this script doesn't just look at where the price is; it looks at how the price behaved over a recent period, smoothes out the "jitter" of the market, and then runs that data through a mathematical model modeled after human brain synapses to predict the current trend strength.

Miscellaneous
Implements a curve of type %1
3 Views
0 Downloads
0 Favorites
_Forex_Nn_Ind_dimicr
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_color1 Red 
#property indicator_color2 Yellow 
#property indicator_minimum 0
#property indicator_maximum 1

// indicator parameters
// To do: read parameters, including weights, from a description file

extern double  BuyLevel = 0.54;
extern double  SellLevel = 0.74;

int     nNocInterval=12;
double  dNocRange=0.012;
int     nNocMa=5;

double  dNnOutput;

int     nRemoveFirst=200;
double  dE=2.7182818;

// indicator buffers
double  arrNocBuffer[];
double  arrNnBuffer[];

double  W[180];
double  WT[11];
double  Input[17];

int     nExtCountedBars=0;

////////////////////////
int init()
{

  // drawing settings
  SetIndexStyle(0,DRAW_LINE);
  SetIndexShift(0,0);
  SetIndexEmptyValue(0,0.0);

  SetIndexStyle(1,DRAW_LINE);
  SetIndexShift(1,0);
  SetIndexEmptyValue(1,0.0);

  IndicatorDigits(4);

  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  string  strIndicatorShortName="forex_nn";
  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

  IndicatorShortName(strIndicatorShortName);

  // indicator buffers mapping
  SetIndexBuffer(0,arrNocBuffer);
  SetIndexBuffer(1,arrNnBuffer);
  
  SetLevelStyle(STYLE_DOT,1);
  SetLevelValue(0,BuyLevel); 
  SetLevelValue(1,SellLevel); 

  //----
  W[0]=1.23284662840642;
  W[1]= -0.511755486703824;
  W[2]=0.0964615833231131;
  W[3]= -0.473951143707364;
  W[4]= -0.190044512653979;
  W[5]=0.385736545872854;
  W[6]=0.18430042807762;
  W[7]=0.250208618037659;
  W[8]= -0.0690710655087922;
  W[9]= -0.507242221145675;
  W[10]= -0.344757112356797;
  W[11]= -0.47518609293024;
  W[12]= -0.419874977285144;
  W[13]=0.74711393594611;
  W[14]=0.439031463137719;
  W[15]=0.519059847259052;
  W[16]=0.164716602638519;
  W[17]=1.4339376008723;
  W[18]=0.115386970035529;
  W[19]=0.496731065206044;
  W[20]= -0.118003872499675;
  W[21]=0.421064118363769;
  W[22]=0.25178510963282;
  W[23]= -0.293541706646167;
  W[24]= -0.448743140388445;
  W[25]=0.475962267396865;
  W[26]=0.569791973166393;
  W[27]=0.49272346954719;
  W[28]= -0.0905883648668217;
  W[29]= -0.0011422810692163;
  W[30]= -0.109523712298978;
  W[31]= -0.549473731166492;
  W[32]=0.562999057730813;
  W[33]= -0.6257614521639;
  W[34]=1.26121467755857;
  W[35]=0.932797035311227;
  W[36]=1.30694528621299;
  W[37]=0.481442159756628;
  W[38]=0.191342196173203;
  W[39]=0.307381497072444;
  W[40]= -0.543891916922809;
  W[41]= -0.249851880333427;
  W[42]= -0.174046638335755;
  W[43]=0.685804168947628;
  W[44]=0.0825153375976359;
  W[45]=0.583188445690559;
  W[46]=1.59329962072699;
  W[47]=0.780358652037702;
  W[48]=0.414232400586238;
  W[49]=0.405262591017787;
  W[50]=0.429133467300143;
  W[51]=4.5707252262589;
  W[52]= -0.974193568331981;
  W[53]= -0.241533739716741;
  W[54]=0.120167658526732;
  W[55]=0.106812446016975;
  W[56]=0.0946906217296473;
  W[57]= -0.973353986014926;
  W[58]=0.329835213992192;
  W[59]=0.354779871566284;
  W[60]=0.21141292154711;
  W[61]= -0.848058903044372;
  W[62]=0.40087506226086;
  W[63]=0.590049464056843;
  W[64]= -0.884409398272446;
  W[65]=0.225682751259687;
  W[66]= -0.779756671959848;
  W[67]=0.516217176242478;
  W[68]= -0.443823132762601;
  W[69]=1.15042401290238;
  W[70]=0.0761295218074771;
  W[71]=0.286354149734076;
  W[72]= -0.246276609126355;
  W[73]=0.407862236319774;
  W[74]=1.35863054128392;
  W[75]= -0.0899655682452336;
  W[76]=0.227209033012602;
  W[77]=0.48882649109837;
  W[78]=0.134262469866172;
  W[79]=0.520662445724424;
  W[80]=0.273921183996895;
  W[81]=0.797893190237165;
  W[82]=0.307232020735568;
  W[83]=0.66453677638372;
  W[84]=0.601517797706549;
  W[85]=2.41522339235385;
  W[86]= -0.54734695793675;
  W[87]= -0.616942416095533;
  W[88]= -0.329078616352176;
  W[89]=0.0343785770105571;
  W[90]=0.183606667410502;
  W[91]= -0.335604884494971;
  W[92]= -0.473071024438756;
  W[93]= -0.445323004747451;
  W[94]= -0.58852224463417;
  W[95]=0.200252466717469;
  W[96]=1.03363268985642;
  W[97]=1.07718838339749;
  W[98]=0.579629620875196;
  W[99]=0.563350223627848;
  W[100]=0.427986370809025;
  W[101]= -0.509749485567783;
  W[102]=3.26893236585458;
  W[103]= -1.74342337399321;
  W[104]=0.715682154705979;
  W[105]=0.602756337362107;
  W[106]= -0.343526606342097;
  W[107]= -1.17938810569175;
  W[108]=0.355240071640326;
  W[109]=0.35521243887383;
  W[110]= -1.44427916588966;
  W[111]= -0.276346018910304;
  W[112]= -0.59742769634354;
  W[113]= -1.10090634603251;
  W[114]=0.647220265319068;
  W[115]= -0.654279251957746;
  W[116]= -0.208823702966868;
  W[117]=0.0498559661839404;
  W[118]= -0.0845632001274042;
  W[119]=5.07943362953022;
  W[120]= -1.15314418917092;
  W[121]= -0.665833645801421;
  W[122]=0.255437001700128;
  W[123]=0.319124707947276;
  W[124]= -0.566939757767682;
  W[125]= -0.47605481843698;
  W[126]=0.39488556620511;
  W[127]= -0.256064309433873;
  W[128]=0.197056621017084;
  W[129]= -0.390050402269382;
  W[130]= -0.137297237383076;
  W[131]=0.545921953801981;
  W[132]= -0.071961640027172;
  W[133]= -0.483747658486652;
  W[134]=0.0250140139153488;
  W[135]=0.0652004545965496;
  W[136]=1.20609868338284;
  W[137]=1.29606665339007;
  W[138]= -0.322563707279071;
  W[139]= -0.233294561636766;
  W[140]= -0.782596575301184;
  W[141]= -0.513649291801566;
  W[142]= -0.635867543624109;
  W[143]= -1.02773789019305;
  W[144]= -0.756729297801529;
  W[145]= -0.235705567459231;
  W[146]= -1.28156572033992;
  W[147]= -0.411746401661486;
  W[148]=0.799399607981531;
  W[149]=0.320928188633003;
  W[150]= -0.000938595231901069;
  W[151]= -0.0237449790280827;
  W[152]= -0.16037405098131;
  W[153]= -0.551435857244773;
  W[154]=0.425892720818933;
  W[155]=0.522796972730267;
  W[156]=0.157836040132023;
  W[157]=0.415584007547781;
  W[158]= -0.0168757976604174;
  W[159]=0.00114783700460871;
  W[160]=0.115566049648634;
  W[161]= -0.211212597329733;
  W[162]=0.258353387832398;
  W[163]=0.337783351644855;
  W[164]=0.04312149688565;
  W[165]=0.149834366219619;
  W[166]=0.181127999640499;
  W[167]=0.744465709021414;
  W[168]=0.333011286627891;
  W[169]= -0.0555669565373262;
  W[170]=0.532057479531659;
  W[171]=0.966738538389819;
  W[172]= -0.609695328091294;
  W[173]=3.1940572214263;
  W[174]=0.827936264074366;
  W[175]=0.953122056124963;
  W[176]=1.05312004796111;
  W[177]=3.50187095403698;
  W[178]= -0.613446281522734;
  W[179]= -0.593921919074533;

  WT[0]= -1.85505582973963;
  WT[1]= -1.24910210484401;
  WT[2]= -0.537052977943149;
  WT[3]=2.61438475233761;
  WT[4]= -0.839540256869364;
  WT[5]=0.11721035714907;
  WT[6]= -0.126760582165264;
  WT[7]= -3.23470003440472;
  WT[8]= -1.35856127136088;
  WT[9]= -2.30050022722987;
  WT[10]= -4.20071031619927;

  return(0);
}

////////////////////
int start()
{
  nExtCountedBars=IndicatorCounted();
  if (nExtCountedBars < 0) return(-1);

  // last counted bar will be recounted
  if (nExtCountedBars > 0) nExtCountedBars--;

  Noc();
  ApplyNn();

  return(0);
}

///////////////////
// For the selected period:

// CLV = ((Close - Low) - (High - Close)) / (High - Low)
void Noc()
{
  int nPos=Bars - nExtCountedBars;

  while (nPos > 0)
  {
    if (nPos > Bars - nRemoveFirst)
    {
      arrNocBuffer[nPos]=0.5;
      nPos--;
      continue;
    }

    double  dClose=Close[nPos];
    double  dLow=Low[ArrayMinimum(Low,nNocInterval,nPos)];
    double  dHigh=High[ArrayMaximum(High,nNocInterval,nPos)];

    arrNocBuffer[nPos]=0.1;

    if (dHigh - dLow > dNocRange)
      arrNocBuffer[nPos]=(((dClose - dLow) - (dHigh - dClose)) / (dHigh - dLow)) / 2 + 0.5;
    else arrNocBuffer[nPos]=(((dClose - dLow) - (dHigh - dClose)) / dNocRange) / 2 + 0.5;

    nPos--;
  }

  if (nNocMa > 1) Ema(arrNocBuffer);
}

///////////////////
void Ema(double &arr[])
{
  double  dPr=2.0 / (nNocMa + 1);
  int     nPos=Bars - nExtCountedBars;

  while (nPos > 0)
  {
    if (nPos == Bars - 2) arrNocBuffer[nPos + 1]=arr[nPos + 1];

    arrNocBuffer[nPos]=arr[nPos] * dPr + arrNocBuffer[nPos + 1] * (1 - dPr);
    nPos--;
  }
}

///////////////////
void ApplyNn()
{
  int nPos=Bars - nExtCountedBars - 2;
  int LayerCount=3;
  int Neurons[3];

  Neurons[0]=17;
  Neurons[1]=10;
  Neurons[2]=1;

  double  LayerOutput0[17];
  double  LayerOutput1[10];
  double  LayerOutput2[1];
  double  xSum;

  while (nPos > 0)
  {

    for (int cnt=0; cnt <= Neurons[0] - 1; cnt++)
    {
      LayerOutput0[cnt]=arrNocBuffer[nPos + cnt] * 2 - 1;
      //Print(LayerOutput0[cnt]) ;
    }

    int w_cnt=0,
        wt_cnt=0;
    for (int xI=0; xI <= LayerCount - 2; xI++)
    {

      //Print("XI=", xI);
      // First Layer by Neurons
      int xTmp=Neurons[xI] - 1;

      //Print("Tmp=", xTmp);
      //Print("Neurons =", Neurons[xI+1]-1);
      for (int xK=0; xK <= Neurons[xI + 1] - 1; xK++)
      {

        
        xSum=0;

        // xTmp - Inputs in First Layer
        for (int xJ=0; xJ <= xTmp; xJ++)
        {

          //Print("XK=", xK, "-Cnt=", w_cnt, "-", wt_cnt);
          if (xI == 0)
          {
            xSum=xSum + LayerOutput0[xJ] * W[w_cnt];
          }

          if (xI == 1)
          {
            xSum=xSum + LayerOutput1[xJ] * W[w_cnt];
          }

          w_cnt+=1;
        }

        //Print("xSum=",xSum);
        xSum=xSum + WT[wt_cnt];
        wt_cnt+=1;

        //Print("xSum=",xSum);
        if (xI == 0)
        {
          LayerOutput1[xK]=Sigmoid(xSum,1);

          //Print("LayerOutput1-"+xK+"=", LayerOutput1[xK]);
        }

        if (xI == 1)
        {

          //Print("!!!!!!1!!!!!!");
          LayerOutput2[xK]=Sigmoid(xSum,1);
        }
      }
    }

    arrNnBuffer[nPos]=LayerOutput2[0];

    nPos--;

  } // for all patterns
}


double Sigmoid(double inp, double parm)
{
  return(1 / (1 + MathExp(-parm * inp)));
}


Comments