Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Indicator - problem with SQL
 Post subject: Indicator - problem with SQL Post rating: 0   New post Posted: Thu 09 Feb, 2012, 19:49 

User rating: 0
Joined: Wed 01 Jun, 2011, 09:57
Posts: 5
Location: Poland,
Hi ,
I would like to prepare indicator based on SQL data. I've decided to use PostgreSQL., but any other may be used too.

In Strategies I have no problem to use PostgreSQL:
- I've placed "postgresql-9.1-901.jdbc4.jar" file into folder JForex\Strategy\files.
- in strategy code I've add line:
@Library("postgresql-9.1-901.jdbc4.jar")

But in indicators we can't use @Library()

I'm prepared simple Indicator and working strategy to show you problem I have.

Let me know how to solve my problem.
Thx.

package jforex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IConsole;
import org.postgresql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
// In indicator I can't use - @Library("postgresql-9.1-901.jdbc4.jar")

public class IndicatorSQLtest implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][] inputs = new double[1][];
    private int timePeriod = 2;
    private double[][] outputs = new double[1][];
   
    private IConsole console;
    private Connection connection = null;
   
    public void onStart(IIndicatorContext context) {
        console = context.getConsole();
        indicatorInfo = new IndicatorInfo("EXAMPIND", "Sums previous values", "My indicators",
                false, false, false, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(2, 2, 100, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (openSQLConnection()==0) {
            // BEGIN my SQL code
            // ...
            // END my SQL code
            closeSQLConnection();
        }
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            double value = 0;
            for (int k = timePeriod; k > 0; k--) {
                value += inputs[0][i - k];
            }
            outputs[0][j] = value;
        }
        return new IndicatorResult(startIndex, j);
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }

    public void setInputParameter(int index, Object array) {
        inputs[index] = (double[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
   
    private int openSQLConnection() {
        console.getOut().println("Start connecting...");
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            console.getOut().println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
            e.printStackTrace();
            return 1;
        }
        //console.getOut().println("PostgreSQL JDBC Driver Registered!");
        try {
            connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/mydatabase", "postgres","postgres");
        } catch (SQLException e) {
            console.getOut().println("Connection Failed! Check output console");
            e.printStackTrace();
            return 2;
        }
        if (connection != null) {
            //console.getOut().println("You made it, take control your database now!");
             return 0;
        } else {
            console.getOut().println("Failed to make connection!");
            return 3;
        }
    }
   
    private void closeSQLConnection() {
        try {
            if (connection!=null && !connection.isClosed()) {
                connection.close();
                //console.getOut().println("Connection closed!");
            }
        } catch (SQLException ignored) {
            console.getErr().println(ignored);
        }
    }
}



package jforex;

import java.util.*;

import com.dukascopy.api.*;
import org.postgresql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
@Library("postgresql-9.1-901.jdbc4.jar")

public class StrategySQLtest implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
   
    private Connection connection = null;

    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        if (openSQLConnection()==0) {
            // BEGIN my SQL code
            // ...
            // END my SQL code
            closeSQLConnection();
        }
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
    private int openSQLConnection() {
        console.getOut().println("Start connecting...");
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            console.getOut().println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
            e.printStackTrace();
            return 1;
        }
        //console.getOut().println("PostgreSQL JDBC Driver Registered!");
        try {
            connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/mydatabase", "postgres","postgres");
        } catch (SQLException e) {
            console.getOut().println("Connection Failed! Check output console");
            e.printStackTrace();
            return 2;
        }
        if (connection != null) {
            //console.getOut().println("You made it, take control your database now!");
             return 0;
        } else {
            console.getOut().println("Failed to make connection!");
            return 3;
        }
    }
   
    private void closeSQLConnection() {
        try {
            if (connection!=null && !connection.isClosed()) {
                connection.close();
                console.getOut().println("Connection closed!");
            }
        } catch (SQLException ignored) {
            console.getErr().println(ignored);
        }
    }
}


 
 Post subject: Re: Indicator - problem with SQL Post rating: 0   New post Posted: Fri 10 Feb, 2012, 08:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
maxxon wrote:
But in indicators we can't use @Library()
This is not the case, you can use libraries for indicators.


 
 Post subject: Re: Indicator - problem with SQL Post rating: 0   New post Posted: Fri 10 Feb, 2012, 11:18 

User rating: 0
Joined: Wed 01 Jun, 2011, 09:57
Posts: 5
Location: Poland,
I understand that it is possible to use libraries in indicators, but how. Have you any example for indicator (not for strategy) ?
When I've uncomment line 9 in my indicator code:
@Library("postgresql-9.1-901.jdbc4.jar")
and try to compile my indicator. I've got following error messages:
10:03:19 ----------
10:03:19 The attribute value is undefined for the annotation type Library
10:03:19 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10:03:19 @Library("postgresql-9.1-901.jdbc4.jar")
10:03:19 2. ERROR in C:\DOCUME~1\User\SETTIN~1\Temp\jfxide\tmp\compile\IndicatorSQLtest.java (at line 9)
10:03:19 ----------
10:03:19 Library cannot be resolved to a type
10:03:19 ^^^^^^^
10:03:19 @Library("postgresql-9.1-901.jdbc4.jar")
10:03:19 1. ERROR in C:\DOCUME~1\User\SETTIN~1\Temp\jfxide\tmp\compile\IndicatorSQLtest.java (at line 9)
10:03:19 ----------

I use newest JFOREX API ver.2.6.57 / Dukascopy ver. 2.14.23 on my Demo account.


Attachments:
IndicatorSQLtest.java [4.65 KiB]
Downloaded 276 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Indicator - problem with SQL Post rating: 0   New post Posted: Fri 10 Feb, 2012, 11:27 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You need to add the import:
import com.dukascopy.api.Library;


 
 Post subject: Re: Indicator - problem with SQL Post rating: 0   New post Posted: Fri 10 Feb, 2012, 12:04 

User rating: 0
Joined: Wed 01 Jun, 2011, 09:57
Posts: 5
Location: Poland,
OK my problem with library is fixed. thanks.


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com