Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Switch on/off action 2

To switch action to the one of the positions (SELL or BUY) I use:
public void businessLogic_EURUSD(double[] outY0, boolean flag0) throws Exception {

        if (outY0 != null) {
            try {

                if (outY0[0] < 0 && outY0[1] < 0 && outY0[2] < 0 && outY0[3] < 0 && outY0[4] < 0 && outY0[5] < 0) {

                    side = SideFactory.SELL;
                    System.out.println("    side  " + side);

                    if (flag0) {

                } else if (outY0[0] > 0 && outY0[1] > 0 && outY0[2] > 0 && outY0[3] > 0 && outY0[4] > 0 && outY0[5] > 0) {

                    side = SideFactory.BUY;
                    System.out.println("    side  " + side);

                    if (!flag0) {

                } else {

Open in new window

to control it I use
System.out.println("client: done logging in\n");
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            while (true) {

                String str = in.readLine();
                if (str != null) {
                    if ("eo".equalsIgnoreCase(str.trim())) {
                    } else if ("m".equalsIgnoreCase(str.trim())) {
                        printMarketData = !printMarketData;
                    } else if ("roo".equalsIgnoreCase(str.trim())) {
                        openOrderMassID = mFxcmGateway.requestOpenOrders();
                        System.out.println(">>> requestOpenOrders = " + openOrderMassID);
                    } else if ("0s".equalsIgnoreCase(str.trim())) {
                        flag0 = true;
                        bLogic_EURUSD = new BusinessLogic_EURUSD(outY0, flag0);
                        bLogic_EURUSD.businessLogic_EURUSD(outY0, flag0);
                        System.out.println("  EUR/USD =>    SELL DIRECTION ");
                    } else if ("0b".equalsIgnoreCase(str.trim())) {
                        flag0 = false;
                        bLogic_EURUSD = new BusinessLogic_EURUSD(outY0, flag0);
                        bLogic_EURUSD.businessLogic_EURUSD(outY0, flag0);
                        System.out.println("  EUR/USD =>  LL DIRECTION ");
                    } else if ("e".equalsIgnoreCase(str.trim())) {

Open in new window

but it doesn't work, as I understand because this command create flag0 value only temporarily and when new instance created from AlgoCalc class this value automatically reverts into default false.
Please advice how to make this order permanent, until I'll change it manually.
Also, how to switch both actions at all temporarily, so this pair will be inactive.
Please advice
Thank you
  • 11
  • 11
1 Solution

I am sorry,  maybe someone will understand it better,
but what I get from your explanation is that depending on the flag0 value in the method
above you either call or not call certain method (AlgoTrade...) which was written by someone else
and then you discover later that your parameter flag0 gets switched by that method, which you do not like.

That has some points ahich are not clear to me, but if in general the picture is right, why wiould not
you create another bopolean flag1 and sve flag0 value in
this flag1, so that you may be sure that no one else will mess with it.

Sorry, if misunderstood, then maybe a little bit more explanation of the context could help.
gbcbrAuthor Commented:
This is long story. I'll show you short picture.
Method algoTradeMarket has two options:
if side is SELL, it's open sell positions, if side is BUY - buy positions.
I need to control manually this process which has 4 options:
1. allow BUY and SELL;
2. allow only BUY;
3. allow only SELL;
4. forbidden all transaction;

for the beginning I start from only controlling side SELL or BUY.
Can you advice some practical solution?

OK. That I understand.
But it does not explain where lies your problem.
So you would say

side = SideFactory.SELL;

and then


in this way you are communicating to AlgoTradeMarket that you want to sell.

And what is the issue? Do you mean that  AlgoTradeMarket does not understand this command?

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

gbcbrAuthor Commented:
No, in this issue, with flag0, I expect that if flag0 settled as true, only SELL side messages will be forwarded to  AlgoTradeMarket_EURUSD.
And if BUY message will be generated, it will blocked on this level.
Well, that is what one can see in your code,
if all these outY0, ... are less than zero and and you set side to the SELL value
and then if flag0 is true excute Algo..
if all thes outY0 are less than zero and you set side to BUY and then if flag0 is false
execute Algo...
So where is the problem with it?
gbcbrAuthor Commented:
The problem is how I can:
1. Change value of flag0 without disturbing work application( now I stop app, comment unnecessary line and start it again - absolutely stupid way).
2. When I change this value in command line, it has to be permanent until new changes by command line will come.
If your application is running and you want to change some parameter within that application
while it is running, then you want to provide for this situation when creating this application by
checking, say, some file regularly, or retirieving the value from database,
say, after each cycle you go to some file and read the value from this file, then
if you change this value manually or by some different program in this file, then
your original  program will read the changed  flag on the next cycle and then make deision on the basis
of this value. Is that something that you have in mind?
gbcbrAuthor Commented:
this algorithm I know, but I don't know how to make it in Java.
I need some samples of code from which I can start my trials.
But you need a little bit more specific if we want to achive something -
so, what I wrote makes sense - you really want to check it from file?
So what kind of code examples you want, e.g. how to read from file,
or something else.  If you can formulate more clearly waht kind of
code examples you need, I' sure it would not be a problem to find or
write some examples for you.

What I mentioned above may look approximately like something below:

int count = 0;

boolean flag = false;
while(true) {

try {
DataInputStream in = new DataInputStream(new FileInputStream("C:\\flag.txt");
String buff = in.readLine();
buff = buff.trim();
if(buff.equals("0")) {flag = false;}
else if(buff.equals("1"))  {flag = true;}
else { break; }
businessLogic_EURUSD(outY, flag);

}catch(Exception ex){

System.out.println(""error " + ex.toString());



This will read file flag.txt before every call to
businesslogic method and will use the retrieved flag
in this method. It reads just one string with one number from file
if it is  zero flag will set to false, if it is one, it will be set to true,
any other value - the process will stop.
 After 1000 executions it will
exit, unless it reads from the file something different from 0 or 1
at some previous cycle.

gbcbrAuthor Commented:
Thank you for your trial.
I don't need any logical on/off.
I have to do them manually.
If I see that market goes up, I have to have instrument to forbid any sell deals which may be generated by bot decision, because in will be local minimum.
I need manual tool which I can use and say, now only BUY  or SELL deals available, or any direction available, or no deals at all.
I repeat, now I stop app, make necessary changes in the code and start again. So, I can control process, but by stupid way.
I don't understand what you mean by manual.
But the main thing you need to provide the way for your application to check some
external (relative to its own flow) entity, say like file or database
and then to do or not to do smth based on the retrieved value.

Once you have such check in place you'll not need to stop it,
but you can change while it is running - so it will have effect ion the next cycle when it checks
(but these cycles can come very quickly).
Then like in the case above you can change the value in the file by text editor
and after you save the value on the next check it will
communicate this new value to the process.
then you can make another program which would provide
gui interface to control so that instead of editing file you'll just need to press the button or change
the checkbox setting.

gbcbrAuthor Commented:
>> I don't understand what you mean by manual.
means I have to push some button or enter some text in command line, but any input methods have to be initialized by myself, according to my decision.
Well, if those decisions can be made based on some data by computer better than
by yourself, than instead of checking what you
modified in the file or listening to the click of the button-
you can at certain point in your program make analysis of the data
and the result will allow to make decision.
This would be always preferable in those cases
when automatic decisions can be as good as (or even better
than ) the human consideration.
In many situations thi is unfortunately still not the case.
This of course  goes beyond programming, though.
gbcbrAuthor Commented:
>> click of the button- ??
gbcbrAuthor Commented:
To create the bottom is no problem.
Problem id WHAT TO WRITE in TO DO area?
Click the button - well for that
you prepare GUI - instantiate Frame (Window)
place there panels and buttons, then register Mouse listener
and write code in the methods which respond to the listeners
This is what all Java Swing or AWT is about, just go to any Java web site, there are
lots of examples. If you have any specific questions, with that we can help here.

Didin't understand about TO DO area

This has penty of examples on how to deal with events
in Java/Swing:

gbcbrAuthor Commented:
OK, I create in JDeveloper JPanel and two JToggleButtons SELL and BUY with mousePressed methods.
public class BusinessLogicControl extends JPanel {
    private JTextPane jTextPane1 = new JTextPane();
    private JToggleButton jToggleButton1 = new JToggleButton();
    private JToggleButton jToggleButton2 = new JToggleButton();

    public BusinessLogicControl() {
        try {
        } catch (Exception e) {

    private void jbInit() throws Exception {
        this.setLayout( null );
        this.setSize(new Dimension(697, 492));
        jTextPane1.setBounds(new Rectangle(60, 65, 65, 30));
        jTextPane1.setBackground(new Color(255, 255, 66));
        jToggleButton1.setBounds(new Rectangle(180, 70, 107, 21));
        jToggleButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
        jToggleButton2.setBounds(new Rectangle(330, 70, 107, 21));
        jToggleButton2.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
        this.add(jToggleButton2, null);
        this.add(jToggleButton1, null);
        this.add(jTextPane1, null);

    private void eurusd_SELL_mousePressed(MouseEvent e) {

    private void eurusd_BUY_mousePressed(MouseEvent e) {

Open in new window

Now I come back to my question >>Problem is WHAT TO WRITE in TO DO area?<<
means how to describe that if I press JToggleButton1 (SELL) boolean flag0 start be true and button start be green and the value of this boolean flag0 into BusinessLogic class will be  set boolean flag0 as true.
public class BusinessLogic_EURUSD {

    boolean flag0=true;
    ISide side;

    public BusinessLogic_EURUSD() throws Exception {

    public void businessLogic_EURUSD(double[] outY0, boolean flag0) throws Exception {

        if (outY0 != null) {
            try {

                if (outY0[0] < 0 && outY0[1] < 0 && outY0[2] < 0 && outY0[3] < 0 && outY0[4] < 0 && outY0[5] < 0) {

                    side = SideFactory.SELL;
                    System.out.println("    side  " + side);

                    if (flag0) {

Open in new window

I check many samples of toggle buttons, but still can't understand how to set this boolean from one position to another and how to send it to BusinessLogic class.
I am not sure what it is you are asking.
Are you just asking how this event-driven system works?

In order to change the button color and change the boolean value you
need to write code inside the mosePressed or maybe better mouseClicked method

I guess you may want to use JToggleButton.setBackground(Color.RED)
method to change the button color and you can assign boolean flag, calll any method from
inside this mouseClicked method, and the system would ensure that this method
would be called when user clicks the button, so that you for thr most part don't need to worry about
 how exactly this method gets called.
gbcbrAuthor Commented:
I'm asking about one sample for this case, just to normalize my mind after reading so much different information about JToggleButton. Now I have complete mess.
Let's say something like this:
boolean flag0 = false;    

private void eurusd_SELL_mouseClicked(MouseEvent e) {
JToggleButton.setboolean flag = true;
bLogic.BusinessLogic_EURUSD(outY, flag0);

Open in new window

But anyway I don't understand how to set value of flag0 permanently, because this class called also from class ArrayConverter
public class ArrayConverter {

    double[][] Y = new double[10][30];
    double[] outY;
    int j;
    AlgoCalcY_EURUSD aCalc_Y_EURUSD;    

    public ArrayConverter() throws Exception {

        aCalc_Y_EURUSD = new AlgoCalcY_EURUSD();

    public void arrayConverter(double[] outX) throws Exception {

        for (int j = 29; j > 0; j--) {

            Y[0][j] = Y[0][j - 1];

        Y[0][j] = outX[0];


Open in new window

Maybe it will be changed again into false by call new BusinessLogic.
How to prevent this changes? So, only the way to set boolean value will be clicking the button.

 Your upper piece of code  is not right
Tghose are not static methods whichcan be applied to class JToggleButton, you need to apply them to instances
of JToggleButton, like jToggleButton1, e.g.


This is wrong syntax:

JToggleButton.setboolean flag = true;

In general JToggleButton is rather complex component,
It looks lthat this is how you read its state:

      AbstractButton abstractButton = (AbstractButton) jToggleButton1;
        boolean selected = abstractButton.getModel().isSelected();

Probably something similar to set the state.

But you probably do not need to do it, as it will change appearance
on each toglgle, you just keep track ofit by
changing boolean variable which would keep track of you togglebutton.

You should not confuse in your mind the state of your button and the variable
in your program, as flag0.
If flag0 can be changed by something else rather than
clicvking on the button, you may need to set up
another boolean to keep track of your button or one boolean for each
of your toggle buttons.

If flag0 is a variable of some of your classes, it cannot be changed by another class, unless you make
special provision to do it on purpose.

If you know that some piece of code may chane it and you want to capture
its previous state, create anothe boolean and keep this one unaffected
by that code, so you can lawyas know what was the previous state

Sorry, I'm probably missing something.


gbcbrAuthor Commented:
give idea how to solve it

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 11
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now