-
Notifications
You must be signed in to change notification settings - Fork 2
/
myRandom.mq5
168 lines (146 loc) · 5.63 KB
/
myRandom.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//+------------------------------------------------------------------+
//| myRandom.mq5 |
//| Copyright © 2007-2022, EarnForex.com |
//| https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007-2022, EarnForex"
#property link "https://www.earnforex.com/metatrader-expert-advisors/myRandom/"
#property version "1.03"
#property description "myRandom - places a trade in random direction."
#property description "You can control stop-loss, take-profit, volume, and entry period."
#include <Trade/Trade.mqh>
input group "Main"
input int RandomEntryPeriod = 1; //RandomEntryPeriod: How many bars to wait before entering a new position?
input int StopLoss = 1200;
input int TakeProfit = 600;
input group "Money management"
input double Lots = 0.1; // Lots - Position will be incremented with this volume
input group "Miscellaneous"
input int Slippage = 3;
input int Magic = 794823491;
input string Commentary = "myRandom";
// Global variables:
int LastBars = 0;
int OrderTaken = 0;
ENUM_SYMBOL_TRADE_EXECUTION Execution_Mode;
double Poin;
int Deviation;
// Main trading object:
CTrade *Trade;
void OnInit()
{
// Checking for unconventional Point digits number.
if ((_Point == 0.00001) || (_Point == 0.001))
{
Poin = _Point * 10;
Deviation = Slippage * 10;
}
else
{
Poin = _Point; // Normal
Deviation = Slippage;
}
Trade = new CTrade;
Trade.SetDeviationInPoints(Deviation);
Trade.SetExpertMagicNumber(Magic);
}
void OnDeinit(const int reason)
{
delete Trade;
}
void OnTick()
{
Execution_Mode = (ENUM_SYMBOL_TRADE_EXECUTION)SymbolInfoInteger(Symbol(), SYMBOL_TRADE_EXEMODE);
if (Execution_Mode != SYMBOL_TRADE_EXECUTION_INSTANT) DoSLTP();
if (LastBars == Bars(_Symbol, _Period)) return;
else LastBars = Bars(_Symbol, _Period);
if (Lots < SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN))
{
Print("Lots parameter < ", SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN), " minimum for ", _Symbol, ".");
return;
}
if (AccountInfoInteger(ACCOUNT_MARGIN_MODE) == ACCOUNT_MARGIN_MODE_RETAIL_NETTING)
{
if (PositionSelect(_Symbol)) return; // Netting mode and the trade is on.
}
else if (AccountInfoInteger(ACCOUNT_MARGIN_MODE) == ACCOUNT_MARGIN_MODE_RETAIL_HEDGING)
{
for (int i = 0; i < PositionsTotal(); i++)
{
if (!PositionSelectByTicket(PositionGetTicket(i))) continue;
if ((PositionGetString(POSITION_SYMBOL) == Symbol()) && (PositionGetInteger(POSITION_MAGIC) == Magic)) return; // The trade is on.
}
}
MathSrand((int)TimeLocal());
if (Bars(_Symbol, _Period) >= OrderTaken + RandomEntryPeriod)
{
if ((MathRand() % 2) == 1)
{
fSell();
}
else
{
fBuy();
}
OrderTaken = Bars(_Symbol, _Period);
}
}
void fBuy()
{
double SL = 0, TP = 0;
double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
if (Execution_Mode == SYMBOL_TRADE_EXECUTION_INSTANT)
{
if (StopLoss > 0) SL = NormalizeDouble(Ask - StopLoss * Poin, _Digits);
if (TakeProfit > 0) TP = NormalizeDouble(Ask + TakeProfit * Poin, _Digits);
}
if (!Trade.PositionOpen(_Symbol, ORDER_TYPE_BUY, Lots, Ask, SL, TP, Commentary))
{
int e = GetLastError();
Print("OrderSend Error: ", e);
}
}
void fSell()
{
double SL = 0, TP = 0;
double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
if (Execution_Mode == SYMBOL_TRADE_EXECUTION_INSTANT)
{
if (StopLoss > 0) SL = NormalizeDouble(Bid + StopLoss * Poin, _Digits);
if (TakeProfit > 0) TP = NormalizeDouble(Bid - TakeProfit * Poin, _Digits);
}
if (!Trade.PositionOpen(_Symbol, ORDER_TYPE_SELL, Lots, Bid, SL, TP, Commentary))
{
int e = GetLastError();
Print("OrderSend Error: ", e);
}
}
// Applies SL and TP to open positions if ECN mode is on.
void DoSLTP()
{
double SL = 0, TP = 0;
for (int i = 0; i < PositionsTotal(); i++)
{
if (!PositionSelectByTicket(PositionGetTicket(i))) continue;
if ((PositionGetString(POSITION_SYMBOL) != Symbol()) || (PositionGetInteger(POSITION_MAGIC) != Magic)) continue; // The trade is on.
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
if (StopLoss > 0) SL = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN) - StopLoss * Poin, _Digits);
if (TakeProfit > 0) TP = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN) + TakeProfit * Poin, _Digits);
}
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
if (StopLoss > 0) SL = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN) + StopLoss * Poin, _Digits);
if (TakeProfit > 0) TP = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN) - TakeProfit * Poin, _Digits);
}
if (((PositionGetDouble(POSITION_SL) != SL) || (PositionGetDouble(POSITION_TP) != TP)) && (PositionGetDouble(POSITION_SL) == 0) && (PositionGetDouble(POSITION_TP) == 0))
{
if (!Trade.PositionModify(_Symbol, SL, TP))
{
int e = GetLastError();
Print("OrderSend Error: ", e);
}
}
}
}
//+------------------------------------------------------------------+