Code sample

sukhsinghin

Member
Joined
Feb 22, 2022
Posts
17
Likes
4
Hello Everyone,
I m new to java but 15 years with C#. I started looking into the shared code by motivewave example (sdk) and code shared by @danielputra, @ScottyA and @Spin. Thanks for all the great contribution and great work. I m able to compile and test with MW all shared studies. All the shared studies code is too big to understand. I am looking for very small sample code to do only one thing on chart like drawing a line or a dot on the candle, so I can understand the very basic required code and start building on top that. I try to make the shared code do the same but it start crashing. Can anyone share a small code sample project to kick start.
Thanks for all the help.
 

Spin

Well-known member
Joined
May 22, 2019
Posts
477
Likes
191
It is not a secret, @Alex2888 :D

It is just far easier to have the code that does not compile on your end loaded into my IDE.
I can then easily check what's wrong.


My PM-inbox is also open for you ;)
 

Alex2888

Member
Joined
Aug 11, 2021
Posts
12
Likes
7
Sounds great gentlemen.

Thanks Spin for these offer 👍

To be honest I didn’t code anything in MW up to now… I can code in MQL but I want to go in the near future with MW-SDK.
I tried a few times to get started already but it felt like sukhsinghins Explanation.

Any recommendations for books or other knowledge sources what is helpful to get started?

BR
Alex
 

sukhsinghin

Member
Joined
Feb 22, 2022
Posts
17
Likes
4
Here is bare bone code to dispaly "Hi" on everybar based on close. Thanks to all for sharing the code, used some the shared ref.
package extensions.Suk;
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;

import com.motivewave.platform.sdk.common.Coordinate;
import com.motivewave.platform.sdk.common.DataContext;
import com.motivewave.platform.sdk.common.DataSeries;
import com.motivewave.platform.sdk.common.Defaults;
import com.motivewave.platform.sdk.common.Enums;
import com.motivewave.platform.sdk.common.FontInfo;
import com.motivewave.platform.sdk.common.Inputs;
import com.motivewave.platform.sdk.common.MarkerInfo;
import com.motivewave.platform.sdk.common.Settings;
import com.motivewave.platform.sdk.common.Util;
import com.motivewave.platform.sdk.common.desc.BooleanDescriptor;
import com.motivewave.platform.sdk.common.desc.EnabledDependency;
import com.motivewave.platform.sdk.common.desc.FontDescriptor;
import com.motivewave.platform.sdk.common.desc.InputDescriptor;
import com.motivewave.platform.sdk.common.desc.IntegerDescriptor;
import com.motivewave.platform.sdk.common.desc.MAMethodDescriptor;
import com.motivewave.platform.sdk.common.desc.MarkerDescriptor;
import com.motivewave.platform.sdk.common.desc.SettingGroup;
import com.motivewave.platform.sdk.common.desc.SettingTab;
import com.motivewave.platform.sdk.common.desc.SettingsDescriptor;
import com.motivewave.platform.sdk.draw.Label;
import com.motivewave.platform.sdk.draw.Marker;
import com.motivewave.platform.sdk.study.RuntimeDescriptor;
import com.motivewave.platform.sdk.study.Study;
import com.motivewave.platform.sdk.study.StudyHeader;

@StudyHeader(namespace="extensions.Suk", id="BAR_TEXT", name="BAR_TEXT", label="Bar Text", menu="Suk",
desc="Add text to bar",overlay=true, supportsBarUpdates=false)


public class DisplyText extends Study {

private final static String BAR_FONT = "fontTrendBar";

@Override
public void initialize(final Defaults defaults){
SettingsDescriptor settingsDescriptor = createSD();
SettingTab generalTab = settingsDescriptor.addTab("General");
Font indicatorFont = new Font("Segoe UI Semibold", Font.PLAIN, 8);
Font trendBarFont = new Font("Segoe UI Semibold", Font.PLAIN, 6);

SettingGroup configGroup = generalTab.addGroup("Configuration");
configGroup.addRow(new FontDescriptor(BAR_FONT, "Bar Font", trendBarFont, new Color(250, 250, 250), true, true, false));

}
@Override
public void clearState(){
super.clearState();
}

private boolean showText(final int index, final DataSeries dataSeries,final double L, final double H,final boolean showText,final FontInfo fontInfo,
final boolean showHL) {
if(showText == true) {
Label lbl = new Label("Hi", fontInfo.getFont(), fontInfo.getColor(), fontInfo.getBgColor());
if(showHL) {
lbl.setLocation(dataSeries.getStartTime(index), Math.abs(H));
}else {
lbl.setLocation(dataSeries.getStartTime(index), Math.abs(L));
}
lbl.getText().setShowOutline(false);
lbl.getText().setTextColor(fontInfo.getColor());
lbl.getText().setHAlign(Enums.TextAlign.HCENTER);

addFigure(lbl);
}
return true;
}

@Override
protected void calculate(final int index, final DataContext dataContext){
if(index < 1){
return; //No data
}

DataSeries dataSeries = dataContext.getDataSeries();

if(dataSeries.isBarComplete(index) == false){
return;
}

double openPrice = dataSeries.getDouble(index, Enums.BarInput.OPEN);
double closePrice = dataSeries.getDouble(index, Enums.BarInput.CLOSE);
double highPrice = dataSeries.getDouble(index, Enums.BarInput.HIGH);
double lowPrice = dataSeries.getDouble(index, Enums.BarInput.LOW);

double barBodySize = Math.abs(openPrice - closePrice);
double barSize = highPrice - lowPrice;
double halfBarSize = barSize * 0.5;
boolean isLargeBarIndicated = false;
boolean isCloseHL = false;


Settings settings = getSettings();
FontInfo trendBarFontInfo = settings.getFont(BAR_FONT);
if(closePrice > openPrice)
{
isCloseHL = true;
showText(index, dataSeries,lowPrice,highPrice,true,trendBarFontInfo,isCloseHL);
}
else
{
showText(index, dataSeries,lowPrice,highPrice,true,trendBarFontInfo,isCloseHL);
}
}
}
 

Attachments

  • Hi.png
    Hi.png
    5.9 KB · Views: 37

Spin

Well-known member
Joined
May 22, 2019
Posts
477
Likes
191
Looks great @sukhsinghin !! Well done :D You seem to be on the right path.

The most useful aides I often use are these:

MW SDK Programming guide

MW SDK Java Docs

Furthermore I also use the 'find in files'-function of my IDE extensively (because most code you need to write has been written before, no ? :LOL:)
 

sukhsinghin

Member
Joined
Feb 22, 2022
Posts
17
Likes
4
Looks great @sukhsinghin !! Well done :D You seem to be on the right path.

The most useful aides I often use are these:

MW SDK Programming guide

MW SDK Java Docs

Furthermore I also use the 'find in files'-function of my IDE extensively (because most code you need to write has been written before, no ? :LOL:)
that's correct @Spin.
looking for sample code function for going back 5 or 10 candle and getting the OHLC of each. I m able to create arraylist going forward. must be some function like dataseries -1 ..-n.
Appreciate if anyone can share or I will share back in next few days once I am able to do it :)
 

Spin

Well-known member
Joined
May 22, 2019
Posts
477
Likes
191
A small hint perhaps, so your brain doesn't fry:

Getiing a value (Open, High, Low, Close, volume, ...) works like this:

Java:
float close = series.getClose(index);

And getting these Values for past bars would work like this:
Java:
float close = series.getClose(index-5);

You could obviously iterate over them with a for ... loop:

Java:
for(int i = 0; i < 5; i++) {
    float Value = series.getClose(index - i);
    debug ("close of bar index - " + i + " = " + Value);
}

(The debug statements go to the 'Study Log'-window. Menu > View > Display > Study log)
 

sukhsinghin

Member
Joined
Feb 22, 2022
Posts
17
Likes
4
A small hint perhaps, so your brain doesn't fry:

Getiing a value (Open, High, Low, Close, volume, ...) works like this:

Java:
float close = series.getClose(index);

And getting these Values for past bars would work like this:
Java:
float close = series.getClose(index-5);

You could obviously iterate over them with a for ... loop:

Java:
for(int i = 0; i < 5; i++) {
    float Value = series.getClose(index - i);
    debug ("close of bar index - " + i + " = " + Value);
}

(The debug statements go to the 'Study Log'-window. Menu > View > Display > Study log)
Thanks @Spin , perfect and I m cooking and will not let it fry :)
 

orishaspollo

Active member
Joined
Nov 24, 2021
Posts
37
Likes
3
How can we shot the ROC % on each bar when I hover over if?
I see Close, Open, High, Low
 

Spin

Well-known member
Joined
May 22, 2019
Posts
477
Likes
191
I don't think that can be done without custom coding, @orishaspollo.

You could program a Study do calculate the ROC for each bar, and then have the value show up in the 'Cursor Data Window'
 

orishaspollo

Active member
Joined
Nov 24, 2021
Posts
37
Likes
3
But the code that was posted before said “hi” on top of every bar
I just need the ROC% somewhere m, maybe when I hover in every candle
 

Spin

Well-known member
Joined
May 22, 2019
Posts
477
Likes
191
Well, to 'just show the ROC%' somewhere, it needs to be calculated first (since it is not a value like Open or Close, that is being pushed in the datastream of your broker / data-provider)

And when it's calculated you would need code to make it show up on a candle when you hover over that candle (because MW does not do that automatically -as a sidenote I will admit that adding the 'Cursor data Window' or the 'Cursor Data' might be sufficient)

So, @orishaspollo, I am afraid I will have to repeat this requires custom coding :)
 

orishaspollo

Active member
Joined
Nov 24, 2021
Posts
37
Likes
3
Okay but after custom coding to calculate how would you display it when you hover it with the mouse over the candle?
 

Spin

Well-known member
Joined
May 22, 2019
Posts
477
Likes
191
1663057284367.png

Easiest thing would be to export the value, so it shows up in the 'Cursor Data Window' (e.g.: 'Range' in the screenshot above)
I do not believe MW SDK has a possibility to add data to a 'mouseOver' or a 'Hover' on a candle.
:)
 
Top