some problems on coding, please help!

 

Dear all,

1. Is there a way I can link up an order with a protect profit order or stop order so that I can use orderselect to find out whether this order has correspondent protect profit order or stop order opened or not?

2. Is there a way to let indicator use a result of a calculation from EA and let EA use a result from an indicator?

Thanks for your kind help.

regards,

wing

 
wing:

Dear all,

1. Is there a way I can link up an order with a protect profit order or stop order so that I can use orderselect to find out whether this order has correspondent protect profit order or stop order opened or not?

2. Is there a way to let indicator use a result of a calculation from EA and let EA use a result from an indicator?


1. I don't think so, not just using OrderSelect(), you have to code your own solution.

2. You could pass a value from an EA to an Indicator using a GlobalVariable, to read an Indicator buffer from an EA you use iCustom.

 
RaptorUK:

1. I don't think so, not just using OrderSelect(), you have to code your own solution.

2. You could pass a value from an EA to an Indicator using a GlobalVariable, to read an Indicator buffer from an EA you use iCustom.


RaptorUK,

Thanks for your reply.

1. I know I have to code it myself or pay someone here. What I don't know is, if I want to code it myself, what method I can code to link up the relationship of them.

2. What's the method if the indicator wants to use a result of calculation from an EA?

regards,

wing

 
RaptorUK:


2. You could pass a value from an EA to an Indicator using a GlobalVariable,

 

Presumably you could actually use GlobalVariables to solve both problems. https://docs.mql4.com/globals

You could store the information as part of the order if you made clever use of order comments or magic numbers. (Although I'm not sure if the comments or magic numbers can be modified after the order has already been placed, so that might be a limitation).

If you use variables (either in a "global" scope withing the EA, or on a "Global" scope within the terminal), then you can do it by using a network of integer variables which you use like "toggles" or "switches". (Eg. a value of "1" for "long" and "2" for "short", etc.). Whenever you make a trade action, you simultaneously keep a record of it on your "behind the scenes" network of integer variables which effectively run in ghost parallel to your trading action. When you need to find info about existing trades then you simply query this network of integer variables instead of trying to query the orders directly. This method is very versatile, but it can get messy though, especially if you forget to update some variables somewhere and the whole thing gets out of step - and it can be a nightmare to debug.

Using GlobalVariables is ultimately preferable, because the normal 'internal' variables within an EA can get deleted or reset all too easily if there is a re-initialization or the EA gets accidentally unloaded (sometimes only one accidental muscle memory click away!). At least GlobalVariables more easily survive the resetting of the terminal or computer, and are much harder to accidentally delete. Also, not only are they easier to read and access while the EA is running (by just pressing F3), but they can also be manually manipulated during the running of the EA (also through the F3 window) - which comes in handy sometimes.

 

wing:

1. Is there a way I can link up an order with a protect profit order or stop order so that I can use orderselect to find out whether this order has correspondent protect profit order or stop order opened or not?

  1. Use the correct terminology. It's called Take Profit and Stop Loss, where is no protect profit.
  2. if (Orderselect(..., MODE_HISTORY)){
       double OCP = OrderClosePrice(),
              TP  = OrderTakeProfit(),
              SL  = OrderStopLoss();
       bool ClosedBySL = MathAbs(SL-OCP) < MathAbs(TP-OCP),
            ClosedByTP = !ClosedBySL;
    }

Reason: