code cleaning, speedup of trader loading

This commit is contained in:
7u83 2017-04-16 16:34:51 +02:00
parent dca1851212
commit c3eeb16431
9 changed files with 51 additions and 258 deletions

View File

@ -1,4 +1,4 @@
#Sun, 16 Apr 2017 08:07:50 +0200
#Sun, 16 Apr 2017 16:33:56 +0200
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=

View File

@ -284,7 +284,7 @@ public final class EditStrategies extends javax.swing.JDialog {
Globals.LOGGER.info(String.format("Base %s\n", base));
ac = Globals.tloader.getStrategyBase(base);
ac = Globals.tloader.getAutoTraderInterface(base);
if (ac == null) {
System.out.print("BASE IST NULL\n");
Globals.LOGGER.info(String.format("Can't load: %s\n", base));
@ -401,7 +401,7 @@ System.out.printf("The big name: %s\n", ac.getClass().getCanonicalName());
if (sd.result==null)
return;
AutoTraderInterface ac = Globals.tloader.getStrategyBase(sd.result.base);
AutoTraderInterface ac = Globals.tloader.getAutoTraderInterface(sd.result.base);
JSONObject cfg = ac.getConfig();
System.out.printf("Initial cfg %s\n", cfg.toString(2));
cfg.put("base", ac.getClass().getCanonicalName());

View File

@ -49,12 +49,11 @@ import sesim.AutoTraderLoader;
* @author 7u83 <7u83@mail.ru>
*/
public class Globals {
public static final String SESIM_FILEEXTENSION = "sesim";
public static final Double SESIM_FILEVERSION = 0.1;
public static final String SESIM_APPTITLE = "SeSim - Stock Exchange Simulator";
public interface CfgListener {
@ -75,9 +74,8 @@ public class Globals {
public static JFrame frame;
// static final String STRATEGYPREFS = "Strategies";
// static final String TRADERPREFS = "Traders";
// static final String STRATEGYPREFS = "Strategies";
// static final String TRADERPREFS = "Traders";
static final String DEVELSTATUS = "devel_status";
public static final String GODMODE = "godmode";
@ -86,19 +84,17 @@ public class Globals {
/**
* Defines keys for preferences
*/
public static final class PrefKeys{
public static final class PrefKeys {
public static String WORKDIR = "workdir";
public static final String CURRENTFILE = "currentfile";
public static final String SESIMVERSION = "version";
public static final String STRATEGIES = "strategies";
public static final String TRADERS = "traders";
}
static public Preferences prefs;
public static class CfgStrings {
@ -140,20 +136,14 @@ public class Globals {
static AutoTraderLoader tloader;
static void initGlobals() {
String[] a = System.getProperty("java.class.path").split(System.getProperty("path.separator"));
ArrayList pathlist = new ArrayList<>(Arrays.asList(a));
System.out.printf("Init tloader\n");
pathlist = new ArrayList<>();
String dp = new java.io.File(SeSimApplication.class.getProtectionDomain()
ArrayList pathlist = new ArrayList<>();
String default_path = new java.io.File(SeSimApplication.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath()).toString();
pathlist.add(dp);
LOGGER.info(String.format("Path %s", dp));
pathlist.add(default_path);
tloader = new AutoTraderLoader(pathlist);
}
static public final Logger LOGGER = Logger.getLogger("com.cauwersin.sesim");
@ -169,16 +159,15 @@ public class Globals {
JSONObject cfgs = new JSONObject(cfglist);
return cfgs;
}
static public final void putStrategies(JSONObject strategies){
static public final void putStrategies(JSONObject strategies) {
Globals.prefs.put(Globals.PrefKeys.STRATEGIES, strategies.toString());
}
static public final void putTraders (JSONArray traders){
static public final void putTraders(JSONArray traders) {
Globals.prefs.put(Globals.PrefKeys.TRADERS, traders.toString());
}
static public JSONObject getStrategy(String name) {
return getStrategies().getJSONObject(name);
}
@ -213,9 +202,6 @@ public class Globals {
prefs.put(PrefKeys.STRATEGIES, cfgs.toString());
}
public static void saveFile(File f) throws FileNotFoundException {
JSONObject sobj = new JSONObject();
@ -233,37 +219,36 @@ public class Globals {
out.close();
}
public static void loadString(String s) throws IOException{
public static void loadString(String s) throws IOException {
JSONObject sobj = new JSONObject(s);
Double version = sobj.getDouble(PrefKeys.SESIMVERSION);
if (version > SESIM_FILEVERSION){
if (version > SESIM_FILEVERSION) {
throw new IOException("File has wrong version.");
}
JSONArray traders = sobj.getJSONArray(PrefKeys.TRADERS);
JSONObject strategies = sobj.getJSONObject(PrefKeys.STRATEGIES);
putStrategies(strategies);
putTraders(traders);
}
public static void clearAll(){
public static void clearAll() {
putStrategies(new JSONObject());
putTraders(new JSONArray());
}
public static void loadFile(File f) throws IOException {
f.getAbsoluteFile();
String s;
s = new String(Files.readAllBytes(f.toPath()));
loadString(s);
}
}

View File

@ -93,10 +93,10 @@ public class SeSimApplication extends javax.swing.JFrame {
//this.setLocationRelativeTo(null);
}
AutoTraderInterface createTraderNew(Exchange se, long id, String name, double money, double shares, JSONObject cfg) {
AutoTraderInterface createTrader(Exchange se, long id, String name, double money, double shares, JSONObject cfg) {
String base = cfg.getString("base");
AutoTraderInterface ac = Globals.tloader.getStrategyBase(base);
AutoTraderInterface ac = Globals.tloader.getAutoTraderInterface(base);
if (ac == null) {
return null;
}
@ -122,7 +122,7 @@ public class SeSimApplication extends javax.swing.JFrame {
String strategy_name = t.getString("Strategy");
JSONObject strategy = Globals.getStrategy(strategy_name);
String base = strategy.getString("base");
AutoTraderInterface ac = Globals.tloader.getStrategyBase(base);
AutoTraderInterface ac = Globals.tloader.getAutoTraderInterface(base);
System.out.printf("Load Strat: %s\n", strategy_name);
System.out.printf("Base %s\n", base);
@ -140,7 +140,7 @@ public class SeSimApplication extends javax.swing.JFrame {
for (int i1 = 0; i1 < count; i1++) {
AutoTraderInterface trader;
trader = this.createTraderNew(Globals.se, id, t.getString("Name") + i1, money, shares, strategy);
trader = this.createTrader(Globals.se, id, t.getString("Name") + i1, money, shares, strategy);
Globals.se.traders.add(trader);

View File

@ -1,60 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="2"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<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">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="statisticsPanel1" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="statisticsPanel1" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="gui.StatisticsPanel" name="statisticsPanel1">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="388" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="288" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
</Container>
</SubComponents>
</Form>

View File

@ -1,131 +0,0 @@
/*
* 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 gui;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class StatisticsDialog extends javax.swing.JDialog {
/**
* Creates new form StatisticsDialog
*/
public StatisticsDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
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() {
statisticsPanel1 = new gui.StatisticsPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
javax.swing.GroupLayout statisticsPanel1Layout = new javax.swing.GroupLayout(statisticsPanel1);
statisticsPanel1.setLayout(statisticsPanel1Layout);
statisticsPanel1Layout.setHorizontalGroup(
statisticsPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 388, Short.MAX_VALUE)
);
statisticsPanel1Layout.setVerticalGroup(
statisticsPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 288, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(statisticsPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(statisticsPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(StatisticsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(StatisticsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(StatisticsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(StatisticsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
StatisticsDialog dialog = new StatisticsDialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private gui.StatisticsPanel statisticsPanel1;
// End of variables declaration//GEN-END:variables
}

View File

@ -307,37 +307,36 @@ public class AutoTraderLoader {
return this.getDefaultStrategyNames(true);
}
public AutoTraderInterface getStrategyBase(String name) {
public AutoTraderInterface getAutoTraderInterface(String name) {
ArrayList<Class<AutoTraderInterface>> traders = this.getInstalledTraders();
System.out.printf("Installed traders %d\n", traders.size());
// System.exit(0);
for (int i = 0; i < traders.size(); i++) {
try {
traders.get(i).getCanonicalName();
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()));
// 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");
}
// if (traders.get(i)==null){
// Globals.LOGGER.info("We have null");
// }
// AutoTraderInterface ac = traders.get(i).newInstance();
AutoTraderInterface ac = this.MakeInstance(traders.get(i));
AutoTraderInterface ac = MakeInstance(traders.get(i));
System.out.printf("Looking for in %s == %s\n", ac.getClass().getCanonicalName(), name);
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.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()));
}

View File

@ -1155,7 +1155,7 @@ public class Exchange {
buy_failed++;
break;
}
System.out.printf("Order ffailed %f %f \n",o.volume,o.limit);
// System.out.printf("Order ffailed %f %f \n",o.volume,o.limit);
return -1;

View File

@ -294,10 +294,10 @@ public class Scheduler extends Thread {
// if (evtime == null) {
// return;
// }
System.out.printf("Cancel my %d\n", e.id);
SortedSet<TimerTaskDef> s = event_queue.get(e.curevtime);
if (s == null) {
System.out.printf("My not found\n");
// System.out.printf("My not found\n");
return;
}
@ -391,7 +391,7 @@ public class Scheduler extends Thread {
while (!set_tasks.isEmpty()) {
TimerTaskDef td = set_tasks.poll();
System.out.printf("There is a set task %d %d\n",td.curevtime,td.newevtime);
// System.out.printf("There is a set task %d %d\n",td.curevtime,td.newevtime);
this.cancelMy(td);
this.addTimerTask(td);