Skip to content

Commit

Permalink
massive changes, probably broke some things; converted to Guice, remo…
Browse files Browse the repository at this point in the history
…ving ModuleLoader (#21); renamed old Commands to RunMode; added ConsoleRunMode; added Commands and Antlr; renamed Esper to Context; moved modules out of packages; started Strategy implementation
  • Loading branch information
tim committed Jun 16, 2014
1 parent 1aea992 commit 76d027a
Show file tree
Hide file tree
Showing 96 changed files with 2,198 additions and 891 deletions.
12 changes: 12 additions & 0 deletions cointrader-default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ db.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
module.path=


# add your custom command packages here, using a colon to separate packages. "org.cryptocoinpartners.command" will
# be automatically appended to the end of the list. Subpackages will also be searched and do not require explicit
#inclusion
# example: module.path=com.mycompany.command:com.othercompany.more.command
command.path=


# XChange Configuration

# to set up your accounts, use xchange.*.username= and xchange.*.password=, like this:
Expand Down Expand Up @@ -50,3 +57,8 @@ xchange.cryptsy.rate.period=2
# the Xchange adapter for Cryptsy requires authentication even for getting quotes, so Cryptsy is off by default
#xchange.cryptsy.listings=LTC.BTC,XPM.LTC,MOON.LTC,DRK.BTC
#xchange.cryptsy.listings=ADT.XPM,ASC.XPM,COL.XPM,DVC.XPM,IFC.XPM,NET.XPM,TIX.XPM,ADT.LTC,ANC.LTC,ASC.LTC,AUR.LTC,CGB.LTC,CNC.LTC,COL.LTC,CPR.LTC,DBL.LTC,DGC.LTC,DOGE.LTC,DVC.LTC,ELP.LTC,EZC.LTC,FLO.LTC,FST.LTC,GLD.LTC,GME.LTC,IFC.LTC,JKC.LTC,MEC.LTC,MEM.LTC,MOON.LTC,MST.LTC,NET.LTC,NXT.LTC,PPC.LTC,PXC.LTC,QRK.LTC,RED.LTC,RYC.LTC,SBC.LTC,SXC.LTC,TIPS.LTC,TIX.LTC,WDC.LTC,XNC.LTC,XPM.LTC,YAC.LTC,ZET.LTC,42.BTC,ALF.BTC,AMC.BTC,ANC.BTC,ARG.BTC,AUR.BTC,BCX.BTC,BEN.BTC,BET.BTC,BQC.BTC,BTB.BTC,BTE.BTC,BTG.BTC,BUK.BTC,CACH.BTC,CAP.BTC,CASH.BTC,CAT.BTC,CGB.BTC,CLR.BTC,CMC.BTC,CNC.BTC,CRC.BTC,CSC.BTC,DEM.BTC,DGC.BTC,DMD.BTC,DOGE.BTC,DRK.BTC,DVC.BTC,EAC.BTC,ELC.BTC,EMD.BTC,EZC.BTC,FFC.BTC,FLAP.BTC,FRC.BTC,FRK.BTC,FST.BTC,FTC.BTC,GDC.BTC,GLC.BTC,GLD.BTC,GLX.BTC,HBN.BTC,IFC.BTC,IXC.BTC,JKC.BTC,KGC.BTC,LEAF.BTC,LK7.BTC,LKY.BTC,LOT.BTC,LTC.BTC,MAX.BTC,MEC.BTC,MEOW.BTC,MINT.BTC,MNC.BTC,MOON.BTC,MZC.BTC,NAN.BTC,NBL.BTC,NEC.BTC,NET.BTC,NMC.BTC,NRB.BTC,NVC.BTC,NXT.BTC,ORB.BTC,OSC.BTC,PHS.BTC,Points.BTC,PPC.BTC,PTS.BTC,PXC.BTC,PYC.BTC,QRK.BTC,RPC.BTC,SBC.BTC,SMC.BTC,SPT.BTC,SRC.BTC,STR.BTC,SXC.BTC,TAG.BTC,TAK.BTC,TEK.BTC,TGC.BTC,TRC.BTC,UNO.BTC,UTC.BTC,VTC.BTC,WDC.BTC,XJO.BTC,XPM.BTC,YAC.BTC,YBC.BTC,ZCC.BTC,ZET.BTC


# Other

console.cursor=ct>
55 changes: 52 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.cryptocoinpartners</groupId>
<artifactId>cointrader</artifactId>
<version>0.2.0-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>

<developers>
<developer>
Expand All @@ -16,12 +16,20 @@
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<xchange.version>2.1.0-SNAPSHOT</xchange.version>
<slf4j-api.version>1.7.5</slf4j-api.version>
<hibernate.version>4.3.4.Final</hibernate.version>
<antlr.version>4.2.2</antlr.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -52,6 +60,11 @@
<artifactId>jcommander</artifactId>
<version>1.30</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.0-beta4</version>
</dependency>
<dependency>
<groupId>com.xeiam.xchange</groupId>
<artifactId>xchange-core</artifactId>
Expand Down Expand Up @@ -110,7 +123,7 @@
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.8</version>
<version>0.9.9-RC1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand All @@ -132,6 +145,16 @@
<artifactId>stringtotime</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${antlr.version}</version>
</dependency>
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>0.9.94</version>
</dependency>
</dependencies>


Expand Down Expand Up @@ -178,12 +201,38 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!--
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/test</contextPath>
</webApp>
</configuration>
</plugin>
-->
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<executions>
<execution>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
28 changes: 28 additions & 0 deletions src/main/antlr4/imports/Base.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
grammar Base;

WS: // ignore whitespace
[ \r\t\n]+
-> skip
;

Amount:
[0-9]+
|
[0-9]* '.' [0-9]+
;

Currency:
[a-zA-Z][a-zA-Z]([a-zA-Z]([a-zA-Z])?)? // 2-4 alphas.. todo isn't there a better way to do this [a-zA-Z]{2-4} doesn't work
;

Exchange:
[a-zA-Z_]+
;

Listing:
Currency '.' Currency
;

Market:
Exchange ':' Listing
;
14 changes: 14 additions & 0 deletions src/main/antlr4/org/cryptocoinpartners/command/Order.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
grammar Order;

import Base;


args : volume market ('stop' stopPrice)? ('limit' limitPrice)? ;

market : Market;

volume : Amount;

stopPrice : Amount;

limitPrice : Amount;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cryptocoinpartners.bin.command;
package org.cryptocoinpartners.bin;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
Expand All @@ -11,7 +11,7 @@

@SuppressWarnings("UnusedDeclaration")
@Parameters(commandNames = "report-jpa",separators = "",commandDescription = "interprets the command-line args as a JPA query")
public class AdHocJpaReportCommand extends JpaReportCommand
public class AdHocJpaReportRunMode extends JpaReportRunMode
{
protected Query getQuery()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.cryptocoinpartners.bin.command;
package org.cryptocoinpartners.bin;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.cryptocoinpartners.module.Esper;
import org.cryptocoinpartners.module.Context;
import org.cryptocoinpartners.util.ModuleLoaderError;


Expand All @@ -11,21 +11,15 @@
*/
@SuppressWarnings("UnusedDeclaration")
@Parameters(commandNames = "backtest", commandDescription = "backtest a strategy (not functional)")
public class BacktestCommand extends Command {
public class BacktestRunMode extends RunMode {

@Parameter(description = "Strategy name to load", arity = 1, required = true)
public String strategyName;

public void run() {
Esper esper = new Esper();
Context context = new Context();
// todo set time manager
// todo load data producing module
try {
esper.loadModule(strategyName);
}
catch( ModuleLoaderError e ) {
throw new Error("Could not load strategy "+strategyName,e);
}
}

}
110 changes: 110 additions & 0 deletions src/main/java/org/cryptocoinpartners/bin/ConsoleRunMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.cryptocoinpartners.bin;

import com.beust.jcommander.Parameters;
import jline.Completor;
import jline.ConsoleReader;
import jline.History;
import org.apache.commons.lang.StringUtils;
import org.cryptocoinpartners.command.Command;
import org.cryptocoinpartners.command.CommandBase;
import org.cryptocoinpartners.command.ConsoleWriter;
import org.cryptocoinpartners.command.ParseError;
import org.cryptocoinpartners.module.Context;
import org.cryptocoinpartners.module.BasicAccountService;
import org.cryptocoinpartners.module.MockOrderService;
import org.cryptocoinpartners.module.BasicQuoteService;
import org.cryptocoinpartners.module.TickWindow;
import org.cryptocoinpartners.module.xchangedata.XchangeData;
import org.cryptocoinpartners.schema.Fund;
import org.cryptocoinpartners.util.Config;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* @author Tim Olson
*/
@SuppressWarnings("UnusedDeclaration")
@Parameters(commandNames = {"console","terminal"}, commandDescription = "run in interactive mode")
public class ConsoleRunMode extends RunMode {

public void run() {
try {
init();
//noinspection InfiniteLoopStatement
while(true) {
String line = console.readLine();
if( StringUtils.isEmpty(line) )
continue;
Matcher matcher = Pattern.compile("^(\\w+)(\\s+.*)?$").matcher(line);
if( !matcher.matches() ) {
out.println("Could not understand command name");
continue;
}
String commandName = matcher.group(1);
Command command = CommandBase.commandForName(commandName,context);
if( command == null ) {
out.println("Unknown command " + commandName + ". Available commands:");
out.printList(CommandBase.allCommandNames());
continue;
}
String argStr = matcher.group(2);
if( argStr == null )
argStr = "";
try {
command.parse(argStr);
}
catch( ParseError e ) {
out.println(e.getMessage());
}
catch( Throwable e ) {
log.warn("Could not parse command " + commandName, e);
}
try {
command.run();
}
catch( Throwable e ) {
log.warn("Could not run command "+commandName,e);
out.println("Internal error: see cointrader.log");
}
}
}
catch( IOException e ) {
throw new Error("Console exception",e);
}
}


private void init() throws IOException {
context = new Context();

context.attach(XchangeData.class);
context.attach(TickWindow.class);
context.attach(BasicQuoteService.class);
context.attach(BasicAccountService.class);
context.attach(MockOrderService.class);

console = new ConsoleReader();
String prompt = Config.combined().getString("console.cursor","ct>");
console.setDefaultPrompt(prompt);
console.setHistory(new History());
console.setUseHistory(true);
console.addCompletor(new Completor() {
public int complete(String s, int i, List list) {
return 0;
}
});
out = new ConsoleWriter(console);
context.attach(PrintWriter.class,out);
}


private Fund fund;
private Context context;
private ConsoleReader console;
private ConsoleWriter out;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.cryptocoinpartners.bin.command;
package org.cryptocoinpartners.bin;


import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.clutch.dates.StringToTime;
import org.cryptocoinpartners.module.Esper;
import org.cryptocoinpartners.module.Context;
import org.cryptocoinpartners.module.SaveTicksCsv;
import org.cryptocoinpartners.module.TickWindow;
import org.cryptocoinpartners.util.Config;
import org.cryptocoinpartners.util.Replay;
import org.joda.time.Instant;

Expand All @@ -14,7 +17,7 @@

@SuppressWarnings("UnusedDeclaration")
@Parameters( commandNames = "dump-ticks", commandDescription = "generate ticks into a csv file" )
public class DumpTicksCommand extends Command
public class DumpTicksRunMode extends RunMode
{

public void run()
Expand Down Expand Up @@ -56,14 +59,14 @@ else if( end == null )
replay = Replay.between(new Instant(start), new Instant(end));


Esper esper = replay.getEsper();
esper.loadModule("tickwindow"); // generate ticks
esper.loadModule("savetickscsv", // save ticks as csv
"savetickscsv.filename", filenames.get(0),
"savetickscsv.na", allowNa
);
Context context = replay.getContext();
context.attach(TickWindow.class); // generate ticks
context.attach(SaveTicksCsv.class, // save ticks as csv
Config.module( "savetickscsv.filename", filenames.get(0),
"savetickscsv.na", allowNa )
);
replay.run();
esper.destroy();
context.destroy();
System.exit(0);
}

Expand Down
Loading

0 comments on commit 76d027a

Please sign in to comment.