This MetaTrader script, named "OpenTiks," is designed to automate trading in the Forex market. Here's a breakdown of what it does, explained in plain language:
Overall Goal:
The script's main goal is to automatically open and manage trades based on specific price patterns and to adjust the stop loss to protect profits (a "trailing stop").
How it Works:
-
Setup (Initialization):
- The script starts by defining some settings that you, the user, can adjust. These include:
TrailingStop
: The distance (in points, a unit of price change) the stop loss will follow the current price to protect profits.StopLoss
: A fixed stop loss value to protect the position when opened.Lots
: The size of the trade (how much currency to buy or sell).magicnumber
: A unique identifier for the trades this script opens, so it doesn't interfere with other trades.PolLots
: A boolean parameter which determine if part of the order must be closed when the trailing stop is triggeredMaxOrders
: maximum numbers of order allowed
- The script starts by defining some settings that you, the user, can adjust. These include:
-
Looking for Trade Opportunities (Start):
-
The script continuously monitors the market for buying or selling opportunities.
-
Checking price conditions:
- Buy signal: The script checks if the last three price highs and opens are continuously increasing. If this happens, it generates a
BuyOp
signal. - Sell signal: The script checks if the last three price highs and opens are continuously decreasing. If this happens, it generates a
SellOp
signal.
- Buy signal: The script checks if the last three price highs and opens are continuously increasing. If this happens, it generates a
-
Time Restriction:
- The
prevtime
variable makes sure that it does not open a new position in the same second as the previous one.
- The
-
Trading permission:
- The script verifies trading is allowed.
-
Maximum order restriction:
- The script will open a new position only when the numbers of open orders are less than
MaxOrders
parameter.
- The script will open a new position only when the numbers of open orders are less than
-
-
Opening Trades:
- When it finds a suitable trade signal (buy or sell), the script checks if number of current orders is lower that the maximum configured orders.
- If the conditions are met:
- Buy order: If the
BuyOp
condition is true, it places a buy order. It includes a stop loss if specified in the initial settings. - Sell order: If the
SellOp
condition is true, it places a sell order. It includes a stop loss if specified in the initial settings.
- Buy order: If the
-
Managing Existing Trades (Trailing Stop):
- For every open trade, it adjusts the stop loss order to follow the price.
- When the current market price surpass the
trldistance
parameter, the script modify the stop loss to lock some profits. - If
PolLots
parameter is true, the script will close part of the order if the trailing stop is triggered.
In Summary:
This script automatically identifies potential buying or selling opportunities based on simple price patterns, opens trades, and then manages those trades by adjusting the stop loss to protect profits as the price moves in a favorable direction.
Profitability Reports
//+------------------------------------------------------------------+
//| OpenTiks.mq4 |
//| Copyright © 2008, ZerkMax |
//| zma@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, ZerkMax"
#property link "zma@mail.ru"
extern int TrailingStop = 30;
extern int StopLoss = 0;
extern double Lots = 0.1;
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()
{
//----
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[0]>High[1]&&High[1]>High[2]&&High[2]>High[3]&&Open[0]>Open[1]&&Open[1]>Open[2]&&Open[2]>Open[3]) BuyOp=true;
if (High[0]<High[1]&&High[1]<High[2]&&High[2]<High[3]&&Open[0]<Open[1]&&Open[1]<Open[2]&&Open[2]<Open[3]) 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);
}
}
}
}
}
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
---