Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools

Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools (http://www.expert-advisor.com/forum/index.php)
-   Programmierung MQL5 (http://www.expert-advisor.com/forum/forumdisplay.php?f=221)
-   -   Unterstützung bei "PositionClose" bei Expert Advisor (http://www.expert-advisor.com/forum/showthread.php?t=6727)

IndiSto 27.07.20 15:10

Unterstützung bei "PositionClose" bei Expert Advisor
 
Hallo liebe Community,

ich versuche einen Expert Advisor in Metatrader 5 zu programmieren.

Auf Basis von unterschiedlichen Indikatoren soll ein Kauf- bzw. Verkaufshandel ausgeführt werden.

Ich habe aktuell Probleme, einen ausgeführten Handel zu schließen und komme nicht weiter. Kann mir bitte jemand eine Gedankenstütze geben?

1. es wird erfolgreich initialisiert
2. es wird ein Trade geöffnet
3. die Indikatorwerte und Anzahl offener "Buy" und "Sell" Positionen werden als Kommentar im Chart geprüft

4. Auf Basis unterschiedlicher Kriterien soll der Trade geschlossen werden. Unten der Code.

Ich wäre sehr dankbar für eure Unterstützung.

Viele Grüße,

IndiSto




#include <Trade/Trade.mqh>

// Globale Variablen
sinput string ________Block_1________="====== EA settings =================================";

input ENUM_TIMEFRAMES Timeframe = PERIOD_D1; // Select period
input double Lots = 0.01; // Select lotsize
input double maxspread = 5; // Enter maximum spread
input int openpos = 1; //Open positions
input string CommentaryBuy ="BUY"; // Comment Buy
input string CommentarySell ="SELL"; // Comment Sell
input string CommentaryCloseBuy ="Close Buy"; // Comment Close Buy Trade
input string CommentaryCloseSell ="Close Sell"; // Comment Close Sell Trade




sinput string ________Block_2________="====== BUY settings =================================";

input bool tradeBUY1 = true; // Trade Buy position
input int TPBUY1 = 2000; // Set Take Profit
input int SLBUY1 = 1000; // Set Stop Loss
input int RSIlow = 35; // RSI threshold
input int ADXlow = 20; // ADX threshold




sinput string ________Block_3________="====== SELL settings =================================";

input bool tradeSELL1 = true; // Trade Sell position
input int TPSELL1 = 2000; // Set Take Profit
input int SLSELL1 = 1000; // Set Stop Loss
input int RSIhigh = 65; // RSI threshold
input int ADXhigh = 20; // ADX threshold




// Signals

// PARABOLIC SAR
double parstep = 0.02;
double parmax = 0.2;
int PARHandle;
double PAR[];

// EMA
int EMAPeriod = 20;
int EMAHandle;
double EMA[];

// ADX
int ADXPeriod = 14;
int ADXHandle;
double ADX[];

// DIP
int DIPHandle;
double DIP[];

// DIM
int DIMHandle;
double DIM[];

// MACD
int MACDPeriod = 9;
int fastma = 12;
int slowma = 26;
int MACDHandle;
double MACD[];

// SIGNAL LINE
int SIGNALHandle;
double SIGNAL[];

// RSI
int RSIPeriod = 14;
int RSIHandle;
double RSI[];




//Initialisierungsfunktion************************** ************************************************** **************

int OnInit()

{

// PARABOLIC SAR
PARHandle = iSAR(_Symbol, Timeframe, parstep, parmax);
ArraySetAsSeries(PAR, true);

// EMA
EMAHandle = iMA(_Symbol, Timeframe, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
ArraySetAsSeries(EMA, true);

// ADX
ADXHandle = iADX(_Symbol, Timeframe, ADXPeriod);
ArraySetAsSeries(ADX, true);

// DIP
DIPHandle = iADX(_Symbol, Timeframe, ADXPeriod);
ArraySetAsSeries(DIP, true);

// DIM
DIMHandle = iADX(_Symbol, Timeframe, ADXPeriod);
ArraySetAsSeries(DIM, true);

// MACD
MACDHandle = iMACD(_Symbol, Timeframe, fastma, slowma, MACDPeriod, PRICE_CLOSE);
ArraySetAsSeries(MACD, true);
//ChartIndicatorAdd(0,0,MACDHandle);

// SIGNAL LINE
SIGNALHandle = iMACD(_Symbol, Timeframe, fastma, slowma, MACDPeriod, PRICE_CLOSE);
ArraySetAsSeries(SIGNAL, true);

// RSI
RSIHandle = iRSI(_Symbol, Timeframe, RSIPeriod, PRICE_CLOSE);
ArraySetAsSeries(RSI, true);

return (INIT_SUCCEEDED);

}




void OnTick()

{

CopyBuffer(PARHandle, 0, 0, 3, PAR); // PARABOLIC SAR Buffer
CopyBuffer(EMAHandle, 0, 0, 3, EMA); // EMA Buffer
CopyBuffer(ADXHandle, 0, 0, 3, ADX); // ADX Buffer
CopyBuffer(DIPHandle, 1, 0, 3, DIP); // DIP Buffer
CopyBuffer(DIMHandle, 2, 0, 3, DIM); // DIM Buffer
CopyBuffer(MACDHandle, 0, 0, 3, MACD); // MACD Buffer
CopyBuffer(SIGNALHandle, 1, 0, 3, SIGNAL); // SIGNAL LINE Buffer
CopyBuffer(RSIHandle, 0, 0, 3, RSI); // RSI Buffer

MqlRates PriceArray[];
ArraySetAsSeries(PriceArray, true);
double price = CopyRates(Symbol(), Timeframe, 0, 3, PriceArray);

double price0 = PriceArray[0].low;
double price1 = PriceArray[0].high;
double PARvalue = NormalizeDouble(PAR[0],5);
double EMAvalue = EMA[0];
double ADXvalue = ADX[0];
double DIPvalue = DIP[0];
double DIMvalue = DIM[0];
double MACDvalue = MACD[0];
double SIGNALvalue = SIGNAL[0];
double RSIvalue = RSI[0];

double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK),_Digits);
double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BI D),_Digits);
double spread = (bid-ask)/_Point;




// BUY Position

if
(
PositionsTotal() < openpos &&
tradeBUY1 == true &&
spread <= maxspread &&
PARvalue < price0 &&
// EMAvalue < price0 &&
ADXvalue > ADXlow &&
DIPvalue > DIMvalue &&
MACDvalue > SIGNALvalue &&
RSIvalue < RSIlow
)

{
CTrade trade;
double slB1 = ask - SLBUY1 * _Point;
double tpB1 = ask + TPBUY1 * _Point;
trade.Buy(Lots, _Symbol, ask, slB1, tpB1, CommentaryBuy);
} // End if Buy




// SELL Position

if
(
PositionsTotal() < openpos &&
tradeSELL1 == true &&
spread <= maxspread &&
PARvalue > price1 &&
// EMAvalue > price1 &&
ADXvalue > ADXhigh &&
DIPvalue < DIMvalue &&
MACDvalue < SIGNALvalue &&
RSIvalue < RSIhigh
)

{
CTrade trade;
double slS1 = bid + SLSELL1 * _Point;
double tpS1 = bid - TPSELL1 * _Point;
trade.Sell(Lots, _Symbol, bid, slS1, tpS1, CommentarySell);
} // End if Sell


// Count Buy positions
int BuyPositions = CountBuyPositions();

// Count Sell positions
int SellPositions = CountSellPositions();

// Check values
Comment("Ask price: ", ask, "\n", "Bid price: ", bid, "\n", "Qty. Buy positions: ", BuyPositions, "\n", "Qty. Sell positions: ", SellPositions, "\n", "ADX low: ", ADXlow, "\n", "ADX high: ", ADXhigh, "\n", "RSI low: ", RSIlow, "\n", "RSI high: ", RSIhigh, "\n", "Price low: ", price0, "\n", "Price high: ", price1, "\n", "PAR SAR: ", PARvalue, "\n", "EMA: ", EMAvalue, "\n", "ADX value: ", ADXvalue, "\n", "DI+ : ", DIPvalue, "\n", "DI -: ",DIMvalue, "\n", "MACD: ", MACDvalue, "\n", "SIGNAL: ", SIGNALvalue, "\n", "RSI: ", RSIvalue);



// Close Buy positions

if(BuyPositions >= 1)
{
for(int i = PositionsTotal()-1; i>=0; i--)
{
string symbol=PositionGetSymbol(i);
if (_Symbol == symbol)
ulong PositionTicket = PositionGetInteger(POSITION_TICKET);

if
(
PARvalue < price0 ||
EMAvalue < price0 ||
ADXvalue < ADXlow ||
DIPvalue < DIMvalue ||
MACDvalue < SIGNALvalue ||
RSIvalue > RSIlow
)

{
CTrade trade;
trade.PositionClose(i);
}

} // End for

} // End if




// Close Sell positions
if(SellPositions >= 1)
{
for(int i = PositionsTotal()-1; i>=0; i--)
{

string symbol=PositionGetSymbol(i);
if (_Symbol == symbol)
ulong PositionTicket = PositionGetInteger(POSITION_TICKET);

if
(
PARvalue < price1 &&
EMAvalue < price1 &&
ADXvalue < ADXhigh &&
DIPvalue > DIMvalue &&
MACDvalue > SIGNALvalue &&
RSIvalue > RSIhigh
)

{
CTrade trade;
trade.PositionClose(i);
}

} // End for

} // End if

} // End void OnTick




// Count Buy Positions

int CountBuyPositions()
{
int NumberOfBuyPositions=0; // local variable for qty positions

for(int i=PositionsTotal()-1; i>=0; i--) // check all positions

{
string CurrencyPair=PositionGetSymbol(i); // Identify currency pair
int PositionDirection=PositionGetInteger(POSITION_TYPE ); // Check position type
if(Symbol()==CurrencyPair) // If chart equals currency pair
if(PositionDirection==POSITION_TYPE_BUY)
{
NumberOfBuyPositions = NumberOfBuyPositions+1;
}
} // End for

return NumberOfBuyPositions;

} // End count functon




// Count Sell Positions

int CountSellPositions()
{
int NumberOfSellPositions=0; // local variable for qty positions

for(int i=PositionsTotal()-1; i>=0; i--) // check all positions

{
string CurrencyPair=PositionGetSymbol(i); // Identify currency pair
int PositionDirection=PositionGetInteger(POSITION_TYPE ); // Check position type
if(Symbol()==CurrencyPair) // If chart equals currency pair
if(PositionDirection==POSITION_TYPE_SELL)
{
NumberOfSellPositions = NumberOfSellPositions+1;
}
} // End for

return NumberOfSellPositions;

} // End count functon

MA-EA 28.07.20 01:17

Soweit ich sehen kann, wird geprüft, ob für alle Symbol() zusammen 1 Trade offen ist. Wenn auf mehreren Symbol() gleichzeitig getradet werden soll, muss auch dementsprechend gesucht und geprüft werden.

Indikator-Trading 28.07.20 02:02

Bitte Kommentare vom MA-EA einfach immer ignorieren...

Du schließt deine Positionen nicht, da du weder die Ticketnummer noch das Symbol als Parameter übergibst, sondern die Schleifen-Durchlaufvariable.

Hier etwas was funktioniert, aber noch nicht schön ist. Es fehlt z.B. die Abfrage nach der Magicnummer usw.:

Code:

  if(BuyPositions >= 1)
    {
      for(int i = PositionsTotal()-1; i>=0; i--)
        {
       
        string symbol=PositionGetSymbol(i);
        if(_Symbol != symbol)
            continue;
        ulong PositionTicket = OrderGetTicket(i);

        if
        (
            PARvalue < price0 ||
            EMAvalue < price0 ||
            ADXvalue < ADXlow ||
            DIPvalue < DIMvalue ||
            MACDvalue < SIGNALvalue ||
            RSIvalue > RSIlow
        )

          {
            trade.PositionClose(PositionTicket);
          }

        } // End for

    } // End if

Im EA ist vor allem bei deinen If-Abfragen noch so einiges im Argen. Buy-Orders werden z.B. nie auslöst.

IndiSto 28.07.20 21:10

Vielen Dank
 
Vielen Dank für die Rückmeldung und für den Tipp mit den Buy Positionen :)


Alle Zeitangaben in WEZ +2. Es ist jetzt 22:27 Uhr.

Powered by vBulletin® Version 3.8.5 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.1
Powered by vBCMS® 2.7.0 ©2002 - 2024 vbdesigns.de
Copyright ©2009 - 2023 by Expert-Advisor.com - Das Metatrader Forum