Try
Download scriptThis script is designed to perform a side-by-side verification of price calculation methods within the MetaTrader platform. Its primary purpose is to ensure that the mathematical "Simple Moving Average" (SMA) remains consistent whether it is calculated using the platform's built-in tools or through a manual calculation method.
The logic follows three distinct paths to reach the same financial conclusion:
1. Manual Data Collection and Calculation The script first creates a small temporary list of the most recent closing prices from the market. It then performs a manual calculation by adding these prices together and dividing them by nine. This represents a "ground-up" approach where the computer is told exactly which numbers to add and how to divide them.
2. Built-in Platform Indicator Simultaneously, the script requests the standard Moving Average value directly from the trading platform's internal library. This uses the software's native formula to determine what the average price should be based on the current market data.
3. Array-Based Calculation The script also calculates the average using a specific "array" method. In this scenario, it takes the list of prices it collected in step one and asks the system to run an averaging calculation specifically on that set of numbers rather than on the live market feed.
The Objective By performing all three calculations at once, the script prints out three different values for you to see. The goal is transparency: if the logic is sound, the result from the manual math, the built-in platform tool, and the array calculation should all align perfectly. It serves as a diagnostic check to confirm that the data being used for trading decisions is accurate across different mathematical approaches.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| trade.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#include <stdlib.mqh>
#include <WinUser32.mqh>
//+------------------------------------------------------------------+
//| script "trading for all money" |
//+------------------------------------------------------------------+
int start()
{
//----
double testarray[10];
double manualSMA=0;
for (int cnt=1; cnt<10; cnt++)
{
testarray[cnt-1]=Close[cnt];
manualSMA=manualSMA+Close[cnt];
//Print (testarray[cnt-1]);
}
manualSMA=manualSMA/9;
Print("IMA: ",DoubleToStr(iMA(NULL,0,9,0,MODE_SMA,PRICE_CLOSE,1),8));
Print("IMAonarray: ",DoubleToStr(iMAOnArray(testarray,0,9,0,MODE_SMA,0),8));
Print("manualSMA: ",DoubleToStr(manualSMA,8));
return(0);
}
//+------------------------------------------------------------------+
Comments