forked from paulphilip/pythoncode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
137 lines (114 loc) · 3.85 KB
/
project.py
File metadata and controls
137 lines (114 loc) · 3.85 KB
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
//+------------------------------------------------------------------+
//| 9/15 EMA Crossover EA - M15, 3 Trades/Day |
//+------------------------------------------------------------------+
#property strict
#include <Trade/Trade.mqh>
input double Lots = 0.01;
input int FastEMA = 9;
input int SlowEMA = 15;
input int MaxTradesPerDay = 3;
input ulong Magic = 123456;
CTrade trade;
int fastHandle, slowHandle;
int todayCount = 0;
datetime lastDay = 0;
//+------------------------------------------------------------------+
int OnInit()
{
if(_Period != PERIOD_M15)
{
Print("Attach EA to M15 timeframe only!");
return(INIT_FAILED);
}
fastHandle = iMA(_Symbol, PERIOD_M15, FastEMA, 0, MODE_EMA, PRICE_CLOSE);
slowHandle = iMA(_Symbol, PERIOD_M15, SlowEMA, 0, MODE_EMA, PRICE_CLOSE);
if(fastHandle == INVALID_HANDLE || slowHandle == INVALID_HANDLE)
{
Print("Failed to create EMA handles!");
return(INIT_FAILED);
}
trade.SetExpertMagicNumber(Magic);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(fastHandle != INVALID_HANDLE) IndicatorRelease(fastHandle);
if(slowHandle != INVALID_HANDLE) IndicatorRelease(slowHandle);
}
//+------------------------------------------------------------------+
void OnTick()
{
// Run only on new M15 candle
static datetime lastBar = 0;
datetime currentBar = iTime(_Symbol, PERIOD_M15, 0);
if(currentBar == lastBar) return;
lastBar = currentBar;
// Reset daily counter
datetime today = TimeCurrent() - (TimeCurrent() % 86400);
if(today != lastDay)
{
todayCount = 0;
lastDay = today;
}
// limit trades per day
if(todayCount >= MaxTradesPerDay)
{
Print("Daily trade limit reached");
return;
}
// If trade already running, skip
if(PositionSelect(_Symbol))
{
Print("Trade already active, waiting...");
return;
}
// Fetch EMA values
double fast[2], slow[2];
ArraySetAsSeries(fast, true);
ArraySetAsSeries(slow, true);
if(CopyBuffer(fastHandle, 0, 0, 2, fast) < 2) return;
if(CopyBuffer(slowHandle, 0, 0, 2, slow) < 2) return;
double fastCurr = fast[0];
double fastPrev = fast[1];
double slowCurr = slow[0];
double slowPrev = slow[1];
// Equity-based SL & TP
double equity = AccountInfoDouble(ACCOUNT_EQUITY);
double SL_money = equity * 0.01;
double TP_money = equity * 0.03;
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
double sl_ticks = SL_money / (tickValue * Lots);
double tp_ticks = TP_money / (tickValue * Lots);
double sl_dist = sl_ticks * tickSize;
double tp_dist = tp_ticks * tickSize;
// ---------------- BUY SIGNAL ----------------
if(fastPrev < slowPrev && fastCurr > slowCurr)
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double sl = NormalizeDouble(ask - sl_dist, digits);
double tp = NormalizeDouble(ask + tp_dist, digits);
if(trade.Buy(Lots, _Symbol, ask, sl, tp))
{
todayCount++;
Print("BUY placed. Total today:", todayCount);
}
return;
}
// ---------------- SELL SIGNAL ----------------
if(fastPrev > slowPrev && fastCurr < slowCurr)
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double sl = NormalizeDouble(bid + sl_dist, digits);
double tp = NormalizeDouble(bid - tp_dist, digits);
if(trade.Sell(Lots, _Symbol, bid, sl, tp))
{
todayCount++;
Print("SELL placed. Total today:", todayCount);
}
return;
}
}
//+------------------------------------------------------------------+