Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools
Zurück   Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools > Metatrader 5 > Programmierung MQL5

Programmierung MQL5 Hier gehts rund ums Programmieren in MQL5.

Login
Benutzername:
Kennwort:


Statistik
Themen: 4973
Beiträge: 43248
Benutzer: 7.219
Aktive Benutzer: 81
Links: 84
Wir begrüßen unseren neuesten Benutzer: Hopfen&Malz
Mit 2.475 Benutzern waren die meisten Benutzer gleichzeitig online (16.01.20 um 22:38).
Neue Benutzer:
vor einem Tag
- Hopfen&Mal...
vor 2 Tagen
- frankmicha...
vor einer Woche
- DFeck
vor einer Woche
- bb1107
vor 2 Wochen
- rg-trader

Onlineuser
'Wer ist online' anzeigen Benutzer: 0
Gäste: 254
Gesamt: 254
Team: 0
Team:  
Benutzer:  
Freunde anzeigen

Empfehlungen

Like Tree1Likes
  • 1 Post By Indikator-Trading
Thema geschlossen
 
Themen-Optionen Thema durchsuchen Ansicht
  #1 (permalink)  
Alt 27.07.20
Auf eigenen Wunsch deaktiviert
 
Registriert seit: Jul 2020
Beiträge: 3
IndiSto befindet sich auf einem aufstrebenden Ast
Question 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

Geändert von IndiSto (27.07.20 um 15:10 Uhr) Grund: Ergänzende Information
  #2 (permalink)  
Alt 28.07.20
Elite Mitglied
 
Registriert seit: Sep 2015
Beiträge: 1.178
MA-EA befindet sich auf einem aufstrebenden Ast
Standard

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.
  #3 (permalink)  
Alt 28.07.20
Benutzerbild von Indikator-Trading
Premium Mitglied
 
Registriert seit: May 2020
Ort: Bielefeld
Beiträge: 345
Indikator-Trading befindet sich auf einem aufstrebenden Ast
Standard

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 likes this.
  #4 (permalink)  
Alt 28.07.20
Auf eigenen Wunsch deaktiviert
 
Registriert seit: Jul 2020
Beiträge: 3
IndiSto befindet sich auf einem aufstrebenden Ast
Standard Vielen Dank

Vielen Dank für die Rückmeldung und für den Tipp mit den Buy Positionen
Thema geschlossen

Lesezeichen

Stichworte
close trade, ea, mql5

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are aus
Pingbacks are aus
Refbacks are aus




Alle Zeitangaben in WEZ +1. Es ist jetzt 13:39 Uhr.





Suchmaschine - Reisen - Wavesnode - Facebook Forum - Spam Firewall
-----------------------------------------------------------------------------------------------------------------------------
Powered by vBulletin® Version 3.8.5 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Powered by vBCMS® 2.7.0 ©2002 - 2024 vbdesigns.de
SEO by vBSEO 3.6.1
Copyright ©2009 - 2023 by Expert-Advisor.com - Das Metatrader Forum
MetaTrader bzw. MetaTrader 4 und MetaTrader 5 sind eingetragene Marken der MetaQuotes Software Corp.
-----------------------------------------------------------------------------------------------------------------------------