Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

logic problem in code- help please
 Post subject: logic problem in code- help please Post rating: 0   New post Posted: Sat 22 Nov, 2014, 10:32 
User avatar

User rating: 2
Joined: Fri 02 Mar, 2012, 22:08
Posts: 200
Location: New Zealand, Dunedin
The following code is meant to compare the current trade order with the previous trade order and do nothing if both orders are in the same direction
Otherwise, close previous order (if still open) and open new order in different direction

When testing it sometimes closes an order and then opens a new order both in the same direction !!
************************************************************************
// if the previous period trade still open and the new trade order
// is in the opposite direction then close the previous trade now
for (IOrder my_order : myEngine.getOrders(myInstrument)) {
if((my_order != null && my_order.getState() == IOrder.State.FILLED) &&
(!curr_trade_dir.equals(prev_trade_dir))) {
my_order.close();
my_order.waitForUpdate(IOrder.State.CLOSED); //wait till the order is closed
myConsole.getOut().println("Order " + my_order.getLabel() + " is closed");
myCanTrade = true;
} else {
myConsole.getOut().println("Next order is in same direction");
myCanTrade = false;
}
}

// place new trade order if required
if ((myCanTrade)&& (myCurrPositionValue > 0)) {
if (curr_trade_dir.equals("UP")){ //bullish
myOrderDirection = OrderCommand.BUY;
double takeProfitPrice = myHistory.getLastTick(myInstrument)
.getBid() + myInstrument.getPipValue() * myTakeProfit;
placeBuyorSell(myOrderDirection, 0, takeProfitPrice);
}
else if(curr_trade_dir.equals("DOWN")){ //bearish
myOrderDirection = OrderCommand.SELL;
double takeProfitPrice = myHistory.getLastTick(myInstrument)
.getAsk() - myInstrument.getPipValue() * myTakeProfit;
placeBuyorSell(myOrderDirection, 0, takeProfitPrice);
}
myCanTrade = false;
}
prev_trade_dir = curr_trade_dir;
curr_trade_dir = "";
**************************************************************

Any ideas on where I am going wrong ?

Bob M


 
 Post subject: Re: logic problem in code- help please Post rating: 0   New post Posted: Sat 22 Nov, 2014, 20:39 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
prev_trade_dir = curr_trade_dir;
curr_trade_dir = "";

these should be placed after 'myCanTrade = false' within that last curly brace.
you only want to make these changes if you make a new trade otherwise there are potentially instances when prev_trade_dir = curr_trade_dir = "", wiping out any sense of direction.

Curiosities: if the old trade is stopped out, based on the code presented, how does myCanTrade get set to true? What about partial fills, do those create multiple filled orders?

Q. If the old trade is stopped out, do you still care about its direction?

i would decouple 'myCanTrade' from the action of closing old trades.
the initial for loop and if statements are dangerous as if there are multiple orders in that order list with one not in the 'FILLED' state following one that is, you will set myCanTrade to false via the 'else' and prevent a trade just because of the ordering/state of orders in the list. You might consider the following example which doesn't require you to completely overhaul your logic:
_barrier = 0; // count filled trades we find and kill

//kill any and all trades in the filled position
for (IOrder my_order : myEngine.getOrders(myInstrument))
{
    if(my_order != null && my_order.getState() == IOrder.State.FILLED)
    {
        _barrier++; //we found one - doesn't matter position in list

        if(!curr_trade_dir.equals(prev_trade_dir)) //since we found one only kill it based on your rules
        {
            my_order.close();
            my_order.waitForUpdate(IOrder.State.CLOSED); //wait till the order is closed
            myConsole.getOut().println("Order " + my_order.getLabel() + " is closed");
           _barrier--; //we killed one - doesn't matter position in list
        }
        else
        {
           myConsole.getOut().println("Next order is in same direction as this order found");
        }
    }
}
//at this point if barrier = 0 we have no more trades or had none when the loop started
myCanTrade = (0 == _barrier) && (!curr_trade_dir.equals(prev_trade_dir)); //1
myCanTrade = (0 == _barrier); //2
//if you answered Yes to the Q. above, use //1 as the rules caused the close(s) OR (the the old trade(s) was(were) stopped out AND you still care about the old direction)
//if you answered No to the Q. above, use //2 as the rules caused the close(s) OR the the old trade(s) was(were) stopped out

// place new trade order if required
if((myCanTrade) && (myCurrPositionValue > 0))
{
   if(curr_trade_dir.equals("UP")){ //bullish
      myOrderDirection = OrderCommand.BUY;
      double takeProfitPrice = myHistory.getLastTick(myInstrument) .getBid() + myInstrument.getPipValue() * myTakeProfit;
      placeBuyorSell(myOrderDirection, 0, takeProfitPrice);
   }
   else if(curr_trade_dir.equals("DOWN"))
   { //bearish
      myOrderDirection = OrderCommand.SELL;
      double takeProfitPrice = myHistory.getLastTick(myInstrument) .getAsk() - myInstrument.getPipValue() * myTakeProfit;
      placeBuyorSell(myOrderDirection, 0, takeProfitPrice);
   }
   //I would duplicate and move these after each of placeBuyorSell since strictly curr_trade_dir = "", "ABC", "123" would trigger them here
   //Keep them here if your idea of "prev trade" / "curr trade" is about the trade "opportunity" that just came up (long vs short)
   //consider duplicating/moving them if your idea of "prev trade" / "curr trade" is about actual trades you manage to take (long vs short)
   prev_trade_dir = curr_trade_dir;
   curr_trade_dir = "";

   //no matter what no more trading (just closing) since we should have just made a trade if we reached here
   myCanTrade = false; 
}


 
 Post subject: Re: logic problem in code- help please Post rating: 0   New post Posted: Sun 23 Nov, 2014, 00:52 
User avatar

User rating: 2
Joined: Fri 02 Mar, 2012, 22:08
Posts: 200
Location: New Zealand, Dunedin
Hi Critical Section

As I read the code, myCanTrade is set to true if (a) previous trade is still open and (b) new trade is in opposite direction
If both are in same direction -take no action

Q. if the previous trade is closed I do not care about its direction

Thank you for adjusted code - let me digest :)

Bob M

p.s. partial fills - I have no experience of - maybe I trade in very small amounts :)


 
 Post subject: Re: logic problem in code- help please Post rating: 0   New post Posted: Sun 23 Nov, 2014, 01:48 
User avatar

User rating: 2
Joined: Fri 02 Mar, 2012, 22:08
Posts: 200
Location: New Zealand, Dunedin
My code is now as follows:-
*************************************************
_barrier = 0; // count filled trades we find and kill

// kill any and all trades in the filled position
for (IOrder my_order : myEngine.getOrders(myInstrument))
{
if(my_order != null && my_order.getState() == IOrder.State.FILLED)
    {
        _barrier++; // we found one - doesn't matter position in list
 
        if(!curr_trade_dir.equals(prev_trade_dir)) //since we found one only kill it based on your rules
        {
            my_order.close();
my_order.waitForUpdate(IOrder.State.CLOSED); //wait till the order is closed
myConsole.getOut().println("Order " + my_order.getLabel() + " is closed");
_barrier--; //we killed one - doesn't matter position in list
        }
        else
        {
           myConsole.getOut().println("Next order is in same direction as this order found");
        }
    }
} // at this point if barrier = 0 we have no more trades or had none when the loop started
myCanTrade = (0 == _barrier);

// place new trade order if required
if((myCanTrade) && (myCurrPositionValue > 0))
{
if(curr_trade_dir.equals("UP")){ //bullish
myOrderDirection = OrderCommand.BUY;
double takeProfitPrice = myHistory.getLastTick(myInstrument) .getBid() + myInstrument.getPipValue() * myTakeProfit;
placeBuyorSell(myOrderDirection, 0, takeProfitPrice);
prev_trade_dir = curr_trade_dir;
curr_trade_dir = "";
   }
   else if(curr_trade_dir.equals("DOWN")){ //bearish
myOrderDirection = OrderCommand.SELL;
double takeProfitPrice = myHistory.getLastTick(myInstrument) .getAsk() - myInstrument.getPipValue() * myTakeProfit;
placeBuyorSell(myOrderDirection, 0, takeProfitPrice);
prev_trade_dir = curr_trade_dir;
curr_trade_dir = "";
}

// no matter what no more trading (just closing) since we should have just made a trade if we reached here
myCanTrade = false;
}

Predicted_Trend = 0;
} // end onBar
**************************************************************************************
and on compilation I am getting the following errors:-

2014-11-23 00:42:32 ----------
2014-11-23 00:42:32 Syntax error, insert "}" to complete Statement
2014-11-23 00:42:32 ^
2014-11-23 00:42:32 Predicted_Trend = 0;
2014-11-23 00:42:32 7. ERROR in C:\Users\Stan\Local Settings\JForex\Stan\jfxide\tmp\compile\us_copiosus_USDJPY_S.java (at line 707)
2014-11-23 00:42:32 ----------
2014-11-23 00:42:32 Syntax error on tokens, delete these tokens
2014-11-23 00:42:32 ^^^^^^^^^^^^
2014-11-23 00:42:32    else if(curr_trade_dir.equals("DOWN")){ //bearish
2014-11-23 00:42:32    }
2014-11-23 00:42:32 6. ERROR in C:\Users\Stan\Local Settings\JForex\Stan\jfxide\tmp\compile\us_copiosus_USDJPY_S.java (at line 694)
2014-11-23 00:42:32 ----------
2014-11-23 00:42:32 Syntax error on tokens, delete these tokens
2014-11-23 00:42:32 ^^^^^^^^^^^^^^^
2014-11-23 00:42:32     }
2014-11-23 00:42:32         }
2014-11-23 00:42:32 5. ERROR in C:\Users\Stan\Local Settings\JForex\Stan\jfxide\tmp\compile\us_copiosus_USDJPY_S.java (at line 680)
2014-11-23 00:42:32 ----------
2014-11-23 00:42:32 Syntax error on tokens, delete these tokens
2014-11-23 00:42:32 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2014-11-23 00:42:32            myConsole.getOut().println("Next order is in same direction as this order found");
2014-11-23 00:42:32         {
2014-11-23 00:42:32         else
2014-11-23 00:42:32         }
2014-11-23 00:42:32 4. ERROR in C:\Users\Stan\Local Settings\JForex\Stan\jfxide\tmp\compile\us_copiosus_USDJPY_S.java (at line 676)
2014-11-23 00:42:32 ----------
2014-11-23 00:42:32 Syntax error on tokens, delete these tokens
2014-11-23 00:42:32 ^^^^^^^^^^^^^^^^^^^^^
2014-11-23 00:42:32             my_order.close();
2014-11-23 00:42:32         {
2014-11-23 00:42:32 3. ERROR in C:\Users\Stan\Local Settings\JForex\Stan\jfxide\tmp\compile\us_copiosus_USDJPY_S.java (at line 671)
2014-11-23 00:42:32 ----------
2014-11-23 00:42:32 Syntax error on tokens, delete these tokens
2014-11-23 00:42:32 ^^^^^^^^^
2014-11-23 00:42:32         if(!curr_trade_dir.equals(prev_trade_dir)) //since we found one only kill it based on your rules
2014-11-23 00:42:32  
2014-11-23 00:42:32 2. ERROR in C:\Users\Stan\Local Settings\JForex\Stan\jfxide\tmp\compile\us_copiosus_USDJPY_S.java (at line 669)
2014-11-23 00:42:32 ----------
2014-11-23 00:42:32 Syntax error on tokens, delete these tokens
2014-11-23 00:42:32 ^^^^^^^^^^^^^
2014-11-23 00:42:32         _barrier++; // we found one - doesn't matter position in list
2014-11-23 00:42:32     {
2014-11-23 00:42:32 1. ERROR in C:\Users\Stan\Local Settings\JForex\Stan\jfxide\tmp\compile\us_copiosus_USDJPY_S.java (at line 667)
2014-11-23 00:42:32 ----------
2014-11-23 00:42:31 Compiling us_copiosus_USDJPY_S.java

Bob M


 
 Post subject: Re: logic problem in code- help please Post rating: 0   New post Posted: Sun 23 Nov, 2014, 08:43 
User avatar

User rating: 2
Joined: Fri 02 Mar, 2012, 22:08
Posts: 200
Location: New Zealand, Dunedin
OK I have sorted the problem............

clean compile achieved

Something odd about spaces in the code

Bob M


 
 Post subject: Re: logic problem in code- help please Post rating: 0   New post Posted: Sun 23 Nov, 2014, 19:14 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
good job Bob, hopefully things are looking better during your test runs.


 

Jump to:  

  © 1998-2024 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com