I need help with the code below. Apparently it's not as functional as I want. After all I did it can only open a trade before stopping, I just want it to double the Tp and SL when the trade hit Sl and back to normal when it hits Takeprofit
- Need help for code
- Close all open orders when first order TP hit
- Swiss Army EA (Automatic order management)
Help anyone please, I will appreciate
Hello
The last deal of a position is the closing deal and its of the opposite type . So a sell will close as a buy .
What you needed to do (for hedging mode accounts) was get the position id of the last deal that closed
Get it's position id
Select all deals with that position id
then the first deal of the new selection is the opening deal
and the last deal of the new selection is the closing deal
also , you were doubling the entire price not just the sl + tp
this code also needs further filtration to ensure you are working with your ea's positions
//---------------------------------+ #include <Trade\Trade.mqh> CTrade trade; input double StopLoss = 60; // Initial Stop Loss in points input double TakeProfit = 60; // Initial Take Profit in points input double LotSize = 0.01; // Lot size for trading input int MaxTrades = 1; // Maximum number of trades to open int totalTrades = 0; // Counter for total trades opened input int MagicNumber = 12345; // Magic number to identify trades void Last_Trade_Check(bool &was_tp,bool &was_sl){ was_sl=false; was_tp=false; if(HistorySelect(0,TimeCurrent())){ //get the ticket of the last deal ulong ticket = HistoryDealGetTicket(HistoryDealsTotal()-1); //now get the id of the position of that deal long position_id=HistoryDealGetInteger(ticket,DEAL_POSITION_ID); //now select all deals for that position ID if(HistorySelectByPosition(position_id)){ //the first deal is the open ticket=HistoryDealGetTicket(0); double op=HistoryDealGetDouble(ticket,DEAL_PRICE); double sl=HistoryDealGetDouble(ticket,DEAL_SL); double tp=HistoryDealGetDouble(ticket,DEAL_TP); //the last deal is the close ticket=HistoryDealGetTicket(HistoryDealsTotal()-1); double cp=HistoryDealGetDouble(ticket,DEAL_PRICE); double prft=HistoryDealGetDouble(ticket,DEAL_PROFIT); //tp if(tp>0.0&&prft>0.0){was_tp=true;} //sl if(sl>0.0&&prft<0.0){was_sl=true;} } } } //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { // Perform cleanup tasks if needed } void OnTick() { bool wasTP=false,wasSL=false; Last_Trade_Check(wasTP,wasSL); if(PositionsTotal() < 1) { if(wasSL) { int slippage = 1000; double Price = SymbolInfoDouble(Symbol(), SYMBOL_BID); double SL = Price + StopLoss * Point() * 2.0; // Stop Loss for new trade double TP = Price - TakeProfit * Point() * 2.0; // Take Profit for new trade int ticket = Order_Send(Symbol(), 1, LotSize, Price, slippage, SL, TP, "FromLastSL", MagicNumber, 0); } if(wasTP || totalTrades == 0) { totalTrades++; int slippage = 1000; double Price = SymbolInfoDouble(Symbol(), SYMBOL_BID); double SL = Price + StopLoss * Point(); // Stop Loss for new trade double TP = Price - TakeProfit * Point(); // Take Profit for new trade int ticket = Order_Send(Symbol(), 1, LotSize, Price, slippage, SL, TP, "FromLastTP", MagicNumber, 0); } } } bool Order_Send(string symbol, int type, double volume, double price, int Slippage, double stoploss, double takeprofit, string comment, int magicnumber, datetime expiration) { trade.SetExpertMagicNumber(magicnumber); trade.SetDeviationInPoints(Slippage); if(type == 0 && trade.Buy(volume, symbol, price, stoploss, takeprofit, comment)) { return true; } if(type == 1 && trade.Sell(volume, symbol, price, stoploss, takeprofit, comment)) { return true; } return false; }
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register