Okay, here's a breakdown of what this MetaTrader script does, explained in a way that avoids technical jargon and is easy to understand:
The script is designed to automatically trade on the Forex market. It uses several technical indicators to identify potential buying and selling opportunities and then automatically opens and closes trades based on these signals.
Here's a more detailed breakdown:
1. Initial Setup (External Parameters):
- The script begins by defining a set of customizable settings, called "external parameters." These settings allow a user to tailor the script's behavior to their preferences and the specific market conditions. Key parameters include:
- Lot Size: The amount of currency to trade in each transaction.
- Take Profit: The profit level at which a trade will automatically close to secure gains. Expressed in points.
- Stop Loss: The loss level at which a trade will automatically close to limit potential losses. Expressed in points.
- Step: The distance needed between orders.
- Moving Averages: Parameters defining several Moving Averages.
- Ichimoku Kinko Hyo: Parameters for the Ichimoku Kinko Hyo.
- MACD (Moving Average Convergence Divergence): Parameters for multiple MACD indicators.
2. Data Preparation and Validation:
- The script checks if enough historical price data is available (at least 100 "bars" worth). If not, it stops running.
- It also validates the "Take Profit" setting, ensuring it's a reasonable value. If the Take Profit is very small, it stops.
3. Technical Indicators:
- The script calculates several technical indicators based on historical price data:
- Moving Averages: It uses a filter with the "EMA" which stands for Exponential Moving Average. And multiple SMMA's which stands for Smoothed Moving Average calculated for different time periods ("blue," "red," and "lime"). These averages help identify trends and potential support/resistance levels.
- Ichimoku Kinko Hyo: This indicator provides a comprehensive view of potential support and resistance areas and trend direction.
- MACD: The script utilizes three MACD indicators, each with different settings. MACD helps identify changes in momentum and potential buy/sell signals.
4. Closing Existing Orders:
- Before opening new trades, the script checks if there are any open orders. Based on certain conditions, it may automatically close these orders to protect profits. These conditions are based on comparing the "FMA" filter with the SMMA's. Also only sells profitables orders.
5. Historical Trade Analysis:
- The script examines the recent trading history to determine if certain orders have already been closed and, if so, what the outcome (profit or loss) was. This information is used to reset flags and potentially close other related orders if certain conditions are met. It only check the last 20 trades.
6. Identifying Buy and Sell Signals:
- The core of the script lies in identifying potential trading signals. It does this by:
- Defining several "buy signals" based on the values of the technical indicators. For example, if the "FMA" filter is above the "blue" and "lime" Moving Averages and the price is above the Ichimoku Cloud, it considers this a potential buy signal. It requires that signal 1 and 5 are true.
- Similarly, it defines "sell signals" based on the opposite conditions. For example, if the "FMA" filter is below the "blue" and "lime" Moving Averages and the price is below the Ichimoku Cloud, it considers this a potential sell signal. It requires that signal 1 and 5 are true.
7. Opening Trades:
- Based on the identified buy and sell signals, the script automatically opens new trades:
- If a buy signal is triggered and a trade isn't already open, it opens a "buy" order.
- If a sell signal is triggered and a trade isn't already open, it opens a "sell" order.
- The script opens at most 3 buy and 3 sell orders.
- It increases the Lot each order.
- Each order will have a take profit and stop loss, both configurable by the user.
- If the price moves against the first order by the specified number of "Step", the script opens another order and so on for the other.
- Every open order is tracked, using the order ticket.
In Summary: This script is an automated trading system that uses a combination of moving averages, the Ichimoku Kinko Hyo indicator, and MACD to generate buy and sell signals. It then automatically opens and closes trades based on these signals, with the goal of profiting from price movements in the Forex market. The user has a range of customizable settings to control the script's behavior and risk tolerance.
//+---------------------------------------------------------+
//| Mc_valute_v8_final.mq4 |
//| Copyright © 2007, Daniil |
//+---------------------------------------------------------+
#property copyright "Copyright © 2007 Daniil"
#property link "simple_jet@mail.ru"
extern string m1="Âûáîð ëîòà ñòîïà è ïðîôèòà";
extern double TakeProfit1 = 300;
extern double TakeProfit = 30;
extern double Stop = 350;
extern double Step = 35;
extern double Lot = 0.1;
extern string m2="Ïàðàìåòðû ñðåäíèõ:";
extern double FilterMA=3;
extern string m3="Ïàðàìåòðû blue:";
extern double period_blue=13;
extern double shift_blue=8;
extern string m4="Ïàðàìåòðû red:";
extern double period_red=8;
extern double shift_red=5;
extern string m5="Ïàðàìåòðû lime:";
extern double period_lime=5;
extern double shift_lime=3;
extern string m10="Ïàðàìåòðû èøèìîêó:";
extern int tenkan_sen = 12;
extern int kijun_sen = 20;
extern int senkou_span_b = 40;
extern string m6="Ïàðàìåòðû MACD:";
extern string m7="MACD #1:";
extern double a1=12;
extern double d1=26;
extern double f1=9;
extern string m8="MACD #2:";
extern double a2=33;
extern double d2=68;
extern double f2=15;
extern string m9="MACD #3:";
extern double a3=33;
extern double d3=68;
extern double f3=15;
double OpenPrice_buy1, OpenPrice_sell1;
double OpenPrice_buy2, OpenPrice_sell2;
double OpenPrice_buy3, OpenPrice_sell3;
double OpenPrice_buy4, OpenPrice_sell4;
int i = 0;
int i2 = 0;
int b1,b2,b3,b4;
int s1,s2,s3,s4;
int c1,c2,c3,c4;
int e1,e2,e3,e4;
//------------------------------=========================<<<<< Start >>>>>=======================------------------------\\
int start()
{
int cnt,ticket,total;
double SMMA_blue,
SMMA_red,
SMMA_lime,
FMA,
FMAprev;
//--- èøèìîêó
double Tenkan_sen;
double Kijun_sen;
double Senkou_Span_A;
double Senkou_Span_B;
double Chinkou_Span;
double MacdCurrent1,
MacdPrevious1,
SignalCurrent1,
SignalPrevious1;
double MacdCurrent2,
MacdPrevious2,
SignalCurrent2,
SignalPrevious2;
double MacdCurrent3,
MacdPrevious3,
SignalCurrent3,
SignalPrevious3;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<2)
{
Print("TakeProfit less than 200");
return(0); // check TakeProfit
}
//--- ôèëüòð
FMA=iMA(NULL,0,FilterMA,0,MODE_EMA,PRICE_CLOSE,0);
FMAprev=iMA(NULL,0,FilterMA,0,MODE_EMA,PRICE_CLOSE,1);
//--- Ñðåäíèå
SMMA_blue=iMA(NULL,0,period_blue,shift_blue,MODE_SMMA,PRICE_MEDIAN,0);
SMMA_red=iMA(NULL,0,period_red,shift_red,MODE_SMMA,PRICE_MEDIAN,0);
SMMA_lime=iMA(NULL,0,period_lime,shift_lime,MODE_SMMA,PRICE_MEDIAN,0);
//--- MACD_1 ñèãíàë - ëèíèÿ
MacdCurrent1=iMACD(NULL,0,a1,d1,f1,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious1=iMACD(NULL,0,a1,d1,f1,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent1=iMACD(NULL,0,a1,d1,f1,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious1=iMACD(NULL,0,a1,d1,f1,PRICE_CLOSE,MODE_SIGNAL,1);
//--- MACD_2
MacdCurrent2=iMACD(NULL,0,a2,d2,f2,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious2=iMACD(NULL,0,a2,d2,f2,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent2=iMACD(NULL,0,a2,d2,f2,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious2=iMACD(NULL,0,a2,d2,f2,PRICE_CLOSE,MODE_SIGNAL,1);
//--- MACD_3
MacdCurrent3=iMACD(NULL,0,a3,d3,f3,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious3=iMACD(NULL,0,a3,d3,f3,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent3=iMACD(NULL,0,a3,d3,f3,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious3=iMACD(NULL,0,a3,d3,f3,PRICE_CLOSE,MODE_SIGNAL,1);
//--- èèøèìîêó
Tenkan_sen=iIchimoku(NULL,0,tenkan_sen,kijun_sen,senkou_span_b,MODE_TENKANSEN,0);
Kijun_sen=iIchimoku(NULL,0,tenkan_sen,kijun_sen,senkou_span_b,MODE_KIJUNSEN,0); // ëèíèÿ ñîïðîòèâëåíèÿ
Senkou_Span_A=iIchimoku(NULL,0,tenkan_sen,kijun_sen,senkou_span_b,MODE_SENKOUSPANA,0);
Senkou_Span_B=iIchimoku(NULL,0,tenkan_sen,kijun_sen,senkou_span_b,MODE_SENKOUSPANB,0);
Chinkou_Span=iIchimoku(NULL,0,tenkan_sen,kijun_sen,senkou_span_b,MODE_CHINKOUSPAN,kijun_sen);
//---------------------------=====================<<<<<< Close orders >>>>>>====================---------------------------\\
int ototal=OrdersTotal();
for(i2=ototal; i2>=0; i2--)
{
if (OrderSelect(i2,SELECT_BY_POS,MODE_TRADES)==true)
{
if (FMA<MathMax(SMMA_blue, SMMA_lime) && OrderType()==OP_BUY && OrderProfit()>0)
{
if (OrderTicket()==b1)// && c1==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_buy1=0;
// c1=1;
continue;
}
/* if (OrderTicket()==b2)// && c2==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_buy2=0;
// c2=1;
continue;
}
if (OrderTicket()==b3)// && c3==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_buy3=0;
// c3=1;
continue;
}
*/
}
if (FMA>MathMin(SMMA_blue, SMMA_lime) && OrderType()==OP_SELL && OrderProfit()>0)
{
if (OrderTicket()==s1)// && e1==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_sell1=0;
// e1=1;
continue;
}
/*
if (OrderTicket()==s2)// && e2==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_sell2=0;
// e2=1;
continue;
}
if (OrderTicket()==s3)// && e3==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_sell3=0;
// e3=1;
continue;
}
*/
}
}
}
//-----------------------------====================<<<<< Work history >>>>>======================----------------------------\\
// retrieving info from trade history
int accTotal=OrdersHistoryTotal();
int n=0;
if ( accTotal>20){n =accTotal-20;}
for(i=accTotal-1; i>=n; i--)
{
//---- check selection result
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
{
//-----buy
if (OrderCloseTime()>0 && OrderType()==OP_BUY && OrderTicket()==b1 && OrderProfit()>0)
{
OpenPrice_buy1=0;
}
if (OrderCloseTime()>0 && OrderType()==OP_BUY && OrderTicket()==b2 && OrderProfit()>0)
{
OpenPrice_buy2=0;
}
if (OrderCloseTime()>0 && OrderType()==OP_BUY && OrderTicket()==b3 && OrderProfit()>0)
{
OpenPrice_buy3=0;
for(cnt=0; cnt<=OrdersTotal()-1; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderTicket()==b1 && c1==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_buy1=0;
c1=1;
}
}
}
for(cnt=0; cnt<=OrdersTotal()-1; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderTicket()==b2 && c2==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_buy2=0;
c2=1;
}
}
}
}
if (OrderCloseTime()>0 && OrderType()==OP_BUY && OrderTicket()==b3 && OrderProfit()<0)
{
OpenPrice_buy3=0;
for(cnt=0; cnt<=OrdersTotal()-1; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderTicket()==b1 && c1==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_buy1=0;
c1=1;
}
}
}
for(cnt=0; cnt<=OrdersTotal()-1; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderTicket()==b2 && c2==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_buy2=0;
c2=1;
}
}
}
}
//-----sell
if (OrderCloseTime()>0 && OrderType()==OP_SELL && OrderTicket()==s1 && OrderProfit()>0)
{
OpenPrice_sell1=0;
}
if (OrderCloseTime()>0 && OrderType()==OP_SELL && OrderTicket()==s2 && OrderProfit()>0)
{
OpenPrice_sell2=0;
}
if (OrderCloseTime()>0 && OrderType()==OP_SELL && OrderTicket()==s3 && OrderProfit()>0)
{
OpenPrice_sell3=0;
for(cnt=0; cnt<=OrdersTotal()-1; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderTicket()==s1 && e1==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_sell1=0;
e1=1;
}
}
}
for(cnt=0; cnt<=OrdersTotal()-1; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderTicket()==s2 && e2==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_sell2=0;
e2=1;
}
}
}
}
if (OrderCloseTime()>0 && OrderType()==OP_SELL && OrderTicket()==s3 && OrderProfit()<0)
{
OpenPrice_sell3=0;
for(cnt=0; cnt<=OrdersTotal()-1; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderTicket()==s1 && e1==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_sell1=0;
e1=1;
}
}
}
for(cnt=0; cnt<=OrdersTotal()-1; cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderTicket()==s2 && e2==0)
{
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
OpenPrice_sell2=0;
e2=1;
}
}
}
}
}
}
//-------------------------------====================<<<<< Signals >>>>>===================-------------------------------\\
//-----buy signals
//#1
bool buy_signal_1=false;
if (FMA>MathMax(SMMA_blue, SMMA_lime))
buy_signal_1=true;
//#2
bool buy_signal_2=false;
if (MacdCurrent1>SignalCurrent1)
buy_signal_2=true;
//#3
bool buy_signal_3=false;
if (MacdCurrent2>SignalCurrent2)
buy_signal_3=true;
//#4
bool buy_signal_4=false;
if (MacdCurrent3>SignalCurrent3)
buy_signal_4=true;
//#5
bool buy_signal_5=false;
// if(Ask>MathMax(Senkou_Span_B, Senkou_Span_A) && Open[0]<MathMax(Senkou_Span_B, Senkou_Span_A))
// if (((MathAbs(Open[1]+Close[1])/2+Ask)/2)>MathMax(Senkou_Span_B, Senkou_Span_A))
if (FMA>MathMax(Senkou_Span_B, Senkou_Span_A))
//if (((Open[1]+Close[1]+Open[2]+Close[2]+Open[3]+Close[3])/6)<MathMax(Senkou_Span_B, Senkou_Span_A) && FMA>MathMax(Senkou_Span_B, Senkou_Span_A))
buy_signal_5=true;
//-----sell signals
//#1
bool sell_signal_1=false;
if (FMA<MathMin(SMMA_blue, SMMA_lime))
sell_signal_1=true;
//#2
bool sell_signal_2=false;
if (MacdCurrent1<SignalCurrent1)
sell_signal_2=true;
//#3
bool sell_signal_3=false;
if (MacdCurrent2<SignalCurrent2)
sell_signal_3=true;
//#4
bool sell_signal_4=false;
if (MacdCurrent3<SignalCurrent3)
sell_signal_4=true;
//#5
bool sell_signal_5=false;
// if(Ask<MathMin(Senkou_Span_B, Senkou_Span_A) && Open[0]>MathMin(Senkou_Span_B, Senkou_Span_A))
// if (((MathAbs(Open[1]+Close[1])/2+Ask)/2)<MathMin(Senkou_Span_B, Senkou_Span_A))
if (FMA<MathMin(Senkou_Span_B, Senkou_Span_A))
// if (((Open[1]+Close[1]+Open[2]+Close[2]+Open[3]+Close[3])/6)>MathMin(Senkou_Span_B, Senkou_Span_A) && FMA<MathMin(Senkou_Span_B, Senkou_Span_A))
sell_signal_5=true;
//---------------------------=====================<<<<< Open Buy >>>>>===================---------------------------------\\
//#1
if(OpenPrice_buy1==0 && buy_signal_1==true && buy_signal_5==true)// && buy_signal_2==true)// && buy_signal_3==true && buy_signal_4==true )
{
ticket=OrderSend(Symbol(),OP_BUY,0.1,Bid,5,0,Bid+TakeProfit1*Point,"priceEX",16384,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OpenPrice_buy1=OrderOpenPrice();
b1=OrderTicket();
// Print("Open1=",OpenPrice_buy1);
}
// return(0);
}
//#2
if((OpenPrice_buy1-Bid)>=Step*Point && OpenPrice_buy2==0 && OpenPrice_buy1!=0)
{
// Print("Span",OpenPrice_buy1);
ticket=OrderSend(Symbol(),OP_BUY,0.2,Bid,5,0,Bid+TakeProfit*Point,"priceEX",16384,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OpenPrice_buy2=OrderOpenPrice();
b2=OrderTicket();
}
// return(0);
}
//#3
if((OpenPrice_buy2-Bid)>=Step*Point && OpenPrice_buy3==0 && OpenPrice_buy2!=0)
{
// Print("Span",OpenPrice_buy1);
ticket=OrderSend(Symbol(),OP_BUY,0.3,Bid,5,Bid-Stop*Point,Bid+TakeProfit*Point,"priceEX",16384,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OpenPrice_buy3=OrderOpenPrice();
b3=OrderTicket();
c1=0;
c2=0;
}
// return(0);
}
//----------------------------====================<<<<< Open Sell >>>>>=====================------------------------------\\
//#1
if (OpenPrice_sell1==0 && sell_signal_1==true && sell_signal_5==true )//&& sell_signal_2==true)// && sell_signal_3==true && sell_signal_4==true)
{
ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,5,0,Bid-TakeProfit1*Point,"priceEX",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OpenPrice_sell1=OrderOpenPrice();
s1=OrderTicket();
// return(0);
}
}
//#2
if ((Bid-OpenPrice_sell1)>=Step*Point && OpenPrice_sell2==0 && OpenPrice_sell1!=0)
{
ticket=OrderSend(Symbol(),OP_SELL,0.2,Bid,5,0,Bid-TakeProfit*Point,"priceEX",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OpenPrice_sell2=OrderOpenPrice();
s2=OrderTicket();
// return(0);
}
}
//#3
if ((Bid-OpenPrice_sell2)>=Step*Point && OpenPrice_sell3==0 && OpenPrice_sell2!=0)
{
ticket=OrderSend(Symbol(),OP_SELL,0.3,Bid,5,Bid+Stop*Point,Bid-TakeProfit*Point,"priceEX",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print ("device");
OpenPrice_sell3=OrderOpenPrice();
s3=OrderTicket();
e1=0;
e2=0;
// return(0);
}
}
// Print ("OOOOOOOOOOO3333",OpenPrice_sell3);
return(0);
}
// return(0);
Comments