Aide au codage - page 156

 
Marco320:
Cher MLaden,

Le prochain indice que je peux obtenir sur le graphique dans mon compte Live et sur la démo de Markets.com (CFD's). Soit il y a un problème pour l'obtenir sur le graphique chez Liquid Markets en démo. Parce que Liquid offre de nombreuses options d'instruments (en particulier les Futures dans MT4), je voudrais les avoir sur le graphique dans cette plateforme. Pouvez-vous voir un problème pour résoudre cette question, car je ne trouve pas.

Merci Marco

C'est probablement dû au fait que cet indicateur est un indicateur décompilé. Depuis la version 500, Metarader empêche la compilation de tels indicateurs.

 
mladen:
C'est probablement dû au fait que cet indicateur est un indicateur décompilé. Depuis la version 500, metarader empêche la compilation de tels indicateurs.

Bonjour MLaden,

Merci, mais c'est étrange parce que toutes les plateformes MT4 des courtiers que j'utilise, sont la version 500.

Salutations Marco

 

J'ai un EA dans lequel je voudrais utiliser une fonction de prise de profit partielle. J'ai codé cette fonction et une fois elle fonctionne bien, d'autres fois elle ne fonctionne pas. Je n'ai pas été capable de comprendre quel est le problème...

Cher Mladen ou MrTools, Auriez-vous la gentillesse de regarder le code et de me donner un conseil sur la façon dont je pourrais trouver le bug et résoudre la fonction de prise de profit partielle ?

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

int signal;

CheckOrders();

int numTrades = GetNumTickets();

signal = CheckSignal();

if (signal == OP_BUY)

{

OpenBuy();

lasttime = TimeCurrent();

}

if (signal == OP_SELL)

{

OpenSell();

lasttime = TimeCurrent();

}

return;

}

// +----------------------------------------------------------------------------+

// | Order checking function

// +----------------------------------------------------------------------------+

void CheckOrders()

{

int typ,i,cnt,ticket;

int Mv1 = 0;

int SL1 = 0;

int Mv2 = 0;

int SL2 = 0;

int Mv3 = 0;

int id;

double PipDist, CLots, buy_nextTP, sell_nextTP;

cnt = OrdersTotal();

for (i=cnt-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)

{

typ = OrderType();

//

if (typ == OP_BUY)

{

if (TrailingStopLoss)

{

if (AdjTrailOnAllOrders(2, buy_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false)

Alert("Scalp_RSI: Modifying BUY order failed!");

}

PipDist = (NormalizeDouble(Bid,Digits) - NormalizeDouble(OrderOpenPrice(),Digits));

PipDist = MathFloor(PipDist/myPoint);

}

if (typ == OP_SELL)

{

if(TrailingStopLoss)

{

if (AdjTrailOnAllOrders(2, sell_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false)

Alert("Scalp_RSI: Modifying SELL order failed!");

}

PipDist = (NormalizeDouble(OrderOpenPrice(),Digits) - NormalizeDouble(Ask,Digits));

PipDist = MathFloor(PipDist/myPoint);

}

if (TakePartialProfit == true && PartCloseLots > 0)

{

if (UseMoneyMgmt != 1) Lotsi = Lots;

CLots = NormalizeDouble(Lotsi * PartCloseLots/100,Decimals); // Computing lots to close

if (CLots < MarketInfo(Symbol(), MODE_MINLOT))

CLots = MarketInfo(Symbol(), MODE_MINLOT);

PartialTP(PipDist,OrderTicket(),CLots);

}

}

}

}

}

//+------------------------------------------------------------------+

//| PartialTP - i.e. TakePartialProfit function

//+------------------------------------------------------------------+

void PartialTP(int pipsval, int ticket, double CLots) //

{

int gle;

int loopcount = 0;

string bER;

string myInfo = "PartialTP";

if(OrderSelect(ticket, SELECT_BY_TICKET)==true)

{

if (OrderType() == OP_BUY)

{

if ((pipsval >= buy_PartialTP) && (pipsval < buy_TakeProfit+1)) //

{

while(true)

{

OrderClose(ticket,CLots,Bid,3,YellowGreen);

gle=GetLastError();

bER=" error="+gle+" "+ErrorDescription(gle);

if(gle==0 || gle==1)

{

logwrite(TradeComment,"CLOSE BUY "+myInfo+ " " + CLots + " at Bid= " + Bid);

buy_PartialTP = buy_TakeProfit+1; //

break;

}

else

{

logwrite(TradeComment,"---ERROR--- CLOSE BUY "+myInfo+bER+ " " + CLots + " at Bid= " + Bid);

RefreshRates();

Sleep(500);

}

loopcount++;

if(loopcount>maxloop)

{

logwrite(TradeComment,"---ERROR--- Giving up on closing BUY order");

return(gle);

}

}

}

}

if (OrderType() == OP_SELL) //

{

if ((pipsval >= sell_PartialTP) && (pipsval < sell_TakeProfit)) //

{

while(true)

{

OrderClose(ticket,CLots,Ask,3,YellowGreen);

gle=GetLastError();

bER=" error="+gle+" "+ErrorDescription(gle);

if(gle==0 || gle==1)

{

logwrite(TradeComment,"CLOSE SELL "+myInfo+ " " + CLots + " at Ask= " + Ask);

sell_PartialTP = sell_TakeProfit; //

break;

}

else

{

logwrite(TradeComment,"---ERROR--- CLOSE SELL "+myInfo+bER+ " " + CLots + " at Ask= " + Ask);

RefreshRates();

Sleep(500);

}

loopcount++;

if(loopcount>maxloop)

{

logwrite(TradeComment,"---ERROR--- Giving up on closing SELL order");

return(gle);

}

}

}

}

} else {

gle=GetLastError();

bER=" Error="+gle+" "+ErrorDescription(gle);

if (gle != 0 || gle!=1)

{

logwrite(TradeComment,"---ERROR--- in selecting order in PartialTP function "+bER);

}

}

}

//+------------------------------------------------------------------+

//| Get number of open trades

//+------------------------------------------------------------------+

int GetNumTickets()

{

int i;

int typ;

numTickets = 0;

for (i = OrdersTotal()-1; i >= 0; i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)

{

typ = OrderType();

if (typ == OP_BUY || typ == OP_SELL)

{

numTickets ++;

}

}

}

}

return (numTickets);

}

//+------------------------------------------------------------------+

//| Get signal

//+------------------------------------------------------------------+

int CheckSignal()

{

return (SignalScalp());

}

//+------------------------------------------------------------------+

//| Get scalp signal

//+------------------------------------------------------------------+

int SignalScalp()

{

int signal=6; //

if (enable_buy){

// Buy-Condition

{

signal = OP_BUY;

}

}

//

//

if (enable_sell){

// Sell-Condition

{

signal = OP_SELL;

}

}

//

//

//

return(signal);

}

//-----

// +----------------------------------------------------------------------------+

// | Buy Order

// +----------------------------------------------------------------------------+

void OpenBuy()

{

int gle = 0;

int ticket = 0;

double SL = 0;

double TP = 0;

double LotsB = Lots; //GetLots();

int loopcount = 0;

while(true)

{

ticket=OrderSend(Symbol(),OP_BUY,LotsB,Ask,Slippage,0,0,TradeComment,Magic,White);

}

}

// +----------------------------------------------------------------------------+

// | Sell Order

// +----------------------------------------------------------------------------+

void OpenSell()

{

int gle = 0;

int ticket = 0;

double SL = 0;

double TP = 0;

double LotsS = Lots;

int loopcount = 0;

while(true)

{

ticket=OrderSend(Symbol(),OP_SELL,LotsS,Bid,Slippage,0,0,TradeComment,Magic,Red);

}

}

bool AdjTrailOnAllOrders(

int TrailType,

int TrailPips,

int Magic,

int Direction,

int FirstMove,

int FirstStopLoss,

int SecondMove,

int SecondStopLoss,

int ThirdMove)

{

double retValue;

return(retValue);

}

//+------------------------------------------------------------------+

//| SetComment function

//+------------------------------------------------------------------+

void SetComment(string s) { TradeComment = s; }

// +----------------------------------------------------------------------------+

// | Pip setting

// +----------------------------------------------------------------------------+

double SetPoint()

{

double mPoint;

return(mPoint);

}

// +----------------------------------------------------------------------------+

// | Log write function

// +----------------------------------------------------------------------------+

void logwrite (string filename, string mydata)

{

int myhandle;

}

 
chrisstoff:
J'ai un EA dans lequel je voudrais utiliser une fonction de prise de profit partielle. J'ai codé cette fonction et une fois elle fonctionne bien, d'autres fois elle ne fonctionne pas. Je n'ai pas réussi à comprendre quel est le problème...

Cher Mladen ou MrTools, auriez-vous l'amabilité d'examiner le code et de me donner un conseil sur la façon dont je pourrais trouver le bug et résoudre la fonction de prise partielle ?

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

int signal;

CheckOrders();

int numTrades = GetNumTickets();

signal = CheckSignal();

if (signal == OP_BUY)

{

OpenBuy();

lasttime = TimeCurrent();

}

if (signal == OP_SELL)

{

OpenSell();

lasttime = TimeCurrent();

}

return;

}

// +----------------------------------------------------------------------------+

// | Order checking function

// +----------------------------------------------------------------------------+

void CheckOrders()

{

int typ,i,cnt,ticket;

int Mv1 = 0;

int SL1 = 0;

int Mv2 = 0;

int SL2 = 0;

int Mv3 = 0;

int id;

double PipDist, CLots, buy_nextTP, sell_nextTP;

cnt = OrdersTotal();

for (i=cnt-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)

{

typ = OrderType();

//

if (typ == OP_BUY)

{

if (TrailingStopLoss)

{

if (AdjTrailOnAllOrders(2, buy_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false)

Alert("Scalp_RSI: Modifying BUY order failed!");

}

PipDist = (NormalizeDouble(Bid,Digits) - NormalizeDouble(OrderOpenPrice(),Digits));

PipDist = MathFloor(PipDist/myPoint);

}

if (typ == OP_SELL)

{

if(TrailingStopLoss)

{

if (AdjTrailOnAllOrders(2, sell_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false)

Alert("Scalp_RSI: Modifying SELL order failed!");

}

PipDist = (NormalizeDouble(OrderOpenPrice(),Digits) - NormalizeDouble(Ask,Digits));

PipDist = MathFloor(PipDist/myPoint);

}

if (TakePartialProfit == true && PartCloseLots > 0)

{

if (UseMoneyMgmt != 1) Lotsi = Lots;

CLots = NormalizeDouble(Lotsi * PartCloseLots/100,Decimals); // Computing lots to close

if (CLots < MarketInfo(Symbol(), MODE_MINLOT))

CLots = MarketInfo(Symbol(), MODE_MINLOT);

PartialTP(PipDist,OrderTicket(),CLots);

}

}

}

}

}

//+------------------------------------------------------------------+

//| PartialTP - i.e. TakePartialProfit function

//+------------------------------------------------------------------+

void PartialTP(int pipsval, int ticket, double CLots) //

{

int gle;

int loopcount = 0;

string bER;

string myInfo = "PartialTP";

if(OrderSelect(ticket, SELECT_BY_TICKET)==true)

{

if (OrderType() == OP_BUY)

{

if ((pipsval >= buy_PartialTP) && (pipsval < buy_TakeProfit+1)) //

{

while(true)

{

OrderClose(ticket,CLots,Bid,3,YellowGreen);

gle=GetLastError();

bER=" error="+gle+" "+ErrorDescription(gle);

if(gle==0 || gle==1)

{

logwrite(TradeComment,"CLOSE BUY "+myInfo+ " " + CLots + " at Bid= " + Bid);

buy_PartialTP = buy_TakeProfit+1; //

break;

}

else

{

logwrite(TradeComment,"---ERROR--- CLOSE BUY "+myInfo+bER+ " " + CLots + " at Bid= " + Bid);

RefreshRates();

Sleep(500);

}

loopcount++;

if(loopcount>maxloop)

{

logwrite(TradeComment,"---ERROR--- Giving up on closing BUY order");

return(gle);

}

}

}

}

if (OrderType() == OP_SELL) //

{

if ((pipsval >= sell_PartialTP) && (pipsval < sell_TakeProfit)) //

{

while(true)

{

OrderClose(ticket,CLots,Ask,3,YellowGreen);

gle=GetLastError();

bER=" error="+gle+" "+ErrorDescription(gle);

if(gle==0 || gle==1)

{

logwrite(TradeComment,"CLOSE SELL "+myInfo+ " " + CLots + " at Ask= " + Ask);

sell_PartialTP = sell_TakeProfit; //

break;

}

else

{

logwrite(TradeComment,"---ERROR--- CLOSE SELL "+myInfo+bER+ " " + CLots + " at Ask= " + Ask);

RefreshRates();

Sleep(500);

}

loopcount++;

if(loopcount>maxloop)

{

logwrite(TradeComment,"---ERROR--- Giving up on closing SELL order");

return(gle);

}

}

}

}

} else {

gle=GetLastError();

bER=" Error="+gle+" "+ErrorDescription(gle);

if (gle != 0 || gle!=1)

{

logwrite(TradeComment,"---ERROR--- in selecting order in PartialTP function "+bER);

}

}

}

//+------------------------------------------------------------------+

//| Get number of open trades

//+------------------------------------------------------------------+

int GetNumTickets()

{

int i;

int typ;

numTickets = 0;

for (i = OrdersTotal()-1; i >= 0; i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)

{

typ = OrderType();

if (typ == OP_BUY || typ == OP_SELL)

{

numTickets ++;

}

}

}

}

return (numTickets);

}

//+------------------------------------------------------------------+

//| Get signal

//+------------------------------------------------------------------+

int CheckSignal()

{

return (SignalScalp());

}

//+------------------------------------------------------------------+

//| Get scalp signal

//+------------------------------------------------------------------+

int SignalScalp()

{

int signal=6; //

if (enable_buy){

// Buy-Condition

{

signal = OP_BUY;

}

}

//

//

if (enable_sell){

// Sell-Condition

{

signal = OP_SELL;

}

}

//

//

//

return(signal);

}

//-----

// +----------------------------------------------------------------------------+

// | Buy Order

// +----------------------------------------------------------------------------+

void OpenBuy()

{

int gle = 0;

int ticket = 0;

double SL = 0;

double TP = 0;

double LotsB = Lots; //GetLots();

int loopcount = 0;

while(true)

{

ticket=OrderSend(Symbol(),OP_BUY,LotsB,Ask,Slippage,0,0,TradeComment,Magic,White);

}

}

// +----------------------------------------------------------------------------+

// | Sell Order

// +----------------------------------------------------------------------------+

void OpenSell()

{

int gle = 0;

int ticket = 0;

double SL = 0;

double TP = 0;

double LotsS = Lots;

int loopcount = 0;

while(true)

{

ticket=OrderSend(Symbol(),OP_SELL,LotsS,Bid,Slippage,0,0,TradeComment,Magic,Red);

}

}

bool AdjTrailOnAllOrders(

int TrailType,

int TrailPips,

int Magic,

int Direction,

int FirstMove,

int FirstStopLoss,

int SecondMove,

int SecondStopLoss,

int ThirdMove)

{

double retValue;

return(retValue);

}

void SetComment(string s) { TradeComment = s; }

double SetPoint()

{

double mPoint;

return(mPoint);

}

void logwrite (string filename, string mydata)

{

int myhandle;

}

christoff

Je ne vois pas quelle valeur vous utilisez pour la variable "Decimals", mais c'est probablement la cause de votre problème. La partie résultante de l'ordre doit aussi être en accord avec la valeur du pas de lot (c'est-à-dire : si le pas de lot est de 0.1 par exemple, vous ne pouvez pas essayer de fermer 0.11 lots). Vérifiez quelle est la dernière erreur après la fermeture partielle qui ne réussit pas (vous pouvez le faire en ajoutant simplement un commentaire après les lignes qui vont comme celle-ci :

bER=" error="+gle+" "+ErrorDescription(gle) ;

si (gle>1) Commentaire(bER) ;

puisque la fonction logwrite dans le code n'est pas opérationnelle

 

Mladen,

Merci pour la réponse.

Eh bien, j'ai raccourci le code parce qu'il n'y a pas de possibilité pour un post d'être plus long que 10.000 caractères. Donc, dans le code original la variable Decimals est dans le init() comme suit :

LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);

if(LotStep == 0.01) {Decimals = 2;}

if(LotStep == 0.1) {Decimals = 1;}

if(LotStep == 1) {Decimals = 0;}[/PHP]

Yes, logwrite function is not functionable in the code posted, also because of the shortening. Normally it looks like:

[PHP]

void logwrite (string filename, string mydata)

{

int myhandle;

string wcalend=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS);

Print(mydata+" "+wcalend);

if(IsTesting()) return(0);

myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";");

if(myhandle>0)

{

FileSeek(myhandle,0,SEEK_END);

FileWrite(myhandle, mydata+" "+wcalend);

FileClose(myhandle);

}

}

Donc, je ne pense pas que le problème émerge à cause de la variable Decimals.

 
chrisstoff:
Mladen,

Merci pour la réponse.

Eh bien, j'ai raccourci le code parce qu'il n'y a pas de possibilité pour un message d'avoir plus de 10.000 caractères. Donc, dans le code original la variable Decimals est dans le init() comme suit :

LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);

if(LotStep == 0.01) {Decimals = 2;}

if(LotStep == 0.1) {Decimals = 1;}

if(LotStep == 1) {Decimals = 0;}[/PHP]

Yes, logwrite function is not functionable in the code posted, also because of the shortening. Normally it looks like:

[PHP]

void logwrite (string filename, string mydata)

{

int myhandle;

string wcalend=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS);

Print(mydata+" "+wcalend);

if(IsTesting()) return(0);

myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";");

if(myhandle>0)

{

FileSeek(myhandle,0,SEEK_END);

FileWrite(myhandle, mydata+" "+wcalend);

FileClose(myhandle);

}

}
Donc, je ne pense pas que le problème vienne de la variable Decimals.

Christoff

De cette façon, la variable Decimals devrait être OK

Dans tous les cas, vérifiez quel code d'erreur vous obtenez en cas d'échec de la clôture (partielle) de l'ordre.

 
mladen:
Je vois que vous n'utilisez pas le nombre magique, donc l'hypothèse est que le nombre ou le symbole magique n'a pas d'importance. Dans ce cas, vérifiez d'abord s'il y a déjà un ordre ouvert - comme ceci :
if (OrdersTotal()<1)

{

ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0);

stop=(Ask-stopsize*Point);

prof=(Ask+profsize*Point);

OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue);

}

Si vous décidez d'utiliser le numéro magique et le symbole, il est préférable de créer une fonction qui comptera les ordres ouverts pour le symbole et/ou le numéro magique spécifié.

Merci mladen.

si (OrdersTotal()<1

Cela a résolu mon problème.

Il Total peut en commander un, mais je veux que chaque paire de devises puisse être ouverte.

Je suis désolé de m'être mal fait comprendre.

Mais je veux dire que pour chaque paire, il faut exécuter un ordre et un stop. Exemple :

J'ouvre un ordre en EURUSD et je n'exécute qu'un seul ordre. Ensuite, d'autres devises peuvent également s'ouvrir en même temps et exécuter un ordre. Je suis désolé pour mon mauvais anglais.

-----------------------------------------------------------------

Merci.

 
hock87:
Merci mladen.

si (OrdersTotal()<1

Cela a résolu mon problème.

Il Total peut en commander un, mais je veux que chaque paire de devises puisse être ouverte.

Je suis désolé de m'être mal fait comprendre.

Mais je veux dire que pour chaque paire, il faut exécuter un ordre et un stop. Exemple :

J'ouvre un ordre en EURUSD et je n'exécute qu'un seul ordre. Ensuite, d'autres devises peuvent également s'ouvrir en même temps et exécuter un ordre. Je suis désolé de mon mauvais anglais.

-----------------------------------------------------------------

Merci.

hock87

Ajoutez ceci :

int TotalOrders = 0;

for (int i=0; i <= OrdersTotal(); i++)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderSymbol() == Symbol())

TotalOrders++;

}

avant la ligne if (OrdersTotal()<1) et remplacez cette ligne par if (TotalOrders<1). De cette façon, un seul ordre ouvert par symbole sera autorisé.

 

Merci beaucoup, mladen.

Je l'essaie maintenant.

 
hock87:
Merci mladen.

si (OrdersTotal()<1

Cela a résolu mon problème.

Il Total peut en commander un, mais je veux que chaque paire de devises puisse être ouverte.

Je suis désolé d'avoir mal compris.

Mais je veux dire que pour chaque paire, il faut exécuter un ordre et un stop. Exemple :

J'ouvre un ordre en EURUSD et je n'exécute qu'un seul ordre. Ensuite, d'autres devises peuvent également s'ouvrir en même temps et exécuter un ordre. Je suis désolé pour mon mauvais anglais.

-----------------------------------------------------------------

Merci.

hock87

S'il vous plaît lisez le post au-dessus de votre post pour une solution de ce problème.

Le code complet pour cela est le suivant :

int TotalOrders = 0;

for (int i=0; i <= OrdersTotal(); i++)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderSymbol() == Symbol())

TotalOrders++;

}

if (TotalOrders<1)

{

ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0);

stop=(Ask-stopsize*Point);

prof=(Ask+profsize*Point);

OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue);

}