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.

How to save a chart screenshot from strategy?
 Post subject: How to save a chart screenshot from strategy? Post rating: 0   New post Posted: Thu 06 Aug, 2009, 11:14 
User avatar

User rating: 0
Joined: Wed 19 Nov, 2008, 20:45
Posts: 10
Is it possible to save chart screenshot from within a strategy as I can do it from JForex application?

Kind regards, Yaroslav.


 
 Post subject: Re: How to save a chart screenshot from strategy? Post rating: 0   New post Posted: Thu 06 Aug, 2009, 13:43 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
A very quick solution:

package jforex;

import java.util.*;

import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.dukascopy.api.*;

@RequiresFullAccess
public class SnapshotStrategy implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
   
   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();
   }

   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 {
       RobotExp.saveImageToFile();   // full screen print
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
    private static class RobotExp {
   
        public static void saveImageToFile() {
            try {
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice[] gs = ge.getScreenDevices();
                if (gs.length > 0 && gs[0] != null) {
                    DisplayMode dm = gs[0].getDisplayMode();
                    Robot robot = new Robot();               
                    BufferedImage bi = robot.createScreenCapture(new Rectangle(dm.getWidth(), dm.getHeight()));                   
                    ImageIO.write(bi, "jpg", new File("C:/imageTest.jpg"));   
                }
            } catch (AWTException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
   
}


 

Jump to:  

  © 1998-2024 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