switched to ant build

This commit is contained in:
7u83
2017-03-19 21:58:49 +01:00
parent cd2d0c65ff
commit 1006c20a0e
136 changed files with 282 additions and 287 deletions

View File

@ -0,0 +1,93 @@
/*
* 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 sesim;
import org.json.JSONObject;
import 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 Exchange.Account getAccount() {
return se.getAccount(account_id);
}
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,49 @@
/*
* 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 sesim;
import javax.swing.JPanel;
import org.json.JSONObject;
/**
*
* @author 7u83
*/
public abstract interface AutoTraderConfig {
public abstract OldAutoTrader createTrader(Exchange se, JSONObject cfg, long id, String name, double money, double shares);
public abstract String getDisplayName();
public abstract AutoTraderGui getGui();
public abstract JSONObject getConfig();
public abstract void putConfig(JSONObject cfg);
public abstract boolean getDevelStatus();
}

View File

@ -0,0 +1,45 @@
/*
* 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 sesim;
import org.json.JSONObject;
/**
*
* @author tobias
*/
public abstract class AutoTraderConfigBase implements AutoTraderConfig{
@Override
public boolean getDevelStatus() {
return true;
}
}

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 sesim;
import javax.swing.JPanel;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AutoTraderGui extends JPanel{
public void save()
{
}
}

View File

@ -0,0 +1,57 @@
/*
* 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 sesim;
import javax.swing.JDialog;
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public interface AutoTraderInterface {
public abstract boolean getDevelStatus();
public abstract String getDisplayName();
public abstract AutoTraderGui getGui();
public abstract JDialog getGuiConsole();
public abstract JSONObject getConfig();
public abstract void putConfig(JSONObject cfg);
public abstract String getName();
public void init(Exchange se, long id, String name, double money, double shares, JSONObject cfg);
public Exchange.Account getAccount();
public void start();
}

View File

@ -0,0 +1,50 @@
/*
* 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 sesim;
import java.util.SortedSet;
import java.util.List;
import java.util.ArrayList;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AutoTraderList {
SortedSet <OldAutoTrader> traders;
AutoTraderList(){
}
List getTraders(){
return new ArrayList();
}
}

View File

@ -0,0 +1,229 @@
/*
* 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 sesim;
import java.io.File;
import java.io.FileFilter;
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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Consumer;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AutoTraderLoader {
/**
* Check if a given class can instaciated as AutoTrader.
*
* @param cls Class to check
* @return true if it is an AutoTrader, otherwise false
*/
public boolean isAutoTrader(Class<?> cls) {
if (Modifier.isAbstract(cls.getModifiers())) {
return false;
}
do {
for (Class<?> i : cls.getInterfaces()) {
if (i.equals(AutoTraderInterface.class)) {
return true;
}
}
} while ((cls = cls.getSuperclass()) != null);
return false;
}
Class<AutoTraderInterface> loadAutoTraderClass(String filename, String classname) {
String clnam = classname.substring(1, classname.length() - 6).replace('/', '.');
File f = new File(filename);
URL url = null;
try {
url = f.toURL();
} catch (MalformedURLException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
}
URL[] urls = new URL[]{url};
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);
try {
Class<?> cls = cl.loadClass(clnam);
System.out.printf("Check Class: %s\n", cls.getCanonicalName());
if (isAutoTrader(cls)) {
return (Class<AutoTraderInterface>) cls;
}
} catch (ClassNotFoundException ex) {
// something wnet wrong, but we ignore it
}
return null;
}
ArrayList<Class<AutoTraderInterface>> traders_cache = null;
public ArrayList<Class<AutoTraderInterface>> getTraders() {
if (traders_cache != null) {
return traders_cache;
}
int curlen = 0;
ArrayList<Class<AutoTraderInterface>> traders;
traders = new ArrayList<>();
for (String classpathEntry : System.getProperty("java.class.path").split(System.getProperty("path.separator"))) {
Consumer<? super Path> pf = new Consumer() {
@Override
public void accept(Object t) {
String fn = ((Path) t).toString();
if (fn.toLowerCase().endsWith(".class")) {
Class<AutoTraderInterface> cls = loadAutoTraderClass(fn, fn.substring(classpathEntry.length()));
if (cls == null) {
return;
}
traders.add(cls);
}
if (fn.toLowerCase().endsWith(".jar")) {
JarInputStream is = null;
try {
File jar = new File(fn);
is = new JarInputStream(new FileInputStream(jar));
JarEntry entry;
while ((entry = is.getNextJarEntry()) != null) {
if (entry.getName().endsWith(".class")) {
// System.out.printf("Entry: %s\n", entry.getDisplayName());
}
}
} catch (IOException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
is.close();
} catch (IOException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
};
try {
Files.walk(Paths.get(classpathEntry))
.filter(Files::isRegularFile)
.forEach(pf);
} catch (IOException ex) {
Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
}
}
traders_cache = traders;
return traders;
}
public ArrayList<String> getDefaultStrategyNames(boolean devel) {
ArrayList<Class<AutoTraderInterface>> trclasses;
trclasses = this.getTraders();
ArrayList<String> ret = new ArrayList<>();
trclasses = getTraders();
for (int i = 0; i < trclasses.size(); i++) {
try {
AutoTraderInterface ac = trclasses.get(i).newInstance();
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 getStrategyBase(String name) {
ArrayList<Class<AutoTraderInterface>> traders = this.getTraders();
for (int i = 0; i < traders.size(); i++) {
try {
AutoTraderInterface ac = traders.get(i).newInstance();
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) {
}
}
return null;
}
public ArrayList getTraders(String dir) {
File f = new File(dir);
File[] ff = f.listFiles();
ArrayList a = new ArrayList();
a.addAll(Arrays.asList(ff));
return a;
}
}

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 sesim;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AutoTraderRunConfig {
AutoTraderConfig config;
int num_traders;
String trader_name;
double initial_money;
double initial_shares;
}

78
src/sesim/Clock.java Normal file
View File

@ -0,0 +1,78 @@
/*
* 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 sesim;
import java.util.Date;
/**
*
* @author tobias
*
* Implements an adjustable clock
*
*/
public class Clock {
private double acceleration=1.0;
private double current_time_millis=0.0;
private long last_time_millis=System.currentTimeMillis();
private boolean pause=false;
public synchronized long currentTimeMillis1() {
long cur = System.currentTimeMillis();
double diff = cur - last_time_millis;
last_time_millis = cur;
if (pause) {
return (long) this.current_time_millis;
}
// System.out.printf("Floaf TM: %f\n", this.current_time_millis);
this.current_time_millis += diff * acceleration;
return (long) this.current_time_millis;
}
/**
* Set the clock acceleration
* @param acceleration
*/
public void setAcceleration(double acceleration){
this.acceleration=acceleration;
}
public void setPause(boolean p){
pause=p;
}
}

1223
src/sesim/Exchange.java Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,61 @@
/*
* 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 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;
/**
* Initialize the ID generator
*
* @param start ID value to start with
*/
public IDGenerator(long start) {
next_id = start;
}
/**
* Initialize ID Generator with start ID = 0
*/
public IDGenerator() {
this(0);
}
/**
* Get the next ID
*
* @return the next generated ID
*/
public synchronized long getNext() {
return next_id++;
}
}

61
src/sesim/Locker.java Normal file
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 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();
}
}

23
src/sesim/Logger.java Normal file
View File

@ -0,0 +1,23 @@
package 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);
}
}

53
src/sesim/MinMax.java Normal file
View File

@ -0,0 +1,53 @@
/*
* 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 sesim;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class MinMax {
public float min;
public float max;
MinMax(float min, float max) {
this.min = min;
this.max = max;
}
public float getDiff() {
return max - min;
}
public float getMin() {
return min;
}
public float getMax() {
return max;
}
}

20
src/sesim/NewJPanel.form Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
<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"/>
<AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,-104,0,0,1,67"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBoxLayout">
<Property name="axis" type="int" value="3"/>
</Layout>
</Form>

56
src/sesim/NewJPanel.java Normal file
View File

@ -0,0 +1,56 @@
/*
* 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 sesim;
/**
*
* @author tobias
*/
public class NewJPanel extends javax.swing.JPanel {
/**
* Creates new form NewJPanel
*/
public NewJPanel() {
initComponents();
}
/**
* 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() {
setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}

169
src/sesim/OHLCData.java Normal file
View File

@ -0,0 +1,169 @@
/*
* 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 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() {
}
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();
}
public int getFrameSize() {
return this.frame_size;
}
public MinMax getMinMax(int first, int last) {
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;
}
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);
}
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,64 @@
/*
* 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 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;
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2017, 7u83
* 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 sesim;
import org.json.JSONObject;
import sesim.Exchange.Account;
/**
*
* @author 7u83
*/
public abstract class OldAutoTrader implements Scheduler.TimerTaskRunner {
protected double account_id;
protected Exchange se;
protected AutoTraderConfig config;
protected String name;
public OldAutoTrader(Exchange se, long id, String name, double money, double shares, AutoTraderConfig config) {
account_id = se.createAccount(money, shares);
Account a = se.getAccount(account_id);
// a.owner=this;
this.se = se;
this.config = config;
this.name = name;
this.id=id;
}
public OldAutoTrader(){
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 Exchange.Account getAccount(){
return se.getAccount(account_id);
}
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();
}

34
src/sesim/Order.java Normal file
View File

@ -0,0 +1,34 @@
/*
* 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 sesim;
/**
*
* @author tobias
*/
public class Order {
}

39
src/sesim/OrderData.java Normal file
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 sesim;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class OrderData {
public long id;
public double limit;
public double volume;
public double executed;
}

76
src/sesim/Quote.java Normal file
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 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();
}*/
}

420
src/sesim/Scheduler.java Normal file
View File

@ -0,0 +1,420 @@
/*
* 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 sesim;
//import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import 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;
Clock clock;
long last_nanos = System.nanoTime();
double current_nanos = 0;
/**
*
* @return
*/
public 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;
}
/**
*
* @return
*/
public long currentTimeMillis1_old() {
long cur_nano = System.nanoTime();
long diff_nano = cur_nano - last_nanos;
last_nanos = cur_nano;
long cur = System.currentTimeMillis();
long diff = (cur - last_time_millis);
//System.out.printf("Diff Nanos: %d Diff Millis %d, ND: %d\n", diff_nano, diff, diff_nano/1000000);
last_time_millis = cur;
// last_time_millis += diff;
//if (diff == 0) {
// diff++;
// }
if (pause) {
return (long) this.current_time_millis;
}
// this.cur_nano += (((double)diff_nano)/1000000.0)*this.acceleration;
// return (long)(cur_nano/1000000.0);
double fac = (((double) diff) + 10.0) * this.acceleration;
System.out.printf("Difdif: %f %f\n", fac, this.acceleration);
this.current_time_millis += ((double) diff) * this.acceleration;
return (long) this.current_time_millis;
}
public long currentTimeMillis() {
// return (long)(cur_nano/1000000.0);
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);
long seconds = (t / 1000) % 60;
long minutes = (t / 1000 / 60) % 60;
long hours = (t / 1000) / (60 * 60);
return String.format("%02d:%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 XXXrescheduleTimerTask(TimerTaskDef task, long time) {
long evtime = time + currentTimeMillis();
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) {
// 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) {
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();
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();
}
}