Here's a breakdown of what this trading script does, explained in a way that doesn't require any programming knowledge:
Overall Purpose:
This script is designed to automatically place buy and sell orders in the market based on a simple trend-following strategy. It looks at recent price movements to identify potential upward or downward trends and then enters trades accordingly. The script also includes features for managing risk and exiting trades, such as trailing stops and time-based filters.
Key Components and Logic:
-
Initial Setup (Initialization):
- When the script is first attached to a chart, it performs a basic setup. This involves checking that the settings are loaded properly and getting ready to monitor price movements.
-
Time-Based Filtering:
- The script has the ability to avoid trading at certain times of the week. This could be used to prevent trades from being opened during periods of low liquidity or when major news events are expected.
- It checks if trading is allowed on Sundays and within specified hours on weekdays (Monday to Thursday and on Friday). If it's outside of the allowed trading hours, the script will close any open positions and stop opening new ones.
-
Trend Identification:
- The script identifies trends by comparing the current price (
High
andOpen
) to prices from a few previous time periods (usingShift1
,Shift2
,Shift3
,Shift4
). - Buy Signal: If the
High
andOpen
prices are consistently higher in the most recent periods compared to previous periods, the script interprets this as an upward trend and generates a buy signal. - Sell Signal: Conversely, if the
High
andOpen
prices are consistently lower in the most recent periods, it's seen as a downward trend, and a sell signal is generated.
- The script identifies trends by comparing the current price (
-
Order Placement:
- If a buy or sell signal is triggered, and the script is allowed to trade based on the time filters, it will attempt to place an order.
- The
Lots
variable determines the size of the trade. The script can either use a fixed lot size set by the user or calculate the lot size dynamically based on the account balance and risk percentage. - A
StopLoss
can be set to automatically close the trade if the price moves against the position by a certain amount. - There's a limit to how many orders can be opened at one time, specified by the
MaxOrders
parameter.
-
Trailing Stop:
- The script includes a trailing stop feature. This means that the stop-loss order is automatically adjusted as the price moves in a favorable direction. The
TrailingStop
parameter determines how far behind the current price the stop-loss order will be placed. - This trailing stop function also includes partial closing of the order if the trailing stop is hit after it is triggered, to realize some profit.
- The script includes a trailing stop feature. This means that the stop-loss order is automatically adjusted as the price moves in a favorable direction. The
-
Risk Management:
- The script has optional risk management features. If enabled (
RiskManagement = true
), the script will calculate the appropriate trade size (Lots
) based on the account's free margin, leverage, and the specified risk percentage (RiskPercent
). This helps to limit the potential losses from any single trade.
- The script has optional risk management features. If enabled (
-
Order Management:
- The script can close all buy or sell orders based on a "magic number" and the symbol of the currency pair if the time-based filters are triggered.
In Simple Terms:
Imagine the script as a robot that watches the price chart, waiting for a clear sign that the price is going up or down. If it sees a sign, it places a trade. It also has safety measures in place, like a "stop-loss" to prevent big losses, and a "trailing stop" that locks in profits as the price moves in the right direction. The robot can also be told to avoid trading at certain times of the day or week.
Profitability Reports
//+------------------------------------------------------------------+
//| OpenTiks.mq4 |
//| Copyright © 2008, ZerkMax |
//| zma@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, ZerkMax"
#property link "zma@mail.ru"
extern string S1="---------------- General Settings";
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=false; //money management
extern double RiskPercent=10; //risk in percentage
extern int magicnumber = 777;
extern bool PolLots = true;
extern int MaxOrders = 1;
extern string S2="---------------- Time Filter";
extern bool TradeOnSunday=true;//|---------------time filter on sunday
extern bool MondayToThursdayTimeFilter=false;//|-time filter the week
extern int MondayToThursdayStartHour=0;//|-------start hour time filter the week
extern int MondayToThursdayEndHour=24;//|--------end hour time filter the week
extern bool FridayTimeFilter=false;//|-----------time filter on friday
extern int FridayStartHour=0;//|-----------------start hour time filter on friday
extern int FridayEndHour=21;//|------------------end hour time filter on friday
int prevtime;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if((TradeOnSunday==false&&DayOfWeek()==0)||(MondayToThursdayTimeFilter&&DayOfWeek()>=1&&DayOfWeek()<=4&&!(Hour()>=MondayToThursdayStartHour&&Hour()<=MondayToThursdayEndHour))||(FridayTimeFilter&&DayOfWeek()==5&&!(Hour()>=FridayStartHour&&Hour()<=FridayEndHour)))
{
CloseBuyOrders(magicnumber);
CloseSellOrders(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 CloseBuyOrders(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());
}
}
}
}
return(0);
}
//|---------close sell orders
int CloseSellOrders(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_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
---