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 get shifted ma values in IStrategy?
 Post subject: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Tue 21 Aug, 2012, 07:53 
User avatar

User rating: 8
Joined: Tue 25 Oct, 2011, 23:02
Posts: 74
Location: Australia, Melbourne
is there anyway to get a shifted moving average value using the standard indicators.ma overrides in IStrategy?
Note that I'm not asking for a shift on the input feed - I want to shift a standard moving average , e.g. an SMA or LWMA forward on the chart by 1, 3 or 5 bars. I guess I can do this programmatically but I was wondering if IIndicators has a method which can return a shifted value.


 
 Post subject: Re: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Tue 21 Aug, 2012, 13:10 
User avatar

User rating: 94
Joined: Mon 06 Feb, 2012, 12:22
Posts: 357
Location: Portugal, Castelo Branco
hi f451:

Maybe i don't understand your question, but you can apply any shift programatically in strategy on every indicator method where this parameter is available and get the corresponding value. You can see this on javadoc.


ma

double ma(Instrument instrument,
Period period,
OfferSide side,
IIndicators.AppliedPrice appliedPrice,
int timePeriod,
IIndicators.MaType maType,
int shift)
throws JFException
Calculates the Moving Average for a bar specified with the shift parameter.
Parameters:
instrument - instrument of the bar
period - period of the bar
side - Bid or Ask side of the bar
appliedPrice - type of input data
timePeriod - time period
maType - type of moving average
shift - number of candles back in time staring from current bar. 0 - current bar (currently generated from ticks), 1 - previous bar (last formed bar), 2 - current bar minus 2 bars and so on
Returns:
value for the specified bar
Throws:
JFException - when parameters are not valid
See Also:
Indicator calculation


Adding a indicator to a chart you have also this option on indicator parameters.

Hope that helps

JL


 
 Post subject: Re: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Wed 22 Aug, 2012, 08:30 
User avatar

User rating: 8
Joined: Tue 25 Oct, 2011, 23:02
Posts: 74
Location: Australia, Melbourne
jlongo wrote:
hi f451:

Maybe i don't understand your question, but you can apply any shift programatically in strategy on every indicator method where this parameter is available and get the corresponding value. You can see this on javadoc.


ma

double ma(Instrument instrument,
Period period,
OfferSide side,
IIndicators.AppliedPrice appliedPrice,
int timePeriod,
IIndicators.MaType maType,
int shift)
throws JFException
Calculates the Moving Average for a bar specified with the shift parameter.
Parameters:
instrument - instrument of the bar
period - period of the bar
side - Bid or Ask side of the bar
appliedPrice - type of input data
timePeriod - time period
maType - type of moving average
shift - number of candles back in time staring from current bar. 0 - current bar (currently generated from ticks), 1 - previous bar (last formed bar), 2 - current bar minus 2 bars and so on
Returns:
value for the specified bar
Throws:
JFException - when parameters are not valid
See Also:
Indicator calculation


Adding a indicator to a chart you have also this option on indicator parameters.

Hope that helps

JL


the shift above refers to the number of bars backwards in history using the specified period of the bar. I'm referring to the standard implementation of shift on a moving average - which moves the values across a chart forward by a number of candles. e.g. shift 1 would be equivalent to shift = -1 above (note this does not imply getting "future" values - just a translation on the chart)


 
 Post subject: Re: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Sun 25 May, 2014, 18:39 

User rating: 0
Joined: Mon 16 Dec, 2013, 23:25
Posts: 9
Location: United StatesUnited States
Greetings,

Is it possible to code a displaced moving average or moving average envelope? This would not be a shift as in candles back in time from the current candle but a dragging of the current moving average forward or backward in time. I would like to be able to buy and sell the crosses of a moving average and a moving average envelope that have been displaced forward and backwards in time with respect to each other. The moving averages and moving average envelopes could be SMA, EMA, etc.


Thank you kindly,
Best regards

gmoney7

Image


Attachments:
Chart_GBP_JPY_10 Mins_snapshot2.png [73.68 KiB]
Downloaded 333 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: how to get shifted ma values in IStrategy? Post rating: 1   New post Posted: Mon 26 May, 2014, 08:42 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Essentially the shifted lines have the same values as the ones with shift=0. Thus calculate the values as if shift were 0 and then just take another value interval, see:
https://www.dukascopy.com/wiki/#Indicator_Calculation


 
 Post subject: Re: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Wed 28 May, 2014, 21:51 

User rating: 0
Joined: Mon 16 Dec, 2013, 23:25
Posts: 9
Location: United StatesUnited States
Dear API Support,

Thank you for the prompt answer to my question. I have been working with Visual JForex but I could not make the shifted MA's work. I know little about java script but I will make an attempt to modify the SMA Crossover Strategy in the JForex Wiki and get back to you.

Thank you very much,
Best regards


gmoney7


 
 Post subject: Re: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Sun 01 Jun, 2014, 03:54 

User rating: 0
Joined: Mon 16 Dec, 2013, 23:25
Posts: 9
Location: United StatesUnited States
Greetings,

I tried to modify the SMA Crossover Strategy initially by shifting just the SMA 10 forward in time by 10 candles. I tried to calculate the indicator by candle interval from https://www.dukascopy.com/wiki/#Indicator_Calculation that you suggested but it does not shift the SMA 10. I have attached the modified strategy and would greatly appreciate your further help.

Thank you,
Regards

gmoney7


Attachments:
SMAStrategyShifted.java [5.26 KiB]
Downloaded 106 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: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Mon 02 Jun, 2014, 13:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You are doing this the wrong way - you simply need to change the reference point, see the calculateSma method.

@Configurable("Feed")
public IFeedDescriptor feedDescriptor =
        new TimePeriodAggregationFeedDescriptor(
                Instrument.EURUSD,
                Period.TEN_SECS,
                OfferSide.ASK,
                Filter.NO_FILTER
        );
@Configurable("")
public int smaPeriod = 2;
@Configurable("")
public int smaShift = 5;
@Configurable("")
public int dataCount = 10;
@Configurable("")
public Color fastColor = Color.GREEN;
@Configurable("")
public Color colorShifted = Color.RED;   

private IHistory history;
private IConsole console;
private IContext context;
private IIndicators indicators;
private IChart chart;
 
@Override
public void onStart(IContext context) throws JFException {
    this.history = context.getHistory();
    this.console = context.getConsole();
    this.indicators = context.getIndicators();
    this.context = context;
    // subscribe the instrument that we are going to work with
    context.setSubscribedInstruments(java.util.Collections.singleton(feedDescriptor.getInstrument()), true);
    if(feedDescriptor.getDataType() == DataType.TICKS){
        console.getWarn().println("The strategy can't trade according to the tick feed!");
        context.stop();
    }
    if(smaShift < 0){
        console.getErr().println("Shift can't be negative - we can't retrieve future bars");
        context.stop();
    }
    context.subscribeToFeed(feedDescriptor, this);
    setupChart();       
    console.getWarn().println("Please manually shift the dashed indicator line by " + smaShift + " bars");
}

private void setupChart(){
    for (IChart c : context.getCharts()){
        if(c.getFeedDescriptor().equals(feedDescriptor)){
            chart = c;
            break;
        }
    }
    //no such chart opened yet - we open it now
    if(chart == null){
        chart = context.openChart(feedDescriptor);
    }
    chart.add(indicators.getIndicator("SMA"), new Object[] { smaPeriod }, new Color[] { fastColor }, new DrawingStyle[] { DrawingStyle.LINE }, new int[] { 1 });
    chart.add(indicators.getIndicator("SMA"), new Object[] { smaPeriod }, new Color[] { colorShifted }, new DrawingStyle[] { DrawingStyle.DASH_LINE }, new int[] { 3 });
   
    IOhlcChartObject ohlc = null;
    for (IChartObject obj : chart.getAll()) {
        if (obj instanceof IOhlcChartObject) {
            ohlc = (IOhlcChartObject) obj;
        }
    }
    if (ohlc == null) {
        ohlc = chart.getChartObjectFactory().createOhlcInformer();
        chart.add(ohlc);
    }
    ohlc.setShowIndicatorInfo(true);
}


@Override
public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {

    try {
        double[] sma = calculateSma(smaPeriod, 1);
        double[] smaShifted = calculateSma(smaPeriod, 1 + this.smaShift);
        console.getOut().format("-----\n last completed feed data: %s\n %s <- last values \n%s <- shifted values \n------",
                feedData,
                arrayToString(sma),
                arrayToString(smaShifted)
        );
    } catch (Exception e) {
        console.getErr().println(e);
        e.printStackTrace();
    }
}

private double[] calculateSma(int timePeriod, int shift) throws JFException{
    ITimedData timedData = history.getFeedData(feedDescriptor, shift);
    Object[] smaObjectsFeed = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { feedDescriptor.getOfferSide() }, "SMA",
            new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { timePeriod }, this.dataCount, timedData.getTime(), 0);
    return (double[]) smaObjectsFeed[0]; // sma has just 1 output
}

private static String arrayToString(double[] arr) {
    StringBuffer sb = new StringBuffer();
    for (int r = 0; r < arr.length; r++) {
        sb.append(String.format("[%s] %.5f; ", r, arr[r]));
    }
    return sb.toString();
}

The example is based on the following example strategy in our wiki:
https://www.dukascopy.com/wiki/#SMA_Crossover/Visual_version
Here is more on visualization options:
https://www.dukascopy.com/wiki/#Add_indicators_on_chart


Attachments:
SmaShiftVisualize.java [5.69 KiB]
Downloaded 99 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: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Sat 07 Jun, 2014, 17:07 

User rating: 0
Joined: Mon 16 Dec, 2013, 23:25
Posts: 9
Location: United StatesUnited States
Hello,

Thank you again for your prompt reply, I apologize for my delay in getting back to you.
I back-tested SmaShiftVisualize.java and found that when I selected an SMA10 with parameter shift of five it resulted in a shifted line that was actually SMA15 instead of an SMA10 that was shifted five candles into the future. Can a selected SMA be displaced forward and backwards in time while still retaining its' original period structure before it was moved?

Thank you very much,
Best regards

gmoney7

Image


Attachments:
Chart_EUR_USD_10 Mins_snapshot.png [48.08 KiB]
Downloaded 350 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: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Thu 12 Jun, 2014, 10:54 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The shift parameter has no effect on timePeriod parameter. Our example shows you the idea, please adjust it for your own purposes.


 
 Post subject: Re: how to get shifted ma values in IStrategy? Post rating: 0   New post Posted: Sat 14 Jun, 2014, 14:34 

User rating: 0
Joined: Mon 16 Dec, 2013, 23:25
Posts: 9
Location: United StatesUnited States
Thank you very much for your help.


 

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