what’s wrong here does anyone know?
void TrailingStopBuy(double Ask)
{
// set the stop loss to 150 points
double SL=NormalizeDouble(Ask-150*_Point,_Digits);
// Go through all positions
for(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol=PositionGetSymbol(i); // get the symbol of the position
if (_Symbol==symbol) // if currency pair is equal
// if we have a sell position
if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)
{
// get the ticket number
ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
// calculate the current stop loss
double CurrentStopLoss=PositionGetDouble(POSITION_SL);
// if current stop loss is more than 150 points
if (CurrentStopLoss>SL)
{
// move the stop loss
trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0);
}
} // End if loop
} // End for loop
} // End
void TrailingStopSell(double Bid)
{
// set the stop loss to 150 points
double SL=NormalizeDouble(Bid+150*_Point,_Digits);
// Go through all positions
for(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol=PositionGetSymbol(i); // get the symbol of the position
if (_Symbol==symbol) // if currency pair is equal
// if we have a sell position
if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)
{
// get the ticket number
ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
// calculate the current stop loss
double CurrentStopLoss=PositionGetDouble(POSITION_SL);
// if current stop loss is more than 150 points
if (CurrentStopLoss>SL)
{
// move the stop loss
trade.PositionModify(PositionTicket,(CurrentStopLoss-10*_Point),0);
}
} // End if loop
} // End for loop
} // End
russian_hedge_fund
Hello everyone
i can’t get the trailing stop to work.
please tell me what is wrong where ?
Thank you in advance
#include
CTrade trade;
void OnTick()
{
double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
double Equity=AccountInfoDouble(ACCOUNT_EQUITY);
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
if(PositionsTotal()==0 && OrdersTotal()==0)
{
trade.BuyStop(1,Ask+1*_Point,_Symbol,Ask-4*_Point,Ask+11*_Point, ORDER_TIME_GTC,0,0);
trade.SellStop(1,Bid-1*_Point,_Symbol,Bid+4*_Point,Bid-11*_Point, ORDER_TIME_GTC,0,0);
}
if(Balance!=Equity)
{
CancelOrder();
}
TrailingStop(Ask, Bid);
}
void CancelOrder()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
ulong OrderTicket = OrderGetTicket(i);
trade.OrderDelete(OrderTicket);
}
}
void TrailingStop(double Ask, double Bid)
{
double SL_Ask = NormalizeDouble(Ask-1*_Point, _Digits);
double SL_Bid = NormalizeDouble(Bid+1*_Point, _Digits);
for (int i=PositionsTotal()-1; i>=0; i--)
{
string symbol=PositionGetSymbol(i);
if (_Symbol==symbol)
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
double PositionBuyPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double CurrentSL = PositionGetDouble(POSITION_SL);
if(CurrentSL>SL_Ask)
{
trade.PositionModify(PositionTicket,(CurrentSL+3*_Point),0);
}
if (Ask==(PositionBuyPrice-2*_Point))
{
trade.PositionModify(PositionTicket,CurrentSL+1*_Point,0);
}
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
double CurrentSL = PositionGetDouble(POSITION_SL);
if(CurrentSL3*_Point),0);
}
if (Bid==(PositionBuyPrice-2*_Point))
{
trade.PositionModify(PositionTicket,CurrentSL+1*_Point,0);
}
}
}
}
}
gaiatsu
as you can guess, the modification takes place on the server and only then, after confirmation in the terminal, the result changes…in my opinion you
wrong sorry for my noobism but still
tara
I would still modify the order. Well, or in some other way signaled to the server that the order has already been modified – the server itself may not
guess.
gaiatsu
I added the code for myself it turned out like this
//+------------------------------------------------------------------+
//| TrailingStop Buy |
//+------------------------------------------------------------------+
void TrailingStopBuy(string symbol_, double Ask, int number)
{
double CalculatedStopLoss=NormalizeDouble(Ask-50*_Point,_Digits); // set the stop loss to 150 points
for(int i=PositionsTotal()-1; i>=0; i--) // Go through all positions
{
string symbol=PositionGetSymbol(i); // get the symbol of the position
if(symbol_==symbol) // if currency pair is equal
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) // if we have a sell position
{
ulong PositionTicket=PositionGetInteger(POSITION_TICKET); // get the ticket number
double CurrentStopLoss=PositionGetDouble(POSITION_SL); // calculate the current stop loss
if(CalculatedStopLoss>CurrentStopLoss) // if current stop loss is more than 150 points
{
trade[number].PositionModify(PositionTicket,(CalculatedStopLoss+10*_Point),0); // move the stop loss
}
} // End if loop
} // End for loop
} // End
//+------------------------------------------------------------------+
//| TrailingStop Sell |
//+------------------------------------------------------------------+
void TrailingStopSell(string symbol_, double Bid, int number)
{
double CalculatedStopLoss=NormalizeDouble(Bid+50*_Point,_Digits); // set the stop loss to 150 points
for(int i=PositionsTotal()-1; i>=0; i--) // Go through all positions
{
string symbol=PositionGetSymbol(i); // get the symbol of the position
if(symbol_==symbol) // if currency pair is equal
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) // if we have a sell position
{
ulong PositionTicket=PositionGetInteger(POSITION_TICKET); // get the ticket number
double CurrentStopLoss=PositionGetDouble(POSITION_SL); // calculate the current stop loss
if(CalculatedStopLoss// if current stop loss is more than 150 points
{
trade[number].PositionModify(PositionTicket,(CalculatedStopLoss-10*_Point),0);// move the stop loss
}
} // End if loop
} // End for loop
} // End
but why when I’m in the black already I have not moved the trailstop stop los as was the original and remains, here are the functions of buy and sell
bool OpenBuyOrder(const string symbol, int number)
{
if (!PositionSelect(symbol))
{
double Ask=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_ASK),_Digits);
trade[number].Buy(Lots,symbol,Ask,NormalizeDouble(Ask-100*_Point, _Digits),0,NULL);
tickets[number] = trade[number].ResultDeal();
}
return(true);
}
//+------------------------------------------------------------------+
bool OpenSellOrder(const string symbol, int number)
{
if (!PositionSelect(symbol))
{
double Bid=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_BID),_Digits);
trade[number].Sell(Lots,symbol,Bid,NormalizeDouble(Bid+100*_Point,_Digits),0, NULL);//NormalizeDouble(Bid-100 * _Point, _Digits)
tickets[number] = trade[number].ResultDeal();
}
return(true);
}
//+------------------------------------------------------------------+
alex_all
barabashkakvn
Combed the code to a more beautiful look.
Note: Forget and get the word “warrant” out of your head. You need to use the words “position”
or “transaction” (“transaction” can be used by persons who have reached the age of majority).
So:
ENUM_POSITION_TYPE
Identifier
Description
POSITION_TYPE_BUY
Buy
POSITION_TYPE_SELL
Sell
Based on this:
//+------------------------------------------------------------------+
//| TrailingStop Buy |
//+------------------------------------------------------------------+
void TrailingStopBuy(double Ask)
{
double SL=NormalizeDouble(Ask-150*_Point,_Digits); // set the stop loss to 150 points
for(int i=PositionsTotal()-1; i>=0; i--) // Go through all positions
{
string symbol=PositionGetSymbol(i); // get the symbol of the position
if(_Symbol==symbol) // if currency pair is equal
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) // if we have a sell position
{
ulong PositionTicket=PositionGetInteger(POSITION_TICKET); // get the ticket number
double CurrentStopLoss=PositionGetDouble(POSITION_SL); // calculate the current stop loss
if(CurrentStopLoss>SL) // if current stop loss is more than 150 points
{
trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0); // move the stop loss
}
} // End if loop
} // End for loop
} // End
//+------------------------------------------------------------------+
//| TrailingStop Sell |
//+------------------------------------------------------------------+
void TrailingStopSell(double Bid)
{
double SL=NormalizeDouble(Bid+150*_Point,_Digits); // set the stop loss to 150 points
for(int i=PositionsTotal()-1; i>=0; i--) // Go through all positions
{
string symbol=PositionGetSymbol(i); // get the symbol of the position
if(_Symbol==symbol) // if currency pair is equal
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) // if we have a sell position
{
ulong PositionTicket=PositionGetInteger(POSITION_TICKET); // get the ticket number
double CurrentStopLoss=PositionGetDouble(POSITION_SL); // calculate the current stop loss
if(CurrentStopLoss>SL) // if current stop loss is more than 150 points
{
trade.PositionModify(PositionTicket,(CurrentStopLoss-10*_Point),0);// move the stop loss
}
} // End if loop
} // End for loop
} // End
I didn’t check further.