Initial commit of multiasset branch

This commit is contained in:
2018-11-30 12:27:41 +01:00
parent 45684a9d0e
commit bb18f7cf03
205 changed files with 8656 additions and 2750 deletions

View File

@ -0,0 +1,119 @@
/*
* Copyright (c) 2017, tobias
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import java.util.concurrent.ConcurrentHashMap;
/**
* Implements a trading account
*/
public class Account implements Comparable {
private Exchange.AccountListener listener = null;
protected final double id;
private double shares;
private double money;
protected AutoTraderInterface owner;
protected final ConcurrentHashMap<Long , Order> orders;
protected ConcurrentHashMap<String,Double> sharesm;
@Override
public int compareTo(Object a) {
Account account = (Account) a;
return this.id - account.id < 0 ? -1 : 1;
}
Account(double id, double money, double shares) {
this.id=id;
//id = (random.nextDouble() + (account_id_generator.getNext()));
orders = new ConcurrentHashMap();
this.money = money;
this.shares = shares;
}
public double getID() {
return id;
}
public double getShares() {
return shares;
}
protected void setShares(double shares){
this.shares = shares;
}
protected void addShares(double shares){
this.shares = this.shares+shares;
}
protected void addShares(String symbol, double shares){
Double d = this.sharesm.get(symbol);
d+=shares;
this.sharesm.put(symbol, d);
// (this.sharesm.get(symbol))+=shares;
}
protected void addMoney(double money){
this.money+=money;
}
protected void setMoney(double money){
this.money=money;
}
public double getShares(String symbol){
return sharesm.get(symbol);
}
public double getMoney() {
return money;
}
public AutoTraderInterface getOwner() {
return owner;
}
public ConcurrentHashMap<Long, Order> getOrders() {
return orders;
}
public void setListener(Exchange.AccountListener al) {
this.listener = al;
}
public void update(Order o) {
if (listener == null) {
return;
}
listener.accountUpdated(this, o);
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import org.json.JSONObject;
import opensesim.old_sesim.Scheduler.TimerTaskRunner;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public abstract class AutoTraderBase implements AutoTraderInterface, TimerTaskRunner {
protected double account_id;
protected Exchange se;
// protected AutoTraderConfig config;
protected String name;
/* public AutoTraderBase(Exchange se, long id, String name, double money, double shares, AutoTraderConfig config) {
account_id = se.createAccount(money, shares);
Exchange.Account a = se.getAccount(account_id);
// a.owner=this;
this.se = se;
this.config = config;
this.name = name;
this.id = id;
}
*/
public AutoTraderBase() {
se = null;
id = 0;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public long getID() {
return id;
}
private long id;
public Account getAccount() {
return se.getAccount(account_id);
}
@Override
public void init(Exchange se, long id, String name, double money, double shares, JSONObject cfg) {
this.account_id = se.createAccount(money, shares);
se.getAccount(account_id).owner = this;
this.se = se;
this.name = name;
this.id = id;
}
public Exchange getSE() {
return se;
}
public abstract void start();
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import javax.swing.JPanel;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AutoTraderGui extends JPanel{
public void save()
{
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import javax.swing.JDialog;
import org.json.JSONObject;
/**
* Interface for auto traders
*
* @author 7u83 <7u83@mail.ru>
*/
public interface AutoTraderInterface extends ConfigurableInterface {
public abstract boolean getDevelStatus();
public abstract String getDisplayName();
/**
* Get a graphical user interface to configure the auto trader.
*
* @return an AutoTraderGui object or null if there is no graphical user
* interface available.
*/
public abstract AutoTraderGui getGui();
public abstract JDialog getGuiConsole();
/**
* Return the name of the auto trader.
*
* @return name
*/
public abstract String getName();
/**
* Initialize the auto trader
*
* @param se Exechange to trade on
* @param id
* @param name Name of auto trader
* @param money Money
* @param shares Number of shares
* @param cfg
*/
public void init(Exchange se, long id, String name, double money, double shares, JSONObject cfg);
public Account getAccount();
public void start();
}

View File

@ -0,0 +1,145 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import opensesim.gui.Globals;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AutoTraderLoader extends SeSimClassLoader<AutoTraderInterface> {
private ArrayList<Class<AutoTraderInterface>> traders_cache = null;
public AutoTraderLoader(ArrayList<String> pathlist) {
super(AutoTraderInterface.class, pathlist);
}
private ClassLoader cl;
@Override
public AutoTraderInterface newInstance(Class<?> cls) {
// ClassLoader cur = Thread.currentThread().getContextClassLoader();
// Thread.currentThread().setContextClassLoader(cl);
AutoTraderInterface ai;
try {
ai = (AutoTraderInterface) cls.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
ai = null;
}
// Thread.currentThread().setContextClassLoader(cur);
return ai;
}
/**
* Get a list of all traders found in class path
*
* @return List of traders
*/
public ArrayList<Class<AutoTraderInterface>> getInstalledTraders() {
return getInstalledClasses(new ArrayList<String>());
}
public ArrayList<String> getDefaultStrategyNames(boolean devel) {
ArrayList<Class<AutoTraderInterface>> trclasses;
// trclasses = this.getInstalledTraders();
ArrayList<String> ret = new ArrayList<>();
trclasses = getInstalledTraders();
for (int i = 0; i < trclasses.size(); i++) {
try {
//AutoTraderInterface ac = trclasses.get(i).newInstance();
AutoTraderInterface ac = this.newInstance(trclasses.get(i));
if (ac.getDevelStatus() && devel == false) {
continue;
}
ret.add(ac.getClass().getCanonicalName());
} catch (Exception e) {
System.out.printf("Can't load \n");
}
}
return ret;
}
public ArrayList<String> getDefaultStrategyNames() {
return this.getDefaultStrategyNames(true);
}
public AutoTraderInterface getAutoTraderInterface(String name) {
ArrayList<Class<AutoTraderInterface>> traders = this.getInstalledTraders();
for (int i = 0; i < traders.size(); i++) {
try {
if (!name.equals(traders.get(i).getCanonicalName())) {
// System.out.printf("Contnue trader\n");
continue;
}
// System.out.printf("Canon name %s\n", traders.get(i).getCanonicalName());
// System.exit(0);
// Globals.LOGGER.info(String.format("Making lll instance of %s", traders.get(i).getCanonicalName()));
// if (traders.get(i)==null){
// Globals.LOGGER.info("We have null");
// }
// AutoTraderInterface ac = traders.get(i).newInstance();
AutoTraderInterface ac = newInstance(traders.get(i));
return ac;
// System.out.printf("Looking for in %s == %s\n", ac.getClass().getCanonicalName(), name);
// if (ac.getClass().getCanonicalName().equals(name)) {
// return ac;
// if (ac.getDisplayName().equals(name)) {
// return ac;}
// }
} catch (Exception ex) {
Globals.LOGGER.info(String.format("Instance failed %s", ex.getMessage()));
}
}
return null;
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import javax.swing.JScrollBar;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class ChartDef {
/**
* width of an x unit in em
*/
public double x_unit_width=1.0;
public ChartPanel mainChart;
public ChartPanel xLegend;
public ChartDef(){
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import java.awt.Graphics2D;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public interface ChartPainterInterface {
abstract public void drawChart(Graphics2D g, ChartPanel p, ChartDef def);
}

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
<Events>
<EventHandler event="mouseMoved" listener="java.awt.event.MouseMotionListener" parameters="java.awt.event.MouseEvent" handler="formMouseMoved"/>
<EventHandler event="mouseClicked" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="formMouseClicked"/>
<EventHandler event="mouseExited" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="formMouseExited"/>
<EventHandler event="mouseEntered" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="formMouseEntered"/>
</Events>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="498" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="341" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
</Form>

View File

@ -0,0 +1,159 @@
package opensesim.old_sesim;
import opensesim.chart.painter.ChartPainter;
import opensesim.gui.Globals;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.util.ArrayList;
import javax.swing.JScrollBar;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class ChartPanel extends javax.swing.JPanel implements AdjustmentListener {
public JScrollBar x_scrollbar = null;
ChartDef chartDef;
public boolean mouseEntered = false;
/**
* Creates new form Chart1
*/
public ChartPanel() {
initComponents();
//setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
/**
* Add a horizontal scrollbar
*
* @param x_scrollbar
*/
public void setXSCrollBar(JScrollBar x_scrollbar) {
if (this.x_scrollbar != null) {
this.x_scrollbar.removeAdjustmentListener(this);
}
this.x_scrollbar = x_scrollbar;
if (this.x_scrollbar != null) {
this.x_scrollbar.addAdjustmentListener(this);
}
}
public void setChartDef(ChartDef def) {
chartDef = def;
}
private ArrayList<ChartPainter> chartPainters = new ArrayList<>();
/**
*
* @param p
*/
public void addChartPainter(ChartPainter p) {
chartPainters.add(p);
}
public void deleteAllChartPinters() {
chartPainters = new ArrayList<>();
}
public boolean delChartPainter(ChartPainter p) {
return true;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (Globals.se == null) {
return;
}
for (ChartPainter painter : chartPainters) {
painter.drawChart((Graphics2D) g, this, chartDef);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
formMouseMoved(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
formMouseExited(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
formMouseEntered(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 498, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 341, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
public Point mouse = null;
private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
Point p = evt.getPoint();
mouse = p;
repaint();
}//GEN-LAST:event_formMouseMoved
private void formMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseEntered
mouseEntered = true;
repaint();
}//GEN-LAST:event_formMouseEntered
private void formMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseExited
mouseEntered = false;
repaint();
}//GEN-LAST:event_formMouseExited
private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
getParent().dispatchEvent(evt);
}//GEN-LAST:event_formMouseClicked
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
this.repaint();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import org.json.JSONObject;
/**
*
* @author 7u83
*/
public abstract interface ConfigurableInterface {
/**
* Get current configuration as JSON object.
*
* @return JSONObject containing the configuration
*/
public abstract JSONObject getConfig();
/**
* Set the configuration by a JSON object.
*
* @param cfg the configuration
*/
public abstract void putConfig(JSONObject cfg);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,70 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
/**
* Implementation of a simple ID generator to create uniqe IDs of type long
*
* @author 7u83 <7u83@mail.ru>
*/
public class IDGenerator {
private long next_id;
private long start_id;
/**
* Initialize the ID generator
*
* @param start ID value to start with
*/
public IDGenerator(long start) {
start_id=start;
reset();
}
/**
* Initialize ID Generator with start ID = 0
*/
public IDGenerator() {
this(0);
}
/**
* Reset the ID generator
*/
public final void reset(){
next_id = start_id;
}
/**
* Get the next ID
*
* @return the next generated ID
*/
public synchronized long getNext() {
return next_id++;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public interface Indicator extends ConfigurableInterface{
public String getName();
public String getDescription();
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import java.util.ArrayList;
/**
*
* @author 7u83 <7u83@mail.ru>
* @param <T>
*/
public class IndicatorLoader extends SeSimClassLoader<Indicator> {
/**
*
* @param class_type
*/
public IndicatorLoader(ArrayList<String> path_list) {
super(Indicator.class, path_list);
}
public ArrayList<String> getNames() {
ArrayList<Class<Indicator>> indicators;
indicators = this.getInstalledClasses();
for (Class<Indicator> ic : indicators) {
Indicator i = (Indicator) this.newInstance(ic);
String name = i.getName();
System.out.printf("Name of Indicator: %s", name);
}
return new ArrayList<String>();
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2016, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import java.util.concurrent.Semaphore;
/**
* A locker object
*
* @author 7u83 <7u83@mail.ru>
*/
public class Locker {
private final Semaphore AVAIL = new Semaphore(1, true);
/**
* Acquire a lock
*
* @return
*/
public boolean lock() {
try {
AVAIL.acquire();
} catch (InterruptedException e) {
return false;
}
return true;
}
/**
* Release a lock
*/
public void unlock() {
AVAIL.release();
}
}

View File

@ -0,0 +1,23 @@
package opensesim.old_sesim;
public class Logger {
static boolean dbg = true;
static boolean info = true;
static void dbg(String s) {
if (!dbg) {
return;
}
System.out.print("DBG: ");
System.out.println(s);
}
static void info(String s) {
if (!info) {
return;
}
System.out.print("INFO: ");
System.out.println(s);
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class MinMax {
protected float min;
protected float max;
protected float min_log;
protected float max_log;
private boolean log;
MinMax(float min, float max) {
this.min = min;
this.max = max;
this.log = false;
}
public float getDiff() {
return !log ? max - min : max_log - min_log;
}
public float getDiff(boolean plog) {
return !plog ? max - min : max_log - min_log;
}
public float getMin() {
return !log ? min : min_log;
}
public float getMin(boolean plog) {
return !plog ? min : min_log;
}
public float getMax() {
return !log ? max : max_log;
}
public float getMax(boolean plog) {
return !plog ? max : max_log;
}
public void setLog(boolean log){
min_log = (float) Math.log(min);
max_log = (float) Math.log(max);
this.log=log;
}
public void setMin(float min){
this.min=min;
}
public void setMax(float max){
this.max=max;
}
public boolean isLog(){
return log;
}
}

View File

@ -0,0 +1,201 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import java.util.*;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class OHLCData {
private float max = 0;
private float min = 0;
private int frame_size = 60000;
int max_size = 100;
public OHLCData() {
}
/**
* Create an OHLCData object that stores OHLCDataItems
*
* @param frame_size Time frame stored in one OHLCDataItem
*/
public OHLCData(int frame_size) {
this.frame_size = frame_size;
}
public float getMax() {
return max;
}
public float getMin() {
return min;
}
public int size() {
return data.size();
}
/**
*
* @return Time frame of OHLCDataItem
*/
public int getFrameSize() {
return this.frame_size;
}
/**
* Get the minimum and maximum value between two OHLCDataItems
* @param first Position of first OHLCDataItem
* @param last Position of last OHCLDataItem
* @return MinMax object containing the calculated values
*/
public MinMax getMinMax(int first, int last) {
if (data.isEmpty())
return new MinMax(0,0);
if (first >= data.size()) {
OHLCDataItem di = data.get(data.size() - 1);
return new MinMax(di.low, di.high);
}
OHLCDataItem di = data.get(first);
MinMax minmax = new MinMax(di.low, di.high);
for (int i = first + 1; i < last && i < data.size(); i++) {
di = data.get(i);
if (di.low < minmax.min) {
minmax.min = di.low;
}
if (di.high > minmax.max) {
minmax.max = di.high;
}
}
return minmax;
}
/**
* Get minimum an maximum from volume
* @param first
* @param last
* @return MinMax found
*/
public MinMax getVolMinMax(int first, int last) {
if (first >= data.size()) {
OHLCDataItem di = data.get(data.size() - 1);
return new MinMax(di.volume, di.volume);
}
OHLCDataItem di = data.get(first);
MinMax minmax = new MinMax(di.volume, di.volume);
for (int i = first + 1; i < last && i < data.size(); i++) {
di = data.get(i);
if (di.volume < minmax.min) {
minmax.min = di.volume;
}
if (di.volume > minmax.max) {
minmax.max = di.volume;
}
}
return minmax;
}
long getFrameStart(long time) {
long rt = time / frame_size;
return rt * frame_size;
}
public ArrayList<OHLCDataItem> data = new ArrayList<>();
public OHLCDataItem get(int n) {
return data.get(n);
}
public void set(int n, OHLCDataItem item){
data.set(n,item);
}
public void add(OHLCDataItem item){
data.add(item);
}
private void updateMinMax(float price) {
if (price > max) {
max = price;
}
if (price < min) {
min = price;
}
}
public Iterator<OHLCDataItem> iterator() {
return data.iterator();
}
// Start and end of current frame
private long current_frame_end = 0;
private long current_frame_start = 0;
public boolean realTimeAdd(long time, float price, float volume) {
if (time >= current_frame_end) {
if (current_frame_end == 0) {
this.min = price;
this.max = price;
}
long last_frame_start = current_frame_start;
current_frame_start = getFrameStart(time);
current_frame_end = current_frame_start + frame_size;
//System.out.printf("TA %d TE %d\n",this.current_frame_start,this.current_frame_end);
data.add(new OHLCDataItem(this.current_frame_start, price, volume));
this.updateMinMax(price);
return true;
}
this.updateMinMax(price);
OHLCDataItem d = data.get(data.size() - 1);
boolean rc = d.update(price, volume);
return rc;
}
}

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class OHLCDataItem {
public float open;
public float high;
public float low;
public float close;
public float volume;
public long time;
public OHLCDataItem(long time,float price, float volume){
this.time=time;
open=low=high=close=price;
this.volume=volume;
}
public boolean update(float price, float volume) {
boolean ret = false;
if (price > high) {
high = price;
ret = true;
}
if (price < low) {
low = price;
ret = true;
}
this.volume = this.volume + volume;
this.close = price;
return ret;
}
public float getAverage(){
return (open+high+low+close)/4;
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2017, tube
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
/**
*
* @author tube
*/
public interface OHLCDataProvider {
}

View File

@ -0,0 +1,132 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Order {
public enum OrderStatus {
OPEN,
PARTIALLY_EXECUTED,
CLOSED,
CANCELED
}
/**
* Definition of order types
*/
public enum OrderType {
BUYLIMIT, SELLLIMIT, STOPLOSS, STOPBUY, BUY, SELL
}
protected final Stock stock;
protected OrderStatus status;
protected OrderType type;
protected double limit;
protected double volume;
protected final double initial_volume;
protected final long id;
protected final long created;
protected final Account account;
double cost;
Order(long id, long created, Account account, Stock stock, OrderType type,
double volume, double limit) {
this.id = id;
this.account = account;
this.type = type;
this.limit = limit;
this.volume = volume;
this.initial_volume = this.volume;
this.created = created;
this.status = OrderStatus.OPEN;
this.stock=stock;
this.cost = 0;
}
public long getID() {
return id;
}
public double getVolume() {
return volume;
}
public double getLimit() {
return limit;
}
public OrderType getType() {
return type;
}
public double getExecuted() {
return initial_volume - volume;
}
public double getInitialVolume() {
return initial_volume;
}
public double getCost() {
return cost;
}
public double getAvaragePrice() {
double e = getExecuted();
if (e <= 0) {
return -1;
}
return cost / e;
}
public Account getAccount() {
return account;
}
public OrderStatus getOrderStatus() {
return status;
}
public long getCreated() {
return created;
}
/**
* Get the stock symbol that this order belongs to
* @return Stock symbol
*/
public String getStockSymbol(){
return stock.getSymbol();
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright (c) 2016, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Quote implements Comparable {
public double bid;
public double bid_volume;
public double ask;
public double ask_volume;
public double price;
public double volume;
public long time;
Locker lock = new Locker();
public void print() {
System.out.print("Quote ("
+ time
+ ") :"
+ price
+ " / "
+ volume
+ "\n"
);
}
public long id;
@Override
public int compareTo(Object o) {
int ret;
Quote q = (Quote)o;
ret = (int)(this.time-q.time);
if (ret !=0)
return ret;
return (int)(this.id-q.id);
}
/* Quote (){
lock.lock();
id=nextid++;
lock.unlock();
}*/
}

View File

@ -0,0 +1,388 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
//import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import opensesim.gui.Globals;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Scheduler extends Thread {
private double acceleration = 1.0;
public void setAcceleration(double val) {
this.acceleration = val;
synchronized (this) {
this.notify();
}
}
public double getAcceleration() {
return this.acceleration;
}
private final SortedMap<Long, SortedSet<TimerTaskDef>> event_queue = new TreeMap<>();
public interface TimerTaskRunner {
long timerTask();
long getID();
}
private boolean terminate = false;
/**
* Terminate the scheduler
*/
public void terminate() {
terminate = true;
synchronized (event_queue) {
event_queue.notifyAll();
}
}
@Override
public void start() {
if (this.isAlive()) {
return;
}
this.initScheduler();
super.start();
}
private class ObjectComparator implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {
return (((TimerTaskRunner) o1).getID() - ((TimerTaskRunner) o2).getID()) < 0 ? -1 : 1;
//return System.identityHashCode(o1) - System.identityHashCode(o2);
}
}
long last_time_millis = System.currentTimeMillis();
double current_time_millis = 0.0;
long last_nanos = System.nanoTime();
double current_nanos = 0;
/**
*
* @return
*/
private long currentTimeMillis1() {
long cur = System.nanoTime();
long diff = cur - last_nanos;
last_nanos = cur;
if (pause) {
return (long) this.current_time_millis;
}
// this.cur_nano += (((double)diff_nano)/1000000.0)*this.acceleration;
// return (long)(cur_nano/1000000.0);
this.current_nanos += (double) diff * (double) this.acceleration;
// this.current_time_millis += ((double) diff) * this.acceleration;
this.current_time_millis = this.current_nanos / 1000000.0;
return (long) this.current_time_millis;
}
public long currentTimeMillis() {
return (long) this.current_time_millis;
}
static public String formatTimeMillis(long t) {
Date date = new Date(t);
// DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
// String dateFormatted = formatter.format(date);
// return dateFormatted;
long seconds = (t / 1000) % 60;
long minutes = (t / 1000 / 60) % 60;
long hours = (t / 1000) / (60 * 60);
return String.format("%d:%02d:%02d", hours, minutes, seconds);
}
AtomicInteger nextTimerTask = new AtomicInteger(0);
public class TimerTaskDef implements Comparable {
TimerTaskRunner taskRunner;
long curevtime;
long newevtime;
int id;
TimerTaskDef(TimerTaskRunner e, long t) {
taskRunner = e;
newevtime = t;
id = nextTimerTask.getAndAdd(1);
}
@Override
public int compareTo(Object o) {
return ((TimerTaskDef) o).id - this.id;
}
}
//LinkedList<TimerTaskDef> set_tasks = new LinkedList<>();
ConcurrentLinkedQueue<TimerTaskDef> set_tasks = new ConcurrentLinkedQueue<>();
/**
*
* @param e
* @param time
* @return The TimerTask created
*/
public TimerTaskDef startTimerTask(TimerTaskRunner e, long time) {
long evtime = time + currentTimeMillis();
TimerTaskDef task = new TimerTaskDef(e, evtime);
set_tasks.add(task);
synchronized (this) {
notify();
}
return task;
}
public void rescheduleTimerTask(TimerTaskDef task, long time) {
long evtime = time + currentTimeMillis();
task.newevtime = evtime;
set_tasks.add(task);
synchronized (this) {
notify();
}
}
private boolean pause = false;
public void pause() {
setPause(!pause);
}
public void setPause(boolean val) {
pause = val;
synchronized (this) {
this.notify();
}
}
public boolean getPause() {
return pause;
}
public long fireEvent(TimerTaskRunner e) {
return e.timerTask();
}
// HashMap<TimerTaskDef, Long> tasks = new HashMap<>();
private boolean addTimerTask(TimerTaskDef e) {
// System.out.printf("Add TimerTask %d %d\n",e.curevtime,e.newevtime);
// long evtime = time + currentTimeMillis();
SortedSet<TimerTaskDef> s = event_queue.get(e.newevtime);
if (s == null) {
s = new TreeSet<>();
event_queue.put(e.newevtime, s);
}
e.curevtime = e.newevtime;
// tasks.put(e, e.evtime);
return s.add(e);
}
private final LinkedList<TimerTaskRunner> cancel_queue = new LinkedList();
public void cancelTimerTask(TimerTaskRunner e) {
cancel_queue.add(e);
}
private void cancelMy(TimerTaskDef e) {
// Long evtime = tasks.get(e.curevtime);
// if (evtime == null) {
// return;
// }
SortedSet<TimerTaskDef> s = event_queue.get(e.curevtime);
if (s == null) {
// System.out.printf("My not found\n");
return;
}
Boolean rc = s.remove(e);
if (s.isEmpty()) {
event_queue.remove(e.curevtime);
}
}
public long runEvents() {
synchronized (event_queue) {
if (event_queue.isEmpty()) {
return -1;
}
long t = event_queue.firstKey();
long ct = currentTimeMillis1();
// ct = t;
if (t > ct) {
//if ((long) diff > 0) {
// System.out.printf("Leave Event Queue in run events %d\n", Thread.currentThread().getId());
// System.out.printf("Sleeping somewat %d\n", (long) (0.5 + (t - this.currentTimeMillis()) / this.acceleration));
// return (long) diff;
return (long) (((double) t - this.currentTimeMillis()) / this.acceleration);
}
if (t < ct) {
// System.out.printf("Time is overslipping: %d\n",ct-t);
this.current_time_millis = t;
this.current_nanos = this.current_time_millis * 1000000.0;
}
// if (t <= ct) {
SortedSet s = event_queue.get(t);
Object rc = event_queue.remove(t);
if (s.size() > 1) {
//System.out.printf("Events in a row: %d\n", s.size());
}
Iterator<TimerTaskDef> it = s.iterator();
while (it.hasNext()) {
TimerTaskDef e = it.next();
// if (s.size() > 1) {
// System.out.printf("Sicku: %d %d\n", e.id, e.curevtime);
// }
long next_t = this.fireEvent(e.taskRunner);
e.newevtime = next_t + t;
this.addTimerTask(e);
}
return 0;
}
}
class EmptyCtr implements TimerTaskRunner {
@Override
public long timerTask() {
// System.out.printf("Current best brice %f\n", Globals.se.getBestPrice());
return 1000;
}
@Override
public long getID() {
return 999999999999999999L;
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
void initScheduler() {
current_time_millis = 0.0;
this.startTimerTask(new EmptyCtr(), 0);
terminate = false;
}
@Override
public void run() {
while (!terminate) {
while (!set_tasks.isEmpty()) {
TimerTaskDef td = set_tasks.poll();
// System.out.printf("There is a set task %d %d\n",td.curevtime,td.newevtime);
this.cancelMy(td);
this.addTimerTask(td);
}
long wtime = runEvents();
if (wtime == 0) {
continue;
}
synchronized (this) {
try {
// System.out.printf("My WTIME %d\n", wtime);
if (wtime != -1 && !pause) {
wait(wtime);
} else {
wait();
}
} catch (Exception e) {
}
}
}
this.event_queue.clear();
}
}

View File

@ -0,0 +1,293 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.logging.Level;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class SeSimClassLoader<T> {
protected ArrayList<String> default_pathlist;
private ArrayList<Class<T>> cache;
final Class<T> class_type;
/**
* Create a SeSimClassLoader object with an empty default path
*
* @param class_type
*/
public SeSimClassLoader(Class<T> class_type) {
this(class_type, new ArrayList<>());
}
/**
* Create a SeSimClassLoader object with fiven default path
*
* @param class_type
* @param pathlist Default path to search classes for
*/
public SeSimClassLoader(Class<T> class_type, ArrayList<String> pathlist) {
this.class_type = class_type;
setDefaultPathList(pathlist);
}
/**
* Set the path list where to search for traders
*
* @param pathlist List of paths
*/
public final void setDefaultPathList(ArrayList<String> pathlist) {
this.default_pathlist = pathlist;
}
/**
* Get a list of all files in a given directory and its sub-directories.
*
* @param path Directory to list
* @return List of files
*/
public ArrayList<File> listFiles(String path) {
ArrayList<File> files = new ArrayList<>();
File fp = new File(path);
if (!fp.isDirectory()) {
files.add(fp);
return files;
}
File[] fList = new File(path).listFiles();
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
files.addAll(listFiles(file.getAbsolutePath()));
}
}
return files;
}
/**
* Create a new instance of specified class
*
* @param cls Class to create an instance of
* @return the instance, null if not successful
*/
public Object newInstance(Class<?> cls) {
try {
return cls.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
return null;
}
}
/**
* Check if a given class provides a certain interface and also if the class
* is not abstract, so it could be instantiated.
*
* @param cls Class to check
* @param iface Interface which the class should provide
* @return true if it is an instance of insclass, otherwise false
*/
public boolean hasInterface(Class<?> cls, Class<?> iface) {
if (Modifier.isAbstract(cls.getModifiers())) {
return false;
}
do {
for (Class<?> i : cls.getInterfaces()) {
if (i == iface) {
return true;
}
}
} while ((cls = cls.getSuperclass()) != null);
return false;
}
private Class<T> loadClass(String directory, String class_name) {
if (class_name == null) {
return null;
}
URL url;
try {
url = new File(directory).toURI().toURL();
} catch (MalformedURLException ex) {
return null;
}
URL[] urls = new URL[]{url};
ClassLoader cl;
cl = new URLClassLoader(urls);
try {
Class<?> cls;
cls = cl.loadClass(class_name);
if (cls == null) {
return null;
}
if (class_type != null) {
if (!hasInterface(cls, class_type)) {
return null;
}
}
if (newInstance(cls) == null) {
return null;
}
return (Class<T>) cls;
} catch (ClassNotFoundException ex) {
return null;
} catch (NoClassDefFoundError e){
return null;
}
}
/**
*
* @param pathlist
* @param additional_pathlist
* @return
*/
public ArrayList<Class<T>> getInstalledClasses0(ArrayList<String> pathlist) {
ArrayList<Class<T>> result = new ArrayList<>();
for (String path : pathlist) {
ArrayList<File> files = listFiles(path);
for (File file : files) {
String fn = file.toString();
if (fn.toLowerCase().endsWith(".class")) {
String class_name;
class_name = fn.substring(path.length());
// in case we are under Windows, replace \ width /
class_name = class_name.replace("\\", "/");
class_name = class_name.substring(1, class_name.length() - 6).replace('/', '.');
Class<T> c = loadClass(path, class_name);
if (null == c) {
continue;
}
result.add(c);
}
if (fn.toLowerCase().endsWith(".jar")) {
JarInputStream jarstream = null;
try {
File jarfile = new File(fn);
jarstream = new JarInputStream(new FileInputStream(jarfile));
JarEntry jarentry;
while ((jarentry = jarstream.getNextJarEntry()) != null) {
String class_name = jarentry.getName();
if (class_name.endsWith(".class")) {
class_name = class_name.substring(0, class_name.length() - 6).replace('/', '.');
Class<T> c = loadClass(path, class_name);
if (null == c) {
continue;
}
result.add(c);
}
}
} catch (IOException ex) {
} finally {
try {
if (jarstream != null) {
jarstream.close();
}
} catch (IOException ex) {
java.util.logging.Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
return result;
}
/**
* Get a list of all traders found in class path
*
* @param additional_pathlist
* @return List of installed Clases
*/
public ArrayList<Class<T>> getInstalledClasses(ArrayList<String> additional_pathlist ) {
if (cache != null) {
return cache;
}
ArrayList<String> pathlist;
pathlist = new ArrayList<>();
pathlist.addAll(default_pathlist);
pathlist.addAll(additional_pathlist);
cache = getInstalledClasses0(pathlist);
return cache;
}
public ArrayList<Class<T>> getInstalledClasses(){
return getInstalledClasses(new ArrayList<>());
}
}

View File

@ -0,0 +1,165 @@
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.old_sesim;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Stock {
private final String symbol;
private String name;
Stock(String symbol) {
this.symbol = symbol;
reset();
}
/**
*
*/
public final void reset() {
order_books = new HashMap();
// Create an order book for each order type
for (Order.OrderType type : Order.OrderType.values()) {
order_books.put(type, new TreeSet(new Exchange.OrderComparator(type)));
}
quoteHistory = new TreeSet();
ohlc_data = new HashMap();
}
String getSymbol() {
return symbol;
}
String getName() {
return name;
}
protected HashMap<Order.OrderType, SortedSet<Order>> order_books;
// protected ConcurrentLinkedQueue<Order> order_queue = new ConcurrentLinkedQueue();
/**
* History of quotes
*/
public TreeSet<Quote> quoteHistory;
HashMap<Integer, OHLCData> ohlc_data = new HashMap<>();
private OHLCData buildOHLCData(int timeFrame) {
OHLCData data = new OHLCData(timeFrame);
if (quoteHistory == null) {
return data;
}
Iterator<Quote> it = quoteHistory.iterator();
while (it.hasNext()) {
Quote q = it.next();
data.realTimeAdd(q.time, (float) q.price, (float) q.volume);
}
return data;
}
public OHLCData getOHLCdata(Integer timeFrame) {
OHLCData data;
data = ohlc_data.get(timeFrame);
if (data == null) {
synchronized (this) {
data = buildOHLCData(timeFrame);
ohlc_data.put(timeFrame, data);
}
}
return data;
}
protected void updateOHLCData(Quote q) {
Iterator<OHLCData> it = ohlc_data.values().iterator();
while (it.hasNext()) {
OHLCData data = it.next();
data.realTimeAdd(q.time, (float) q.price, (float) q.volume);
}
}
protected void addOrderToBook(Order o) {
order_books.get(o.type).add(o);
switch (o.type) {
case BUY:
case BUYLIMIT:
break;
case SELL:
case SELLLIMIT:
break;
}
}
/**
*
* @param type
* @param depth
* @return
*/
public ArrayList<Order> getOrderBook(Order.OrderType type, int depth) {
SortedSet<Order> book = order_books.get(type);
if (book == null) {
return null;
}
ArrayList<Order> ret;
synchronized (this) {
ret = new ArrayList<>();
Iterator<Order> it = book.iterator();
for (int i = 0; i < depth && it.hasNext(); i++) {
Order o = it.next();
if (o.volume <= 0) {
// throw an exception here
}
ret.add(o);
}
}
return ret;
}
}