Education MQL II. Lesson 6
Hello, dear readers! Today we learn to control the open positions. At the end of the lesson we will write expert. A major difference that experts from the past is that a way out of the position will be not only stoporderu, but on the condition of the closure.
6.1. The number of open positions
For the number of open positions with the variable TotalTrades. If you want to perform and always have only one open position, the use of design
If TotalTrades = 0 then {-----------};
or
If TotalTrades = 1 then exit;
6.2 Function Order Value
Feature OrderValue - values of the parameters of open positions.
Syntax: OrderValue (position, mode)
Position - the number of open positions;
Mode - this option can take multiple values, depending on what settings you need to get the position.
Mode may be as follows: VAL_CLOSEPRICE - current closing price;
VAL_CLOSETIME - closing time;
VAL_COMMENT - Comment;
VAL_LOTS - the number of lots;
VAL_OPENPRICE - the price of opening position;
VAL_OPENTIME - the opening position;
VAL_PROFIT - current profit;
VAL_STOPLOSS - Trigger price stoplossa;
VAL_SWAP - a swap for the current shift position;
VAL_SYMBOL - currency pair;
VAL_TAKEPROFIT - Trigger price teykprofita;
VAL_TICKET - Number of position;
VAL_TYPE - the type of position (buy, sell, etc.).
With several open positions, or even one, to identify the parameters of an open position is convenient to use the cycle for.
for cnt = 1 to TotalTrades
(
if OrderValue (cnt, VAL_TYPE) <= OP_SELL and / / an open position? OP_BUY or OP_SELL
OrderValue (cnt, VAL_SYMBOL) = Symbol then / / instrument matches?
(
If OrderValue (cnt, VAL_TYPE) = OP_BUY then / / open long position
... ... ... ... ... ... ..
6.3. Feature ModifyOrder
This function is needed that would change the set stop orders. For example, this feature is used to podzhatiya stoplossa (trailing stop). Using this feature, you can also change teykprofit.
Syntax: ModifyOrder (order, price, stoploss, takeprofit, color)
Order-on this parameter being the identification of specific orders;
Stoploss-new stoploss;
Takeprofit-new teykprofit;
Color-color in which okrasitsya icon on the schedule of prices;
Example changes stoplossa.
If TrailingStop> 0 then / / you put in nastroykahtreylingstop
(/ / It means we are going to check it
If (Bid-OrderValue (cnt, VAL_OPENPRICE))> (Point * TrailingStop) then
(
If OrderValue (cnt, VAL_STOPLOSS) <(Bid-Point * TrailingStop) then
(
ModifyOrder (OrderValue (cnt, VAL_TICKET), OrderValue (cnt, VAL_OPENPRICE),
Bid-Point * TrailingStop, OrderValue (cnt, VAL_TAKEPROFIT), Red);
Exit;
6.4 Function CloseOrder
Using this feature, being the closing of open positions. Most often this occurs by an established conditions. Ie together with the operator, if then.
Syntax: CloseOrder (order, lots, price, slippage, color);
Order - on this parameter being the identification of specific orders;
Lots - the number of lots in the open position;
Slippage - prosklzyvanie;
Color-color in which okrasitsya icon on the schedule of prices;
Sample.
CloseOrder (OrderValue (cnt, VAL_TICKET), OrderValue (cnt, VAL_LOTS), Bid, 3, Violet);
6.5 Expert
The basic take an expert from the last release, t.k.ego purpose of securing the material.
I hope you will understand what this expert, all you need to do is, in this and previous lessons. Next issue, I plan to make a practical, t.k.teper you have sufficient knowledge that would write a rather serious experts. I would be very glad if you, dear readers, have written to me what an expert you'd like to see on the pages of the next issue. The most interesting algorithm will be implemented in the next issue
./*[[
Name: = Expert6
Author: = Fxtest.ru
Link: = forextimes.ru
Lots: = 1.00
Stop Loss: = 80
Take Profit: = 80
Trailing Stop: = 0
]] * /
defines: per (7), Upzone (70), Downzone (30), BigTrend (100);
/ / per-period RSI; Upzone, Downzone-overzony, BigTrend-period average showing
global trend
var: r1 (0), r2 (0), m1 (0), m2 (0), cnt (0);
/ / R1-time value of RSI r2-before value of RSI
r1 = iRSI (per, 1);
r2 = iRSI (per, 2);
m1 = iMA (BigTrend, MODE_SMA, 1);
/ / MA simple type (SMA)
m2 = iMA (BigTrend, MODE_SMA, 2);
if TotalTrades <1 then
(
/ / play if there is no open positions
if r2> r1 and r2> Upzone and m1
/ / if an extreme value and above the upper overzony, and the global trend is downwards x, sell
(
SetOrder (op_sell, lots, bid, 3, ask + stoploss * point, ask-takeprofit * point, red); exit;);
if r2
/ / if an extreme value and below the lower overzony, and the global trend is up, buy
(
SetOrder (op_buy, lots, ask, 3, bid-stoploss * point, bid + takeprofit * point, green);
exit;
);
);
if TotalTrades> 0 then
(
for cnt = 1 to TotalTrades
(
if OrderValue (cnt, VAL_TYPE) <= OP_SELL and
/ / position and buy or sell currency pair skotoroy experts
OrderValue (cnt, VAL_SYMBOL) = Symbol then
(
If OrderValue (cnt, VAL_TYPE) = OP_BUY then
/ / if you buy
(
if r2> 50 and r1
/ / Condition to closing
(
CloseOrder (OrderValue (cnt, VAL_TICKET), OrderValue (cnt, VAL_LOTS), Bid, 3, Violet);
/ / close position
Exit;
);
);
If OrderValue (cnt, VAL_TYPE) = OP_SELL then
/ / if you sell
(if r2 <50> r2 then
/ / Condition to closing
(
CloseOrder (OrderValue (cnt, VAL_TICKET), OrderValue (cnt, VAL_LOTS), ask, 3, Violet);
/ / close position
Exit;
);
);
);
);
);
No comments:
Post a Comment