This script is designed to automatically trade on the foreign exchange market (Forex) using a specific set of rules. It looks at price charts to identify potential buying and selling opportunities. Here's a breakdown:
-
What it does: The script is essentially a robot that watches the Forex market and tries to make profitable trades automatically. It makes decisions based on three things:
- Moving Averages (EMAs): These are lines that smooth out the price data over a specific period, helping to identify trends. The script uses three different EMAs with different periods (10, 25, and 50).
- Parabolic SAR (SAR): This is another indicator that helps identify potential trend reversals and entry/exit points. The script looks at SAR values on the current timeframe and also on a 15-minute timeframe.
- Existing Positions: The script keeps track of open buy and sell trades it has already made.
-
How it works:
- Initialization: When the script starts, it sets up some initial values, like the periods for the EMAs, settings for the SAR indicators, the size of trades it will make, and limits on the maximum number of simultaneous buy and sell trades.
- Main Loop: The script constantly runs in the background, checking the current market conditions.
- Checking for Signals: In each cycle, the script does the following:
- Calculate Indicators: It calculates the values of the three EMAs and the two SAR indicators using the current and historical price data.
- Identify Buy/Sell Conditions: The script then uses a combination of conditions to determine if it should buy or sell. It compares the EMA values to determine the trend and also uses the SAR values.
- Open Orders: If all conditions are met, the script sends a command to the trading platform to open either a buy or sell order for a specific amount of currency. The script also sets stop-loss and take-profit levels. These are price levels at which the trade will be automatically closed to limit losses or secure profits.
- Close Orders: If the EMAs shift to an unfavorable configuration the script will proceed to close existing orders if present.
- Position Management: The script also keeps track of how many open buy and sell orders it has. It will not open new orders if it has already reached the maximum allowed number of positions.
-
Key parameters:
ema1_period
,ema2_period
,ema3_period
: These define the lookback period for calculating the three EMAs, which are used to identify the trend.stepfast
,stepfast15m
,maximumfast
: These are settings for the Parabolic SAR indicator, influencing its sensitivity to price changes.SL
,TP
: These define the Stop Loss and Take Profit levels, which are the number of pips (a unit of price movement) away from the opening price at which the trade will be automatically closed.Lots
: This determines the volume of currency to trade in each order.maxpos
: The script will only have this many positions of each type open, 1 is a simple approach
In summary, this script is an automated trading system that uses moving averages and Parabolic SAR indicators to generate buy and sell signals. It aims to identify trends and potential reversals, opening and closing trades automatically based on predefined rules.
//+------------------------------------------------------------------+
//| FT_Parabolic SAR_EMA.mq4 |
//| FORTRADER.RU, Þðèé, ftyuriy@gmail.com |
//| http://FORTRADER.RU, Parabolic SAR + Ñðåäíèå |
//+------------------------------------------------------------------+
#property copyright "FORTRADER.RU, Þðèé, ftyuriy@gmail.com"
#property link "http://FORTRADER.RU, MACD + Parabolic SAR"
/*Ðàçðàáîòàíî äëÿ 49 âûïóñêà æóðíàëà FORTRADER.Ru. Ñèñòåìà ïî ñðåäíåé è MACD.
Îò÷åòû: http://finfile.ru/index.php/files/get/EYLOCZ45tW/sarma2.zip, http://finfile.ru/index.php/files/get/f_rINNIekN/sarma.zip
Ñåò ôàéëû:http://finfile.ru/index.php/files/get/hKV6K515_n/sar-ma-eurusd1h.set
Îáñóæäåíèå:http://fxnow.ru/group_discussion_view.php?group_id=49&grouptopic_id=269&grouppost_id=2682#post_2682
Àðõèâ æóðíàëà: http://www.fortrader.ru/arhiv.php
49 âûïóñê: http://www.fortrader.ru/
*/
extern int ema1_period = 10 ;
extern int ema2_period = 25 ;
extern int ema3_period = 50 ;
//íàñòðîéêè ïàðàáîëèêà
extern double stepfast=0.02;
extern double stepfast15m=0.02;
extern double maximumfast=0.2;
int SL=1500;
int TP=1500;
extern int mn=1;
int err;
extern int MG=564651;
extern double Lots=0.01;
extern int maxpos=1;
int bars;
int start()
{
if(bars!=Bars)
{
bars=Bars;
OpenPattern();
}
return(0);
}
int okbuy,oksell;
int OpenPattern()
{
double op,sl,tp;
//çàãðóçèì ïàðàáîëèêè
double fastsar=iSAR(NULL,0,stepfast,maximumfast,1);
double fastsar15=iSAR(NULL,15,stepfast15m,maximumfast,1);
double ma1=iMA(NULL,0,ema1_period,0,MODE_EMA,PRICE_CLOSE,1);
double ma2=iMA(NULL,0,ema2_period,0,MODE_EMA,PRICE_CLOSE,1);
double ma3=iMA(NULL,0,ema3_period,0,MODE_EMA,PRICE_CLOSE,1);
if(ma1<ma2 && ma1<ma3){okbuy=1;}
if(ma1>ma2 && ma1>ma3){oksell=1;}
if(oksell==1 && ma1<ma2 && ma1<ma3 && fastsar>Close[1] && fastsar15>Close[1] && CountPos(0)<maxpos )
{
op=Bid;if(SL>0){sl=Bid+SL*Point*mn;}if(TP>0){tp=Bid-TP*Point*mn;}
err=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"4 FORTRADER.RU",MG,0,Red);
if(err<0){Print("OrderSend()- Îøèáêà OP_SELL. op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());return(-1);}
oksell=0;
}
if(okbuy==1 && ma1>ma2 && ma1>ma3 && fastsar<Close[1] && fastsar15<Close[1] && CountPos(1)<maxpos )
{
op=Ask;if(SL>0){sl=Ask-SL*Point*mn;}if(TP>0){tp=Ask+TP*Point*mn;}
err=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"6 FORTRADER.RU",MG,0,Red);
if(err<0){Print("OrderSend()- Îøèáêà OP_BUY. op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());return(-1);}
okbuy=0;
}
if(ma1<ma2 && ma1<ma3 ){_OrderClose(1);}
if(ma1>ma2 && ma1>ma3){_OrderClose(0);}
return(err);
}
//Ïðîâåðÿåì êîëè÷åñâòî ïîçèöèé.
int CountPos(int type)
{//Îïèñàíèå http://fxnow.ru/blog.php?user=Yuriy&blogentry_id=66
int i;
int col;
int count=0 ;
for( i=0; i<=OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && type==1 && OrderMagicNumber()==MG ){count++;}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && type==0 && OrderMagicNumber()==MG ){count++;}
}
}
return(count);
}
int _OrderClose(int type)
{int err;
for(int i=1; i<=OrdersTotal(); i++)
{
if (OrderSelect(i-1,SELECT_BY_POS)==true)
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && type==1 && OrderMagicNumber()==MG )
{
err=OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
if(err<0){Print("OrderClose()- Îøèáêà çàêðûòèÿ OP_BUY. OrderTicket "+OrderTicket()+" OrderLots() "+OrderLots()+" Bid "+Bid+" "+GetLastError());return(-1);}
}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && type==0 && OrderMagicNumber()==MG)
{
err=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
if(err<0){Print("OrderClose()- Îøèáêà çàêðûòèÿ OP_SELL. OrderTicket "+OrderTicket()+" OrderLots() "+OrderLots()+" Ask "+Ask+" "+GetLastError());return(-1);}
}
}
}
return(0);
}
Comments