Hello,
I am trying for a couple of days to solve 1 problem in my EA which I can not solve.
The "issue" is following:
Lets say I have a Sell trade running. After few positive pips the price reverses and the initial sell trade becomes negative. After x pips in wrong direction I want to open 1 hedge (buy) trade for
this specific initial sell trade. So far so good. I managed to count pips for each trade and know when I have to hedge it. I can also send the hedge order. The problem is that my rule of negative pips is true for a long time (each tick, as long the pips are negative) so the EA keeps opening hedge trades...I just dont know how I could control this to open 1 hedge for 1 initial trade.
This is my code:
Can anybody guide me please in the right direction?
Thanks a lot and have a good day.
Use a boolean status flag to indicate if you have already hedged or not and take action accordingly.
But really there is no point in doing what you are calling hedging because it is not. You are paying (via additional spread) to go flat whilst the market moves without you trading it. So you are simply losing trading opportunites and paying for the privilage.
It is the curse of the retail trader to think hedging means taking an opposite trade.
Use a boolean status flag to indicate if you have already hedged or not and take action accordingly.
But really there is no point in doing what you are calling hedging because it is not. You are paying (via additional spread) to go flat whilst the market moves without you trading it. So you are simply losing trading opportunites and paying for the privilage.
It is the curse of the retail trader to think hedging means taking an opposite trade.
Thanks for your answer. I know about the trade issue and I take that "risk" because its just the initial step for further development. I was thinking of something like:
- loop all floating trades. if a trade is found that is x pips in negative --> open counter trade AND
- put that trade into an array with unique identifier (Pair+OrderOpenPrice+Magic1 + 0 -> no counter trade or 1 -> counter trade active).
The problem is that I cant get them into an array. Or maybe there are other - more simple ways - to flag the trades...
Am really lost here...
Thanks for your answer. I know about the trade issue and I take that "risk" because its just the initial step for further development. I was thinking of something like:
- loop all floating trades. if a trade is found that is x pips in negative --> open counter trade AND
- put that trade into an array with unique identifier (Pair+OrderOpenPrice+Magic1 + 0 -> no counter trade or 1 -> counter trade active).
The problem is that I cant get them into an array. Or maybe there are other - more simple ways - to flag the trades...
Am really lost here...
Try these structures :
You will also need to send the log structure down to the SetHedge function along with the original ticket so that you can log the hedge ticket
struct hedge_log_entry{ int ticket,hedged_with; hedge_log_entry(void){ticket=-1;hedged_with=-1;} void set(int _ticket){ ticket=_ticket; hedged_with=-1; } void set_hedge(int _hedge_ticket){ hedged_with=_hedge_ticket; } }; struct hedge_log{ hedge_log_entry hedges[]; hedge_log(void){reset();} ~hedge_log(void){reset();} void reset(){ArrayFree(hedges);} int add(int _ticket){ int ns=ArraySize(hedges)+1; ArrayResize(hedges,ns,0); hedges[ns-1].set(_ticket); return(ns-1); } int find(int _ticket){ for(int i=0;i<ArraySize(hedges);i++){ if(hedges[i].ticket==_ticket){return(i);} } return(-1); } int find_and_set_hedged_ticket(int _original_ticket,int _its_hedged_ticket){ //find the original trade int found=find(_original_ticket); //if not found , log if(found==-1){found=add(_original_ticket);} //set hedged ticket hedges[found].set_hedge(_its_hedged_ticket); return(found); } int remove(int _ix){ int ns=ArraySize(hedges)-1; hedges[_ix]=hedges[ns]; if(ns>0){ArrayResize(hedges,ns,0);}else{ ArrayFree(hedges); } return(ns); } bool is_this_hedged_or_a_hedge(int _ticket){ bool exists=false,is_hedged=false,is_hedge=false; for(int i=0;i<ArraySize(hedges);i++){ /* you are interested in trades that are not hedged and that are not hedges themselves its also a fact that if you look for whether or not a trade is hedged you are interested in that trade so we will add it */ //if this trade exists if(hedges[i].ticket==_ticket){ exists=true; //if it is hedged if(hedges[i].hedged_with!=-1){ is_hedged=true; } //we found it so bounce break; } //if this trade is a hedge if(hedges[i].hedged_with==_ticket){ is_hedge=true; //we found it so bounce break; } } //if it did not exist and it was not a hedge , log it if(!exists&&!is_hedge){add(_ticket);} return(is_hedged||is_hedge); } }; hedge_log HL; void HedgeMyPortfolio(string sym="", int type_=-1, int trademagic_=-1,hedge_log &_log) { if (sym == "") { sym = Symbol(); } for(int i=OrdersTotal()-1; i>=0; i--) { if ( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) ) { if ( sym=="" || OrderSymbol()==sym ) { if ( type_<0 || OrderType()==type_ || OrderType()==OP_BUY || OrderType()==OP_SELL ) { if ( trademagic_<0 || OrderMagicNumber()==trademagic_ ) { //if order we are checking is not hedged or not a hedge itself , continue processing if(!_log.is_this_hedged_or_a_hedge(OrderTicket())) { if (OrderType()==OP_BUY) { if ( GetProfitPips(OrderTicket(),sym,OP_BUY,Magic1)<=-50) SetHedge(OP_SELL,MarketInfo(sym,MODE_BID),Magic2); } else if (OrderType()==OP_SELL) { if ( GetProfitPips(OrderTicket(),sym,OP_SELL,Magic1)<=-50) SetHedge(OP_BUY,MarketInfo(sym,MODE_ASK),Magic2); } } // } } } } } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I am trying for a couple of days to solve 1 problem in my EA which I can not solve.
The "issue" is following:
Lets say I have a Sell trade running. After few positive pips the price reverses and the initial sell trade becomes negative. After x pips in wrong direction I want to open 1 hedge (buy) trade for
this specific initial sell trade. So far so good. I managed to count pips for each trade and know when I have to hedge it. I can also send the hedge order. The problem is that my rule of negative pips is true for a long time (each tick, as long the pips are negative) so the EA keeps opening hedge trades...I just dont know how I could control this to open 1 hedge for 1 initial trade.
This is my code:
Can anybody guide me please in the right direction?
Thanks a lot and have a good day.