Basically, I want my EA to manage open trades with OrderModify().
My EA is programmed to take trades when 2 Moving Averages are crossing.
Every time the 2 Moving Averages cross, it opens 2 trades.
Trade 1:
Take Profit = 1.0 * ATR
Stop Loss = 1.5 * ATR
Trade 2:
No Take Profit
Stop Loss = 1.5 * ATR
When price hits Take Profit on that one trade, I want my EA to simply move Stop Loss to Breakeven on the other trade.
But how do I program it to know what the Take Profit (1.0 * ATR) was at that specific moment it opened the trade?
All feedback is greatly appreciated!
maybe you can validate the number of opened positions for that symbol:
- if opened positions=0 you can evaluate the conditions to open the 2 trades.
- if 1 opened position then immediately set StopLoss to OpenPrice
maybe you can validate the number of opened positions for that symbol:
- if opened positions=0 you can evaluate the conditions to open the 2 trades.
- if 1 opened position then immediately set StopLoss to OpenPrice
Thanks for the help!
I think I found out how to do it, but now I'm facing a new problem.
For some reason, the EA keeps modifying the orders incorrectly. Sometimes it works, sometimes it doesn't.
Here is the code and I assume that the problem is at the bottom:
//+------------------------------------------------------------------+ //| OBVmod_MA_EA.mq4 | //| Copyright 2019, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2019, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict input int EAFastMA=7; input int EAFastMAShift=0; input int EAFastMAMethod=0; //input int EAFastMAAppliedTo=0; input int EASlowMA=66; input int EASlowMAShift=0; input int EASlowMAMethod=0; extern int MoveSLtoBEafter=100; extern int PipsLockedIn=10; //input int EASlowMAAppliedTo=0; extern double LotSize = 0.01; //extern int MagicNumber =0; extern int ATR_PeriodValue =14; double pips; // Defining parameters for opening trades. extern int TakeProfitPercent = 100; extern double StopLossPercent = 150; extern int Slippage = 3; int MagicNumber1 = 1090608; double myLots; double myATR; double StopLoss; double TakeProfit; double myPoint; int ticket1 = -1; int ticket1type = -1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- double TickSize = MarketInfo(Symbol(),MODE_TICKSIZE); if (TickSize == 0.00001 || TickSize == 0.001) pips = TickSize*10; else pips = TickSize; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // Making ATR and defining SL & TP { myATR = iATR(NULL, PERIOD_D1, ATR_PeriodValue, 1)/pips; TakeProfit = myATR * TakeProfitPercent/100.0; StopLoss = myATR * StopLossPercent/100.0; } //---Defining iCustom Indicators { double CurrentFast=iCustom(NULL,0,"Indicators\\Trendtwolinecross\\(i)OBVmod_indicator_MA",EAFastMA,EAFastMAShift,EAFastMAMethod,1,1); double CurrentSlow=iCustom(NULL,0,"Indicators\\Trendtwolinecross\\(i)OBVmod_indicator_MA",EASlowMA,EASlowMAShift,EASlowMAMethod,1,1); double PreviousFast=iCustom(NULL,0,"Indicators\\Trendtwolinecross\\(i)OBVmod_indicator_MA",EAFastMA,EAFastMAShift,EAFastMAMethod,1,2); double PreviousSlow=iCustom(NULL,0,"Indicators\\Trendtwolinecross\\(i)OBVmod_indicator_MA",EASlowMA,EASlowMAShift,EASlowMAMethod,1,2); int TicketBuy; int TicketBuyBE; int TicketSell; int TicketSellBE; { if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow && OrdersTotal()>0) { //code to close Sell order(s) int total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; switch(type) { //Close opened long positions case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); break; //Close opened short positions case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); } } } if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow && OrdersTotal()>0) { //code to close buy order(s) int total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; switch(type) { //Close opened long positions case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); break; //Close opened short positions case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); } } } //----code to open orders if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow && OrdersTotal()==0) //open buy order if no open orders { //code to open buy order with TP 1 x ATR TicketBuy= OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,1,0,Green); // Open Buy order without TP TicketBuyBE=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),NULL,NULL,2,0,Green); } if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow && OrdersTotal()==0) //open sell order if no open orders { //code to open sell orderwith TP 1x ATR TicketSell=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,1,0,Red); // Open Sell order without TP TicketSellBE=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),NULL,NULL,2,0,Red); } } } // Move SL for trades without tp to BE //For Buy Orders { int BuyOrderTP; //for(int b1=OrdersTotal()-1;b1>=0;b1--) { if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==1) if(OrderType()==OP_BUY) BuyOrderTP=OrderTakeProfit(); } //for(int b=OrdersTotal()-1;b>=0;b--) { if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==2) if(OrderType()==OP_BUY) if(Ask>BuyOrderTP) if(OrderOpenPrice()>OrderStopLoss()); OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsLockedIn*pips),OrderTakeProfit(),0,clrGreen); } } //For Sell Orders { int SellOrderTP; //for(int s1=OrdersTotal()-1;s1>=0;s1--) { if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==1) if(OrderType()==OP_SELL) SellOrderTP=OrderTakeProfit(); } //for(int s=OrdersTotal()-1;s>=0;s--) { if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==2) if(OrderType()==OP_SELL) if(Bid<SellOrderTP) if(OrderOpenPrice()<OrderStopLoss()); OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsLockedIn*pips),OrderTakeProfit(),0,clrRed); } } } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Basically, I want my EA to manage open trades with OrderModify().
My EA is programmed to take trades when 2 Moving Averages are crossing.
Every time the 2 Moving Averages cross, it opens 2 trades.
Trade 1:
Take Profit = 1.0 * ATR
Stop Loss = 1.5 * ATR
Trade 2:
No Take Profit
Stop Loss = 1.5 * ATR
When price hits Take Profit on that one trade, I want my EA to simply move Stop Loss to Breakeven on the other trade.
But how do I program it to know what the Take Profit (1.0 * ATR) was at that specific moment it opened the trade?
All feedback is greatly appreciated!