How to code? - page 266

 
ixbone:
Hi all,

i want to collect all orders (buy&sell) of a pair by magic and if they reach a breakeven, all orders should be closed.

any guidance very welcome, thanks

extern bool PairBreakeven=true;// Breakeven per pair all orders of same pair/magic

extern double Pairbreakevengain=6;// gain in pips required to enable the break even

extern double Pairbreakeven=3;// break even, order closed, 3 pip profit/slippage

double Pairmovebreakeven;

if(PairBreakeven==true){

if(Pairbreakevengain>0)Pairmovebreakeven(Pairbreakevengain,Pairbreakeven);

CloseBuyOrders(Magic);

CloseSellOrders(Magic);

}

void Pairmovebreakeven(double Pairbreakevengain,double Pairbreakeven){

RefreshRates();

if(OrdersTotal()>0){

for(int i=OrdersTotal();i>=0;i++){

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic ){

return;

}

}

}

}

int CloseBuyOrders(int Magic){ //op_sell is similar

int total=OrdersTotal();

for(int cnt=total-1;cnt>=0;cnt--){

OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol()){

if(OrderType()==OP_BUY){

OrderClose(OrderTicket(),OrderLots(),Bid,3*mt);

}

}

}

return(0);

}

The main problem with this code is in the BE function, it never really moved stop or take any action. If you don't state your intention in words, I can't really detect what you are trying to do here from the code.

You want the code to detect all the orders in your account which has a stop loss=order open price and close all of them? So the actually break even action is done by other EA? Would like to help but can't figure out what you want to do.

 
christinaLi:
The main problem with this code is in the BE function, it never really moved stop or take any action. If you don't state your intention in words, I can't really detect what you are trying to do here from the code. You want the code to detect all the orders in your account which has a stop loss=order open price and close all of them? So the actually break even action is done by other EA? Would like to help but can't figure out what you want to do.

christinaLi,

sorry, for my misleading.

-i want to collect all orders of a pair by magic and summarize the profits from all orders, regardless of buy, sell, tp, sl, example: we have 8 buy & sell orders, profit summarized is 6 pips // extern double Pairbreakevengain=6;// gain in pips required to enable the break even

-if the profit is +6 pips for all orders of a pair (buy&sell),all orders (buy/sell) sl should be set stoploss to currentprice -3 //extern double Pairbreakeven=3;// break even, order closed, 3 pip profit/slippage

-if Pairbreakevengain falls from profit of all orders of a pair example: 8 mixed buy/sell orders profit is sum 6 back to 3, all orders are closed

i have a correct working module for a single order breakeven and i have tried to modify it by myself, i'm lack on mt4 code skills, many many years ago, i learned assembler...

Should i post the single breakeven order function?

thanks for help

IX

 

Hi, IX,

I know what you are trying to do now. It can be done.

Things is somewhat complicated by the fact you could have buys and sells at the same time. Because when you talk about "current price", for buys it means Bid, for sells it means Ask, because only those are realistic price in order to close orders. So you can see at any given point when you adjust the stop loss for your orders, buys and sells are given a difference price. If your spread is not constant, there are lots of chances your orders won't be closed at the same time, sometimes it's possible half of your orders won't be closed.

I think the good way to do this is not to actually move the stop loss, instead the EA will activate a memory once overall 6 pips profit is reached, then simply close out all orders at market while overall profit falls to 3 pips. In this case, EA forces to close out orders, they are not stopped out by MT4.

 
christinaLi:
Hi, IX,

I know what you are trying to do now. It can be done.

Things is somewhat complicated by the fact you could have buys and sells at the same time. Because when you talk about "current price", for buys it means Bid, for sells it means Ask, because only those are realistic price in order to close orders. So you can see at any given point when you adjust the stop loss for your orders, buys and sells are given a difference price. If your spread is not constant, there are lots of chances your orders won't be closed at the same time, sometimes it's possible half of your orders won't be closed.

I think the good way to do this is not to actually move the stop loss, instead the EA will activate a memory once overall 6 pips profit is reached, then simply close out all orders at market while overall profit falls to 3 pips. In this case, EA forces to close out orders, they are not stopped out by MT4.

Hi christinaLi,

i agree to 100%, its to complicated, i understand what you mean, maybe a pair(basket) calculation without moving a stoploss is the solution, like:

- select all orders per pair and magic

- summarize profit of all orders

- if profit sum is = 6pips

- close all orders, if no slippage occours, breakeven will be 6pips "profit", if slippage 3pips result for breakeven is 3pips "profit"

1) the advantage is: no problems at all

2) disadvantage: no (3pips) room for all orders, if i have 7 buys and 6 sells hedged orders, if the one buy order increase 6 pips, all orders closed, is there an option to mark all orders in memory to be closed if 6 pips reached (breakeven=true) by a decrease of 3pips (6-3= 3pips profit) (breakevencloseorders=true)?

The 7buy and 6sell hedged orders can't rise over 6 pips with #1, they are always closed.

With #2, 6 pips are only a activation marker, if decrease to 3 pips, close all orders, if rise over 6 pips, example to 15 pips, there is room from +3pips till 15pips which results in 12 pips profit

spread is not calculated at this time! because 7buys & 6sells summarize with 2pips spread to 26pips to breakeven "0" must have for the one buy order and to must have 32 pips with #1 and 29 pips must have with #2 for the one buy order - am i right?

thanks for your suggestions

IX

 
ixbone:
Hi christinaLi,

i agree to 100%, its to complicated, i understand what you mean, maybe a pair(basket) calculation without moving a stoploss is the solution, like:

- select all orders per pair and magic

- summarize profit of all orders

- if profit sum is = 6pips

- close all orders, if no slippage occours, breakeven will be 6pips "profit", if slippage 3pips result for breakeven is 3pips "profit"

1) the advantage is: no problems at all

2) disadvantage: no (3pips) room for all orders, if i have 7 buys and 6 sells hedged orders, if the one buy order increase 6 pips, all orders closed, is there an option to mark all orders in memory to be closed if 6 pips reached (breakeven=true) by a decrease of 3pips (6-3= 3pips profit) (breakevencloseorders=true)?

The 7buy and 6sell hedged orders can't rise over 6 pips with #1, they are always closed.

With #2, 6 pips are only a activation marker, if decrease to 3 pips, close all orders, if rise over 6 pips, example to 15 pips, there is room from +3pips till 15pips which results in 12 pips profit

spread is not calculated at this time! because 7buys & 6sells summarize with 2pips spread to 26pips to breakeven "0" must have for the one buy order and to must have 32 pips with #1 and 29 pips must have with #2 for the one buy order - am i right?

thanks for your suggestions

IX

I think you misunderstood me, what you want can be accurately acheived, only no actual modify of stop loss. I will find some time to make the code today and demonstrate for you. Christina

 
ixbone:
Hi christinaLi,

i agree to 100%, its to complicated, i understand what you mean, maybe a pair(basket) calculation without moving a stoploss is the solution, like:

- select all orders per pair and magic

- summarize profit of all orders

- if profit sum is = 6pips

- close all orders, if no slippage occours, breakeven will be 6pips "profit", if slippage 3pips result for breakeven is 3pips "profit"

1) the advantage is: no problems at all

2) disadvantage: no (3pips) room for all orders, if i have 7 buys and 6 sells hedged orders, if the one buy order increase 6 pips, all orders closed, is there an option to mark all orders in memory to be closed if 6 pips reached (breakeven=true) by a decrease of 3pips (6-3= 3pips profit) (breakevencloseorders=true)?

The 7buy and 6sell hedged orders can't rise over 6 pips with #1, they are always closed.

With #2, 6 pips are only a activation marker, if decrease to 3 pips, close all orders, if rise over 6 pips, example to 15 pips, there is room from +3pips till 15pips which results in 12 pips profit

spread is not calculated at this time! because 7buys & 6sells summarize with 2pips spread to 26pips to breakeven "0" must have for the one buy order and to must have 32 pips with #1 and 29 pips must have with #2 for the one buy order - am i right?

thanks for your suggestions

IX

Actually, now I've gave it a bit more thought, what you asked can't be done.

This is rather interesting, I've coded a lot of similar functions, you can calculate BE intern of dollar amount or pips, but they are all the same order type. It can't be done with both buys and sells. For example, if you have x buys (they can be different lot size), at some point, all dollar profit added together is 0, so now you are at BE. Then if they are buys, if you want 3 pips in profit, then simply add 3 pips to current Bid, then you have a target price. if all sells, you subtract 3 pips from current Ask, the you have a target price. But if you have both buys and sells, what do you do? add or subtract?

How do you define you have 3 pips profit in the following situation? 1 lot buy at 1.4100, 2 lot sell at 1.4150?

The only thing if can be done is use dollar profit, not pip profit I think.

 
christinaLi:
I think you misunderstood me, what you want can be accurately acheived, only no actual modify of stop loss. I will find some time to make the code today and demonstrate for you. Christina

Thank you Christina,

i appreciate your knowledge very much!

Michael

 
christinaLi:
Actually, now I've gave it a bit more thought, what you asked can't be done.

This is rather interesting, I've coded a lot of similar functions, you can calculate BE intern of dollar amount or pips, but they are all the same order type. It can't be done with both buys and sells. For example, if you have x buys (they can be different lot size), at some point, all dollar profit added together is 0, so now you are at BE. Then if they are buys, if you want 3 pips in profit, then simply add 3 pips to current Bid, then you have a target price. if all sells, you subtract 3 pips from current Ask, the you have a target price. But if you have both buys and sells, what do you do? add or subtract?

How do you define you have 3 pips profit in the following situation? 1 lot buy at 1.4100, 2 lot sell at 1.4150?

The only thing if can be done is use dollar profit, not pip profit I think.

Christina,

Add or substract doesnt matter, because the lot sum of 7 buy versus 6 sell orders are enough to reach normal profits, BE for all orders of a pair should work as a safety feature:

With this hedge feature, you save a lot of margin, with steps between like 50 pips, your account cant get wiped, if normal profits not reached, BE should close all orders and start with a new signal and first order.

For buy/sell signal i use price action.

Files:
unbenannt.png  7 kb
 
ixbone:
Christina,

Add or substract doesnt matter, because the lot sum of 7 buy versus 6 sell orders are enough to reach normal profits, BE for all orders of a pair should work as a safety feature:

With this hedge feature, you save a lot of margin, with steps between like 50 pips, your account cant get wiped, if normal profits not reached, BE should close all orders and start with a new signal and first order.

For buy/sell signal i use price action.

Additional info:

- i trade 8-10 majors at the same time for diversification (partly correlation), so its better to trade 10 pairs with 0.01 as one pair with 0.1

- i douple my steps between the orders by, 25,50,100,200,400,

example:

1st order 1.5000

2nd order 1.5025

3rd.........1.050

4th.........1.100

and so on

 

I would like to help with this function you are talking about but it seems I can't simply figure out your logic. This really has nothing to do with other logic of the strategy .

I would simply like to know this.

If you have 1 lot buy at 1.4110 and 2 lot sell at 1.4200. At what price you think you have 6 pips gain?

Reason: