BearBullBalance_AD

Author: Scriptong
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1Uses files from the file system
0 Views
0 Downloads
0 Favorites
BearBullBalance_AD
ÿþ#property copyright "Scriptong"

#property link      "http://advancetools.net"

#property description "B>1@065=85 A8;K 1K:>2 8 <5425459 A B8:>2>9 B>G=>ABLN. ?8A0=85 4>ABC?=> =0 A09B5 AdvanceTools.net 2 @0745;5 \""8:>2K5 >1J5<K.\""

#property version "300.517"

#property strict



#property indicator_separate_window

#property indicator_buffers 4

#property indicator_color1 clrGray

#property indicator_color2 clrGray

#property indicator_color3 clrLime

#property indicator_color4 clrRed



#property indicator_width3 2

#property indicator_width4 2



struct TickStruct                                                                                  // !B@C:BC@0 4;O 70?8A8 40==KE >1 >4=>< B8:5

{

   datetime time;

   double   bid;

   double   ask;   

};



enum ENUM_TICKSPOINTS

{

    TICKS,                                                                                         // "8:8

    POINTS                                                                                         // C=:BK

};





// 0AB@>5G=K5 ?0@0<5B@K 8=48:0B>@0

input ENUM_TICKSPOINTS  i_useTicksAtPrice       = TICKS;                                           // 0;0=A 2 B8:0E 8;8 2 ?C=:B0E

input int               i_indBarsCount          = 50000;                                           // >;8G5AB2> 10@>2 4;O >B>1@065=8O



// =48:0B>@=K5 1CD5@0

double g_bullBuffer[];

double g_bearBuffer[];

double g_bullPrevalenceBuffer[];

double g_bearPrevalenceBuffer[];





// @>G85 3;>10;L=K5 ?5@5<5==K5 8=48:0B>@0

bool g_activate,                                                                                   // @87=0: CA?5H=>9 8=8F80;870F88 8=48:0B>@0

     g_init;                                                                                       // 5@5<5==0O 4;O 8=8F80;870F88 AB0B8G5A:8E ?5@5<5==KE 2=CB@8 DC=:F89 2 <><5=B ?@>2545=8O..

                                                                                                   // ..?>2B>@=>9 8=8F80;870F88

                                                                                                   

double g_tickSize;



TickStruct        g_ticks[];                                                                       // 0AA82 4;O E@0=5=8O B8:>2, ?>ABC?82H8E ?>A;5 =0G0;0 @01>BK 8=48:0B>@0                    



//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Custom indicator initialization function                                                                                                                                                          |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

int OnInit()

{

   g_activate = false;                                                                             // =48:0B>@ =5 8=8F80;878@>20=

   g_init = true;

   

   if (!IsTuningParametersCorrect())                                                               // 525@=> C:070==K5 7=0G5=8O =0AB@>5G=KE ?0@0<5B@>2 - ?@8G8=0 =5C40G=>9 8=8F80;870F88

      return (INIT_FAILED);                                 

           

   if (!BuffersBind())                             

      return (INIT_FAILED);                                 

       

   if (!IsLoadTempTicks())                                                                         // 03@C7:0 40==KE > B8:0E, A>E@0=5==KE 70 ?@54K4CI89 ?5@8>4 @01>BK 8=48:0B>@0   

      return INIT_FAILED;

           

   g_activate = true;                                                                              // =48:0B>@ CA?5H=> 8=8F80;878@>20=

   return(INIT_SUCCEEDED);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| @>25@:0 :>@@5:B=>AB8 =0AB@>5G=KE ?0@0<5B@>2                                                                                                                                                      |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsTuningParametersCorrect()

{

   string name = WindowExpertName();



   int period = Period();

   if (period == 0)

   {

      Alert(name, ": D0B0;L=0O >H81:0 B5@<8=0;0 - ?5@8>4 0 <8=CB. =48:0B>@ >B:;NG5=.");

      return (false);

   }

   

   g_tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);

   if (g_tickSize == 0)

   {

      Alert(name, ": D0B0;L=0O >H81:0 B5@<8=0;0 - 25;8G8=0 H030 >4=>3> B8:0 @02=0 =C;N. =48:0B>@ >B:;NG5=.");

      return (false);

   }



   

   return (true);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| !2O7K20=85 1CD5@>2 8=48:0B>@0 A <0AA820<8                                                                                                                                                         |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool BuffersBind()

{

   string name = WindowExpertName();



   if (

       !SetIndexBuffer(0, g_bullBuffer)                  ||

       !SetIndexBuffer(1, g_bearBuffer)                  ||

       !SetIndexBuffer(2, g_bullPrevalenceBuffer)        ||

       !SetIndexBuffer(3, g_bearPrevalenceBuffer)       

      )

   {

      Alert(name, ": >H81:0 A2O7K20=8O <0AA82>2 A 1CD5@0<8 8=48:0B>@0. H81:0 !", GetLastError());

      return (false);

   }



   for (int i = 0; i < 4; i++)   

   {

      SetIndexStyle(i, DRAW_HISTOGRAM);

      SetIndexEmptyValue(i, 0);

   }

      

   SetIndexLabel(0, "Bull Power");

   SetIndexLabel(1, "Bear Power");

   SetIndexLabel(2, "Bull Prevalence");

   SetIndexLabel(3, "Bear Prevalence");

   

   return (true);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| 'B5=85 40==KE > B8:0E, =0:>?;5==KE 2 B5G5=85 ?@54K4CI59 @01>G59 A5AA88 ?@>3@0<<K                                                                                                                  |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsLoadTempTicks()

{

   // B:@KB85 D09;0 B8:>2>9 8AB>@88

   int hTicksFile = FileOpen(Symbol() + "temp.tks", FILE_BIN | FILE_READ | FILE_SHARE_READ | FILE_SHARE_WRITE);

   if (hTicksFile < 1)

      return true;

      

   //  0A?@545;5=85 ?0<OB8 4;O <0AA820 g_ticks

   int recSize = (int)(FileSize(hTicksFile) / sizeof(TickStruct));   

   if (ArrayResize(g_ticks, recSize, 1000) < 0)

   {

      Alert(WindowExpertName(), ": =5 C40;>AL @0A?@545;8BL ?0<OBL 4;O ?>4:0G:8 40==KE 87 2@5<5==>3> D09;0 B8:>2. =48:0B>@ >B:;NG5=.");

      FileClose(hTicksFile);

      return false;

   }

   

   // 'B5=85 D09;0

   int i = 0;

   while (i < recSize)

   {

      if (FileReadStruct(hTicksFile, g_ticks[i]) == 0)

      {

         Alert(WindowExpertName(), ": >H81:0 GB5=8O 40==KE 87 2@5<5==>3> D09;0. =48:0B>@ >B:;NG5=.");

         return false;

      }

      

      i++;

   }



   FileClose(hTicksFile);

   return true;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Custom indicator deinitialization function                                                                                                                                                        |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   if (!IsSavedFile())                                                                             // A;8 =8 >48= 87 ?>4:;NG5==KE 8=48:0B>@>2 =5 A>E@0=8; 40==K5, B> 8E A>E@0=8B B5:CI89 8=48:0B>@

      SaveTempTicks();                                                                             // !>E@0=5=85 40==KE > B8:0E, =0:>?;5==KE 70 B5:CI89 ?5@8>4 @01>BK 8=48:0B>@0   

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| @>25@:0 =0;8G8O 70?8A0==KE 40==KE 4@C38< 8=48:0B>@><                                                                                                                                             |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsSavedFile()

{

   // >;CG5=85 2@5<5=8 ?>ABC?;5=8O ?>A;54=53> 70?8A0==>3> B8:0

   int lastTickIndex = ArraySize(g_ticks) - 1;

   if (lastTickIndex < 0)                                                                          // 8 >48= B8: =5 1K; ?>;CG5=. 0?8AL 40==KE =5 B@51C5BAO

      return true;



   // B:@KB85 D09;0 B8:>2>9 8AB>@88

   int hTicksFile = FileOpen(Symbol() + "temp.tks", FILE_BIN | FILE_READ | FILE_SHARE_READ | FILE_SHARE_WRITE);

   if (hTicksFile < 1)

      return false;

   

   // 5@5<5I5=85 : ?>A;54=59 70?8A8 2 D09;5

   if (!FileSeek(hTicksFile, -sizeof(TickStruct), SEEK_END))

   {

      FileClose(hTicksFile);

      return false;

   }

   

   // 'B5=85 ?>A;54=59 70?8A8 8 70:@KB85 D09;0

   TickStruct tick;

   uint readBytes = FileReadStruct(hTicksFile, tick);

   FileClose(hTicksFile);

   if (readBytes == 0)

      return false;

  

   // !@02=5=85 40BK B8:0, 70?8A0==>3> 2 D09;5, 8 40BK ?>A;54=53> ?>ABC?82H53> B8:0

   return tick.time >= g_ticks[lastTickIndex].time;                                                // 0B0/2@5<O ?>A;54=53> 70?8A0==>3> 2 D09;5 B8:0 1>;LH5 8;8 @02=0 40B5/2@5<5=8..

                                                                                                   // ..70@538AB@8@>20==>3> B8:0. =0G8B, D09; C65 70?8A0=, 8 ?>2B>@=0O 70?8AL =5 B@51C5BAO

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| !>E@0=5=85 40==KE > B8:0E, =0:>?;5==KE 70 B5:CICN @01>GCN A5AA8N ?@>3@0<<K                                                                                                                        |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void SaveTempTicks()

{

   // !>740=85 D09;0 B8:>2>9 8AB>@88

   int hTicksFile = FileOpen(Symbol() + "temp.tks", FILE_BIN | FILE_READ | FILE_WRITE | FILE_SHARE_READ | FILE_SHARE_WRITE);

   if (hTicksFile < 1)

      return;

   

   // 0?8AL D09;0

   int total = ArraySize(g_ticks), i = 0;

   while (i < total)

   {

      if (FileWriteStruct(hTicksFile, g_ticks[i]) == 0)

      {

         Print("H81:0 A>E@0=5=8O 40==KE 2> 2@5<5==K9 D09;...");

         return;

      }

      

      i++;

   }



   FileClose(hTicksFile);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| @82545=85 7=0G5=89 : B>G=>AB8 >4=>3> B8:0                                                                                                                                                        |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

double NP(double value)

{

   if (g_tickSize == 0)

      return (0);

      

   return (MathRound(value / g_tickSize) * g_tickSize);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| =8F80;870F8O 2A5E 8=48:0B>@=KE 1CD5@>2                                                                                                                                                           |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void BuffersInitializeAll()

{

   ArrayInitialize(g_bullBuffer, 0);     

   ArrayInitialize(g_bearBuffer, 0);     

   ArrayInitialize(g_bullPrevalenceBuffer, 0);     

   ArrayInitialize(g_bearPrevalenceBuffer, 0);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| ?@545;5=85 8=45:A0 10@0, A :>B>@>3> =5>1E>48<> ?@>872>48BL ?5@5@0AG5B                                                                                                                            |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

int GetRecalcIndex(int& total, const int ratesTotal, const int prevCalculated)

{

   // ?@545;5=85 ?5@2>3> 10@0 8AB>@88, =0 :>B>@>< 1C4CB 4>ABC?=K 045:20B=K5 7=0G5=8O 8=48:0B>@0

   total = ratesTotal - 1;                                                                         

                                                   

   //  <>65B 7=0G5=8O 8=48:0B>@0 =5 =C6=> >B>1@060BL =0 2A59 8AB>@88?

   if (i_indBarsCount > 0 && i_indBarsCount < total)

      total = MathMin(i_indBarsCount, total);                      

                                                   

   // 5@2>5 >B>1@065=85 8=48:0B>@0 8;8 ?@>87>H;0 ?>4:0G:0 40==KE, B. 5. =0 ?@54K4CI5< B8:5 10@>2 1K;> =5 =0 >48= 10@ <5=LH5, :0: ?@8 =>@<0;L=>< @0728B88 8AB>@88, 0 =0 420 8;8 1>;55 10@>2 <5=LH5

   if (prevCalculated < ratesTotal - 1)                     

   {       

      BuffersInitializeAll();

      return (total);

   }

   

   // >@<0;L=>5 @0728B85 8AB>@88. >;8G5AB2> 10@>2 B5:CI53> B8:0 >B;8G05BAO >B :>;8G5AB20 10@>2 ?@54K4CI53> B8:0 =5 1>;LH5, G5< =0 >48= 10@

   return (MathMin(ratesTotal - prevCalculated, total));                            

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| >;LH5 ;8 ?5@2>5 G8A;>, G5< 2B>@>5?                                                                                                                                                               |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsFirstMoreThanSecond(double first, double second)

{

   return (first - second > Point / 10);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//|  02=K ;8 A@02=8205<K5 25;8G8=K?                                                                                                                                                                   |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsValuesEquals(double first, double second)

{

   return (MathAbs(first - second) < Point / 10);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| 'B5=85 >4=>3> B8:0 87 D09;0                                                                                                                                                                       |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsReadTimeAndBidAskOfTick(int hTicksFile, TickStruct &tick)

{

   if (FileIsEnding(hTicksFile))

   {

      FileClose(hTicksFile);

      return false;

   }

   

   uint bytesCnt = FileReadStruct(hTicksFile, tick);

   if (bytesCnt == sizeof(TickStruct))

      return true;

   

   FileClose(hTicksFile);

   return false;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| 1@01>B:0 >4=>3> B8:0                                                                                                                                                                             |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void ProcessOneTick(int index, TickStruct &curTick, TickStruct &lastTick)

{

   // 1=0@C65= @>AB F5=K

   if (IsFirstMoreThanSecond(curTick.ask, lastTick.ask))

   {

      if (i_useTicksAtPrice == POINTS)

      {

         if (lastTick.ask > 0.0)

            g_bullBuffer[index] += MathRound((curTick.ask - lastTick.ask) / g_tickSize);

      }

      else

         g_bullBuffer[index]++;

   }



   // 1=0@C65=> ?045=85 F5=K

   if (IsFirstMoreThanSecond(lastTick.bid, curTick.bid))

   {

      if (i_useTicksAtPrice == POINTS)

      {

         if (lastTick.bid > 0.0)

            g_bearBuffer[index] -= MathRound((lastTick.bid - curTick.bid) / g_tickSize);

      }

      else

         g_bearBuffer[index]--;

   }

      

   lastTick = curTick;

    

   // "8:>2 @>AB0 F5=K 1>;LH5, G5< B8:>2 ?045=8O F5=K    

   if (g_bullBuffer[index] >= MathAbs(g_bearBuffer[index]))

   {

      g_bearPrevalenceBuffer[index] = 0;

      g_bullPrevalenceBuffer[index] = g_bullBuffer[index] + g_bearBuffer[index];

      return;

   }



   // "8:>2 ?045=8O F5=K 1>;LH5, G5< B8:>2 @>AB0 F5=K    

   if (MathAbs(g_bearBuffer[index]) > g_bullBuffer[index])

   {

      g_bullPrevalenceBuffer[index] = 0;

      g_bearPrevalenceBuffer[index] = g_bearBuffer[index] + g_bullBuffer[index];

   }

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| B>1@065=85 40==KE ?> 8AB>@8G5A:8< 10@0<, =0G8=0O A C:070==>3>                                                                                                                                    |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void ProcessOldCandles(int limit, TickStruct &lastTick)

{

   // B:@KB85 D09;0 B8:>2>9 8AB>@88

   int hTicksFile = FileOpen(Symbol() + ".tks", FILE_BIN | FILE_READ | FILE_SHARE_READ | FILE_SHARE_WRITE);

   if (hTicksFile < 1)

      return;

      

   // >8A: ?5@2>3> B8:0, ?@8=04;560I53> 10@C limit 8;8 ;N1><C 1>;55 ?>74=5<C 10@C

   TickStruct tick = {0, 0, 0};

   while (!IsStopped())

   {

      if (!IsReadTimeAndBidAskOfTick(hTicksFile, tick))

         return;

         

      if (tick.time >= Time[limit])

         break;

   }

   lastTick = tick;

   int barIndex = iBarShift(NULL, 0, tick.time);

   

   // B>1@065=85 40==KE

   while (barIndex >= 0)

   {

      if (!IsReadTimeAndBidAskOfTick(hTicksFile, tick))

         return;

      

      if (tick.time >= Time[barIndex] + PeriodSeconds())

         barIndex = iBarShift(NULL, 0, tick.time);

         

      ProcessOneTick(barIndex, tick, lastTick);

   }

   

   FileClose(hTicksFile);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| >102;5=85 2 >:=> >B>1@065=8O 8=48:0B>@0 40==KE, =0:>?;5==KE @0=55 2> 2@5<O @01>BK 8=48:0B>@0                                                                                                     |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void ProcessBufferTicks(int limit, TickStruct &lastTick)

{

   int total = ArraySize(g_ticks), i = 0;

   datetime limitTime = Time[limit];

   

   if (lastTick.time > 0)

      limitTime = lastTick.time + 1;



   // >8A: 40==KE 2 1CD5@5, 2@5<O :>B>@KE 1>;LH5 8;8 @02=> 2@5<5=8 >B:@KB8O 10@0 limit, 8;8 1>;LH5, G5< 40==K5 > ?>A;54=5< ?@>G8B0==>< 87 D09;0 B8:5

   while (i < total && g_ticks[i].time < limitTime)

      i++;

      

   if (i >= total)                                                                              // 5B 40==KE 4;O >B>1@065=8O

      return;

      

   // >?8@>20=85 40==KE 2 1CD5@0 8=48:0B>@0

   while (i < total)

   {

      int barIndex = iBarShift(NULL, 0, g_ticks[i].time);

      ProcessOneTick(barIndex, g_ticks[i], lastTick);

      i++;  

   }

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| 0?8AL 40==KE > B8:5 2 <0AA82 g_ticks                                                                                                                                                             |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool IsUpdateTicksArray(TickStruct &tick)

{

   int total = ArraySize(g_ticks);

   if (ArrayResize(g_ticks, total + 1, 100) < 0)

   {

      Alert(WindowExpertName(), ": 8=48:0B>@C =5 E20B05B ?0<OB8 4;O A>E@0=5=8O 40==KE >1 >G5@54=>< B8:5.");

      return false;

   }

   

   g_ticks[total] = tick;

   return true;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| >102;5=85 B8:0 2 <0A82 =>2KE B8:>2                                                                                                                                                               |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

bool AddNewTick(TickStruct &tick)

{

   tick.time = TimeCurrent();

   tick.bid = Bid;

   tick.ask = Ask;

   

   // >102;5=85 >4=>3> B8:0 2 <0AA82 E@0=5=8O B8:>2   

   if (IsUpdateTicksArray(tick))

      return true;



   g_activate = false;

   return false;

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| B>1@065=85 40==KE 8=48:0B>@0                                                                                                                                                                     |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

void ShowIndicatorData(int limit, int total)

{

   static TickStruct lastTick = {0, 0, 0};



   if (limit > 0)

   {

      if (limit > 1)

      {

         lastTick.time = 0;

         lastTick.bid = 0;

         lastTick.ask = 0;

      }

      ProcessOldCandles(limit, lastTick);

      ProcessBufferTicks(limit, lastTick);

   }

         

   TickStruct tick = {0, 0, 0};

   if (!AddNewTick(tick))

      return;

      

   ProcessOneTick(0, tick, lastTick);

}

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

//| Custom indicator iteration function                                                                                                                                                               |

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime& time[],

                const double& open[],

                const double& high[],

                const double& low[],

                const double& close[],

                const long& tick_volume[],

                const long& volume[],

                const int& spread[])

{

   if (!g_activate)                                                                                // A;8 8=48:0B>@ =5 ?@>H5; 8=8F80;870F8N, B> @01>B0BL >= =5 4>;65=

      return rates_total;                                 

                                                   

   int total;   

   int limit = GetRecalcIndex(total, rates_total, prev_calculated);                                // ! :0:>3> 10@0 =0G8=0BL >1=>2;5=85?



   ShowIndicatorData(limit, total);                                                                // B>1@065=85 40==KE 8=48:0B>@0

   

   return(rates_total);

}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---