For thebare minimum trailing ma stoploss code for darwin ea....... appears to work for sells..... yellow ma is 20.......h
//----
View attachment 29490
top lines in code box will not display correctly for some reason..... most likely html conflicts with < .....
the includes are,
#include <Trade\OrderInfo.mqh>
#include <Trade\Trade.mqh>
CTrade trade;
//------
Code://----- #include#include CTrade trade; CPositionInfo m_position; // object of CPositionInfo class COrderInfo m_order; // object of COrderInfo class double Ask,Bid,high,low; input int contracts = 1; input double profit = 1000; input double dailytarget = 2000; input bool trailingstophighlow = false; input bool trailingpsar = false; input bool trailingstopma = false; input ulong magicnumber = 7000; // magicnumber input int periods = 20; //--------- int OnInit() { //--- trade.SetExpertMagicNumber(magicnumber); // used to set trades magicnumber sent by other functions //------- //--- return(INIT_SUCCEEDED); } //------ on tick code void OnTick() { //--- Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); high = NormalizeDouble(iHigh(NULL,PERIOD_M15,1),_Digits); low = NormalizeDouble(iLow(NULL,PERIOD_M15,1),_Digits); double mas[]; int ma = iMA(NULL,PERIOD_CURRENT,periods,0,MODE_SMA,PRICE_CLOSE); ArraySetAsSeries(mas,true); CopyBuffer(ma,0,0,3,mas); //------ if(trailingstopma && (mas[0] > 0.0)) { trailingstopma(mas[0]); } //---- //---- } //------- just the function itself void trailingstopma(double ma) // { double newstoploss = NormalizeDouble(ma,_Digits); for(int i=PositionsTotal()-1; i>=0; i--) { string symbol = PositionGetSymbol(i); ulong pm = PositionGetInteger(POSITION_MAGIC); if((_Symbol == symbol) && (pm == magicnumber)) { if(PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_BUY) { ulong ticket = PositionGetInteger(POSITION_TICKET); double currentstoploss = PositionGetDouble(POSITION_SL); if(currentstoploss < ma) { trade.PositionModify(ticket,newstoploss,0); } } if(PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_SELL) { ulong ticket = PositionGetInteger(POSITION_TICKET); double currentstoploss = PositionGetDouble(POSITION_SL); if(currentstoploss > ma) { trade.PositionModify(ticket,newstoploss,0); } } } } }
<
brackets not to get garbled by the forum, it's necessary to insert them directly to the code box, like this:#include <Trade\OrderInfo.mqh> #include <Trade\Trade.mqh> CTrade trade;
void trailingpsar(double psar) // { int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); double ticksize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE ); double adjusted = round(psar / ticksize ) * ticksize; double newstoploss = NormalizeDouble(adjusted,digits); for(int i=PositionsTotal()-1; i>=0; i--) { string symbol = PositionGetSymbol(i); ulong pm = PositionGetInteger(POSITION_MAGIC); ulong ticket = PositionGetInteger(POSITION_TICKET); double currentstoploss = PositionGetDouble(POSITION_SL); double tp = PositionGetDouble(POSITION_TP); if((_Symbol == symbol) && (pm == magicnumber)) { if(PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_BUY) { if(currentstoploss < newstoploss) { trade.PositionModify(ticket,newstoploss,tp); } } if(PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_SELL) { if(currentstoploss > newstoploss) { trade.PositionModify(ticket,newstoploss,tp); } } } } }
const int total = SymbolsTotal(true); for(int i = 0; i < total; ++i) { string sym = SymbolName(i,true); Print(sym); }
//------ //------ void trailsellstoppsar(double psar) // { int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); double ticksize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE ); double adjusted = round(psar / ticksize ) * ticksize; double newprice = NormalizeDouble(adjusted,digits); for(int i=OrdersTotal()-1; i>=0; i--) // use orderstotal not positionstotal { string symbol = OrderGetString(ORDER_SYMBOL); ulong pm = OrderGetInteger(ORDER_MAGIC); ulong ticket = OrderGetTicket(i); double sl = OrderGetDouble(ORDER_SL); double tp = OrderGetDouble(ORDER_TP); double price = OrderGetDouble(ORDER_PRICE_OPEN); if((_Symbol == symbol) && (pm == magicnumber) && (OrderGetInteger(ORDER_TYPE) == 5)) // 5 is sellstop { if(OrderGetInteger(ORDER_TYPE) == 5) { if(price < newprice) { trade.OrderModify(ticket,newprice,sl,tp,ORDER_TIME_GTC,0,0); // make sure existing sl and tp are still valid...... or modify them also } } } // if((_Symbol == symbol) } // for(int i= } //----
ObjectSetString(0, PanelLabel, OBJPROP_TOOLTIP, symbol + SymbolInfoString(symbol, SYMBOL_DESCRIPTION)); ObjectSetString(0, PanelLabel, OBJPROP_TEXT, symbol + " alma");
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); double ts = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE); double tv = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_VALUE); double ps = SymbolInfoDouble( _Symbol, SYMBOL_POINT ); double pv = (tv * ps) / ts; double hi = ObjectGetDouble(0,"hi",OBJPROP_PRICE); // highest line double lo = ObjectGetDouble(0,"lo",OBJPROP_PRICE); // lowest line double ahi = round(hi / ts ) * ts; // this is to adjust line to set tick size double alo = round(lo / ts ) * ts; // this is to adjust line to set tick size double value = (ahi-alo) * tv * (1/ts); Comment(DoubleToString(value,2)); //--- return value of prev_calculated for next call return(rates_total); }