package test1;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test5 extends JFrame {
private JLabel textLabel;
private JButton italicButton;
private JButton boldButton;
public test5() {
setTitle("Font Changer");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
textLabel = new JLabel("Hello World!");
textLabel.setFont(new Font("Serif", Font.PLAIN, 24));
panel.add(textLabel);
italicButton = new JButton("Italic");
italicButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Font font = textLabel.getFont().deriveFont(Font.ITALIC);
textLabel.setFont(font);
}
});
panel.add(italicButton);
boldButton = new JButton("Bold");
boldButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Font font = textLabel.getFont().deriveFont(Font.BOLD);
textLabel.setFont(font);
}
});
panel.add(boldButton);
add(panel);
}
public static void main(String[] args) {
test5 fontChanger = new test5();
fontChanger.setVisible(true);
}
}
程序修改如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JFrame1 extends JFrame implements ActionListener {
private JLabel textLabel;
private JRadioButton italicButton;
private JRadioButton boldButton;
public JFrame1() {
super("Font Changer");
setSize(400, 300);
// 创建面板和标签
JPanel panel = new JPanel(new BorderLayout());
textLabel = new JLabel("Hello World!", JLabel.CENTER);
panel.add(textLabel, BorderLayout.CENTER);
// 创建按钮组和单选按钮,并添加到面板中
ButtonGroup buttonGroup = new ButtonGroup();
italicButton = new JRadioButton("ITALIC");
italicButton.addActionListener(this);
buttonGroup.add(italicButton);
boldButton = new JRadioButton("BOLD");
boldButton.addActionListener(this);
buttonGroup.add(boldButton);
JPanel buttonPanel = new JPanel();
buttonPanel.add(italicButton);
buttonPanel.add(boldButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
// 将面板添加到窗口中,并设置窗口居中显示
add(panel);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
// 根据单选按钮的状态设置标签字体样式
Font font = textLabel.getFont();
if (italicButton.isSelected()) {
font = font.deriveFont(Font.ITALIC);
} else if (boldButton.isSelected()) {
font = font.deriveFont(Font.BOLD);
} else {
font = font.deriveFont(Font.PLAIN);
}
textLabel.setFont(font);
}
public static void main(String[] args) {
JFrame1 fontChanger = new JFrame1();
fontChanger.setVisible(true);
}
}
让我们开发 testIndexMA.mq5 测试指标,和在这里描述的指标类似,并且加上了移动平均:
//+------------------------------------------------------------------+ //| testDistance.mq5 | //| 2016 MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 2 input color clr= clrGreen; input color clrMA = clrMagenta; input int maperiod = 10; //MA 周期数 double ind[],ma[]; //+------------------------------------------------------------------+ //| 自定义指标初始化函数 | //+------------------------------------------------------------------+ //int h,h1; int OnInit() { //--- 指标缓冲区映射 ArraySetAsSeries(ind,true); SetIndexBuffer(0,ind); IndicatorSetString(INDICATOR_SHORTNAME,"testdistance"); IndicatorSetInteger(INDICATOR_DIGITS,2); PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE); PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID); PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2); PlotIndexSetInteger(0,PLOT_LINE_COLOR,clr); PlotIndexSetString(0,PLOT_LABEL,"_tstdistance_"); ArraySetAsSeries(ma,true); SetIndexBuffer(1,ma); PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE ); PlotIndexSetInteger(1, PLOT_LINE_STYLE, STYLE_SOLID ); PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 1 ); PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrMA ); PlotIndexSetString (1, PLOT_LABEL, "_tstdistance_MA" ); //--- return(INIT_SUCCEEDED); } string pair[]={"EURUSD","GBPUSD","AUDUSD","NZDUSD","USDCAD","USDCHF","USDJPY"}; bool bDirect[]={false,false,false,false,true,true,true}; int iCount=7; double GetValue(int shift) { double res=1.0,t; double dBuf[1]; for(int i=0; i<iCount; i++) { t=CopyClose(pair[i],PERIOD_CURRENT,shift,1,dBuf); if(!bDirect[i]) dBuf[0]=1/dBuf[0]; res*=dBuf[0]; }//end for (int i = 0; i < iCount; i++) return (NormalizeDouble(MathPow (res, 1/(double)iCount), _Digits) ); } //+------------------------------------------------------------------+ //| 自定义指标迭代函数 | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { if(prev_calculated==0 || rates_total>prev_calculated+1) { int rt=rates_total; for(int i=1; i<rt; i++) { ind[i]= GetValue(i); } rt -= maperiod; for (int i = 1; i< rt; i++) { ma[i] = GetMA(ind, i, maperiod, _Digits); } } else { ind[0]= GetValue(0); ma[0] = GetMA(ind, 0, maperiod, _Digits); } //--- 返回 prev_calculated 的值用于下一次调用 return(rates_total); } void OnDeinit(const int reason) { string text; switch(reason) { case REASON_PROGRAM: text="指标通过调用 ExpertRemove() 函数终止运行";break; case REASON_INITFAILED: text="这个数值表示 OnInit() 处理函数 "+__FILE__+" 返回了非零数值";break; case REASON_CLOSE: text="终端已经被关闭"; break; case REASON_ACCOUNT: text="账户已经改变";break; case REASON_CHARTCHANGE: text="交易品种或者时段已经改变";break; case REASON_CHARTCLOSE: text="图表被关闭";break; case REASON_PARAMETERS: text="输入参数已经改变";break; case REASON_RECOMPILE: text="程序 "+__FILE__+" 被重新编译";break; case REASON_REMOVE: text="程序 "+__FILE__+" 被从图表上删除";break; case REASON_TEMPLATE: text="图表上应用了新的模板";break; default:text="其它原因"; } PrintFormat("%s",text); } //+------------------------------------------------------------------+ double GetMA(const double& arr[], int index , int period, int digit) { double m = 0; for (int j = 0; j < period; j++) m += arr[index + j]; m /= period; return (NormalizeDouble(m,digit)); }
使用这组输入数据,指标画出了带有移动平均的美元指数。把第49行和第50行这样修改:
string pair[]={"EURUSD", "EURJPY", "EURCHF", "EURGBP", "EURNZD", "EURCAD", "EURAUD"}; bool bDirect[]={true,true,true,true,true,true,true};
再次编译文件 testIndexMA2.mq5. 结果我们会得到类似的指标,显示了欧元指数。把它放到 EURUSD H1:
我们对指标的绝对值并不感兴趣。让我们对移动平均指标的交叉点来计数,计算潜在的入场点。就像在前面的文章中所说的,这些点应该在烛形关闭的时候固定,我们正是这样做的。使用垂直线标记侦测到的进场点: 蓝色表示买入而红色表示卖出,结果明显是正面的,但是,利润相对较小并且不稳定,所以应该增加获利的能力。首先,不要忘记货币对的第二个货币,并把美元指数指标加到一个独立的窗口中:
用垂直线标记移动平均和美元指数图的交叉。让我们分析结果,
这样就得到了第一个实用的结论: 当进入市场时要考虑到两种货币的指数。推荐当一个货币走弱,而另一个货币走强的时候进入市场,其中一个信号就是指数图与移动平均交叉。但是,这个信号还不够: 首先,要等待第二个货币走向相反方向。
还是有延迟的问题: 对于货币对中的两个货币,在指数图和移动平均交叉点之间可能的最大距离是多少?显然,最小(并且最佳)距离是0。很难使用最大延迟来给出清楚的答案,尽管很明显应该使用某个距离。如果一个货币走弱和另一个货币的走强时间相差较远,进入市场就是危险的,在这种情况下,我们会面对背离和趋势的走弱。
所以,我们应该考虑根据组合的跟随趋势指标来进入市场。为了能够更准确的评估潜在入场点,让我们转到上面谈到的指标绝对值的问题。