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.

Saving closing price of last order in a variable till next profitable trade
 Post subject: Saving closing price of last order in a variable till next profitable trade Post rating: 0   New post Posted: Thu 20 Dec, 2012, 21:00 

User rating: 0
Joined: Tue 16 Oct, 2012, 12:23
Posts: 40
To Jforex Support,

I want to make the a modification in my strategy which is as follows :

If a trade closes in loss, its closing price should be stored in a variable, say x. The next trade should open only if it crosses x. If it results in a loss, then the next trade should also open only if it crosses x. The value of x can change only if a trade results in profit.

For example, if a EURUSD BUY trade results in a loss and closes at 1.3200, this value should be stored in a variable. The next trade should be taken only if it crosses 1.3200. If it results in loss too, then the next trade should also open only if it crosses 1.3200. This value of 1.3200 should remain unchanged until there is profitable trade.

I used the following approach :

public void onMessage(IMessage message) throws JFException {
        if (message.getType() == IMessage.Type.ORDER_CLOSE_OK && message.getOrder().getState() == IOrder.State.CLOSED)
       {
        lastorderclosed = message.getOrder();
        if (lastorderclosed.getProfitLossInUSD() < 0)
       {
        ......................}
       else
       {
        ......................}
       }
       }


Kindly suggest how I should make this modification.

Regards,

Jimmy.


 
 Post subject: Re: Saving closing price of last order in a variable till next profitable trade Post rating: 0   New post Posted: Fri 21 Dec, 2012, 08:27 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Which part is not clear to you how to realize?


 
 Post subject: Re: Saving closing price of last order in a variable till next profitable trade Post rating: 0   New post Posted: Fri 21 Dec, 2012, 15:09 

User rating: 0
Joined: Tue 16 Oct, 2012, 12:23
Posts: 40
To Jforex Support,

I know how to fetch the closing price of the last order closed and open the current trade if it crosses it. But I don't know how to fetch the closing price of the first trade that resulted in a loss and open the subsequent trades with respect to it.

In other words, I know how to open the first trade. And I know how to open the second trade which depends on the closing price of the first trade. But I don't know how to open the third and subsequent trades which depend on the closing price of the first trade. How should I fetch the closing price of the first trade to open the third and subsequent trades?


 
 Post subject: Re: Saving closing price of last order in a variable till next profitable trade Post rating: 0   New post Posted: Fri 21 Dec, 2012, 15:36 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
JimmyHowl8 wrote:
But I don't know how to open the third and subsequent trades which depend on the closing price of the first trade. How should I fetch the closing price of the first trade to open the third and subsequent trades?
Simply fetch the first closed order in a field (i.e. global variable) and for subsequent orders take its close price.
private IOrder firstClosedOrder;

public void onStart(IContext context) throws JFException{
//...
}
public void onMessage(IMessage message) throws JFException {
    if (message.getType() == IMessage.Type.ORDER_CLOSE_OK && message.getOrder().getState() == IOrder.State.CLOSED) {
        if(firstClosedOrder== null){
             firstClosedOrder = message.getOrder();
             console.getOut().println("first closed order: " + firstClosedOrder);
        } else {
             console.getOut().println("subsequent closed order: " + message.getOrder() + " \n first order close price: " + firstClosedOrder.getClosePrice());
        }
       
    }
}


 
 Post subject: Re: Saving closing price of last order in a variable till next profitable trade Post rating: 0   New post Posted: Fri 21 Dec, 2012, 18:39 

User rating: 0
Joined: Tue 16 Oct, 2012, 12:23
Posts: 40
But this method that you suggested will save the closing price of only the very first order after starting the strategy. But what I wanted was that the closing price of only the first order that results in a loss should be saved. And this value will be replaced by the closing price of the first trade which results in a loss after a profitable trade. This means that the first loss-making trade closed after a profitable trade will become the first trade for the subsequent trades.

Let me give you an example.

- First trade - Profitable - Closing price not saved

- Second trade - Loss-making - CLOSING PRICE SAVED
- Third trade - Opened if it crosses the saved closing price - Loss-making - Closing price not saved
- Fourth trade - Opened if it crosses the saved closing price - Loss-making - Closing price not saved
- Fifth trade - Opened if it crosses the saved closing price - Profitable - Closing price not saved
- Sixth trade - Opened if it crosses the saved closing price - Profitable - Closing price not saved

- Seventh trade - Loss making - CLOSING PRICED SAVED BY REPLACING THE PREVIOUSLY SAVED PRICE
- Eighth trade - Opened if it crosses the saved closing price - Profitable - Closing price not saved
..............

I hope now it is now clear what is my strategy. Could you plz tell me how to save the closing price of the first loss-making trade and then replace it with the closing price of the first loss-making trade after a profitable trade?


 
 Post subject: Re: Saving closing price of last order in a variable till next profitable trade Post rating: 0   New post Posted: Tue 25 Dec, 2012, 14:57 

User rating: 0
Joined: Tue 16 Oct, 2012, 12:23
Posts: 40
I request Jforex Support to plz give me a suggestion.


 
 Post subject: Re: Saving closing price of last order in a variable till next profitable trade Post rating: 3   New post Posted: Wed 26 Dec, 2012, 03:21 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
I've not compiled the following but this here is the general idea.

// initialise to 'true' if non-existent first trade closed in profit i.e. store first loser
// initialise to 'false' if non-existent first trade closed in loss i.e. wait for first profit before storing first loser
private boolean profitableCloseBooked = true;

// losing close following a profitable close
private IOrder savedLosingClose = null;

public void onStart(IContext context) throws JFException{
//...
}

public void onMessage(IMessage message) throws JFException
{
   if(message.getType() == IMessage.Type.ORDER_CLOSE_OK && message.getOrder().getState() == IOrder.State.CLOSED)
   {
      if(message.getOrder().getProfitLossInUSD() < 0)
      {
         if(profitableCloseBooked == true)
         {
            console.getOut().println("Losing trade booked with save: " + message.getOrder());
            console.getOut().println("and replacing current saved loser: " + savedLosingClose);
            savedLosingClose = message.getOrder();
            profitableCloseBooked = false;
         }
         else
         {
            console.getOut().println("Losing trade booked without save: " + message.getOrder());
         }
      }
      else // considers getProfitLossInUSD() >= 0 to be profit unless the above check is changed to '<=0'
      {
         profitableCloseBooked = true;
         console.getOut().println("Profitable trade booked: " + message.getOrder());
      }
   }
}


 

Jump to:  

cron
  © 1998-2025 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