This script is an automated trading program designed for the MetaTrader platform. It's designed to automatically buy and sell a particular currency pair based on a calculated value derived from price movements and volume.
Here's a breakdown of the script's logic:
-
Initialization:
- It starts by defining some basic settings like a "magic number" (MAGICMA) to identify orders placed by this specific script and some external parameters that can be adjusted by the user:
- PERIOD: A length of time used in a moving average calculation
- MODE: A type of moving average
- RISK: The percentage of the account balance the script is allowed to risk on any single trade.
- MAXORDERS: The maximum number of trades the script can have open at the same time.
- It starts by defining some basic settings like a "magic number" (MAGICMA) to identify orders placed by this specific script and some external parameters that can be adjusted by the user:
-
Lot Size Calculation:
- The script has a function to calculate the optimal trade size ("LotsOptimized"). This function determines how many units of the currency to trade based on the account balance, risk percentage, available margin (funds available for trading), and the maximum and minimum allowed trade sizes set by the broker. It makes sure the trade size doesn't exceed what the account can handle or what the broker allows.
-
Main Trading Logic:
- The
start()
function contains the core logic that runs repeatedly. - It first defines several variables to be used in the subsequent calculations.
- Moving Average Calculation: The script uses several moving averages (high, low, open, close) over the time period set by the PERIOD variable.
- GO Calculation: The script then calculates a "GO" value which is a combination of the difference between the open and close prices, the high and open prices, the low and open prices, the close and low prices and the close and high prices, multiplied by the volume of the latest candle.
- Closing Existing Orders:
- If the "GO" value is negative, the script iterates through all open orders. If it finds any open buy orders for the specified currency pair placed by this script, it closes them.
- If the "GO" value is positive, the script iterates through all open orders. If it finds any open sell orders for the specified currency pair placed by this script, it closes them.
- Opening New Orders:
- The script checks that either GO is above zero or GO is below zero and also makes sure that a special time-stamp variable is not current. The TimeBuySell global variable is a means of checking that a trade has not already been placed. Finally, it makes sure that the maximum number of orders set by the MAXORDERS variable has not been reached.
- It then checks if the account has enough funds to place a new trade, given the calculated lot size.
- If the "GO" value is positive, the script places a buy order for the currency pair, using the calculated lot size.
- If the "GO" value is negative, the script places a sell order for the currency pair, using the calculated lot size.
- After attempting to place an order, the script stores a timestamp, the current time, in the global variable Symbol()MAGICMATimeBuySell. This stops an order from being placed in successive iterations of the start() function, until this timestamp is changed in a later iteration.
- The
In summary, this script calculates a value based on price and volume, and uses this value to decide whether to buy or sell a currency pair. It manages risk by calculating an appropriate trade size and limiting the number of open trades. It aims to close out any existing positions which are now contrary to the position determined by the GO value.
//+------------------------------------------------------------------+
//| GO.mq4 |
//| Copyright © 2006, Victor Chebotariov |
//| http://www.chebotariov.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Victor Chebotariov"
#property link "http://www.chebotariov.com/"
#define MAGICMA 30072006
int PERIOD = 174;
int MODE = 0;
extern double RISK = 30.0;
extern int MAXORDERS = 5;
//+------------------------------------------------------------------+
//| Ðàñ÷åò îïòèìàëüíîãî îáúåìà ëîòà |
//+------------------------------------------------------------------+
double LotsOptimized()
{
//----
double Lots=NormalizeDouble(AccountBalance()*RISK/100000.0,1);
if(AccountFreeMargin()<(1000*Lots)){Lots=NormalizeDouble(AccountFreeMargin()*RISK/100000.0,1);}
if(Lots>MarketInfo(Symbol(),MODE_MAXLOT)){Lots=MarketInfo(Symbol(),MODE_MAXLOT);}
if(Lots<MarketInfo(Symbol(),MODE_MINLOT)){Lots=MarketInfo(Symbol(),MODE_MINLOT);}
//----
return(Lots);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//+------------------------------------------------------------------+
//if(!IsDemo()){return(0);} // óäàëèòü äëÿ ðàáîòû íà ðåàëüíîì ñ÷åòå |
//+------------------------------------------------------------------+
int cnt,ticket,MODEMA;
int total=OrdersTotal();
if(MODE==0){MODEMA=MODE_SMA;}
if(MODE==1){MODEMA=MODE_EMA;}
if(MODE==2){MODEMA=MODE_SMMA;}
if(MODE==3){MODEMA=MODE_LWMA;}
HideTestIndicators(true);
double high =iMA(NULL,0,PERIOD,0,MODEMA,PRICE_HIGH,0);
double low =iMA(NULL,0,PERIOD,0,MODEMA,PRICE_LOW,0);
double open =iMA(NULL,0,PERIOD,0,MODEMA,PRICE_OPEN,0);
double close=iMA(NULL,0,PERIOD,0,MODEMA,PRICE_CLOSE,0);
double GO=((close-open)+(high-open)+(low-open)+(close-low)+(close-high))*Volume[0];
HideTestIndicators(false);
if(GO<0)
{
for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);return(0);}
}
}
if(GO>0)
{
for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);return(0);}
}
}
if((GO>0 || GO<0) && (!GlobalVariableCheck("Symbol()"+"MAGICMA"+"TimeBuySell") || GlobalVariableGet("Symbol()"+"MAGICMA"+"TimeBuySell")!=Time[0] && total<MAXORDERS))
{
if(AccountFreeMargin()<(1000*LotsOptimized()))
{Print("Ó Âàñ íåäîñòàòî÷íî äåíåã. Ñâîáîäíîé ìàðæè = ", AccountFreeMargin());return(0);}
if(GO>0)
{
ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"MoneyPlus",MAGICMA,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
}
else Print("Error opening BUY order : ",GetLastError());return(0);
}
if(GO<0)
{
ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"MoneyPlus",MAGICMA,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
GlobalVariableSet("Symbol()"+"MAGICMA"+"TimeBuySell",Time[0]);
}
else Print("Error opening SELL order : ",GetLastError());return(0);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments