Rejoindre la communauté
banner_forum
Devenez membre de la plus grande
communauté francophone sur le Forex
Partagez, échangez et apprenez en gagnant des crédits sur votre compte de trading

Modifier un EA pour qu'il inverse ses trades

  • estebane

    Bonjour à tous,
    j'ai téléchargé il y a quelques semaines un EA gratuit qui fonction avec la parabole de sar.
    Les résultats ne sont pas probants alors je me demandais s'il était possible de modifier le code source de façon à inverser les trades, c'est à dire que lorsque l'EA déclanche normalement un Sell celui-ci mette à la place un Buy et vice-versa ? Dans la logique, les pertes devraient se transformer en gains. Quelqu'un sait-il faire ça ? J'ai essayé mais je n'y parvient pas j'ai des erreurs dans le code que je ne parviens pas à résoudre faute de connaissance.
    Merci à vous.

    voici le code source non modifié :

    Code
    //+------------------------------------------------------------------+ //| pp2.mq4 | //| Copyright © 2011, | //| | //+------------------------------------------------------------------+ #property copyright " www.FxAutomated.com" #property link "http://www.FxAutomated.com" //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ //+------------------------------------------------------------------ extern string ToDonateAndMoreVisit ="www.FxAutomated.com"; extern string ForexSignals ="www.TradingBug.com"; extern string ManagedAccounts ="www.TradingBug.com"; extern int StopLoss =90; // SL for an opened order extern int TakeProfit =20; // ?? for an opened order extern bool Trailing =false; // turn trailing on or off extern int Trail_Stop=10; // Trailing distance extern double Lots =0.1; // Strictly set amount of lots extern int Slippage =5; // Price Slippage extern string Caution ="Advanced settings follow. Dont change if you dont know what you are doing."; extern double Step =0.001; //Parabolic setting extern double Maximum =0.2; //Parabolic setting extern int OpenDel =0; // wont open if current price has already covered this in the positive from bar open price extern int OpenAft =0; // wont open if movement hasnt covered this peeps in the positive extern bool SARclose =true; // enable or disable pSAR signals to close orders on signal shift extern bool Reverse =false; // enable or disable /*****************************************************-----READ THIS-------****************************************************** *******************************************************************************************************************************/ //----------------------------------------------------------------------------------------------------------------------------- /*DONATE TO SUPPORT MY FREE PROJECTS AND TO RECEIVE NON OPEN PROJECTS AND ADVANCED VERSIONS OF EXISTING PROJECTS WHEN AVAILABLE: //------------------------------------------------------------------------------------------------------------------------------ __my paypal and moneybookers email is [email protected] anyone can easily join moneybookers at www.moneybookers.com or paypal at www.paypal.com and pay people via their email through numerous payment methods__*/ //------------------------------------------------------------------------------------------------------------------------------ //SUPPORT AND INQUIRIES EMAIL: [email protected] //------------------------------------------------------------------------------------------------------------------------------ /******************************************************************************************************************************* *************************************************--------END------*************************************************************/ //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //trading criteria bool result; double price; int cmd,error,TP,SL,StopMultd,OpenDelayBuy,OpenDelaySell,OpenAfterBuy,OpenAfterSell,OpenAfter,OpenDelay,Tral_Stop; int digits=MarketInfo("EURUSD",MODE_DIGITS); if(digits==5){StopMultd=10;} else{StopMultd=1;} TP=NormalizeDouble(TakeProfit*StopMultd,Digits); SL=NormalizeDouble(StopLoss*StopMultd,Digits); OpenDelay=OpenDel*StopMultd; OpenAfter=OpenAft*StopMultd; Tral_Stop=Trail_Stop*StopMultd; string csymb=Symbol(); double bidp =MarketInfo(csymb,MODE_BID); double askp =MarketInfo(csymb,MODE_ASK); OpenDelayBuy=bidp-iLow(NULL,0,0)*Point; OpenDelaySell=iHigh(NULL,0,0)-askp*Point; OpenAfterBuy=bidp-iLow(NULL,0,0)*Point; OpenAfterSell=iHigh(NULL,0,0)-askp*Point; //-------------------------------------------------------------------+ //Check open orders //-------------------------------------------------------------------+ if(OrdersTotal()>0){ for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders { if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available { if(OrderMagicNumber()==110001) {int halt1=1;} } } } //-------------------------------------------------------------------+ /*-----------------------------------------------------------------------------------------------------------------------*/ if(Reverse==false){ //start reverse is false /*-----------------------------------------------------------------------------------------------------------------------*/ if ((iSAR(NULL, 0,Step,Maximum, 0)<iClose(NULL,0,0))&&(iSAR(NULL, 0,Step,Maximum, 1)>iClose(NULL,0,1))) //Signal Buy { /*****************************************************/ if((SARclose==true)&&(OrdersTotal()>0)){ if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) { cmd=OrderType(); //---- open order is sell if(cmd==OP_SELL) { while(true) { if(cmd==OP_BUY) price=Bid; else price=Ask; result=OrderClose(OrderTicket(),OrderLots(),price,Slippage,CLR_NONE); if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); } else error=0; if(error==135) RefreshRates(); else break; } } } else Print( "Error when order select ", GetLastError()); } /******************************************************/ // Closing Sell if(((OpenDelay>=OpenDelayBuy)||(OpenDelay==0))&&(OpenAfter<OpenAfterBuy)){ //open buy if(halt1!=1) { int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,NormalizeDouble(Ask-SL*Point,Digits),NormalizeDouble(Ask+TP*Point,Digits),"psar bug 6 order ",110001);//Opening Buy if(openbuy==TRUE) {Alert("Order opened !!!"); PlaySound("alert.wav"); } } } } if ((iSAR(NULL, 0,Step,Maximum, 0)>iClose(NULL,0,0))&&(iSAR(NULL, 0,Step,Maximum, 1)<iClose(NULL,0,1))) //Signal Sell { /*************************************************/ if((SARclose==true)&&(OrdersTotal()>0)){ if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) { cmd=OrderType(); //---- open order is buy if(cmd==OP_BUY) { while(true) { if(cmd==OP_BUY) price=Bid; else price=Ask; result=OrderClose(OrderTicket(),OrderLots(),price,Slippage,CLR_NONE); if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); } else error=0; if(error==135) RefreshRates(); else break; } } } else Print( "Error when order select ", GetLastError()); } /****************************************************************/ // Closing Buy if(((OpenDelay>=OpenDelaySell)||(OpenDelay==0))&&(OpenAfter<OpenAfterSell)){ //open sell if(halt1!=1) { int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,NormalizeDouble(Bid+SL*Point,Digits),NormalizeDouble(Bid-TP*Point,Digits),"psar bug 6 order ",110001);//Opening Sell if(opensell==TRUE) {Alert("Order opened !!!"); PlaySound("alert.wav"); } } } } /*-----------------------------------------------------------------------------------------------------------------------*/ }//end reverse false /*-----------------------------------------------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------------------------------------------*/ else{ // reverse true /*-----------------------------------------------------------------------------------------------------------------------*/ if ((iSAR(NULL, 0,Step,Maximum, 0)<iClose(NULL,0,0))&&(iSAR(NULL, 0,Step,Maximum, 1)>iClose(NULL,0,1))) //Signal Buy { /*************************************************/ if((SARclose==true)&&(OrdersTotal()>0)){ if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) { cmd=OrderType(); //---- open order is buy if(cmd==OP_BUY) { while(true) { if(cmd==OP_BUY) price=Bid; else price=Ask; result=OrderClose(OrderTicket(),OrderLots(),price,Slippage,CLR_NONE); if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); } else error=0; if(error==135) RefreshRates(); else break; } } } else Print( "Error when order select ", GetLastError()); } /****************************************************************/ // Closing Buy if(((OpenDelay>=OpenDelaySell)||(OpenDelay==0))&&(OpenAfter<OpenAfterSell)){ //open sell if(halt1!=1) { int opensellr=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,NormalizeDouble(Bid+SL*Point,Digits),NormalizeDouble(Bid-TP*Point,Digits),"psar bug 6 order ",110001);//Opening Sell if(opensellr==TRUE) {Alert("Order opened !!!"); PlaySound("alert.wav"); } } } } if ((iSAR(NULL, 0,Step,Maximum, 0)>iClose(NULL,0,0))&&(iSAR(NULL, 0,Step,Maximum, 1)<iClose(NULL,0,1))) //Signal Sell { /*****************************************************/ if((SARclose==true)&&(OrdersTotal()>0)){ if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)) { cmd=OrderType(); //---- open order is sell if(cmd==OP_SELL) { while(true) { if(cmd==OP_BUY) price=Bid; else price=Ask; result=OrderClose(OrderTicket(),OrderLots(),price,Slippage,CLR_NONE); if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); } else error=0; if(error==135) RefreshRates(); else break; } } } else Print( "Error when order select ", GetLastError()); } /******************************************************/ // Closing Sell if(((OpenDelay>=OpenDelayBuy)||(OpenDelay==0))&&(OpenAfter<OpenAfterBuy)){ //open buy if(halt1!=1) { int openbuyr=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,NormalizeDouble(Ask-SL*Point,Digits),NormalizeDouble(Ask+TP*Point,Digits),"psar bug 6 order ",110001);//Opening Buy if(openbuyr==TRUE) {Alert("Order opened !!!"); PlaySound("alert.wav"); } } } } /*------------------------------------------------------------------------------------------------------------------------*/ } // end reverse true /*------------------------------------------------------------------------------------------------------------------------*/ if((Trailing==true)&&(OrdersTotal()>0)){ //----------------------------------------------------------------------------------------------------------------------- // Trail /*-----------------------------------------------------------------------------------------------------------------------*/ string Symb=Symbol(); // Symbol //------------------------------------------------------------------------------- 2 -- for(i=1; i<=OrdersTotal(); i++) // Cycle searching in orders { if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available { // Analysis of orders: int Tip=OrderType(); // Order type if(OrderSymbol()!=Symb||Tip>1)continue;// The order is not "ours" double SLt=OrderStopLoss(); // SL of the selected order //---------------------------------------------------------------------- 3 -- while(true) // Modification cycle { double TS=Tral_Stop; // Initial value int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);//Min. distance if (TS < Min_Dist) // If less than allowed TS=Min_Dist; // New value of TS //------------------------------------------------------------------- 4 -- bool Modify=false; // Not to be modified switch(Tip) // By order type { case 0 : // Order Buy if (NormalizeDouble(SLt,Digits)< // If it is lower than we want NormalizeDouble(Bid-TS*Point,Digits)) { SLt=Bid-TS*Point; // then modify it string Text="Buy "; // Text for Buy Modify=true; // To be modified } break; // Exit 'switch' case 1 : // Order Sell if (NormalizeDouble(SLt,Digits)> // If it is higher than we want NormalizeDouble(Ask+TS*Point,Digits) || NormalizeDouble(SLt,Digits)==0)//or equal to zero { SLt=Ask+TS*Point; // then modify it Text="Sell "; // Text for Sell Modify=true; // To be modified } } // End of 'switch' if (Modify==false) // If it is not modified break; // Exit 'while' //------------------------------------------------------------------- 5 -- double TPt =OrderTakeProfit(); // TP of the selected order double Price =OrderOpenPrice(); // Price of the selected order int Ticket=OrderTicket(); // Ticket of the selected order Alert ("Modification ",Text,Ticket,". Awaiting response.."); bool Ans=OrderModify(Ticket,Price,SLt,TPt,0);//Modify it! //------------------------------------------------------------------- 6 -- if (Ans==true) // Got it! :) { Alert ("Order ",Text,Ticket," is modified:)"); break; // From modification cycle. } //------------------------------------------------------------------- 7 -- break; } // End of modification cycle //---------------------------------------------------------------------- 8 -- } // End of order analysis } // End of order search //------------------------------------------------------------------------------- 9 -- int Error=GetLastError(); // Failed :( switch(Error) // Overcomable errors { case 130:Alert("Wrong stops. Retrying."); RefreshRates(); // Update data // At the next iteration case 136:Alert("No prices. Waiting for a new tick.."); while(RefreshRates()==false) // To the new tick Sleep(1); // Cycle delay // At the next iteration case 146:Alert("Trading subsystem is busy. Retrying "); Sleep(500); // Simple solution RefreshRates(); // Update data // At the next iteration // Critical errors case 2 : Alert("Common error."); // Exit 'switch' case 5 : Alert("Old version of the client terminal."); // Exit 'switch' case 64: Alert("Account is blocked."); // Exit 'switch' case 133:Alert("Trading is prohibited"); // Exit 'switch' default: Alert("Occurred error ",Error);//Other errors } // From modification cycle //------------------------------------------------------------------------------------------------------------------------ // end trail //-------------------------------------------------------------------------------------------------------------------------- } //---------- return(0); } //------------------------------------------------------------------+
    Modifié le 2014-09-03 17:56:06 par AliX
  • JJFlash

    La fonction qui passe les ordres d'achat ou de vente c'est "OrderSend".
    Tu mets OP_SELL à la place de OP_BUY, et vice versa. Faut que tu changes les takeprofit et les stoploss, sinon il va te sortir un refus de lancer le trade
  • estebane — en réponse à JJFlash dans son message #96744

    Bonsoir,
    merci pour ta réponse, c'est ce que j'avais fais sans succès, peut-être effectivement à cause des SL et TP mais peux-tu développer ce que tu veux dire par changer les SL et TP ?
  • JJFlash

    http://docs.mql4.com/trading/ordersend
    Sur ce lien tu as tout les paramètres qu'il faut pour la fonction OrderSend. Dans ces paramètres tu as le stoploss et le takeprofit. Il faut que tu les modifies pour que les takeprofit et stoploss correspondent bien au OP_SELL et OP_BUY que tu as échangé.
  • estebane

    celà reste du chinois pour moi mais merci pour ta réponse, je vais essayer de me dépatouiller.
  • estebane

    Bonjour,
    malgrés toute ma bonne volonté je n'y parviens pas, meme avec l'EA Moving Average (qui ne contient pas de stop loss ou TP), il y aurait-il une ame charitable qui accepterais de m'inverser les ordres sur l'EA moving average svp ? C'est à dire que l'EA place un sell à la place d'un buy et vice versa.

    le code :
    Code
    //+------------------------------------------------------------------+ //| Moving Average.mq4 | //| Copyright 2005-2014, MetaQuotes Software Corp. | //| http://www.mql4.com | //+------------------------------------------------------------------+ #property copyright "2005-2014, MetaQuotes Software Corp." #property link "http://www.mql4.com" #property description "Moving Average sample expert advisor" #define MAGICMA 20131111 //--- Inputs input double Lots =0.1; input double MaximumRisk =0.02; input double DecreaseFactor=3; input int MovingPeriod =12; input int MovingShift =6; //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //--- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //--- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ double LotsOptimized() { double lot=Lots; int orders=HistoryTotal(); // history orders total int losses=0; // number of losses orders without a break //--- select lot size lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //--- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for(int i=orders-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //--- if(OrderProfit()>0) break; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); } //--- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double ma; int res; //--- go trading only for first tiks of new bar if(Volume[0]>1) return; //--- get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //--- sell conditions if(Open[1]>ma && Close[1]<ma) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); return; } //--- buy conditions if(Open[1]<ma && Close[1]>ma) { res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue); return; } //--- } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { double ma; //--- go trading only for first tiks of new bar if(Volume[0]>1) return; //--- get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //--- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue; //--- check order type if(OrderType()==OP_BUY) { if(Open[1]>ma && Close[1]<ma) { if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White)) Print("OrderClose error ",GetLastError()); } break; } if(OrderType()==OP_SELL) { if(Open[1]<ma && Close[1]>ma) { if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White)) Print("OrderClose error ",GetLastError()); } break; } } //--- } //+------------------------------------------------------------------+ //| OnTick function | //+------------------------------------------------------------------+ void OnTick() { //--- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //--- calculate open orders by current symbol if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else CheckForClose(); //--- } //+------------------------------------------------------------------+
    Modifié le 2014-10-06 10:10:25 par estebane
  • foreartgone

    une fois la modif effectuée!
    enregister, puis compile...
    si correct pas d'erreur... dans l'éditeur du mt4, mt5...
    il y a plusieurs fichiers qui interviennent...