-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRSI_70_30_simple.mq5
95 lines (84 loc) · 3.6 KB
/
RSI_70_30_simple.mq5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <Trade/Trade.mqh> // class that is able ot modify trades
CTrade trade; //this is a class, object variable of type CTrade named "trade" (this can be changed)
int rsiHandle;
ulong posTicket; //global ticket variable i.e. trade ID
//double tradePrice;
double SLperc = 0.005;
double TPperc = 0.005;
int OnInit(){
Print("OnInit"+_Symbol);
rsiHandle = iRSI(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE); //iRSI("EURUSD",5,14,PRICE_CLOSE);
return 0;
}
void OnDeinit(const int reason)
{
Print("OnDeInit");
}
void OnTick()
{
//check conditions, open or close trades, modify positions
double rsi[]; // empty array
CopyBuffer(rsiHandle, 0, 1,2,rsi);
// handle, buffer number 0 starts from 0 only one buffer, starting pos, count (how many candles before 1st, number of values in array), array
// if more than one candle calculated, then the most recent one is in the last array position, the one furthest back in time is at 0
if (rsi[1] > 70){ // && rsi[1] < rsi[0]
Print(rsi[0]);
//Close buy position if there is a sell signal
if (posTicket > 0 && PositionSelectByTicket(posTicket)){
int posType = (int) PositionGetInteger(POSITION_TYPE);
if (posType == POSITION_TYPE_BUY){
trade.PositionClose(posTicket);
posTicket = 0;
}
}
if (posTicket <= 0){ //if ticket ID (trade ID) larger smaller or equal 0, open trade. As soon as 1 trade is open, ticket ID is larger 0.
trade.Sell(0.01,_Symbol); //price set top 0 is market price, sl = stop loss, tp = take profit
posTicket = trade.ResultOrder(); //need to stop buy/sell at every tick by identifying uinque trade ID
//tradePrice = trade.RequestPrice();
}
} else if (rsi[1] < 30){ // rsi[1] < 30 && rsi[1] > rsi[0]
Print(rsi[0]);
//Close sell position if there is a sell signal
if (posTicket > 0 && PositionSelectByTicket(posTicket)){
int posType = (int) PositionGetInteger(POSITION_TYPE);
if (posType == POSITION_TYPE_SELL){
trade.PositionClose(posTicket);
posTicket = 0;
}
}
if (posTicket <= 0){
trade.Buy(0.01,_Symbol);
posTicket = trade.ResultOrder();
//tradePrice = trade.RequestPrice();
}
}
//modify trade
if (PositionSelectByTicket(posTicket)){
double posPrice = PositionGetDouble(POSITION_PRICE_OPEN); //get price at position opening
double posSL = PositionGetDouble(POSITION_SL); //get stop loss price
double posTP = PositionGetDouble(POSITION_TP); //get take profit price
int posType = (int) PositionGetInteger(POSITION_TYPE); //get position type, add (int) before to define that this is an integer
//For Long position
if (posType == POSITION_TYPE_BUY){
if (posSL == 0){
double sl = posPrice - (posPrice * SLperc); //posPrice - 0.006
double tp = posPrice + (posPrice * TPperc); //posPrice + 0.006
trade.PositionModify(posTicket,sl,tp);
}
} else if (posType == POSITION_TYPE_SELL){ //For Short position
if (posSL == 0){
double sl = posPrice + (posPrice * SLperc); //posPrice + 0.006
double tp = posPrice - (posPrice * TPperc); //posPrice - 0.006
trade.PositionModify(posTicket,sl,tp);
}
}
} else {
posTicket = 0; //need to set to 0 again, otherwise posTicket stays above 0 and no new trades are opened
}
Comment("RSI=",rsi[0],"\n","Ticket=",posTicket,"\n","TradePrice=");
}
int ourFunction(string txt)
{
Print("this is our function"+txt);
return 1;
}