//+------------------------------------------------------------------+ //| SampleEA.mq5 - TrueTest sample for trying the EA Trade Tester. | //| A textbook 9/21 moving-average crossover with faults left in ON | //| PURPOSE so the first read has something to find: | //| - it reads the PC clock (TimeLocal) for its trading hours; | //| - a setting (UseTrailing) is never used; | //| - a function (CloseAll) is never called; | //| - the order result is not checked; | //| - lot size doubles after a loss (martingale). | //| It is not a strategy to trade. Not financial advice. | //+------------------------------------------------------------------+ #property strict #include #include "SampleUtils.mqh" input int FastMA = 9; input int SlowMA = 21; input double StartLots = 0.01; input int StopPips = 30; input int TakePips = 60; input int StartHour = 8; input int EndHour = 20; input bool UseTrailing = true; CTrade trade; double lots; int fastHandle, slowHandle; int OnInit() { fastHandle = iMA(_Symbol, PERIOD_H1, FastMA, 0, MODE_SMA, PRICE_CLOSE); slowHandle = iMA(_Symbol, PERIOD_H1, SlowMA, 0, MODE_SMA, PRICE_CLOSE); lots = StartLots; return(INIT_SUCCEEDED); } void CloseAll() { for(int i = PositionsTotal() - 1; i >= 0; i--) trade.PositionClose(PositionGetTicket(i)); } void OnTick() { MqlDateTime t; TimeToStruct(TimeLocal(), t); if(t.hour < StartHour || t.hour >= EndHour) return; if(PositionsTotal() > 0) return; double f[2], s[2]; if(CopyBuffer(fastHandle, 0, 1, 2, f) < 2 || CopyBuffer(slowHandle, 0, 1, 2, s) < 2) return; double pip = PipSize(); double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK), bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); if(f[0] <= s[0] && f[1] > s[1]) trade.Buy(lots, _Symbol, ask, ask - StopPips * pip, ask + TakePips * pip); if(f[0] >= s[0] && f[1] < s[1]) trade.Sell(lots, _Symbol, bid, bid + StopPips * pip, bid - TakePips * pip); } void OnTradeTransaction(const MqlTradeTransaction &tr, const MqlTradeRequest &rq, const MqlTradeResult &rs) { if(tr.type == TRADE_TRANSACTION_DEAL_ADD && HistoryDealSelect(tr.deal)) { double p = HistoryDealGetDouble(tr.deal, DEAL_PROFIT); if(HistoryDealGetInteger(tr.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT) lots = (p < 0) ? lots * 2 : StartLots; } }