Okay, here's a breakdown of what this MetaTrader script does, explained in plain language:
Overall Purpose:
This script is designed to automatically place buy or sell orders in the Forex market based on a simple price pattern it detects. It also has some built-in features to manage risk and protect profits.
Here's how it works step by step:
-
Configuration: The script starts by letting you, the user, set several important parameters. These include:
- Shift values: These are numbers that tell the script how many previous time periods (like the last few minutes or hours) to look at when deciding whether to buy or sell.
- Trailing Stop: A setting that automatically adjusts the stop-loss order (an order that limits your potential losses) as the price moves in your favor, helping to lock in profits.
- Stop Loss: Sets a fixed level at which an order will automatically close to prevent excessive losses.
- Lots: This refers to the size of the trades the script will make.
- Risk Management: A switch to enable or disable the automatic calculation of order volume size
- Risk Percent: The percentage of your account balance that the script will risk on each trade.
- Magic Number: A unique identifier that helps the script manage its own trades separately from any other trades you might be making manually or with other scripts.
- Partial Close: A switch to enable or disable partial closing of the trade after the trail is activated
- Max Orders: The max orders that the EA will let open.
-
Initial Setup: The script performs a few setup tasks when it's first activated, but these are not defined within the current code.
-
Decision Making (The Core Logic): This is where the script determines whether to place a buy or sell order.
- Looking at Prices: It compares the high and open prices of the last four time periods (as defined by the "Shift" values).
- Buy Signal: If the high and open prices have been consistently increasing over those four periods, the script interprets this as a signal to buy.
- Sell Signal: Conversely, if the high and open prices have been consistently decreasing, it sees this as a signal to sell.
-
Time Check: The script confirms if the current time is the same as the previous time it checked. If it is the same, the script does nothing and waits for the next tick.
-
Trade Permission Check: This checks to see if automated trading is enabled on the account. If not, the current time is reset and the script does nothing and waits for the next tick.
-
Order Placement: If a buy or sell signal is triggered, and the maximum number of orders isn't already open, the script places an order.
- Order Type: The script places either a "Buy" or "Sell" order based on the signals described above.
- Order Size (Lots): The script uses the "Lots" value defined in the settings.
- Stop Loss and Take Profit: If a "Stop Loss" value is set, the script includes a stop-loss order to limit potential losses. There is no take profit defined within the current code.
- Magic Number: The script assigns the specified "Magic Number" to the order, so it can later identify and manage it.
-
Trailing Stop: After an order is placed, the script will track the price movements and automatically adjust the stop-loss order to protect profits.
- If the order is a buy and the price has moved in the favor of the order a specified amount the stop loss will be adjusted to protect profits.
- If the order is a sell and the price has moved in the favor of the order a specified amount the stop loss will be adjusted to protect profits.
- If partial close is enabled a partial close of the order will occur when the trail occurs.
In Summary:
The script is a basic automated trading system that looks for simple price trends and places buy or sell orders accordingly. It includes features for setting trade sizes, limiting potential losses, and protecting profits with a trailing stop.
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 Lots = 0.1;
extern bool RiskManagement=true; //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()
{
//----
//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 (Lots == 0 ) Lots = 0.1;
}}
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
{
Print(Lots);
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);
}
}
}
}
}
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
---