Saturday, March 7, 2009

Programming in MQL 4: Trading System. Part 3.

In this article, we will continue the theme begun in the previous two issues of the journal Forex Magazine, and write the Adviser on the basis described in the previous section the general scheme of the advisers.



So, based on the MTS trading system to select the indicator RSI. Once a reservation, that this system is purely illustrative and are not scheduled for any of the financial markets. Moreover, the values of the parameters of the system are taken at random and have nothing to do with that really required in this situation.



Here to launch verbal description of the system:



One open position simultaneously.



Indicator RSI. Period indicator parameter is set adviser.



1. Rules of the opening lines: signal to open a short position will be the intersection of the curve RSI levels down 80% and 70% running. That is, if the intersection of the level 70% No 80% crossing level, the price of such behavior should not be taken into account, the signal for opening a long position will be the intersection of the curve RSI upward two levels of 20% and 30% running. That is, if the intersection of the level 30% No 20% of the level crossing, it does not take into account the price of such behavior.



2. Terms of closing the position: a signal to close long positions will be the intersection of the bottom-up level of 80%, a signal to close short positions will be the intersection of top-down level of 20%. In addition to these two signals at the closing position, the warrant will be closed by stop-loss'u, which will be calculated as a percentage of current price. The percentage will be raised as a parameter adviser.



3. The size of the position will be calculated as a percentage of the deposit. The percentage will be raised as a parameter adviser.



Then follow, in accordance with the scenario described in previous articles on trading systems.



With the help of advisers to the writing wizard, create a skeleton Adviser RSI-advisor. Do not forget the second stage of the master of writing advisers add parameters: nRsiPeriod type int, and the initial value of 8; fStopLoss type double and the initial value of 1.5 and fLots type double and the initial value of 0.10.



Upon receipt of frame indicator, start to fill his logic.



There are no special restrictions on the input parameters we have imposed, and therefore function Adviser init () will remain unchanged.



All the rules that we have indicated in the verbal description of the trading system will be checked and processed in the functions of adviser to start (). Here's the source code of the indicator, in which all the steps detailed comment.



//+----------------------------------------------- -------------------+

/ / | RSI-advisor.mq4 |

/ / | Copyright © 2004, Horn |

/ / | Alexander@indus.ru |

//+----------------------------------------------- -------------------+

# define copyright "Copyright © 2004, Horn"

# define link "alexander@indus.ru"

int g_nRsiPeriod;

double g_fStopLoss;

double g_fLots;

int init (int nRsiPeriod = 8, double fStopLoss = 0.5, double fLots =. 10)

(

g_nRsiPeriod = nRsiPeriod;

g_fStopLoss = fStopLoss;

g_fLots = fLots;

return (0);

)

int deinit ()

(

return (0);

)

int start ()

(

/ / If this function was called MetaTrader'om,

/ / Means a change in price or advisor

/ / Run the first time.

int nShift = 0;

int nOrders = 0;

double fCurrentRSI = 0;

double fPreviousRSI = 0;

double fShiftRSI = 0;

/ / Do not be 10-second limit?

if (CurTime () - LastAttemptTime () <10) return (0);

/ / Get values RSI indicator at the current and previous bar

fCurrentRSI = iRSI (g_nRsiPeriod, 0, PRICE_CLOSE, MODE_FIRST, 0);

fPreviousRSI = iRSI (g_nRsiPeriod, 0, PRICE_CLOSE, MODE_FIRST, 1);

/ / Do not exceed the number of open orders?

nOrders = OrderTotal ();

if ((nOrders <1)> = 1000 * g_fLots * 1.5)) (

/ / If no open positions, and the account is free

/ / Means that we check the values of indicators

/ / To check the signal for opening a new position.

/ / Check whether the fall in the current bar value of RSI

/ / In the corridor from 30% to 70%.

if ((fCurrentRSI) <70> 30) (

/ / Check whether the value of RSI in the previous bar more

/ / 70% line

if (fPreviousRSI> 70) (

/ / A chance to open a short position!

/ / Check remaining only after it matter

/ / RSI zone perekuplennosti 80% -100%

for (nShift = 2; nShift
fShiftRSI = iRSI (g_nRsiPeriod, 0, PRICE_CLOSE, MODE_FIRST, nShift);

if (fShiftRSI> = 80) (

/ / Open a short position and exit

OrderSet (OP_SELL, g_fLots, Bid, 3, Ask + (Ask * g_fStopLoss/100), 0, Blue);

return (0);

) else if (fShiftRSI <70) (

/ / Value RSI "was" not out of the perekuplennosti 80% -100%

/ / A, rising from the corridor 30% -70%, "chatter"

/ / In the corridor of 70% -80%. This intersection of RSI and

/ / Level 70%, we do not see how the signal and ends

/ / Handle the previous bars.

break;

)

)

) Else

/ / Check whether the value at the previous bar RSI is less than

/ / 30% line

if (fPreviousRSI <= 30) (

/ / A chance to open a long position!

/ / Left only to check whether the value rose

/ / RSI zone pereprodannosti 0% -20%

for (nShift = 2; nShift
fShiftRSI = iRSI (g_nRsiPeriod, 0, PRICE_CLOSE, MODE_FIRST, nShift);

if (fShiftRSI <= 20) (

/ / Open a long position and exit

OrderSet (OP_BUY, g_fLots, Ask, 3, Bid-(Bid * g_fStopLoss/100), 0, Red);

return (0);

) else if (fShiftRSI> 30) (

/ / Value RSI "was" not out of the pereprodannosti 0% -20%

/ / But, down the corridor from 30% -70%, "chatter"

/ / In the corridor of 20% -30%. This intersection of RSI and

/ / Level 30%, we do not see how the signal and ends

/ / Handle the previous bars.

break;

)

)

)

)

)

/ / If it is not possible to open a new position,

/ / You should check, is not it time to close the already open-minded.

for (nShift = 0; nShift
int nType = OrderGet (nShift, VAL_TYPE);

if (nType == OP_BUY) (

if (fCurrentRSI> = 80) (

/ / Your purchase, we are in zone perekuplennosti,

/ / Therefore, it is time to close

OrderClose (OrderGet (nShift, VAL_TICKET), OrderGet (nShift, VAL_LOTS),

Bid, 3, Violet);

return (0);

)

) Else if (nType == OP_SELL) (

if (fCurrentRSI <= 20) (

/ / Selling, we got into a zone pereprodannosti,

/ / Therefore, it is time to close

OrderClose (OrderGet (nShift, VAL_TICKET), OrderGet (nShift, VAL_LOTS),

Ask, 3, Magenta);

return (0);

)

)

)

return (0);

)



Just the simple terms of trade described MTS provides a small number of input parameters, which are always evaluated as one of the benefits of the trading system. When the opportunity to test the advisor, we will need to pick the optimal time indicator, depending on the time period under consideration, the amount of stop-loss'a, depending on the volatility of the currency and the size of the lot. The size of the lot should be selected by the rules applicable moneymanagement'a. In this case, one could write a function that, given these rules, are automatically calculated to the size of the next transaction. Proposed method used only for ease of implementation.



It should be noted that the final wording of the language MQL 4 will allow contact to the values of historical data and obtain the values of indicators of different time periods. In the current version of the language MQL 4 implements this feature through the two functions work with arrays. Options ArrayCopyRates () and ArrayCopySeries () are parameters symbol_name and period, through which you can specify the character and time period of historical data required for backup.











Alexander Ivanov

to Forex Magazine

fxtrade@tomsk.ru

No comments: