s_Sort

This script is designed to analyze and organize recent market price data within the MetaTrader platform. Its primary function is to take a specific set of historical price points and arrange them into a structured, descending order (from highest to lowest).

Data Collection

The process begins by identifying the closing prices of the most recent 10 candles on the current chart. The script captures these values and stores them in a list, maintaining their original chronological sequence. At this initial stage, the data is "unsorted," meaning it reflects the random fluctuations of the market as they occurred over time.

Organizational Logic

To ensure the data is handled accurately without losing its context, the script creates two parallel systems:

  1. The Price List: A collection of the actual price values.
  2. The Index List: A corresponding set of labels that tracks the original position of each price.

By maintaining these two lists simultaneously, the tool ensures that even when prices are moved around to be organized, the script "remembers" where each price originally sat in the timeline.

Sorting Process

The core logic utilizes a comparison method to reorder the data. The script systematically compares every price in the list against every other price. Whenever it identifies a price that is lower than another, it swaps their positions within the list. This continues until the highest prices are moved to the front and the lowest prices move to the back.

Final Output

Once the sorting is complete, the script outputs the results to the platform's journal. It displays:

  • The Original State: A preview of the prices in their raw, chronological order.
  • The Sorted State: The final organized list, showing both the new ranked positions and the original historical indices.

This logic provides a clear view of the price range over the last 10 periods, highlighting the extremes (highest and lowest) while preserving the data's integrity.

13 Views
0 Downloads
0 Favorites
s_Sort
//+------------------------------------------------------------------+
//|                                                       s_Sort.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   double PriceArray[];//áóäåì ñîðòèðîâàòü ýòîò ìàññèâ
   ArrayResize(PriceArray,10);//ìàññèâ áóäåò ñîñòîÿòü èç 10-è ýëåìåíòîâ

   //çàïîëíèì ìàññèâ çíà÷åíèÿ öåí çàêðûòèÿ ñ ïîñëåäíèõ 10 áàðîâ
   for(int i=0;i<ArraySize(PriceArray);i++){
       PriceArray[i]=Close[i];  
   }
   
   //èìååì íåñîðòèðîâàíûé ìàññèâ PriceArray[]
   
   //âûâîäèì â æóðíàë íåñîðòèðîâàííûé ìàññèâ
   Print("Íåñîðòèðîâàííûé:");
   for(i=0;i<ArraySize(PriceArray);i++){
      Print(i,"-",PriceArray[i]);
   }   
   

   //ñîçäàäèì ìàññèâ äëÿ èíäåêñòîâ
   int IndexArray[];
   
   ArrayResize(IndexArray,ArraySize(PriceArray));//ìàññèâ äëÿ èíäåêñîâ äîëæåí ñîñòîÿòü èç òàêîãî æå êîëè÷åñòâà ýëåìåíòîâ êàê è ñîðòèðóåìûé ìàññèâ

   //çàïîëíÿåì ìàññèâ äëÿ èíäåêñîâ èíäåêñàìè
   
   for(i=0;i<ArraySize(IndexArray);i++){
       IndexArray[i]=i;  
   }   

   //äâå âðåìåííûå ïåðåìåííûõ, êîòîðûå ïîòðåáóþòñÿ ïðè îáìåíå çíà÷åíèé ñîðòèðóåìûõ ìàññèâîâ
   double tmpPriceArray;
   int tmpIndexArray;

   //ñîðòèðóåì
   
   for(i=0;i<ArraySize(PriceArray);i++){
      for(int j=0;j<ArraySize(PriceArray);j++){
         //äëÿ ñîðòèðîâêè â 
         //îáðàòíîì ïîðÿäêå 
         // ïîñòàâèòü   ">" 
         if(ND(PriceArray[j])<ND(PriceArray[i])){
            tmpPriceArray=PriceArray[j];
            tmpIndexArray=IndexArray[j];
            PriceArray[j]=PriceArray[i];
            IndexArray[j]=IndexArray[i];
            PriceArray[i]=tmpPriceArray;
            IndexArray[i]=tmpIndexArray;            
         }
      }   
   }
  
   //âûâîäèì â æóðíàë ñîðòèðîâàííûé ìàññèâ
   Print("Ñîðòèðîâàííûé:");
   for(i=0;i<ArraySize(PriceArray);i++){
      Print(IndexArray[i],"-",PriceArray[i]);
   }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

double ND(double v){
   return(NormalizeDouble(v,Digits));
}

Comments