-
Notifications
You must be signed in to change notification settings - Fork 2
/
myRandom.mq4
158 lines (144 loc) · 5.29 KB
/
myRandom.mq4
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
//+------------------------------------------------------------------+
//| myRandom.mq4 |
//| 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 strict
#property description "myRandom - places a trade in random direction."
#property description "You can control stop-loss, take-profit, volume, and entry period."
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;
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;
}
}
void OnTick()
{
Execution_Mode = (ENUM_SYMBOL_TRADE_EXECUTION)SymbolInfoInteger(Symbol(), SYMBOL_TRADE_EXEMODE);
if (Execution_Mode != SYMBOL_TRADE_EXECUTION_INSTANT) DoSLTP();
if (LastBars == Bars) return;
else LastBars = Bars;
if (Lots < SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN))
{
Print("Lots parameter < ", SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN), " minimum for ", _Symbol, ".");
return;
}
MathSrand((int)TimeLocal());
int count = 0;
int total = OrdersTotal();
for (int pos = 0; pos < total; pos++)
{
if (OrderSelect(pos, SELECT_BY_POS) == false) continue;
if ((OrderMagicNumber() == Magic) && (OrderSymbol() == Symbol())) return; // Don't open more than 1 trade.
}
if (Bars >= OrderTaken + RandomEntryPeriod)
{
if ((MathRand() % 2) == 1)
{
fSell();
}
else
{
fBuy();
}
OrderTaken = Bars;
}
}
void fBuy()
{
double SL = 0, TP = 0;
RefreshRates();
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);
}
int result = OrderSend(Symbol(), OP_BUY, Lots, Ask, Deviation, SL, TP, Commentary, Magic);
if (result == -1)
{
int e = GetLastError();
Print("OrderSend Error: ", e);
}
}
void fSell()
{
double SL = 0, TP = 0;
RefreshRates();
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);
}
int result = OrderSend(Symbol(), OP_SELL, Lots, Bid, Deviation, SL, TP, Commentary, Magic);
if (result == -1)
{
int e = GetLastError();
Print("OrderSend Error: ", e);
}
}
// Applies SL and TP to open positions in ECN mode.
void DoSLTP()
{
double SL = 0, TP = 0;
for (int i = 0; i < OrdersTotal(); i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if ((OrderMagicNumber() == Magic) && (OrderSymbol() == Symbol()))
{
if (OrderType() == OP_BUY)
{
if (StopLoss > 0) SL = NormalizeDouble(OrderOpenPrice() - StopLoss * Poin, _Digits);
if (TakeProfit > 0) TP = NormalizeDouble(OrderOpenPrice() + TakeProfit * Poin, _Digits);
if (((OrderStopLoss() != SL) || (OrderTakeProfit() != TP)) && (OrderStopLoss() == 0) && (OrderTakeProfit() == 0))
{
if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0))
{
int e = GetLastError();
Print("OrderModify Error: ", e);
}
}
}
else if (OrderType() == OP_SELL)
{
if (StopLoss > 0) SL = NormalizeDouble(OrderOpenPrice() + StopLoss * Poin, _Digits);
if (TakeProfit > 0) TP = NormalizeDouble(OrderOpenPrice() - TakeProfit * Poin, _Digits);
if (((OrderStopLoss() != SL) || (OrderTakeProfit() != TP)) && (OrderStopLoss() == 0) && (OrderTakeProfit() == 0))
{
if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0))
{
int e = GetLastError();
Print("OrderModify Error: ", e);
}
}
}
}
}
}
//+------------------------------------------------------------------+