Einzelnen Beitrag anzeigen
  #2 (permalink)  
Alt 25.02.16
RetepM RetepM ist offline
Mitglied
 
Registriert seit: Feb 2016
Beiträge: 240
RetepM befindet sich auf einem aufstrebenden Ast
Standard

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