Solved

Switch on/off action 2

Posted on 2011-02-12
22
217 Views
Last Modified: 2012-05-11
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) {
                        AlgoTradeMarket_EURUSD.getInstance(null).algoTradeMarket(side);
                    }


                } 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) {
                        AlgoTradeMarket_EURUSD.getInstance(null).algoTradeMarket(side);
                    }

                } 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())) {
                        mFxcmGateway.logout();
                        System.exit(0);

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
0
Comment
Question by:gbcbr
  • 11
  • 11
22 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

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.
0
 

Author Comment

by:gbcbr
Comment Utility
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?


0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
OK. That I understand.
But it does not explain where lies your problem.
So you would say

side = SideFactory.SELL;

and then

 AlgoTradeMarket_EURUSD.getInstance(null).algoTradeMarket(side);

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?





0
 

Author Comment

by:gbcbr
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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?
0
 

Author Comment

by:gbcbr
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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?
0
 

Author Comment

by:gbcbr
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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) {
if(count>1000)break;


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

}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.




0
 

Author Comment

by:gbcbr
Comment Utility
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.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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.




0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:gbcbr
Comment Utility
>> 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.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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.
 
0
 

Author Comment

by:gbcbr
Comment Utility
>> click of the button- ??
sample?
0
 

Author Comment

by:gbcbr
Comment Utility
To create the bottom is no problem.
Problem id WHAT TO WRITE in TO DO area?
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

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

http://download.oracle.com/javase/tutorial/uiswing/events/index.html
0
 

Author Comment

by:gbcbr
Comment Utility
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 {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setLayout( null );
        this.setSize(new Dimension(697, 492));
        this.setAutoscrolls(true);
        jTextPane1.setBounds(new Rectangle(60, 65, 65, 30));
        jTextPane1.setText("EUR/USD");
        jTextPane1.setBackground(new Color(255, 255, 66));
        jToggleButton1.setText("SELL");
        jToggleButton1.setBounds(new Rectangle(180, 70, 107, 21));
        jToggleButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    eurusd_SELL_mousePressed(e);
                }
            });
        jToggleButton2.setText("BUY");
        jToggleButton2.setBounds(new Rectangle(330, 70, 107, 21));
        jToggleButton2.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    eurusd_BUY_mousePressed(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) {
                        AlgoTradeMarket_EURUSD.getInstance(null).algoTradeMarket(side);
                    }

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.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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.
0
 

Author Comment

by:gbcbr
Comment Utility
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;
JToggleButton.setBackground(Color.GREEN)
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];

        aCalc_Y_EURUSD.outY(Y);
    }

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.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility

 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.

jToggleButton1.setBackGround(...)

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.

 




 
0
 

Author Closing Comment

by:gbcbr
Comment Utility
give idea how to solve it
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now