Aiuto per la codifica - pagina 156

 
Marco320:
Caro MLaden,

Il prossimo indi che posso ottenere sul grafico nel mio conto Live e sul demo di Markets.com (CFD). O c'è un problema per ottenerlo sul grafico di Liquid Markets su demo. Dato che Liquid offre diverse opzioni di strumenti (in particolare Futures in MT4) vorrei averlo sul grafico in quella piattaforma. Si può vedere un problema che sorge questo problema, perché non riesco a trovare.

Thx Marco

Probabilmente è dovuto al fatto che quell'indicatore è un indicatore decompilato. Dalla build 500 metarader impedisce la compilazione di tali indicatori

 
mladen:
Probabilmente è dovuto al fatto che quell'indicatore è un indicatore decompilato. Dalla build 500 metarader impedisce la compilazione di tali indicatori

Ciao MLaden,

Grazie, ma è strano perché tutte le piattaforme MT4 dei broker che uso, sono la versione 500.

Saluti Marco

 

Ho un EA in cui vorrei utilizzare la funzione di take profit parziale. Ho codificato questa funzione e una volta funziona bene, altre volte no. Non sono riuscito a capire qual è il problema...

Caro Mladen o MrTools, sareste così gentili da esaminare il codice e darmi un consiglio su come potrei trovare il bug e risolvere la funzione di presa parziale?

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

//| 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:
Ho un EA in cui vorrei utilizzare la funzione di take profit parziale. Ho codificato questa funzione e una volta funziona bene, altre volte no. Non sono riuscito a capire qual è il problema...

Caro Mladen o MrTools, saresti così gentile da dare un'occhiata al codice e darmi un consiglio su come potrei trovare il bug e risolvere la funzione di presa parziale?

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

//| 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

Non riesco a vedere che valore stai usando per la variabile "Decimali", ma probabilmente questa è la causa del tuo problema. Anche la parte risultante dell'ordine deve essere conforme al valore del passo di lotto (cioè: se il passo di lotto è 0,1 per esempio, non puoi provare a chiudere 0,11 lotti). Controllate qual è l'ultimo errore dopo la chiusura parziale che non va a buon fine (potete farlo semplicemente aggiungendo un commento dopo le linee che vanno come questa:

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

if (gle>1) Comment(bER);

dato che la funzione logwrite nel codice non è operativa

 

Mladen,

Grazie per la risposta.

Beh, ho accorciato il codice perché non c'è la possibilità che un post sia più lungo di 10.000 caratteri. Quindi, nel codice originale la variabile Decimals si trova nell'init() come segue:

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);

}

}

Quindi, non credo che il problema emerga a causa della variabile Decimali.

 
chrisstoff:
Mladen,

Grazie per la risposta.

Beh, ho accorciato il codice perché non c'è la possibilità che un post sia più lungo di 10.000 caratteri. Quindi, nel codice originale la variabile Decimals si trova nell'init() come segue:

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);

}

}
Quindi, non credo che il problema emerga a causa della variabile Decimali.

Christoff

In questo modo la variabile Decimals dovrebbe essere OK

In ogni caso, controlla quale codice di errore stai ottenendo in caso di chiusura (parziale) dell'ordine non riuscita

 
mladen:
Vedo che non stai usando il numero magico, quindi il presupposto è che non importa quale sia il numero magico o il simbolo. In questo caso prima controlla se c'è già un ordine aperto - come questo:
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);

}

Se si decide di utilizzare il controllo del numero magico e del simbolo, allora è meglio fare una funzione che conterà gli ordini aperti per il simbolo specificato e / o il numero magico

Grazie mladen.

se (OrdiniTotali()<1

Aveva risolvere il mio problema.

È totale può ordinare uno, ma voglio che ogni coppia di valute può aprire.

Mi dispiace di aver frainteso.

Ma io intendo è in ogni coppia eseguire un ordine e stop. Esempio:

Ora ho aperto l'ordine in EURUSD eseguire un solo ordine. Poi altre valute anche possono aprire nello stesso tempo eseguire un ordine. Mi dispiace per il mio povero inglese.

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

Grazie.

 
hock87:
Grazie mladen.

se (OrdiniTotali()<1

Aveva risolvere il mio problema.

È totale può ordinare uno, ma voglio che ogni coppia di valute può aprire.

Mi dispiace di aver frainteso.

Ma io intendo è in ogni coppia eseguire un ordine e stop. Esempio:

Ora ho aperto l'ordine in EURUSD eseguire un solo ordine. Poi altre valute anche possono aprire nello stesso tempo eseguire un ordine. Mi dispiace per il mio povero inglese.

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

Grazie.

garretto87

Aggiungi questo :

int TotalOrders = 0;

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

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

if (OrderSymbol() == Symbol())

TotalOrders++;

}

prima della linea if (OrdersTotal()<1) e sostituisci quella linea con if (TotalOrders<1). In questo modo solo un ordine aperto per simbolo sarà permesso

 

Grazie mille, mladen.

Lo provo ora.

 
hock87:
Grazie mladen.

se (OrdiniTotali()<1

Aveva risolvere il mio problema.

È totale può ordinare uno, ma voglio che ogni coppia di valute può aprire.

Mi dispiace di aver frainteso.

Ma io intendo è in ogni coppia eseguire un ordine e stop. Esempio:

Ora ho aperto ordine in EURUSD eseguire un solo ordine. Poi altre valute anche possono aprire nello stesso tempo eseguire un ordine. Mi dispiace per il mio povero inglese.

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

Grazie.

garretto87

Si prega di leggere il post sopra il tuo post per una soluzione di questo problema

Il codice completo per questo è il seguente:

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);

}

Motivazione: