Okay, here's a breakdown of what this MetaTrader script does, explained in plain language:
This script is designed to automatically trade on the Forex market. It operates on the principle of identifying specific price patterns and then executing buy or sell orders based on those patterns. It also incorporates features for managing risk and protecting profits.
Here's how it works, step by step:
-
Setup and Configuration: The script starts by loading a set of instructions and settings that you, the user, define. These include:
- Shifts: These settings determine how many previous time periods (like previous candles on a chart) the script looks back to compare prices. The script compares highs and opens across these "shifts" to identify a trend.
- Trailing Stop: This is a risk management feature. If a trade is making a profit, the stop-loss order (an order to automatically close the trade if the price moves against you) is automatically adjusted to lock in some of those profits.
- Stop Loss: This sets a fixed limit on how much you're willing to lose on a trade. If the price moves against your trade and hits this level, the trade will automatically close.
- Profit Target: The script monitors your overall account profit. If your profit reaches a pre-defined target, the script will close all open trades managed by it to secure those gains.
- Lots: This specifies the size of the trades the script will place. A "lot" is a standardized unit of currency. This can be a fixed amount or calculated dynamically.
- Risk Management: This feature allows the script to automatically adjust the trade size based on your account balance and a specified risk percentage. This helps to prevent you from risking too much on any single trade.
- Magic Number: This is a unique identifier that the script uses to manage its own trades, allowing it to distinguish them from trades placed manually or by other scripts.
- Max Orders: It limits the number of open orders.
- PolLots: This allows close part of an order.
-
Identifying Trading Opportunities (Buy or Sell Signals): The script then constantly monitors the price movements of the currency pair it's attached to. It looks for a very specific pattern:
- Buy Signal: It checks if the high and open prices of the current time period are higher than the high and open prices of the three previous time periods. If this is true, the script interprets it as a potential uptrend and generates a "buy" signal.
- Sell Signal: Conversely, it checks if the high and open prices of the current time period are lower than the high and open prices of the three previous time periods. If this is true, the script interprets it as a potential downtrend and generates a "sell" signal.
-
Executing Trades: If the script detects a buy or sell signal and it hasn't already reached its maximum number of open orders, it will execute a trade:
- Buy Order: If a buy signal is triggered, the script sends an order to buy the currency pair. The size of the trade (the "lot size") is determined by the settings you configured earlier. The script may also set a stop-loss order at a specified distance from the entry price.
- Sell Order: If a sell signal is triggered, the script sends an order to sell the currency pair. The lot size is determined as above, and a stop-loss order may be set.
-
Managing Open Trades (Trailing Stop and Profit Target): Once a trade is open, the script continues to monitor it.
- Trailing Stop: If the trade starts to make a profit, the script will automatically adjust the stop-loss order to lock in some of those profits. The distance the stop-loss trails behind the current price is determined by the "Trailing Stop" setting. Also close a part of the order in case the trailing activates.
- Profit Target: The script is also monitoring your overall account balance. If your account equity exceeds your initial balance plus the "Profit Target" amount, the script will close all open trades to secure your gains.
-
Close all orders: If the condition to close the orders is meet will close all trades opened by the script and the end
In summary, this script is an automated trading system that attempts to identify short-term trends in the Forex market and profit from them. It uses a simple price pattern recognition method to generate buy and sell signals, and it incorporates risk management features like stop-loss orders and trailing stops to protect your capital.
Profitability Reports
//+------------------------------------------------------------------+
//| OpenTiks.mq4 |
//| Copyright © 2008, ZerkMax |
//| zma@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, ZerkMax"
#property link "zma@mail.ru"
extern int Shift1 = 0;
extern int Shift2 = 1;
extern int Shift3 = 2;
extern int Shift4 = 3;
extern int TrailingStop = 30;
extern int StopLoss = 0;
extern double ProfitTarget=100000;
extern double Lots = 0.1;
extern bool RiskManagement=false; //money management
extern double RiskPercent=10; //risk in percentage
extern int magicnumber = 777;
extern bool PolLots = true;
extern int MaxOrders = 1;
int prevtime;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//profit target
if(AccountEquity()>(AccountBalance()+ProfitTarget))
{
CloseOrders(magicnumber);
return(0);
}
//risk management
bool MM=RiskManagement;
if(MM){if(RiskPercent<0.1||RiskPercent>100){Comment("Invalid Risk Value.");return(0);}
else{Lots=MathFloor((AccountFreeMargin()*AccountLeverage()*RiskPercent*Point*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*
MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT);}}
if(MM==false){Lots=Lots;}
int i=0;
int total = OrdersTotal();
for(i = 0; i <= total; i++)
{
if(TrailingStop>0)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == magicnumber)
{
TrailingStairs(OrderTicket(),TrailingStop);
}
}
}
bool BuyOp=false;
bool SellOp=false;
if (High[Shift1]>High[Shift2]&&High[Shift2]>High[Shift3]&&High[Shift3]>High[Shift4]&&Open[Shift1]>Open[Shift2]&&Open[Shift2]>Open[Shift3]&&Open[Shift3]>Open[Shift4]) BuyOp=true;
if (High[Shift1]<High[Shift2]&&High[Shift2]<High[Shift3]&&High[Shift3]<High[Shift4]&&Open[Shift1]<Open[Shift2]&&Open[Shift2]<Open[Shift3]&&Open[Shift3]<Open[Shift4]) SellOp=true;
if(Time[0] == prevtime)
return(0);
prevtime = Time[0];
if(!IsTradeAllowed())
{
prevtime = Time[1];
return(0);
}
if (total < MaxOrders || MaxOrders == 0)
{
if(BuyOp)
{
if (StopLoss!=0)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-(StopLoss*Point),0,"OpenTiks_Buy",magicnumber,0,Green);
}
else
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"OpenTiks_Buy",magicnumber,0,Green);
}
}
if(SellOp)
{
if (StopLoss!=0)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+(StopLoss*Point),0,"OpenTiks_Sell",magicnumber,0,Red);
}
else
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"OpenTiks_Sell",magicnumber,0,Red);
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
void TrailingStairs(int ticket,int trldistance)
{
int Spred=Ask - Bid;
if (OrderType()==OP_BUY)
{
if((Bid-OrderOpenPrice())>(Point*trldistance))
{
if(OrderStopLoss()<Bid-Point*trldistance || (OrderStopLoss()==0))
{
OrderModify(ticket,OrderOpenPrice(),Bid-Point*trldistance,OrderTakeProfit(),0,Green);
if (PolLots)
if (NormalizeDouble(OrderLots()/2,2)>MarketInfo(Symbol(), MODE_MINLOT))
{
OrderClose(ticket,NormalizeDouble(OrderLots()/2,2),Ask,3,Green);
}
else
{
OrderClose(ticket,OrderLots(),Ask,3,Green);
}
}
}
}
else
{
if((OrderOpenPrice()-Ask)>(Point*trldistance))
{
if((OrderStopLoss()>(Ask+Point*trldistance)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*trldistance,OrderTakeProfit(),0,Red);
if (PolLots)
if (NormalizeDouble(OrderLots()/2,2)>MarketInfo(Symbol(), MODE_MINLOT))
{
OrderClose(ticket,NormalizeDouble(OrderLots()/2,2),Bid,3,Green);
}
else
{
OrderClose(ticket,OrderLots(),Bid,3,Green);
}
}
}
}
}
//|---------close buy orders
int CloseOrders(int Magic)
{
int result,total=OrdersTotal();
for (int cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3);
switch(OrderType())
{
case OP_BUYLIMIT:
case OP_BUYSTOP:
result=OrderDelete(OrderTicket());
}
}
if(OrderType()==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3);
switch(OrderType())
{
case OP_SELLLIMIT:
case OP_SELLSTOP:
result=OrderDelete(OrderTicket());
}
}
}
}
return(0);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---