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 MQL4 (http://www.expert-advisor.com/forum/forumdisplay.php?f=220)
-   -   Trailing Stop und Takeprofit (http://www.expert-advisor.com/forum/showthread.php?t=4820)

xman256 25.02.16 13:21

Trailing Stop und Takeprofit
 
Hallo
Ich möchte durch einen EA im laufenden Betrieb den Stop und den Takeprofit ändern. Leider funktioniert mein Programm mit dem ModifyOrder nicht. Was habe ich falsch gemacht ? Wer hat eine Idee ?

ob = Open Buy Kurs
os = Open Sell Kurs
ab = Sollabstand zum Eröffnungskurs

void CheckForClose()
{ bool res;

//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average

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 ((Bid+ab*PointX())>ob)
{
ob=Bid+ab*PointX();
slb=Bid-ab*PointX();
res=OrderModify(OrderTicket(), OrderOpenPrice(), slb,ob,0,Blue);
}

}
}
if (OrderType()==OP_SELL)
{
if ((Ask-ab*PointX())<os)
{
os=Ask-ab*PointX();
sls=Ask+ab*PointX();
res=OrderModify(OrderTicket(), OrderOpenPrice(), sls,os, 0,Red);
}
}
}

Danke im vorraus
xman256

RetepM 25.02.16 14:34

Hi, ich baue, wenn überhaupt, Trailingstopps direkt in den EA. Hier ist eine ältere Variante, die selbstständig läuft. Bitte zuerst unbedingt in einer Demo probieren!!" Achtung, das Teil ändert den TP nur, wenn der Trailingstopp erreicht wurde. Viel Spaß!

#property copyright "Peter"
#property link ""

#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern int MagicBuy;
extern int TrailingStopBuy = 30;
extern int NewTakeProfitBuy = 40;
extern int TrailingGapBuy = 20;
extern int MagicSell;
extern int TrailingStopSell = 30;
extern int NewTakeProfitSell = 40;
extern int TrailingGapSell = 20;

double PipValue=1;
bool Terminated = false;
string LF = "\n";
int NDigits = 4;
int ObjCount = 0;
int current = 0;

int init()
{
NDigits = Digits;
if (false) ObjectsDeleteAll();
Comment("");
return (0);
}

int start()
{
if (Bars < 10)
{
Comment("Not enough bars");
return (0);
}
if (Terminated == true)
{
Comment("EA Terminated.");
return (0);
}

OnEveryTick();
return (0);
}

void OnEveryTick()
{
PipValue = 1;
if (NDigits == 3 || NDigits == 5) PipValue = 10;

IfOrderExistsBuy();
IfOrderExistsSell();

}

void IfOrderExistsBuy()
{
bool exists = false;
for (int i=OrdersTotal()-1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicBuy)
{
exists = true;
}
}
else
{
Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
}

if (exists)
{
TrailingStopBuy();

}
}

void TrailingStopBuy()
{
for (int i=OrdersTotal()-1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicBuy)
{
double takeprofit = OrderTakeProfit();

if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TrailingStopBuy*PipValue*Point)
{
if (OrderStopLoss() < Ask-(TrailingStopBuy+TrailingGapBuy)*PipValue*Point)
{
if (NewTakeProfitBuy != 0) takeprofit = Ask+(NewTakeProfitBuy + TrailingStopBuy)*PipValue*Point;
bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask-TrailingStopBuy*PipValue*Point, takeprofit, OrderExpiration(), Green);
if (ret1 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TrailingStopBuy*PipValue*Point)
{
if (OrderStopLoss() > Bid+(TrailingStopBuy+TrailingGapBuy)*PipValue*Poin t)
{
if (NewTakeProfitBuy != 0) takeprofit = Bid-(NewTakeProfitBuy + TrailingStopBuy)*PipValue*Point;
bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStopBuy*PipValue*Point, takeprofit, OrderExpiration(), Green);
if (ret2 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
}
}
else
Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

void IfOrderExistsSell()
{
bool exists = false;
for (int i=OrdersTotal()-1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicSell)
{
exists = true;
}
}
else
{
Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
}

if (exists)
{
TrailingStopSell();

}
}

void TrailingStopSell()
{
for (int i=OrdersTotal()-1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicSell)
{
double takeprofit = OrderTakeProfit();

if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TrailingStopSell*PipValue*Point)
{
if (OrderStopLoss() < Ask-(TrailingStopSell+TrailingGapSell)*PipValue*Point)
{
if (NewTakeProfitSell != 0) takeprofit = Ask+(NewTakeProfitSell + TrailingStopSell)*PipValue*Point;
bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask-TrailingStopSell*PipValue*Point, takeprofit, OrderExpiration(), Green);
if (ret1 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TrailingStopSell*PipValue*Point)
{
if (OrderStopLoss() > Bid+(TrailingStopSell+TrailingGapSell)*PipValue*Po int)
{
if (NewTakeProfitSell != 0) takeprofit = Bid-(NewTakeProfitSell + TrailingStopSell)*PipValue*Point;
bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStopSell*PipValue*Point, takeprofit, OrderExpiration(), Green);
if (ret2 == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
}
}
else
Print("OrderSelect() error - ", ErrorDescription(GetLastError()));

}

int deinit()
{
if (false) ObjectsDeleteAll();

return (0);
}

Bööörni-breuser 28.03.16 12:47

EA mit Trailingstop
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo
Habe mal deinen EA mit Trailingstop ausprobiert handelt aber nicht auf EUR-USD
Dann habe ich etwas modefiziert auf zusätzliche Pending Order handelt aber auch nicht. Kanst du mir einen Tipp geben was falsch ist.
Programm anbei.
MfG
Bööörni-breuser http://www.expert-advisor.com/forum/...1&d=1459161918

MA-EA 28.03.16 13:24

Bin jetzt alles Andere als nen Profi, aber ich hab nirgendwo Befehle zur
Orderabgabe gefunden.

Ob das mit Hour/Min so hinhaut weiß ich auch nicht.

yytrader 28.03.16 16:36

Hallo Bööörni-breuse,

ich habe mal kurz in deinen angehängten EA geschaut.
Verwende bitte Tabulatoren oder Leerzeichen, um den Code leserlich zu machen.

Ein paar Punkte die mir aufgefallen sind:
1. TrailingStopBuy der Name wird als externe Variable und als Funktionsname verwendet.
2. Wenn du nur eine sell Order hast, wird die Funktion TrailingStopBuy nicht durchlaufen!
3.Der Kram mit dem (exists) wird nicht benötigt, du kannst die Funktion immer aufrufen, wenn keine passende Order gefunden wird, passiert auch nichts.
4,Konzentriere dich zuerst auf eine Funktion und arbeite dort so lange, bis sie funktioniert. Auch ich, trotz jahrelanger Erfahrung, kann nicht sofort den kompletten Code mit allen Varianten verstehen. Ich arbeite mich von Funktion zu Funktion vor, bis (hoffentlich) alles funktioniert.

Hier mal ein Code, ich habe ihn nicht getestet, allerdings sollte jetzt einiges klarer sein:
Code:

void TrailingStopBuy()
{
for (int i=OrdersTotal()-1; i >= 0; i--)
{
  if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
  {
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicBuy)
      {
        double takeprofit = OrderTakeProfit();
        double NewStopValue = Ask-TrailingStopBuy*PipValue*Point;
        if (OrderType() == OP_BUY && OrderStopLoss() < NewStopValue)
        {
            if (NewTakeProfitBuy != 0)
              takeprofit = Ask+(NewTakeProfitBuy + TrailingStopBuy)*PipValue*Point;
            bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), NewStopValue, takeprofit, OrderExpiration(), Green);
            if (ret1 == false)
              Print("OrderModify() error - ", ErrorDescription(GetLastError()));
           
        }
       
        NewStopValue = Bid+TrailingStopBuy*PipValue*Point;
        if (OrderType() == OP_SELL && OrderStopLoss() > NewStopValue)
        {
            if (NewTakeProfitBuy != 0)
              takeprofit = Bid-(NewTakeProfitBuy + TrailingStopBuy)*PipValue*Point;
            bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStopBuy*PipValue*Point, takeprofit, OrderExpiration(), Green);
            if (ret2 == false)
              Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
      }
  }
  else
      Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
  }
}


Gruß

yytrader


Alle Zeitangaben in WEZ +1. Es ist jetzt 06:19 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