file loading works now
This commit is contained in:
parent
3790b172ef
commit
d461decaa8
@ -1,4 +1,4 @@
|
|||||||
#Sat, 08 Apr 2017 16:28:12 +0200
|
#Sat, 08 Apr 2017 22:54:05 +0200
|
||||||
annotation.processing.enabled=true
|
annotation.processing.enabled=true
|
||||||
annotation.processing.enabled.in.editor=false
|
annotation.processing.enabled.in.editor=false
|
||||||
annotation.processing.processors.list=
|
annotation.processing.processors.list=
|
||||||
|
@ -79,7 +79,7 @@ public class EditAutoTraderList extends javax.swing.JPanel {
|
|||||||
ja.put(jo);
|
ja.put(jo);
|
||||||
}
|
}
|
||||||
|
|
||||||
Globals.prefs.put("Traders", ja.toString());
|
Globals.prefs.put(Globals.PrefKeys.TRADERS, ja.toString());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ public final class EditStrategies extends javax.swing.JDialog {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
final String STRATEGYPREFS = "Strategies";
|
//final String STRATEGYPREFS = "Strategies";
|
||||||
|
|
||||||
void reloadStrategyConfigs() {
|
void reloadStrategyConfigs() {
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ public final class EditStrategies extends javax.swing.JDialog {
|
|||||||
|
|
||||||
private void jComboBoxStrategySelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxStrategySelectorActionPerformed
|
private void jComboBoxStrategySelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxStrategySelectorActionPerformed
|
||||||
|
|
||||||
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
|
String cfglist = Globals.prefs.get(Globals.PrefKeys.STRATEGIES, "{}");
|
||||||
JSONObject cfgs = new JSONObject(cfglist);
|
JSONObject cfgs = new JSONObject(cfglist);
|
||||||
String item = (String) this.jComboBoxStrategySelector.getSelectedItem();
|
String item = (String) this.jComboBoxStrategySelector.getSelectedItem();
|
||||||
|
|
||||||
@ -354,12 +354,12 @@ System.out.printf("The big name: %s\n", ac.getClass().getCanonicalName());
|
|||||||
item = item + " - " + d.getName();
|
item = item + " - " + d.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
|
String cfglist = Globals.prefs.get(Globals.PrefKeys.STRATEGIES, "{}");
|
||||||
JSONObject cfgs = new JSONObject(cfglist);
|
JSONObject cfgs = new JSONObject(cfglist);
|
||||||
|
|
||||||
cfgs.put(item, o);
|
cfgs.put(item, o);
|
||||||
|
|
||||||
Globals.prefs.put(STRATEGYPREFS, cfgs.toString());
|
Globals.prefs.put(Globals.PrefKeys.STRATEGIES, cfgs.toString());
|
||||||
|
|
||||||
//this.reloadStrategyConfigs();
|
//this.reloadStrategyConfigs();
|
||||||
this.initComboBox();
|
this.initComboBox();
|
||||||
@ -381,14 +381,14 @@ System.out.printf("The big name: %s\n", ac.getClass().getCanonicalName());
|
|||||||
String selected = (String) this.jComboBoxStrategySelector.getSelectedItem();
|
String selected = (String) this.jComboBoxStrategySelector.getSelectedItem();
|
||||||
System.out.printf("Selected: %s\n", selected);
|
System.out.printf("Selected: %s\n", selected);
|
||||||
|
|
||||||
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
|
String cfglist = Globals.prefs.get(Globals.PrefKeys.STRATEGIES, "{}");
|
||||||
JSONObject cfgs = new JSONObject(cfglist);
|
JSONObject cfgs = new JSONObject(cfglist);
|
||||||
|
|
||||||
//System.out.printf("CFG vorher: %s\n", cfgs.toString(3));
|
//System.out.printf("CFG vorher: %s\n", cfgs.toString(3));
|
||||||
cfgs.remove(selected);
|
cfgs.remove(selected);
|
||||||
|
|
||||||
// System.out.printf("Neue nachher: %s\n", cfgs.toString(3));
|
// System.out.printf("Neue nachher: %s\n", cfgs.toString(3));
|
||||||
Globals.prefs.put(STRATEGYPREFS, cfgs.toString());
|
Globals.prefs.put(Globals.PrefKeys.STRATEGIES, cfgs.toString());
|
||||||
|
|
||||||
this.initComboBox();
|
this.initComboBox();
|
||||||
|
|
||||||
|
@ -51,6 +51,8 @@ import sesim.AutoTraderLoader;
|
|||||||
public class Globals {
|
public class Globals {
|
||||||
|
|
||||||
public static final String SESIM_FILEEXTENSION = "sesim";
|
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 static final String SESIM_APPTITLE = "SeSim - Stock Exchange Simulator";
|
||||||
|
|
||||||
|
|
||||||
@ -73,8 +75,8 @@ public class Globals {
|
|||||||
|
|
||||||
public static JFrame frame;
|
public static JFrame frame;
|
||||||
|
|
||||||
static final String STRATEGYPREFS = "Strategies";
|
// static final String STRATEGYPREFS = "Strategies";
|
||||||
static final String TRADERPREFS = "Traders";
|
// static final String TRADERPREFS = "Traders";
|
||||||
|
|
||||||
static final String DEVELSTATUS = "devel_status";
|
static final String DEVELSTATUS = "devel_status";
|
||||||
public static final String GODMODE = "godmode";
|
public static final String GODMODE = "godmode";
|
||||||
@ -85,8 +87,16 @@ public class Globals {
|
|||||||
* Defines keys for preferences
|
* Defines keys for preferences
|
||||||
*/
|
*/
|
||||||
public static final class PrefKeys{
|
public static final class PrefKeys{
|
||||||
|
|
||||||
public static String WORKDIR = "workdir";
|
public static String WORKDIR = "workdir";
|
||||||
public static final String CURRENTFILE = "currentfile";
|
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;
|
static public Preferences prefs;
|
||||||
@ -130,7 +140,7 @@ public class Globals {
|
|||||||
static AutoTraderLoader tloader;
|
static AutoTraderLoader tloader;
|
||||||
|
|
||||||
static void initGlobals() {
|
static void initGlobals() {
|
||||||
String[] a = System.getProperty("java.class.path").split(System.getProperty("path.separator"));
|
String[] a = System.getProperty("java.class.path").split(System.getProperty("path.separator"));
|
||||||
ArrayList pathlist = new ArrayList<>(Arrays.asList(a));
|
ArrayList pathlist = new ArrayList<>(Arrays.asList(a));
|
||||||
System.out.printf("Init tloader\n");
|
System.out.printf("Init tloader\n");
|
||||||
|
|
||||||
@ -149,17 +159,26 @@ public class Globals {
|
|||||||
static public final Logger LOGGER = Logger.getLogger("com.cauwersin.sesim");
|
static public final Logger LOGGER = Logger.getLogger("com.cauwersin.sesim");
|
||||||
|
|
||||||
static public final JSONArray getTraders() {
|
static public final JSONArray getTraders() {
|
||||||
String traders_json = Globals.prefs.get(TRADERPREFS, "[]");
|
String traders_json = Globals.prefs.get(PrefKeys.TRADERS, "[]");
|
||||||
JSONArray traders = new JSONArray(traders_json);
|
JSONArray traders = new JSONArray(traders_json);
|
||||||
return traders;
|
return traders;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public final JSONObject getStrategies() {
|
static public final JSONObject getStrategies() {
|
||||||
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
|
String cfglist = Globals.prefs.get(PrefKeys.STRATEGIES, "{}");
|
||||||
JSONObject cfgs = new JSONObject(cfglist);
|
JSONObject cfgs = new JSONObject(cfglist);
|
||||||
return cfgs;
|
return cfgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static public final void putStrategies(JSONObject strategies){
|
||||||
|
Globals.prefs.put(Globals.PrefKeys.STRATEGIES, strategies.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
static public final void putTraders (JSONArray traders){
|
||||||
|
Globals.prefs.put(Globals.PrefKeys.TRADERS, traders.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static public JSONObject getStrategy(String name) {
|
static public JSONObject getStrategy(String name) {
|
||||||
return getStrategies().getJSONObject(name);
|
return getStrategies().getJSONObject(name);
|
||||||
}
|
}
|
||||||
@ -191,14 +210,11 @@ public class Globals {
|
|||||||
static public final void saveStrategy(String name, JSONObject cfg) {
|
static public final void saveStrategy(String name, JSONObject cfg) {
|
||||||
JSONObject cfgs = getStrategies();
|
JSONObject cfgs = getStrategies();
|
||||||
cfgs.put(name, cfg);
|
cfgs.put(name, cfg);
|
||||||
prefs.put(STRATEGYPREFS, cfgs.toString());
|
prefs.put(PrefKeys.STRATEGIES, cfgs.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class FileStrings {
|
public static class FileKeys {
|
||||||
|
|
||||||
public static final String SESIMVERSION = "sesim_version";
|
|
||||||
public static final String STRATEGIES = "strategies";
|
|
||||||
public static final String TRADERS = "traders";
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,9 +226,9 @@ public class Globals {
|
|||||||
JSONArray traders = getTraders();
|
JSONArray traders = getTraders();
|
||||||
JSONObject strategies = getStrategies();
|
JSONObject strategies = getStrategies();
|
||||||
|
|
||||||
sobj.put(FileStrings.SESIMVERSION, "0.1");
|
sobj.put(PrefKeys.SESIMVERSION, SESIM_FILEVERSION);
|
||||||
sobj.put(FileStrings.STRATEGIES, strategies);
|
sobj.put(PrefKeys.STRATEGIES, strategies);
|
||||||
sobj.put(FileStrings.TRADERS, traders);
|
sobj.put(PrefKeys.TRADERS, traders);
|
||||||
|
|
||||||
PrintWriter out;
|
PrintWriter out;
|
||||||
out = new PrintWriter(f.getAbsolutePath());
|
out = new PrintWriter(f.getAbsolutePath());
|
||||||
@ -222,9 +238,24 @@ public class Globals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void loadFile(File f) throws IOException {
|
public static void loadFile(File f) throws IOException {
|
||||||
String s = new String(Files.readAllBytes(f.toPath()));
|
|
||||||
|
f.getAbsoluteFile();
|
||||||
|
|
||||||
|
String s;
|
||||||
|
s = new String(Files.readAllBytes(f.toPath()));
|
||||||
JSONObject sobj = new JSONObject(s);
|
JSONObject sobj = new JSONObject(s);
|
||||||
|
|
||||||
|
Double version = sobj.getDouble(PrefKeys.SESIMVERSION);
|
||||||
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,10 +33,12 @@ import java.awt.event.WindowAdapter;
|
|||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.awt.event.WindowListener;
|
import java.awt.event.WindowListener;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
import java.util.logging.Handler;
|
import java.util.logging.Handler;
|
||||||
|
import java.util.logging.Level;
|
||||||
import java.util.logging.LogRecord;
|
import java.util.logging.LogRecord;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.prefs.Preferences;
|
import java.util.prefs.Preferences;
|
||||||
@ -614,10 +616,26 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
|
|
||||||
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
|
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
|
||||||
JFileChooser fc = getFileChooser();
|
JFileChooser fc = getFileChooser();
|
||||||
if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
File f = fc.getSelectedFile();
|
||||||
|
Globals.loadFile(f);
|
||||||
|
String workdir = fc.getCurrentDirectory().getAbsolutePath();
|
||||||
|
Globals.prefs.put(Globals.PrefKeys.WORKDIR, workdir);
|
||||||
|
Globals.prefs.put(Globals.PrefKeys.CURRENTFILE, f.getAbsolutePath());
|
||||||
|
setTitle(f.getName());
|
||||||
|
return;
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Can't load file: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}//GEN-LAST:event_openMenuItemActionPerformed
|
}//GEN-LAST:event_openMenuItemActionPerformed
|
||||||
|
|
||||||
// initialize a JFileChose with worging directory and extension
|
// initialize a JFileChose with worging directory and extension
|
||||||
@ -675,7 +693,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
|
|
||||||
if (f.exists() && saveAs) {
|
if (f.exists() && saveAs) {
|
||||||
String s = String.format("File %s already exists. Do you want to overwrite?", fn);
|
String s = String.format("File %s already exists. Do you want to overwrite?", fn);
|
||||||
int dialogResult = JOptionPane.showConfirmDialog(null, s, "Warning", JOptionPane.YES_NO_OPTION);
|
int dialogResult = JOptionPane.showConfirmDialog(this, s, "Warning", JOptionPane.YES_NO_OPTION);
|
||||||
if (dialogResult != JOptionPane.YES_OPTION) {
|
if (dialogResult != JOptionPane.YES_OPTION) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -698,7 +716,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
|
||||||
JOptionPane.showMessageDialog(this, "Can't save file " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
JOptionPane.showMessageDialog(this, "Can't save file: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
101
src/resources/files/defaultcfg.json
Normal file
101
src/resources/files/defaultcfg.json
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
{
|
||||||
|
"traders": [
|
||||||
|
{
|
||||||
|
"Money": "100.0",
|
||||||
|
"Strategy": "Tube",
|
||||||
|
"Enabled": "true",
|
||||||
|
"Count": "10000",
|
||||||
|
"Shares": "100.0",
|
||||||
|
"Name": "Mond"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Money": "1000.0",
|
||||||
|
"Strategy": "NasenTrading",
|
||||||
|
"Enabled": "true",
|
||||||
|
"Count": "100",
|
||||||
|
"Shares": "10.0",
|
||||||
|
"Name": "Nase"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"strategies": {
|
||||||
|
"Tube": {
|
||||||
|
"sell_wait": [
|
||||||
|
10000,
|
||||||
|
50000
|
||||||
|
],
|
||||||
|
"initla_delay": [
|
||||||
|
0,
|
||||||
|
5
|
||||||
|
],
|
||||||
|
"sell_limit": [
|
||||||
|
-2,
|
||||||
|
2
|
||||||
|
],
|
||||||
|
"sell_wait_after": [
|
||||||
|
1000,
|
||||||
|
30000
|
||||||
|
],
|
||||||
|
"buy_wait": [
|
||||||
|
10000,
|
||||||
|
50000
|
||||||
|
],
|
||||||
|
"sell_volume": [
|
||||||
|
100,
|
||||||
|
100
|
||||||
|
],
|
||||||
|
"buy_wait_after": [
|
||||||
|
10,
|
||||||
|
30
|
||||||
|
],
|
||||||
|
"buy_volume": [
|
||||||
|
100,
|
||||||
|
100
|
||||||
|
],
|
||||||
|
"buy_limit": [
|
||||||
|
-2,
|
||||||
|
2
|
||||||
|
],
|
||||||
|
"base": "traders.RandomTraderA"
|
||||||
|
},
|
||||||
|
"NasenTrading": {
|
||||||
|
"sell_wait": [
|
||||||
|
10000,
|
||||||
|
50000
|
||||||
|
],
|
||||||
|
"initla_delay": [
|
||||||
|
0,
|
||||||
|
111
|
||||||
|
],
|
||||||
|
"sell_limit": [
|
||||||
|
-2,
|
||||||
|
9
|
||||||
|
],
|
||||||
|
"sell_wait_after": [
|
||||||
|
1000,
|
||||||
|
30000
|
||||||
|
],
|
||||||
|
"buy_wait": [
|
||||||
|
10000,
|
||||||
|
50000
|
||||||
|
],
|
||||||
|
"sell_volume": [
|
||||||
|
100,
|
||||||
|
100
|
||||||
|
],
|
||||||
|
"buy_wait_after": [
|
||||||
|
10,
|
||||||
|
30
|
||||||
|
],
|
||||||
|
"buy_volume": [
|
||||||
|
100,
|
||||||
|
100
|
||||||
|
],
|
||||||
|
"buy_limit": [
|
||||||
|
-2,
|
||||||
|
9
|
||||||
|
],
|
||||||
|
"base": "traders.RandomTraderA"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"version": 0.1
|
||||||
|
}
|
@ -158,7 +158,7 @@ public class RandomTraderB extends AutoTraderBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean getDevelStatus() {
|
public boolean getDevelStatus() {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long cancelOrders() {
|
public long cancelOrders() {
|
||||||
|
Loading…
Reference in New Issue
Block a user