4 to 5 digits correction, 130

 

Hi All,

I have this weird thing in my code, and I just can get my head around it...


Ok i have a function handleOpenPositions() which should manage my trailing and stoploss. ( Ok where you See VirtOrder, just read Order)


void handleOpenPositions(){
for(int i = VirtOrdersTotal(); i >= 0; i--){
VirtOrderSelect(i, SELECT_BY_POS, MODE_TRADES, "handleOpenPositions");
if(VirtOrderSymbol() == Symbol() && StrategyTrailingStop() > 0 && VirtOrderMagicNumber() == MAGICNUMBER){
if(VirtOrderType() == OP_SELL){
if(VirtOrderOpenPrice() - Bid >= StrategyPips()*Point){
if(VirtOrderStopLoss() > (Bid + Point*StrategyTrailingStop())){
VirtOrderModify(VirtOrderTicket(), VirtOrderOpenPrice(),Bid + Point*StrategyTrailingStop(), Bid - StrategyTakeProfit()*Point, 800, Purple);
}
}
}
if(VirtOrderType() == OP_BUY){
if(Ask - VirtOrderOpenPrice() >= StrategyPips()*Point){
if(VirtOrderStopLoss() < (Ask - Point*StrategyTrailingStop())){
VirtOrderModify(VirtOrderTicket(), VirtOrderOpenPrice(),Ask - Point*StrategyTrailingStop(), Ask + StrategyTakeProfit()*Point, 800, Yellow);
}
}
}
}
}
}


this should send a Modify a some point to VirtOrderModify(). In which VirtOrderCorrectStop() is called to correct wrong put digits. Check the function below...


bool VirtOrderModify( int iTicket, double dPrice, double dStopLoss, double dTakeProfit, datetime dtExpiration, color cArrowColor=CLR_NONE){
VirtOrderSelect(iTicket, SELECT_BY_POS, MODE_TRADES);
double dlPrice = 0;
int iOrdertype = VirtOrderType();
if (iOrdertype == OP_BUY || iOrdertype == OP_BUYLIMIT || iOrdertype == OP_BUYSTOP){
dlPrice = Ask;
}
if (iOrdertype == OP_SELL || iOrdertype == OP_SELLLIMIT || iOrdertype == OP_SELLSTOP){
dlPrice = Bid;
}
double dlStopLoss = VirtOrderCorrectStop(VirtOrderSymbol(), VirtOrderType(), 0, dlPrice, dStopLoss);
double dlTakeProfit = VirtOrderCorrectStop(VirtOrderSymbol(), VirtOrderType(), 1, dlPrice, dTakeProfit);
Print("modi ",sellorbuy(VirtOrderType())," ",dPrice," sl:",dStopLoss," sla:",dlStopLoss," tp:",dTakeProfit," tpa:",dlTakeProfit," p:",dlPrice," b:",Bid," a:",Ask);
return(OrderModify(iTicket, dPrice, dlStopLoss, dlTakeProfit, dtExpiration, cArrowColor));
}


int VirtOrdersDigits(int iInteger = 0, int iFunction = VIRTORDER_FUNC_GET){

static double ilInteger = 0;
if (iFunction == VIRTORDER_FUNC_PUT){
ilInteger = iInteger;
}
return(ilInteger);
}


double VirtOrderCorrectStop(string sSymbol, int iMode, int iStopType, double dPrice, double dPriceStop){

//iStopType 0 = stoploss
//iStopType 1 = takeprofit
double dlPriceStop = dPriceStop;
if (dPriceStop != 0){
int iStopCalc = 0;
if (iMode == OP_BUY || iMode == OP_BUYLIMIT || iMode == OP_BUYSTOP){
iStopCalc = 0;
}
if (iMode == OP_SELL || iMode == OP_SELLLIMIT || iMode == OP_SELLSTOP){
iStopCalc = 1;
}
if (iStopType == 1){
if (iStopCalc == 0){
iStopCalc = 1;
}
if (iStopCalc == 1){
iStopCalc = 0;
}
}

if (VirtOrdersDigits() != MarketInfo(sSymbol,MODE_DIGITS)){
double dFactor=0;
double dDelta = MarketInfo(sSymbol,MODE_DIGITS) - VirtOrdersDigits();
dFactor = MathPow(10,dDelta);

if (iStopCalc == 0){
dlPriceStop = dPrice - ((dPrice-dlPriceStop)/Point)*dFactor*Point; //div
}
if (iStopCalc == 1){
dlPriceStop = dPrice + ((dlPriceStop-dPrice)/Point)*dFactor*Point; //div
}
}
}
return(dlPriceStop);
}


now the problem... I keep getting a 130 error (wrong stoploss).


VirtOrdersDigits = 4

MarketInfo(sSymbol,MODE_DIGITS) = 5


output of the print :

modi 1.0452 sl:1.0451 sla:1.0441 tp:1.0454 tpa:1.0472 b:1.0449 a:1.0452


Now I understand I could adjust the inputs to the first function to work with the 5 digits... But that feels like solving the problem in the wrong direction ;-)


Any help is welcome!


grtz, Russell


--

corrected typo

 

Well, it's either one or both:

a) StrategyTrailingStop(), retrieves less pips than what you get by MarketInfo's MODE_STOPLEVEL ;

b) strict formatting to 4 digits may not be supported, and it may want 5 digits ;

You could also give up using VirtOrdersDigits and use MODE_DIGITS exclusively

 

Thanks for the reply!


StrategyTrailingStop() = 20

MODE_STOPLEVEL = 100


That is what I'm trying to correct in VirtOrderCorrectStop()


I'll try to Normailze the value, see if that helps.


Thanks!

 
The 100 pips must be something that Oanda calls pipettes ... : a pip is on the 5th digit now, so 100 pipettes like this are actually 10 normal pips ; When you set up StrategyTrailingStop() at 20 you thought in normal pips, hence the headache...
 

I see I can't correct it this way... The correction functions are ok, but the conditions in handleOpenPositions() are evaluated on the wrong information... so I have to correct the conditions too.


Thanks for your help.


Russell

Reason: