Need an EA/SCRIPT to close an order at custom TP and SL levels - page 2

 
ubzen:
You didn't make any mistakes. Price doesn't always move 1_Pipette_at_a_Time. Not in real-live, neither in the strategy-tester. I would have been surprised if you received exactly 0.10 for both after allot of trades ... real or tested.


Interesting, then how do you explain this:


I did a quick backtest, in case 1 i used my custom TP & SL i set it to 100 pipette (10 pips) and the normal TP & SL was set to 999 just to not interfere with the experiment, and i got this:

Bars in test 4468281
Ticks modelled 14715851
Modelling quality 25.00%
Mismatched charts errors 0
Initial deposit 100000000
Total net profit -30.85
Gross profit 1013.48
Gross loss -1044.32
Profit factor 0.97
Expected payoff -0.01
Absolute drawdown 45.39
Maximal drawdown 61.48 (0.00%)
Relative drawdown 0.00% (61.48)
Total trades 2454
Short positions (won %) 1208 (48.34%)
Long positions (won %) 1246 (47.35%)
Profit trades (% of total) 1174 (47.84%)
Loss trades (% of total) 1280 (52.16%)

Largest
profit trade 1.82
loss trade -1.91

Average
profit trade 0.86
loss trade -0.82

Maximum
consecutive wins (profit in money) 9 (8.00)
consecutive losses (loss in money) 13 (-10.16)

Maximal
consecutive profit (count of wins) 8.00 (9)
consecutive loss (count of losses) -10.16 (13)

Average
consecutive wins 2
consecutive losses 2


And in test 2 i set my custom SL & TP system to 9999 and used the normal built in TP & SL system an set it to 100 pipette (10 pips) for both cases

Bars in test 4468282
Ticks modelled 14715898
Modelling quality 25.00%
Mismatched charts errors 0
Initial deposit 100000000
Total net profit -74.3
Gross profit 869.61
Gross loss -943.91
Profit factor 0.92
Expected payoff -0.03
Absolute drawdown 79.59
Maximal drawdown 95.92 (0.00%)
Relative drawdown 0.00% (95.92)
Total trades 2481
Short positions (won %) 1223 (48.41%)
Long positions (won %) 1258 (47.54%)
Profit trades (% of total) 1190 (47.96%)
Loss trades (% of total) 1291 (52.04%)

Largest
profit trade 0.74
loss trade -0.8

Average
profit trade 0.73
loss trade -0.73

Maximum
consecutive wins (profit in money) 11 (8.04)
consecutive losses (loss in money) 12 (-8.76)

Maximal
consecutive profit (count of wins) 8.04 (11)
consecutive loss (count of losses) -8.76 (12)

Average
consecutive wins 2
consecutive losses 2


There is 10 pipettes missing in average in both cases,how come, if you say they are not exact, i understand that, but given that no slippage is simulated, the distorsion is likely to be caused by gaps or missing ticks or whatever, but if they are not precise, with the same settings they should have almost identical results both my custom SL/TP and the pre-build SL/TP in MT4, yet they have 10 pipette difference,Why i that?

 
Proximus:


As you can see the average profit was 0.17, and average loss 0.13, but how the heck can that happen if i have 10 pipette SL & TP, so the average win and loss should be 0.10 both +- 0.01 because of spread and since slippage is not simulated....,so i guess i made an error in the code,so please help me fix it :)

If your Spread is 1 pip (10 points) you will have a 1 pip loss as soon as you open a trade . . .
 

Proximus: Interesting, then how do you explain this: I did a quick backtest, in case 1 i used my custom TP & SL i set it to 100 pipette (10 pips) and the normal TP & SL was set to 999 just to not interfere with the experiment, and i got this:

There is 10 pipettes missing in average in both cases,how come, if you say they are not exact, i understand that, but given that no slippage is simulated, the distorsion is likely to be caused by gaps or missing ticks or whatever, but if they are not precise, with the same settings they should have almost identical results both my custom SL/TP and the pre-build SL/TP in MT4, yet they have 10 pipette difference,Why i that?

I do-not get the same results as you. My guess is that the codes you posted considers spreads && the one from within your custom_TP_SL does-not consider spreads.

Bars in test 29623
Ticks modelled 58236
Modelling quality n/a
Mismatched charts errors 0
Initial deposit 10000.00
Total net profit -119.90
Gross profit 280.02
Gross loss -399.93
Profit factor 0.70
Expected payoff -0.21
Absolute drawdown 127.06
Maximal drawdown 137.36 (1.37%)
Relative drawdown 1.37% (137.36)
Total trades 565
Short positions (won %) 277 (42.96%)
Long positions (won %) 288 (44.44%)
Profit trades (% of total) 247 (43.72%)
Loss trades (% of total) 318 (56.28%)
Largest
profit trade 3.20
loss trade -3.18
Average
profit trade 1.13
loss trade -1.26

Maximum
consecutive wins (profit in money) 6 (6.54)
consecutive losses (loss in money) 12 (-15.90)
Maximal
consecutive profit (count of wins) 6.54 (6)
consecutive loss (count of losses) -15.90 (12)
Average
consecutive wins 2
consecutive losses 2


extern double CLOSELOSSPIPS=100;
extern double CLOSEPROFITPIPS=100;
double magic=777;
double slippage=0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    Send1OrderOnly();
    ExitWithOClose();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void ExitWithOClose(){
    if(OrdersTotal()<1)return;
    for (int cnt = OrdersTotal()-1 ; cnt >= 0; cnt--) { 
        if(!OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES)){
            Print("Order SELECT failed, error # ", GetLastError() );
            return; // You should return instead of processing lines below.
        }
        if(OrderSymbol() == Symbol() && OrderMagicNumber()==magic) { 
            if(OrderProfit()<0)
                if( MathAbs(OrderOpenPrice()-OrderClosePrice()) >CLOSELOSSPIPS*Point )  
                    OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Yellow); 
            if(OrderProfit()>0)
                if( MathAbs(OrderOpenPrice()-OrderClosePrice()) >CLOSEPROFITPIPS*Point)  
                    OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Yellow);
}   }   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Send1OrderOnly(){
    if(OrdersTotal()>0)return;
    int xOrderType=RndOrdTypGen(); 
    if(xOrderType==OP_BUY){double P=Ask; color C=Blue;}else{P=Bid; C=Red;}
    OrderSend(Symbol(),xOrderType,0.01,P,slippage,0,0,"",magic,0,C);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int RndOrdTypGen(){
    static int seed; seed+=GetTickCount()/3.14159; MathSrand(seed);
    if(MathRand()%2==0){return(OP_BUY);} else {return(OP_SELL);}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

The above test was conducted with Period_M1 OpenPrice_TestMode. I went back and tested with Every-Tick and got the following.

Average
profit trade 0.94
loss trade -1.09

The profit below 1 had me concerned ... therefore I added the following:

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void deinit(){
    double totalSwaps=0, totalCommissions=0;
    for(int i=OrdersHistoryTotal()-1; i>=0; i--){
        if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
        if(OrderMagicNumber()!=magic)continue;
        if(OrderSymbol()!=Symbol())continue; 
        totalSwaps+=OrderSwap();
        totalCommissions+=OrderCommission();
    }
    Alert("totalSwaps="+totalSwaps);
    Alert("totalCommissions="+totalCommissions);
}
And Bingo:
2013.12.19 06:32:01 2013.01.29 23:59 Helping EURUSDi,M1: Alert: totalSwaps=-0.15200000

2013.12.19 06:32:01 2013.01.29 23:59 Helping EURUSDi,M1: Alert: totalCommissions=-56.63000000

Yep, thats right (negative) -56.63 in commission on minimum lots within 1 month.
 
ubzen:

The above test was conducted with Period_M1 OpenPrice_TestMode. I went back and tested with Every-Tick and got the following.

Average
profit trade 0.94
loss trade -1.09

The profit below 1 had me concerned ... therefore I added the following:

And Bingo:
2013.12.19 06:32:01 2013.01.29 23:59 Helping EURUSDi,M1: Alert: totalSwaps=-0.15200000

2013.12.19 06:32:01 2013.01.29 23:59 Helping EURUSDi,M1: Alert: totalCommissions=-56.63000000

Yep, thats right (negative) -56.63 in commission on minimum lots within 1 month.

I see now, i dont have commissions but i didnt included a time filter in the ordersend code so it might be possible that the loss was due to night trading so swaps were added also.Thanks for explaining through!
Reason: