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.

Merge special Buys with Buys, Sells with Sells for every open instrument
 Post subject: Merge special Buys with Buys, Sells with Sells for every open instrument Post rating: 0   New post Posted: Sat 21 Jul, 2018, 11:08 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
// Just merges all buys with buys and sells with sells positions together in larger lots for each open currency pairs
// Strategy does not change trading exposure


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.IUserInterface;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;

public class Merge_Special_BUYwithBUY_SELLwithSELL implements IStrategy {

   private boolean mergeInProgress = false;
   private int counter = 0;
   private IEngine engine;
   private IContext context;
   private IConsole console;
   private IHistory history;
   private IUserInterface userInterface;

   @Configurable("Merge Buys with Buys")
   public boolean mergeBB = true;

   @Configurable("Merge Sells with Sell")
   public boolean mergeSS = true;

   @Override
   public void onStart(IContext context) throws JFException {

      context.getConsole();
      engine = context.getEngine();

   }

   public void onTick(Instrument instr, ITick tick) throws JFException {
      if (mergeBB) {
         megaMergeBUYS();
      }
      if (mergeSS) {
         megaMergeSELLS();
      }

   }

   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
   }

   public void onStop() throws JFException {
   }

   public IContext getContext() {
      return context;
   }

   public void setContext(IContext context) {
      this.context = context;
   }

   public IConsole getConsole() {
      return console;
   }

   public void setConsole(IConsole console) {
      this.console = console;
   }

   public IHistory getHistory() {
      return history;
   }

   public void setHistory(IHistory history) {
      this.history = history;
   }

   public IUserInterface getUserInterface() {
      return userInterface;
   }

   public void setUserInterface(IUserInterface userInterface) {
      this.userInterface = userInterface;
   }

   @Override
   public void onAccount(IAccount account) throws JFException {
   }

////merge////
   private void megaMergeBUYS() throws JFException {
      if (!mergeInProgress) {
         List<IOrder> allPositions = engine.getOrders();
         Map<Instrument, List<IOrder>> allByInstruments = new HashMap<Instrument, List<IOrder>>();
         for (IOrder order : allPositions) {
            if (order.isLong()) {
               Instrument instr = order.getInstrument();
               List<IOrder> orders = allByInstruments.get(instr);
               if (orders == null) {
                  orders = new ArrayList<IOrder>();
                  allByInstruments.put(instr, orders);
               }
               orders.add(order);
            }
         }
         for (Instrument instr : allByInstruments.keySet()) {
            List<IOrder> list = allByInstruments.get(instr);
            if (list.size() > 1) {
               mergeOrders(list);
               mergeInProgress = true;
            }
         }
      }
   }

   private void megaMergeSELLS() throws JFException {
      if (!mergeInProgress) {
         List<IOrder> allPositions = engine.getOrders();
         Map<Instrument, List<IOrder>> allByInstruments = new HashMap<Instrument, List<IOrder>>();
         for (IOrder order : allPositions) {
            if (!order.isLong()) {
               Instrument instr = order.getInstrument();
               List<IOrder> orders = allByInstruments.get(instr);
               if (orders == null) {
                  orders = new ArrayList<IOrder>();
                  allByInstruments.put(instr, orders);
               }
               orders.add(order);
            }
         }
         for (Instrument instr : allByInstruments.keySet()) {
            List<IOrder> list = allByInstruments.get(instr);
            if (list.size() > 1) {
               mergeOrders(list);
               mergeInProgress = true;
            }
         }
      }
   }

   public void mergeOrders(List<IOrder> positions) throws JFException {

      if (positions == null || positions.size() == 0) {
         return;
      }
      engine.mergeOrders("Merged" + counter + System.currentTimeMillis(), positions.toArray(new IOrder[0]));
      counter++;
   }

   public void onMessage(IMessage message) throws JFException {
   }
}


Attachments:
Merge_Special_BUYwithBUY_SELLwithSELL.java [6.54 KiB]
Downloaded 140 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Merge special Buys with Buys, Sells with Sells for every open instrument Post rating: 0   New post Posted: Fri 03 Aug, 2018, 19:20 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
V2
Fixed - removed interference with pending orders


Attachments:
Merge_Special_BUYwithBUY_SELLwithSELL_V2.java [6.49 KiB]
Downloaded 136 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 

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