need advice from a trawl pro that controls two positions independently of each other, how can it be implemented? - page 4

 
charony:

on the 2sell the trawl should have put the SL at 850

i got it close at 950, not at manual trade

How can it be solved in the code?

Why would it do that? If the price has only reached 899, the trall should be 50 pips away, so somewhere around 950 it should be.
 
sanyooooook:
Why would I do that? If the price has only reached 899, the trall should be 50 pips away, so somewhere around 950 it should be.

he closed it on SL from 1sell
 
Let's wait and see if anyone who knows comes along.
 
Doesn't anyone know
 
charony:
does not anyone know


You probably want a trall for a position to be activated only when the price has moved to the right side by at least X pips from the open price of the position (e.g. 100)? If so, it is one line of code additionally in the trall function

Another variant is that a trall is triggered when X points have passed from the open price: go 100 points - trall -50 points, go another 100 points - trall again at -50, etc.

 
Avals:


Perhaps you need the trawl for a position to be activated only when the price has moved in the required direction by at least X points from the open price of the position (e.g. 100)? If yes, then this is one line of code additionally in the trall function

There is also a variant that a trall will be triggered when X points pass from the opening price: pass 100p - trall -50p, pass another 100p - trall again at -50p, etc.


I have it like that, i.e. the trall is turned on at certain events

there are no problems when one position is opened,

if you open 2 or more positions, trawl exposes only one SL,

if any position reaches the SL

all positions of this type are closed

I need the trailers to hold positions of one type

independently

 
charony:


I have it that way, i.e. the trawl is switched on at certain events

when one position is open there is no problem,

if you open 2 or more positions, the trawl only sets one SL,

when any position reaches SL

all positions of this type are closed


You set the same stop loss for all positions at TralingStop points from the last price and move only in the direction of the open position. I.e. for all positions the buy trailing is set from the maximum for the holding period and of course they all close at the same time as the maximum will be common. Consider what you need, what is the algorithm that closes "positions of the same type independently of each other". There are a lot of variants.
 

In settings SL is not set i.e. =0, TP also =0,

Trawl=50 pips for all positions and all types

Trailing should lead positions independently of each other

at 1000=pos1Buy price went north at 1050 Trawl puts SL for pos1Buy

price went further north, reached 1090, SL for pos1Buy remained 1050, no SL for pos2Buy

Price went south, got to 1050, both positions are closed.

I need SL to close only pos1

pos2 should close on SL if the price reaches 1140 ,

I.e. at 1130 there is SL for pos2, and from 1140 the price falls below 1130

should trigger SL for pos2

поз2Buy--------------------------------------------------------------------------1080

SL---------------------------------------------------------------------------------1050

поз1Buy--------------------------------------------------------------------------1000

 

When you open an order you can set it to Time[0] or something else, date, time or whatever.)

takes:

level_ts - trailing stop in points

level_step - Trailing step

mn - Magik, if -1, it will trawl all orders.


//+----------------------------------------------------------------------------+
//| TrailingStop.                                                              |
//|   траллит все ордера, на уровень выставленный во входных параметрах.       |
//+----------------------------------------------------------------------------+
void TrailingStop(int level_ts, int level_step = 5, int mn = -1) 
{
   int ot, err, quantity_orders, cx;
   bool fm;
   double op, as, bd, pt, sl, tp, ut;
//----
   quantity_orders = OrdersTotal();
   if(quantity_orders >= 0) {
      for(cx = 0; cx < quantity_orders; cx++) {
         if(!OrderSelect(cx, SELECT_BY_POS, MODE_TRADES)) {
            Print("Error: SimpleTrailing(), OrderSelect: " + cx + ". Ошибка: " + GetLastError());
         }
         if (mn < 0 || OrderMagicNumber()==mn) {   
            pt = MarketInfo(OrderSymbol(), MODE_POINT);
            bd = MarketInfo(OrderSymbol(), MODE_BID);
            as = MarketInfo(OrderSymbol(), MODE_ASK);
            op = OrderOpenPrice();
            ot = OrderType();
            sl = OrderStopLoss();
            tp = OrderTakeProfit();
            ut = level_ts + level_step;
         
            if(ot == OP_BUY) {
               if(sl == 0 || sl < op) {                              // если стопа нет или стоп ниже цены открытия
                  if(op <  (bd - ut * pt)) {                         // если цена открытия ниже чем текущая цена минус ( стоп - шаг стопа)
                     sl = bd - level_ts * pt;
                  }
               }
               else {
                  if(sl < (bd - ut * pt) && bd > (op + ut * pt)) {
                     sl = bd - level_ts * pt;
                  }
               }
            }
            if(ot == OP_SELL) {
               if(sl == 0 || sl > op) {
                  if(op > (as + ut * pt)) {
                     sl = as + level_ts * pt;
                  }
               }
               else {
                  if(sl > (as + ut * pt) && as < (op - ut * pt)) {
                     sl = as + level_ts * pt;
                  }
               }
            }
            if (sl != OrderStopLoss()) {
               op = NormalizeDouble(op, Digits);
               tp = NormalizeDouble(tp, Digits);
               sl = NormalizeDouble(sl, Digits);
               fm = OrderModify(OrderTicket(), op, sl, tp, 0, CLR_NONE);
               if (fm) {
                  if (UseSound) PlaySound(NameFileSound);
               }
               else {
                  err=GetLastError();
                  Print("Error(",err,") SimpleTrailing(): ",ErrorDescription(err));
               }
            }
         }
      }
   }
}
 
charony:

In settings SL is not set i.e. =0, TP also =0,

Trawl=50 pips for all positions and all types

Trailing should lead positions independently of each other

at 1000=pos1Buy price went north at 1050 Trawl puts SL for pos1Buy

price went further north, reached 1090, SL for pos1Buy remained 1050, no SL for pos2Buy

Price went south, got to 1050, both positions are closed.

I need SL to close only pos1

pos2 should close on SL if the price reaches 1140 ,

I.e.at 1130 there is SL for pos2, and from 1140 the price falls below 1130

should trigger SL for pos2

поз2Buy--------------------------------------------------------------------------1080

SL---------------------------------------------------------------------------------1050

поз1Buy--------------------------------------------------------------------------1000


if I understand correctly, you need to replace

  if (NormalizeDouble(SL,Digits)<// Если ниже желаемого..
               NormalizeDouble(Bid-TS,Digits))

at

  if (NormalizeDouble(SL,Digits)<// Если ниже желаемого..
               NormalizeDouble(Bid-TS,Digits) && (Bid>=Price+TS))
the same for sell.
Reason: