-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsellstructureEEA.mq5
110 lines (81 loc) · 4.07 KB
/
sellstructureEEA.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
#property script_show_inputs
//--- input parameters
input int RiskPercentage = 1; // Risk Percentage
input int RewardRatio = 2; // Reward Ratio
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
CTrade *m_trade;
CSymbolInfo *m_symbol;
CPositionInfo *m_position_info;
CAccountInfo *m_account;
#define MAX_PERCENT 0.2
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart() {
m_trade = new CTrade();
m_symbol = new CSymbolInfo();
m_position_info = new CPositionInfo();
m_account = new CAccountInfo();
m_symbol.Name(Symbol());
m_symbol.RefreshRates();
double point = m_symbol.Point();
double digits = m_symbol.Digits();
double spread = m_symbol.Spread();
double lots = TradeSize();
double sl = NormalizeDouble(m_symbol.Bid() + AccountPercentStopPips(lots) * point, (int)digits);
double tp = NormalizeDouble(m_symbol.Bid() - (AccountPercentStopPips(lots) * RewardRatio) * point, (int)digits);
if(!m_trade.PositionOpen(Symbol(), ORDER_TYPE_SELL, lots, m_symbol.Bid(), sl, tp))
Print("PositionOpen() Sell FAILED!!. Return code=",m_trade.ResultRetcode(), ". Code description: ",m_trade.ResultRetcodeDescription());
if(m_position_info != NULL)
delete m_position_info;
if(m_symbol != NULL)
delete m_symbol;
if(m_trade != NULL)
delete m_trade;
if(m_account != NULL)
delete m_account;
}
//+-------------------------------------------------------------------------+
//| Money Managment |
//+-------------------------------------------------------------------------+
double TradeSize() {
double lots_min = m_symbol.LotsMin();
double lots_max = m_symbol.LotsMax();
long leverage = m_account.Leverage();
double lots_size = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_CONTRACT_SIZE);
double lots_step = m_symbol.LotsStep();;
double percentage = RiskPercentage / 100;
if(percentage > MAX_PERCENT) percentage = MAX_PERCENT;
double final_account_balance = MathMin(m_account.Balance(), m_account.Equity());
int normalization_factor = 0;
double lots = 0.0;
if(lots_step == 0.01) { normalization_factor = 2; }
if(lots_step == 0.1) { normalization_factor = 1; }
lots = (final_account_balance*(RiskPercentage/100.0))/(lots_size/leverage);
lots = NormalizeDouble(lots, normalization_factor);
if (lots < lots_min) { lots = lots_min; }
if (lots > lots_max) { lots = lots_max; }
//----
return( lots );
}
double AccountPercentStopPips(double lots) {
double balance = MathMin(m_account.Balance(), m_account.Equity());
double moneyrisk = balance * RiskPercentage / 100;
double spread = m_symbol.Spread();
double point = m_symbol.Point();
double ticksize = m_symbol.TickSize();
double tickvalue = m_symbol.TickValue();
double tickvaluefix = tickvalue * point / ticksize; // A fix for an extremely rare occasion when a change in ticksize leads to a change in tickvalue
double stoploss = moneyrisk / (lots * tickvaluefix ) - spread;
if (stoploss < m_symbol.StopsLevel())
stoploss = m_symbol.StopsLevel(); // This may rise the risk over the requested
stoploss = NormalizeDouble(stoploss, 0);
return (stoploss);
}
//+------------------------------------------------------------------+
/*A simple script that allows you to open a Short position using the current price location with a specified Risk Percentage and Reward Ratio.
Pretty simple, drag the script onto your chart and the inputs to change the Risk Percentage and Reward Ratio will pop up, change those values if you want to, hit the Ok button and the trade will execute.
*/