This script is designed to automate trading in the Forex market using the MetaTrader platform. Here's a breakdown of how it works:
1. Initial Setup & Configuration:
-
The script starts by reading in a set of rules and settings. These settings, defined as "extern," allow you to customize the script's behavior without needing to modify the code itself. These settings include:
- Stochastic Oscillator Parameters: These define how the script uses a technical indicator called the Stochastic Oscillator. (Kperiod, Dperiod, Slowing, Method, PriceUsing)
- Order Size: Maximum amount to risk on a trade (MaxLot) and a factor to calculate the lot size based on the account balance (LotSpliter).
- Profit & Loss Levels: Values for setting target profits (TakeProfit), stop-loss levels to limit potential losses (StopLoss), minimum profit thresholds (MinProfit), and profit levels at which the stop-loss might be adjusted (TrailingStop, ProfitPoints).
- Trading Conditions: Rules that must be met before the script opens or closes a trade (Condition1, Condition2, CloseByOtherSideCondition).
2. Main Operation - The "Start" Function:
- This is the heart of the script, where the magic happens. It's executed repeatedly by the MetaTrader platform to analyze the market and manage trades.
- Checking for Existing Trades: It first checks if any trades are already open.
- If no trades are open, it decides whether to open a new buy or sell order based on the trading conditions (see step 3).
- If a trade is already open, it manages the existing trade, by the stop loss.
3. Determining Buy or Sell Signals (The "U" Functions):
- The script uses two sets of rules, "U1" and "U2", to decide if it should buy or sell.
- U1: This rule set uses the Stochastic Oscillator, a popular technical indicator that measures the momentum of price. It looks for situations where the Stochastic Oscillator suggests the market is either "overbought" (likely to fall) or "oversold" (likely to rise). The parameters specified can impact on this part.
- U2: This rule set uses Fractals, which identify potential reversal points on a chart. It looks for patterns of fractals to indicate an upward or downward trend.
- Combining Rules: The "U" function combines the results of "U1" and "U2" based on whether Condition1 or Condition2 are enabled (set to 1). If either of the conditions are met on one of the two rules, it returns a buy or sell signal.
4. Opening Trades (OrderBuy & OrderSell):
- If the "U" function signals a buy or sell, the script determines the appropriate order size ("Lot") based on your account balance and the specified LotSpliter parameter.
- The script then sends an order to the Forex broker to open a buy or sell position at the current market price. It also sets the stop-loss and take-profit levels based on the configured settings if any.
5. Managing Open Trades:
- If a trade is already open, the script continuously monitors its performance and takes the following actions:
- Trailing Stop: It adjusts the stop-loss level upwards for buy orders or downwards for sell orders as the price moves in a favorable direction. This helps to lock in profits and limit potential losses.
- Profit Target: If the trade reaches a certain profit level ("ProfitPoints") and the current profit is below a minimum level ("MinProfit"), the script closes the trade. This is a profit-taking mechanism.
- Opposite Signal: If the opposite signal is generated by the "U" function based on "CloseByOtherSideCondition", the script closes the existing trade and prepares to open a trade in the opposite direction.
6. Closing Trades (CloseOnlyOrder):
- The script has a function to close the current active trade based on some of the conditions mentioned above (profit taking or signal reversal).
In short, this script is an automated trading system that uses technical indicators (Stochastic Oscillator, Fractals) to generate buy and sell signals, manage open trades with trailing stops and profit targets, and close trades based on certain conditions. The overall goal is to automate trading decisions and potentially generate profits.
#property copyright "cloud666@rbcmail.ru"
extern int Kperiod= 8;
extern int Dperiod= 8;
extern int Slowing= 4;
extern int Method= 3 ;
extern int PriceUsing= 1;
extern double MaxLot= 0.0;
extern double TakeProfit= 0.000;
extern double TrailingStop= 0.01;
extern double StopLoss= 0.05;
extern double MinProfit= 0.0000;
extern double ProfitPoints= 0.000;
extern int Condition1= 1;
extern int Condition2= 1;
extern double LotSpliter= 0.1;
extern int CloseByOtherSideCondition=1;
double Lot;
double PP=0;
double slu,sld,a,b;
double tp,sl;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Alert("V2");
tp=TakeProfit;
sl=StopLoss;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
// if(AccountServer()!="SIG-Demo.com"){return(0);}
OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY);
if(DayOfYear() == TimeDayOfYear(OrderCloseTime()))
{
return(0);
}
if(OrdersTotal()==0)
{
PP=0;
preinit();
if(U()==1)
{
OrderBuy();
return(0);
}
if(U()==2)
{
OrderSell();
return(0);
}
return(0);
}
if(OrdersTotal()==1)
{
SelectOnlyOrder();
slu=Bid-OrderOpenPrice();
b=Bid;
sld=OrderOpenPrice()-Ask;
a=Ask;
if(OrderType()==0)
{
if((slu)>PP)
{
PP=slu;
}
if(((slu)>0.001) && (OrderStopLoss()<(b-TrailingStop)) && (OrderOpenPrice()<(b-TrailingStop)) && (OrderProfit()>MathAbs(OrderSwap())))
{
if(TrailingStop!=0)
{
OrderModify(OrderTicket(), 0, b-TrailingStop, 0, 0, 0);
}
}
}
if(OrderType()==1)
{
if((sld)>PP)
{
PP=sld;
}
if(((sld)>0.001) && (OrderStopLoss()>(a+TrailingStop)) && (OrderOpenPrice()>(a+TrailingStop)))
{
if(TrailingStop!=0)
{
OrderModify(OrderTicket(), 0, a+TrailingStop, 0, 0, 0);
}
}
}
if(ProfitPoints!=0)
{
if(OrderType()==0 && PP>=ProfitPoints && (slu)<=MinProfit)
{
CloseOnlyOrder();
return(0);
}
if(OrderType()==1 && PP>=ProfitPoints && (sld)<=MinProfit)
{
CloseOnlyOrder();
return(0);
}
}
if(CloseByOtherSideCondition==1)
{
if(OrderType()==0 && U()==2)
{
CloseOnlyOrder();
return(0);
}
if(OrderType()==1 && U()==1)
{
CloseOnlyOrder();
return(0);
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
int U()
{
if((U1()==2 && Condition1==1) || (U2()==2 && Condition2==1)){return(2);}
if((U1()==1 && Condition1==1) || (U2()==1 && Condition2==1)){return(1);}
return(0);
}
int U1()
{
if(iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_SIGNAL,1)>=80)
{
if(iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_SIGNAL,2)<=iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_MAIN,2))
{
if(iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_SIGNAL,1)>=iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_MAIN,1))
{
return(2);
}
}
}
if(iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_SIGNAL,1)<=20)
{
if(iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_SIGNAL,2)>=iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_MAIN,2))
{
if(iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_SIGNAL,1)<=iStochastic(Symbol(),Period(),Kperiod,Dperiod,Slowing,Method,PriceUsing,MODE_MAIN,1))
{
return(1);
}
}
}
return(0);
}
int U2()
{
double fu=0,fd=0;
int f=0,shift=2;
while(f<2)
{
if(iFractals(Symbol(),Period(),MODE_UPPER,shift)>0)
{
fu=fu+1;
f=f+1;
}
if(iFractals(Symbol(),Period(),MODE_LOWER,shift)>0)
{
fd=fd+1;
f=f+1;
}
shift=shift+1;
}
if(fu==2){return(2);}
if(fd==2){return(1);}
return(0);
}
int preinit()
{
Lot=NormalizeDouble(MathFloor(LotSpliter*AccountBalance()*AccountLeverage()/Ask/MathPow(10,Digits+1)*10)/10,1);
if(MaxLot>0 && Lot>MaxLot){Lot=MaxLot;}
if(Lot>MarketInfo(Symbol(),25)){Lot=MarketInfo(Symbol(),25);}
return(0);
}
int OrderBuy()
{
if(StopLoss!=0 && TakeProfit!=0)
{
OrderSend(Symbol(), 0, NormalizeDouble(Lot,1), Ask, 0, NormalizeDouble(Ask-StopLoss,4), NormalizeDouble(Ask+TakeProfit,4), 0, 0, 0, 0);
return(0);
}
if(StopLoss==0 && TakeProfit!=0)
{
OrderSend(Symbol(), 0, NormalizeDouble(Lot,1), Ask, 0, 0, NormalizeDouble(Ask+TakeProfit,4), 0, 0, 0, 0);
return(0);
}
if(StopLoss==0 && TakeProfit==0)
{
OrderSend(Symbol(), 0, NormalizeDouble(Lot,1), Ask, 0, 0, 0, 0, 0, 0, 0);
return(0);
}
if(StopLoss!=0 && TakeProfit==0)
{
OrderSend(Symbol(), 0, NormalizeDouble(Lot,1), Ask, 0, NormalizeDouble(Ask-StopLoss,4), 0, 0, 0, 0, 0);
return(0);
}
return(0);
}
int OrderSell()
{
if(StopLoss!=0 && TakeProfit!=0)
{
OrderSend(Symbol(), 1, NormalizeDouble(Lot,1), Bid, 0, NormalizeDouble(Bid+StopLoss,4), NormalizeDouble(Bid-TakeProfit,4), 0, 0, 0, 0);
return(0);
}
if(StopLoss==0 && TakeProfit!=0)
{
OrderSend(Symbol(), 1, NormalizeDouble(Lot,1), Bid, 0, 0, NormalizeDouble(Bid-TakeProfit,4), 0, 0, 0, 0);
return(0);
}
if(StopLoss==0 && TakeProfit==0)
{
OrderSend(Symbol(), 1, NormalizeDouble(Lot,1), Bid, 0, 0, 0, 0, 0, 0, 0);
return(0);
}
if(StopLoss!=0 && TakeProfit==0)
{
OrderSend(Symbol(), 1, NormalizeDouble(Lot,1), Bid, 0, NormalizeDouble(Bid+StopLoss,4), 0, 0, 0, 0, 0);
return(0);
}
return(0);
}
int CloseOnlyOrder()
{
SelectOnlyOrder();
RefreshRates();
if(OrderType()==0)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 0, 0);
}
else if(OrderType()==0)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 0, 0);
}
return(0);
}
int SelectOnlyOrder()
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
return(0);
}
Comments