#include input double Lots = 0.1; input double LotFactor = 2; input int TpPoints = 100; input int SlPoints = 100; input int Magic = 111; CTrade trade; bool isTradeAllowed = true; int OnInit(){ trade.SetExpertMagicNumber(Magic); return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ } void OnTick(){ if(isTradeAllowed){ double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); double tp = ask + TpPoints * _Point; double sl = ask - SlPoints * _Point; ask = NormalizeDouble(ask,_Digits); tp = NormalizeDouble(tp,_Digits); sl = NormalizeDouble(sl,_Digits); if(trade.Buy(Lots,_Symbol,ask,sl,tp)){ isTradeAllowed = false; } } } void OnTradeTransaction( const MqlTradeTransaction& trans, // trade transaction structure const MqlTradeRequest& request, // request structure const MqlTradeResult& result // response structure ){ if(trans.type == TRADE_TRANSACTION_DEAL_ADD){ CDealInfo deal; deal.Ticket(trans.deal); HistorySelect(TimeCurrent()-PeriodSeconds(PERIOD_D1),TimeCurrent()+10); if(deal.Magic() == Magic && deal.Symbol() == _Symbol){ if(deal.Entry() == DEAL_ENTRY_OUT){ Print(__FUNCTION__," > Closed pos #",trans.position); if(deal.Profit() > 0){ isTradeAllowed = true; }else{ if(deal.DealType() == DEAL_TYPE_BUY){ double lots = deal.Volume() * LotFactor; lots = NormalizeDouble(lots,2); double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); double tp = ask + TpPoints * _Point; double sl = ask - SlPoints * _Point; ask = NormalizeDouble(ask,_Digits); tp = NormalizeDouble(tp,_Digits); sl = NormalizeDouble(sl,_Digits); trade.Buy(lots,_Symbol,ask,sl,tp); }else if(deal.DealType() == DEAL_TYPE_SELL){ double lots = deal.Volume() * LotFactor; lots = NormalizeDouble(lots,2); double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); double tp = bid - TpPoints * _Point; double sl = bid + SlPoints * _Point; bid = NormalizeDouble(bid,_Digits); tp = NormalizeDouble(tp,_Digits); sl = NormalizeDouble(sl,_Digits); trade.Sell(lots,_Symbol,bid,sl,tp); } } } } } }