- 浏览: 217763 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
liminshaoye:
为什么我的一次成功之后就不能发送第二次了呢?
Java 使用SMSLib通过串口通讯收发短信 -
sziitjiang:
面试的题目,慢慢研究,还是能学到很多东西
JAVA 面试题 -
sziitjiang:
老大,我百度了一下SMG引擎,把你的博客给百度出来了!
SMG引擎 1.3.0 功能说明 -
jinkunaier:
,受教了!
深入探索SOAP1.1--使用SAAJ1.2.1 -
genggeng:
THX,对我入门很有帮助......
Java 使用SMSLib通过串口通讯收发短信
//[i] 观察均线赢利能力 MAProfit
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Yellow
#import "speak.dll"
void gRate(int rate);
void gVolume(int rate);
void gPitch(int rate);
void gSpeak(string text);
#import
#define SIGNAL_NONE 0
#define SIGNAL_SHORT 1
#define SIGNAL_LONG 2
extern bool bOptimize= true; // True: Find the best single MA by optimizing(switch time frame to re-optimize)
extern bool bOptimizeIntersect= true; // True: optimize for minimum intersections, otherwise optimize for max profit
extern int PeriodMA= 400; // If you do not want to optimize, you can define a period
extern int Method= 0; // Method for MA 0= Simple, 1= Expotential, 2= Smoothed, 3= Linear weighted
extern bool DrawTringles= true; // Draws triangles for the simulated trading
extern int MinMA= 5; // Minimum test for optimizing
extern int MaxMA= 500; // Maximum test for optimizing
extern int StepMA= 1; // Step during optimizion, 1 tests every MA, 10 tests every 10th etc.
extern int CountOptimize= 300; // Number of candles for optimizing
extern int RepaintBars= 3000; // Number of candles on which we draw triangles and calculate the win/loss
extern bool Alarm= true; // Make a visible alert on new signal
extern bool bSpeak= true; // Speak the alert with gspeak
string AlertShort= "alert.wav";
string AlertLong= "alert.wav";
bool bNeedOptimize;
int ColorLongTrade= MediumSpringGreen;
int ColorShortTrade= Red;
int ColorBadTrade= Violet;
bool bAlertViaAlert= true;
datetime lastSignalTime= 0;
datetime firstTradeCandle; // we trade 300 candles but at all calls it should be the same candle
int Method1= MODE_SMA;
int Method2= MODE_SMA;
int Price1= PRICE_MEDIAN;
int Price2= PRICE_HIGH;
int Price3= PRICE_LOW;
double ExtMapBuffer[]; // Fast MA curve
double ExtMapBuffer2[]; // Fast MA curve
int OldPeriod; // The period, we check for changes and re-init ourself
int 规定点差;
int cBars; // Saved number of bars in order to see if new bar
string OldSymbol; // If symbol canges, we re-initialize
string rsiMessage; // Last Message RSI Oversold/Overbought
int MaxObj= 0;
int yesterday, today; // index of yesterday and today
double gTradeOpen[],
gTradeMin[],
gTradeMax[];
int gTradeCmd[],
gTradeStart[],
gTradeEnd[],
gTradeID;
int gStartShort,
gEndShort,
gStartLong,
gEndLong;
//------------------初始化---------------------+
bool bInit;
int init()
{
OldPeriod= -1;
OldSymbol= "";
bNeedOptimize= true;
lastSignalTime= 0 ;
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(0, ExtMapBuffer);
SetIndexBuffer(1, ExtMapBuffer2);
IndicatorShortName("Cross Moving Average");
SetIndexLabel(0, "Fast Moving Average");
makelabel("profit", 0, 20, "Profit Total", White);
makelabel("profityesterday", 300, 20, "Profit Total", White);
makelabel("signal", 500, 20, "Signal", White);
makelabel("message", 600, 20, "Message", White);
makelabel("message2", 600, 40, "Message", White);
makelabel("hint", 400, 0, "MAProfit(C) Thomas Quester", White);
makelabel("hint2", 600, 0, "look at source code for description", White);
cBars= 0;
ArrayResize(gTradeOpen, 500);
ArrayResize(gTradeMin, 500);
ArrayResize(gTradeMax, 500);
ArrayResize(gTradeCmd, 500);
ArrayResize(gTradeStart, 500);
ArrayResize(gTradeEnd, 500);
SendVars();
firstTradeCandle= 0;
bInit= true;
return(0);
}
//------------------反初始化---------------------+
int deinit()
{
DeleteObjects();
DelVars();
return(0);
}
//------------------主函数---------------------+
int start()
{
int 已计= IndicatorCounted();
规定点差= MarketInfo(Symbol(), MODE_SPREAD);
int i; // some running integer
int start;
int objid; // the number of triangles
string name; // name of triangle
double d; // some double
double maPeriod; // moving average Period
double profit= 0; // profit of "trade" as difference between open and close
double open; // the open price
int day; // the day
datetime opentime; // the time of open trade
int openid; // the index of open trade
int signal, s; // active and new singal
double totalProfit; // total profit
double totalProfitYesterday; // total profit yesterday
bool newBar;
string speak;
string alert;
double ma, price, maHigh;
if(bNeedOptimize)
Optimize();
bNeedOptimize= false;
speak= "*";
alert= "*";
//SpeakRSI();
signal= SIGNAL_NONE;
if(Bars!=cBars)
newBar= true;
else
newBar= false;
cBars= Bars;
//if(Period()!=OldPeriod || Symbol()!=OldSymbol)
if(bInit){
OldPeriod= Period();
OldSymbol= Symbol();
Print("Symbol Changed, last Speak time set to ", TimeToStr(lastSignalTime));
}
objid= 0;
//--删除旧物件
if(newBar)
DeleteObjects();
//--when starts today?
CalcDays();
totalProfit= 0;
totalProfitYesterday= 0;
if(lastSignalTime==0)
lastSignalTime= Time[0];
ObjectCreate("today", OBJ_VLINE, 0, Time[today], 0);
ObjectCreate("yesterday", OBJ_VLINE, 0, Time[yesterday], 0);
start= Bars;
if(start>RepaintBars)
start= RepaintBars;
if(RepaintBars!=0)
if(start>RepaintBars)
start= RepaintBars;
ma= iMA(NULL, NULL, PeriodMA, 0, Method, PRICE_MEDIAN, start);
// find first bar to start trading
int tradeStart= RepaintBars;
if(firstTradeCandle==0){
if(Bars>RepaintBars){
firstTradeCandle= Time[RepaintBars];
tradeStart= RepaintBars;
}
else{
firstTradeCandle= Time[Bars];
tradeStart= Bars;
}
}
else{
for(i= 0;i<Bars;i++){
if(Time[i]==firstTradeCandle){
tradeStart= i;
break;
}
}
}
//Print("TradeStart= ", tradeStart, " Trade Start Time= ", TimeToStr(firstTradeCandle));
int good= 0;
int fails= 0;
int totalWin= 0;
for(i=Bars; i>=0; i--){
ma= iMA(NULL, NULL, PeriodMA, 0, Method, PRICE_LOW, i);
maHigh= iMA(NULL, NULL, PeriodMA, 0, Method, PRICE_HIGH, i);
ExtMapBuffer[i]= ma;
ExtMapBuffer2[i]= maHigh;
if(i<tradeStart){
price= (High[i]+Low[i])/2;
s= signal;
{
if(Low[i]<ma)
s= SIGNAL_SHORT;
if(High[i]>maHigh)
s= SIGNAL_LONG;
}
if(s!=signal){
if(s==SIGNAL_SHORT){
if(Time[i]>lastSignalTime && bSpeak){
Print("Speak time ", TimeToStr(Time[i]));
lastSignalTime= Time[i];
alert= AlertShort;
speak= "New signal at "+PairName(Symbol())+ " "+SpeakTime()+". signal is short. ";
SetText("signal", "Short");
}
}
if(s==SIGNAL_LONG){
if(Time[i]>lastSignalTime && bSpeak){
Print("Speak time ", TimeToStr(Time[i]));
lastSignalTime= Time[i];
alert= AlertLong;
speak= "New signal at "+PairName(Symbol()) + " " + SpeakTime()+". Signal is long. ";
SetText("signal", "Long");
}
}
profit= 0;
if(signal==SIGNAL_SHORT){
profit= open-Close[i];
profit /= Point;
profit -= 规定点差;
}
if(signal==SIGNAL_LONG){
profit= Close[i]-open;
profit /= Point;
profit -= 规定点差;
}
totalWin+= profit;
if(profit<0)
fails++;
if(profit>0)
good++;
if(signal!=SIGNAL_NONE){
objid++;
DrawTriangle(objid, signal, opentime, Open[openid], Time[i], Close[i], profit);
}
//ExtMapBuffer2[i]= profit;
if(i<=today)
totalProfit+= profit;
if(i<=yesterday && i>today)
totalProfitYesterday+= profit;
opentime= Time[i];
openid= i;
signal= s;
open= Open[i];
}
profit= 0;
}
}
//--terminate open "trade"
if(signal!=SIGNAL_NONE){
i= 0;
if(signal==SIGNAL_SHORT){
profit= open-Close[i];
profit /= Point;
profit -= 规定点差;
SetText("signal", "Short");
}
if(signal==SIGNAL_LONG){
profit= Close[i]-open;
profit /= Point;
profit -= 规定点差;
SetText("signal", "Long");
}
//ExtMapBuffer2[i]= profit;
totalProfit += profit;
totalWin += profit;
if(profit<0)
fails++;
if(profit>0)
good++;
objid++;
DrawTriangle(objid, signal, opentime, Open[openid], Time[i], Close[i], profit);
}
i= totalProfit;
SetText("profit", "Profit totay with MA "+PeriodMA+" is "+i+" Pips");
SetText("message2", "Total Profit= "+totalWin+" Fails= "+fails+" Wins= "+good);
if(totalWin < 0)
ObjectSet("message2", OBJPROP_COLOR, Red);
else
ObjectSet("message2", OBJPROP_COLOR, GreenYellow);
i= totalProfitYesterday;
SetText("profityesterday", "Profit yesterday is "+i+" Pips");
MaxObj= objid+1;
bInit= false;
if(alert!="*")
Alert(alert);
if(speak!="*")
LoudAlert(speak);
return(0);
}
//---------------------------------------+
void SendVars()
{
string sym= Symbol()+Period();
GlobalVariableSet(sym+"PeriodMA", PeriodMA);
GlobalVariableSet(sym+"Method", Method);
}
//---------------------------------------+
void GetVars()
{
string sym= Symbol()+Period();
PeriodMA= GlobalVariableGet(sym+"PeriodMA");
Method= GlobalVariableGet(sym+"Method");
}
//---------------------------------------+
void DelVars()
{
string sym= Symbol()+Period();
GlobalVariableDel(sym+"PeriodMA");
GlobalVariableDel(sym+"Method");
}
//---------------------------------------+
void LoudAlert(string s)
{
if(bAlertViaAlert)
Alert(s);
Print(s);
if(bSpeak)
gSpeak(s); // uncomment this for speak
}
//---------------------------------------+
string FormatNumber(string s)
{
int c;
while(true){
c= StringGetChar(s, StringLen(s)-1);
if(c!='.' && c!='0')
break;
s= StringSubstr(s, 0, StringLen(s)-1);
}
return(s);
}
//---------------------------------------+
string LongName(string s)
{
if(s=="EUR") s= "Euro ";
if(s=="USD") s= "US Dollar ";
if(s=="JPY") s= "Japanese Yen";
if(s=="CAD") s= "Canadian Dollar";
if(s=="AUD") s= "Australian Dollar";
if(s=="NZD") s= "New Zeeland Dollar";
if(s=="CHF") s= "Swiss Francs";
return(s);
}
//---------------------------------------+
string PairName(string s)
{
string a, b;
a= StringSubstr(s, 0, 3);
b= StringSubstr(s, 3, 3);
a= LongName(a);
if(StringLen(a)>3)
a= a + " to ";
b= LongName(b);
return(a+b);
}
//---------------------------------------+
string SpeakTime()
{
int p;
string s;
p= Period();
switch(p){
case 30: s= "half hour"; break;
case 60: s= "One hour"; break;
case 120: s= "Two horus"; break;
case 240: s= "Four hours"; break;
case 1440: s= "One day"; break;
case 10080: s= "One week"; break;
case 43200: s= "One month"; break;
default: s= p+" Minutes";
}
return(s);
}
//---------------------------------------+
int trades;
double wins, losses;
double CalcProfit(int bars, int mode1, int price1, int mode2, int price2, int price3, int periodMA)
{
double ma; // moving average small and long value
double profit= 0; // profit of "trade" as difference between open and close
double totalProfit;
int i, gOpenTime, openid;
double d, open, price, min, max;
int s, signal;
signal= SIGNAL_NONE;
totalProfit= 0;
open= 0;
gTradeID= -1;
for(i=bars; i>=0; i--){
ma= iMA(NULL, NULL, periodMA, 0, mode1, price1, i);
s= signal;
price= (High[i]+Low[i])/2;
if(ma < price) s= SIGNAL_SHORT;
if(ma>price) s= SIGNAL_LONG;
// calc min/max
if(signal!=SIGNAL_NONE){
if(price<min)
min= price;
if(price>max)
max= price;
}
if(s!=signal){
if(gTradeID>=0){
gTradeMin[gTradeID]= min;
gTradeMax[gTradeID]= max;
gTradeEnd[gTradeID]= i;
gTradeCmd[gTradeID]= s;
}
gTradeID++;
gTradeOpen[gTradeID]= price;
gTradeStart[gTradeID]= i;
gTradeEnd[gTradeID]= 0;
min= 99999;
max= -9999;
profit= 0;
if(signal==SIGNAL_SHORT){
profit= open-price;
profit /= Point;
profit -= 规定点差;
totalProfit += profit;
}
if(signal==SIGNAL_LONG){
profit= price-open;
profit /= Point;
profit -= 规定点差;
totalProfit += profit;
}
gOpenTime= Time[i];
openid= i;
signal= s;
open= price;
}
}
if(signal!=SIGNAL_NONE){
profit= 0;
i= 0;
if(signal==SIGNAL_SHORT){
profit= open-Open[i];
profit /= Point;
profit -= 规定点差;
totalProfit += profit;
}
if(signal==SIGNAL_LONG) {
profit= Open[i]-open;
profit /= Point;
profit -= 规定点差;
totalProfit += profit;
}
}
return(totalProfit);
}
//---------------------------------------+
void Optimize()
{
if(bOptimize) {
if(bOptimizeIntersect)
OptimizeIntersects();
else
OptimizeAll();
}
else
SetText("message", "Optimizing is disabled");
}
//---------------------------------------+
void OptimizeIntersects()
{
int i, s, intersects, minInterSects;
double ma;
minInterSects= 9999;
for(s= MinMA; s<= MaxMA;s+= StepMA){
intersects= 0;
for(i= CountOptimize;i>=0;i--){
ma= iMA(NULL, NULL, s, 0, Method, PRICE_MEDIAN, i);
if(ma>=Low[i] && ma <= High[i])
intersects++;}
if(intersects < minInterSects){
minInterSects= intersects;
PeriodMA= s;
}
}
SetText("message", "BestMA= "+PeriodMA+" Intersects= "+minInterSects);
}
//---------------------------------------+
void OptimizeAll()
{
double profit, maxProfit;
int s, l, a;
int bestMA;
bestMA= 0;
maxProfit= -9999;
for(s= MinMA; s<=MaxMA; s+=StepMA){
//a= s*130;
//a /= 100;
profit= CalcProfit(CountOptimize, Method, PRICE_MEDIAN, Method, PRICE_HIGH, PRICE_LOW, s);
if(profit>maxProfit && profit!=0){
bestMA= s;
maxProfit= profit;
}
}
Method1= Method;
Method2= Method;
Price1= PRICE_MEDIAN;
Price2= PRICE_HIGH;
Price3= PRICE_LOW;
PeriodMA= bestMA;
SetText("message", "BestMA= "+PeriodMA+" Profit= "+maxProfit);
SendVars();
}
//---------------------------------------+
void CalcDays()
{
int 日期;
int i;
日期= TimeDay(Time[0]);
for(i=0; i<Bars; i++){
if(TimeDay(Time[i])!=日期){
today= i-1;
break;
}
}
//--when starts yesterday
日期= TimeDay(Time[today+1]);
for(i=today+1; i<Bars; i++){
if(TimeDay(Time[i])!=日期){
yesterday= i-1;
break;
}
}
}
//---------------------------------------+
void DrawTriangle(int objid, int signal, datetime opentime, double openprice, datetime timenow, double pricenow, double profit)
{
string name;
int i;
if(DrawTringles){
//Print("signal= ", Sig2Str(signal), " open= ", openprice, " close= ", pricenow, " profit= ", profit);
name= "profit"+objid;
ObjectCreate(name, OBJ_TRIANGLE, 0, opentime, openprice, timenow, openprice, timenow, pricenow);
if(signal==SIGNAL_SHORT)
ObjectSet(name, OBJPROP_COLOR, ColorShortTrade);
else
ObjectSet(name, OBJPROP_COLOR, ColorLongTrade);
ObjectSet(name, OBJPROP_BACK, false);
if(profit<0){
ObjectSet(name, OBJPROP_WIDTH, 1);
}
else
ObjectSet(name, OBJPROP_WIDTH, 3);
}
name= "win"+objid;
i= profit;
ObjectCreate(name, OBJ_TEXT, 0, timenow, pricenow-20*Point);
ObjectSetText(name, i+" Pips", 8, "Tahoma", White);
}
//---------------------------------------+
void DeleteObjects()
{
int i;
string name;
for(i=0; i<500; i++){
name= "profit"+i;
ObjectDelete(name);
name= "win"+i;
ObjectDelete(name);
}
/*
for(i= 0;i<CountOptimize;i++)
{
name= "mas"+Time[i];
ObjectDelete(name);
name= "masO"+Time[i];
ObjectDelete(name);
name= "masC"+Time[i];
ObjectDelete(name);
name= "mal"+Time[i];
ObjectDelete(name);
}
*/
MaxObj= 0;
}
//---------------------------------------+
void makelabel(string lblname, int x, int y, string txt, color txtcolor)
{
ObjectCreate(lblname, OBJ_LABEL, 0, 0, 0);
ObjectSet(lblname, OBJPROP_CORNER, 0);
ObjectSetText(lblname, txt, 8, "Verdana", txtcolor);
ObjectSet(lblname, OBJPROP_XDISTANCE, x);
ObjectSet(lblname, OBJPROP_YDISTANCE, y);
}
//---------------------------------------+
void SetText(string name, string txt)
{
ObjectSetText(name, txt, 7, "Verdana", White);
}
//---------------------------------------+
void Rectangle(string name, datetime time, double price, int col)
{
string name1;
name1= name + Time[time];
ObjectCreate(name1, OBJ_RECTANGLE, 0, Time[time], price, Time[time+1], price+Point/2);
ObjectSet(name1, OBJPROP_COLOR, col);
}
//---------------------------------------+
void Rectangle2(string name, datetime time, datetime time2, double price, int col)
{
string name1;
name1= name + time;
ObjectCreate(name1, OBJ_RECTANGLE, 0, time, price, time2, price+Point/2);
ObjectSet(name1, OBJPROP_COLOR, col);
}
// creates a speakable text about other symbols
//---------------------------------------+
string Sig2Str(int signal)
{
string r= "undef";
switch(signal){
case SIGNAL_NONE: r= "none"; break;
case SIGNAL_LONG: r= "long"; break;
case SIGNAL_SHORT: r= "short"; break;
}
return(r);
}
//---------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Yellow
#import "speak.dll"
void gRate(int rate);
void gVolume(int rate);
void gPitch(int rate);
void gSpeak(string text);
#import
#define SIGNAL_NONE 0
#define SIGNAL_SHORT 1
#define SIGNAL_LONG 2
extern bool bOptimize= true; // True: Find the best single MA by optimizing(switch time frame to re-optimize)
extern bool bOptimizeIntersect= true; // True: optimize for minimum intersections, otherwise optimize for max profit
extern int PeriodMA= 400; // If you do not want to optimize, you can define a period
extern int Method= 0; // Method for MA 0= Simple, 1= Expotential, 2= Smoothed, 3= Linear weighted
extern bool DrawTringles= true; // Draws triangles for the simulated trading
extern int MinMA= 5; // Minimum test for optimizing
extern int MaxMA= 500; // Maximum test for optimizing
extern int StepMA= 1; // Step during optimizion, 1 tests every MA, 10 tests every 10th etc.
extern int CountOptimize= 300; // Number of candles for optimizing
extern int RepaintBars= 3000; // Number of candles on which we draw triangles and calculate the win/loss
extern bool Alarm= true; // Make a visible alert on new signal
extern bool bSpeak= true; // Speak the alert with gspeak
string AlertShort= "alert.wav";
string AlertLong= "alert.wav";
bool bNeedOptimize;
int ColorLongTrade= MediumSpringGreen;
int ColorShortTrade= Red;
int ColorBadTrade= Violet;
bool bAlertViaAlert= true;
datetime lastSignalTime= 0;
datetime firstTradeCandle; // we trade 300 candles but at all calls it should be the same candle
int Method1= MODE_SMA;
int Method2= MODE_SMA;
int Price1= PRICE_MEDIAN;
int Price2= PRICE_HIGH;
int Price3= PRICE_LOW;
double ExtMapBuffer[]; // Fast MA curve
double ExtMapBuffer2[]; // Fast MA curve
int OldPeriod; // The period, we check for changes and re-init ourself
int 规定点差;
int cBars; // Saved number of bars in order to see if new bar
string OldSymbol; // If symbol canges, we re-initialize
string rsiMessage; // Last Message RSI Oversold/Overbought
int MaxObj= 0;
int yesterday, today; // index of yesterday and today
double gTradeOpen[],
gTradeMin[],
gTradeMax[];
int gTradeCmd[],
gTradeStart[],
gTradeEnd[],
gTradeID;
int gStartShort,
gEndShort,
gStartLong,
gEndLong;
//------------------初始化---------------------+
bool bInit;
int init()
{
OldPeriod= -1;
OldSymbol= "";
bNeedOptimize= true;
lastSignalTime= 0 ;
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(0, ExtMapBuffer);
SetIndexBuffer(1, ExtMapBuffer2);
IndicatorShortName("Cross Moving Average");
SetIndexLabel(0, "Fast Moving Average");
makelabel("profit", 0, 20, "Profit Total", White);
makelabel("profityesterday", 300, 20, "Profit Total", White);
makelabel("signal", 500, 20, "Signal", White);
makelabel("message", 600, 20, "Message", White);
makelabel("message2", 600, 40, "Message", White);
makelabel("hint", 400, 0, "MAProfit(C) Thomas Quester", White);
makelabel("hint2", 600, 0, "look at source code for description", White);
cBars= 0;
ArrayResize(gTradeOpen, 500);
ArrayResize(gTradeMin, 500);
ArrayResize(gTradeMax, 500);
ArrayResize(gTradeCmd, 500);
ArrayResize(gTradeStart, 500);
ArrayResize(gTradeEnd, 500);
SendVars();
firstTradeCandle= 0;
bInit= true;
return(0);
}
//------------------反初始化---------------------+
int deinit()
{
DeleteObjects();
DelVars();
return(0);
}
//------------------主函数---------------------+
int start()
{
int 已计= IndicatorCounted();
规定点差= MarketInfo(Symbol(), MODE_SPREAD);
int i; // some running integer
int start;
int objid; // the number of triangles
string name; // name of triangle
double d; // some double
double maPeriod; // moving average Period
double profit= 0; // profit of "trade" as difference between open and close
double open; // the open price
int day; // the day
datetime opentime; // the time of open trade
int openid; // the index of open trade
int signal, s; // active and new singal
double totalProfit; // total profit
double totalProfitYesterday; // total profit yesterday
bool newBar;
string speak;
string alert;
double ma, price, maHigh;
if(bNeedOptimize)
Optimize();
bNeedOptimize= false;
speak= "*";
alert= "*";
//SpeakRSI();
signal= SIGNAL_NONE;
if(Bars!=cBars)
newBar= true;
else
newBar= false;
cBars= Bars;
//if(Period()!=OldPeriod || Symbol()!=OldSymbol)
if(bInit){
OldPeriod= Period();
OldSymbol= Symbol();
Print("Symbol Changed, last Speak time set to ", TimeToStr(lastSignalTime));
}
objid= 0;
//--删除旧物件
if(newBar)
DeleteObjects();
//--when starts today?
CalcDays();
totalProfit= 0;
totalProfitYesterday= 0;
if(lastSignalTime==0)
lastSignalTime= Time[0];
ObjectCreate("today", OBJ_VLINE, 0, Time[today], 0);
ObjectCreate("yesterday", OBJ_VLINE, 0, Time[yesterday], 0);
start= Bars;
if(start>RepaintBars)
start= RepaintBars;
if(RepaintBars!=0)
if(start>RepaintBars)
start= RepaintBars;
ma= iMA(NULL, NULL, PeriodMA, 0, Method, PRICE_MEDIAN, start);
// find first bar to start trading
int tradeStart= RepaintBars;
if(firstTradeCandle==0){
if(Bars>RepaintBars){
firstTradeCandle= Time[RepaintBars];
tradeStart= RepaintBars;
}
else{
firstTradeCandle= Time[Bars];
tradeStart= Bars;
}
}
else{
for(i= 0;i<Bars;i++){
if(Time[i]==firstTradeCandle){
tradeStart= i;
break;
}
}
}
//Print("TradeStart= ", tradeStart, " Trade Start Time= ", TimeToStr(firstTradeCandle));
int good= 0;
int fails= 0;
int totalWin= 0;
for(i=Bars; i>=0; i--){
ma= iMA(NULL, NULL, PeriodMA, 0, Method, PRICE_LOW, i);
maHigh= iMA(NULL, NULL, PeriodMA, 0, Method, PRICE_HIGH, i);
ExtMapBuffer[i]= ma;
ExtMapBuffer2[i]= maHigh;
if(i<tradeStart){
price= (High[i]+Low[i])/2;
s= signal;
{
if(Low[i]<ma)
s= SIGNAL_SHORT;
if(High[i]>maHigh)
s= SIGNAL_LONG;
}
if(s!=signal){
if(s==SIGNAL_SHORT){
if(Time[i]>lastSignalTime && bSpeak){
Print("Speak time ", TimeToStr(Time[i]));
lastSignalTime= Time[i];
alert= AlertShort;
speak= "New signal at "+PairName(Symbol())+ " "+SpeakTime()+". signal is short. ";
SetText("signal", "Short");
}
}
if(s==SIGNAL_LONG){
if(Time[i]>lastSignalTime && bSpeak){
Print("Speak time ", TimeToStr(Time[i]));
lastSignalTime= Time[i];
alert= AlertLong;
speak= "New signal at "+PairName(Symbol()) + " " + SpeakTime()+". Signal is long. ";
SetText("signal", "Long");
}
}
profit= 0;
if(signal==SIGNAL_SHORT){
profit= open-Close[i];
profit /= Point;
profit -= 规定点差;
}
if(signal==SIGNAL_LONG){
profit= Close[i]-open;
profit /= Point;
profit -= 规定点差;
}
totalWin+= profit;
if(profit<0)
fails++;
if(profit>0)
good++;
if(signal!=SIGNAL_NONE){
objid++;
DrawTriangle(objid, signal, opentime, Open[openid], Time[i], Close[i], profit);
}
//ExtMapBuffer2[i]= profit;
if(i<=today)
totalProfit+= profit;
if(i<=yesterday && i>today)
totalProfitYesterday+= profit;
opentime= Time[i];
openid= i;
signal= s;
open= Open[i];
}
profit= 0;
}
}
//--terminate open "trade"
if(signal!=SIGNAL_NONE){
i= 0;
if(signal==SIGNAL_SHORT){
profit= open-Close[i];
profit /= Point;
profit -= 规定点差;
SetText("signal", "Short");
}
if(signal==SIGNAL_LONG){
profit= Close[i]-open;
profit /= Point;
profit -= 规定点差;
SetText("signal", "Long");
}
//ExtMapBuffer2[i]= profit;
totalProfit += profit;
totalWin += profit;
if(profit<0)
fails++;
if(profit>0)
good++;
objid++;
DrawTriangle(objid, signal, opentime, Open[openid], Time[i], Close[i], profit);
}
i= totalProfit;
SetText("profit", "Profit totay with MA "+PeriodMA+" is "+i+" Pips");
SetText("message2", "Total Profit= "+totalWin+" Fails= "+fails+" Wins= "+good);
if(totalWin < 0)
ObjectSet("message2", OBJPROP_COLOR, Red);
else
ObjectSet("message2", OBJPROP_COLOR, GreenYellow);
i= totalProfitYesterday;
SetText("profityesterday", "Profit yesterday is "+i+" Pips");
MaxObj= objid+1;
bInit= false;
if(alert!="*")
Alert(alert);
if(speak!="*")
LoudAlert(speak);
return(0);
}
//---------------------------------------+
void SendVars()
{
string sym= Symbol()+Period();
GlobalVariableSet(sym+"PeriodMA", PeriodMA);
GlobalVariableSet(sym+"Method", Method);
}
//---------------------------------------+
void GetVars()
{
string sym= Symbol()+Period();
PeriodMA= GlobalVariableGet(sym+"PeriodMA");
Method= GlobalVariableGet(sym+"Method");
}
//---------------------------------------+
void DelVars()
{
string sym= Symbol()+Period();
GlobalVariableDel(sym+"PeriodMA");
GlobalVariableDel(sym+"Method");
}
//---------------------------------------+
void LoudAlert(string s)
{
if(bAlertViaAlert)
Alert(s);
Print(s);
if(bSpeak)
gSpeak(s); // uncomment this for speak
}
//---------------------------------------+
string FormatNumber(string s)
{
int c;
while(true){
c= StringGetChar(s, StringLen(s)-1);
if(c!='.' && c!='0')
break;
s= StringSubstr(s, 0, StringLen(s)-1);
}
return(s);
}
//---------------------------------------+
string LongName(string s)
{
if(s=="EUR") s= "Euro ";
if(s=="USD") s= "US Dollar ";
if(s=="JPY") s= "Japanese Yen";
if(s=="CAD") s= "Canadian Dollar";
if(s=="AUD") s= "Australian Dollar";
if(s=="NZD") s= "New Zeeland Dollar";
if(s=="CHF") s= "Swiss Francs";
return(s);
}
//---------------------------------------+
string PairName(string s)
{
string a, b;
a= StringSubstr(s, 0, 3);
b= StringSubstr(s, 3, 3);
a= LongName(a);
if(StringLen(a)>3)
a= a + " to ";
b= LongName(b);
return(a+b);
}
//---------------------------------------+
string SpeakTime()
{
int p;
string s;
p= Period();
switch(p){
case 30: s= "half hour"; break;
case 60: s= "One hour"; break;
case 120: s= "Two horus"; break;
case 240: s= "Four hours"; break;
case 1440: s= "One day"; break;
case 10080: s= "One week"; break;
case 43200: s= "One month"; break;
default: s= p+" Minutes";
}
return(s);
}
//---------------------------------------+
int trades;
double wins, losses;
double CalcProfit(int bars, int mode1, int price1, int mode2, int price2, int price3, int periodMA)
{
double ma; // moving average small and long value
double profit= 0; // profit of "trade" as difference between open and close
double totalProfit;
int i, gOpenTime, openid;
double d, open, price, min, max;
int s, signal;
signal= SIGNAL_NONE;
totalProfit= 0;
open= 0;
gTradeID= -1;
for(i=bars; i>=0; i--){
ma= iMA(NULL, NULL, periodMA, 0, mode1, price1, i);
s= signal;
price= (High[i]+Low[i])/2;
if(ma < price) s= SIGNAL_SHORT;
if(ma>price) s= SIGNAL_LONG;
// calc min/max
if(signal!=SIGNAL_NONE){
if(price<min)
min= price;
if(price>max)
max= price;
}
if(s!=signal){
if(gTradeID>=0){
gTradeMin[gTradeID]= min;
gTradeMax[gTradeID]= max;
gTradeEnd[gTradeID]= i;
gTradeCmd[gTradeID]= s;
}
gTradeID++;
gTradeOpen[gTradeID]= price;
gTradeStart[gTradeID]= i;
gTradeEnd[gTradeID]= 0;
min= 99999;
max= -9999;
profit= 0;
if(signal==SIGNAL_SHORT){
profit= open-price;
profit /= Point;
profit -= 规定点差;
totalProfit += profit;
}
if(signal==SIGNAL_LONG){
profit= price-open;
profit /= Point;
profit -= 规定点差;
totalProfit += profit;
}
gOpenTime= Time[i];
openid= i;
signal= s;
open= price;
}
}
if(signal!=SIGNAL_NONE){
profit= 0;
i= 0;
if(signal==SIGNAL_SHORT){
profit= open-Open[i];
profit /= Point;
profit -= 规定点差;
totalProfit += profit;
}
if(signal==SIGNAL_LONG) {
profit= Open[i]-open;
profit /= Point;
profit -= 规定点差;
totalProfit += profit;
}
}
return(totalProfit);
}
//---------------------------------------+
void Optimize()
{
if(bOptimize) {
if(bOptimizeIntersect)
OptimizeIntersects();
else
OptimizeAll();
}
else
SetText("message", "Optimizing is disabled");
}
//---------------------------------------+
void OptimizeIntersects()
{
int i, s, intersects, minInterSects;
double ma;
minInterSects= 9999;
for(s= MinMA; s<= MaxMA;s+= StepMA){
intersects= 0;
for(i= CountOptimize;i>=0;i--){
ma= iMA(NULL, NULL, s, 0, Method, PRICE_MEDIAN, i);
if(ma>=Low[i] && ma <= High[i])
intersects++;}
if(intersects < minInterSects){
minInterSects= intersects;
PeriodMA= s;
}
}
SetText("message", "BestMA= "+PeriodMA+" Intersects= "+minInterSects);
}
//---------------------------------------+
void OptimizeAll()
{
double profit, maxProfit;
int s, l, a;
int bestMA;
bestMA= 0;
maxProfit= -9999;
for(s= MinMA; s<=MaxMA; s+=StepMA){
//a= s*130;
//a /= 100;
profit= CalcProfit(CountOptimize, Method, PRICE_MEDIAN, Method, PRICE_HIGH, PRICE_LOW, s);
if(profit>maxProfit && profit!=0){
bestMA= s;
maxProfit= profit;
}
}
Method1= Method;
Method2= Method;
Price1= PRICE_MEDIAN;
Price2= PRICE_HIGH;
Price3= PRICE_LOW;
PeriodMA= bestMA;
SetText("message", "BestMA= "+PeriodMA+" Profit= "+maxProfit);
SendVars();
}
//---------------------------------------+
void CalcDays()
{
int 日期;
int i;
日期= TimeDay(Time[0]);
for(i=0; i<Bars; i++){
if(TimeDay(Time[i])!=日期){
today= i-1;
break;
}
}
//--when starts yesterday
日期= TimeDay(Time[today+1]);
for(i=today+1; i<Bars; i++){
if(TimeDay(Time[i])!=日期){
yesterday= i-1;
break;
}
}
}
//---------------------------------------+
void DrawTriangle(int objid, int signal, datetime opentime, double openprice, datetime timenow, double pricenow, double profit)
{
string name;
int i;
if(DrawTringles){
//Print("signal= ", Sig2Str(signal), " open= ", openprice, " close= ", pricenow, " profit= ", profit);
name= "profit"+objid;
ObjectCreate(name, OBJ_TRIANGLE, 0, opentime, openprice, timenow, openprice, timenow, pricenow);
if(signal==SIGNAL_SHORT)
ObjectSet(name, OBJPROP_COLOR, ColorShortTrade);
else
ObjectSet(name, OBJPROP_COLOR, ColorLongTrade);
ObjectSet(name, OBJPROP_BACK, false);
if(profit<0){
ObjectSet(name, OBJPROP_WIDTH, 1);
}
else
ObjectSet(name, OBJPROP_WIDTH, 3);
}
name= "win"+objid;
i= profit;
ObjectCreate(name, OBJ_TEXT, 0, timenow, pricenow-20*Point);
ObjectSetText(name, i+" Pips", 8, "Tahoma", White);
}
//---------------------------------------+
void DeleteObjects()
{
int i;
string name;
for(i=0; i<500; i++){
name= "profit"+i;
ObjectDelete(name);
name= "win"+i;
ObjectDelete(name);
}
/*
for(i= 0;i<CountOptimize;i++)
{
name= "mas"+Time[i];
ObjectDelete(name);
name= "masO"+Time[i];
ObjectDelete(name);
name= "masC"+Time[i];
ObjectDelete(name);
name= "mal"+Time[i];
ObjectDelete(name);
}
*/
MaxObj= 0;
}
//---------------------------------------+
void makelabel(string lblname, int x, int y, string txt, color txtcolor)
{
ObjectCreate(lblname, OBJ_LABEL, 0, 0, 0);
ObjectSet(lblname, OBJPROP_CORNER, 0);
ObjectSetText(lblname, txt, 8, "Verdana", txtcolor);
ObjectSet(lblname, OBJPROP_XDISTANCE, x);
ObjectSet(lblname, OBJPROP_YDISTANCE, y);
}
//---------------------------------------+
void SetText(string name, string txt)
{
ObjectSetText(name, txt, 7, "Verdana", White);
}
//---------------------------------------+
void Rectangle(string name, datetime time, double price, int col)
{
string name1;
name1= name + Time[time];
ObjectCreate(name1, OBJ_RECTANGLE, 0, Time[time], price, Time[time+1], price+Point/2);
ObjectSet(name1, OBJPROP_COLOR, col);
}
//---------------------------------------+
void Rectangle2(string name, datetime time, datetime time2, double price, int col)
{
string name1;
name1= name + time;
ObjectCreate(name1, OBJ_RECTANGLE, 0, time, price, time2, price+Point/2);
ObjectSet(name1, OBJPROP_COLOR, col);
}
// creates a speakable text about other symbols
//---------------------------------------+
string Sig2Str(int signal)
{
string r= "undef";
switch(signal){
case SIGNAL_NONE: r= "none"; break;
case SIGNAL_LONG: r= "long"; break;
case SIGNAL_SHORT: r= "short"; break;
}
return(r);
}
//---------------------------------------+
相关推荐
总的来说,MT4的变色均线是一种强大的交易工具,它通过视觉提示帮助交易者快速识别市场趋势,但同时也要求使用者具备一定的市场理解和风险管理能力。熟练掌握并灵活运用这种指标,可以显著提升交易效率,但不应忽视...
均线之花 sarlery:均线以及交易系统 在技术分析中,均线是一种非常重要的工具,能够帮助投资者判断股票的走势和趋势。本文将详细介绍均线的两两组合使用情况,包括不同的周期组合和具体的应用方法。 一、均线的...
文华财经指标公式源码均线指标变色均线.doc 文华财经指标公式源码均线指标变色均线.doc是财经指标公式的源码,用于生成均线指标,并对其进行变色处理。本节将详细解释标题、描述、标签和部分内容中的知识点。 标题...
在金融交易领域,技术分析是投资者常用的一种策略,其中,均线指标是不可或缺的工具之一。本文将详细讨论“144均线”以及其在MT4(MetaTrader 4)平台上的应用,结合“经典1小时-144均线数值”这一主题,探讨这一...
均线交差报警指标,均线交差报警指标均线交差报警指标均线交差报警指标均线交差报警指标
均线EA单均线,K线与均线金叉做多,死叉做空,自由设定参数。
标题中的“外汇EA_mt4双均线交易_mt4双均线ea_外汇EA”指的是基于MetaTrader 4(MT4)交易平台的自动交易系统,通常被称为Expert Advisor(EA)。这个EA是设计用于外汇市场的交易策略,其核心是基于双移动平均线(MA...
标题与描述中提到的“MT4编程实例...总之,MT4编程实例:会变色的均线,不仅是对技术分析工具的一次创新应用,也是对MT4平台编程能力的深度挖掘。它不仅增强了交易决策的直观性,还为交易者提供了灵活的策略实施框架。
通过K线可以观察到市场参与者的行为特征。 #### 三、基于单均线的波段划分方法 - **波段定义**:本研究中的波段是指价格围绕均线波动所形成的上升或下降区间。波段的起点和终点分别由价格突破均线的方向性变化来...
国泰君安发布的《价格走势观察之基于均线的分段方法》专题报告,深入探讨了一种新颖的市场分析工具——基于均线的价格分段方法。这种方法旨在通过分析价格与均线之间的关系来识别市场的波动规律,从而帮助投资者更好...
均线技术兵法是股市分析的重要工具之一,通过对不同周期均线的观察以及背离现象的理解,可以帮助投资者更好地把握市场动态,及时作出决策。然而,需要注意的是,在某些特殊市场状态下,均线背离技术可能不适用,投资...
均线EA单均线,K线与均线金叉做多,死叉做空,自由设定参数。
MAXMA和MINMA可以帮助我们更直观地观察这种排列。 - K线颜色变化:变色K线提供了一种视觉提示,帮助投资者迅速把握市场的多空力量对比。例如,连续出现洋红色K线可能预示着强烈的上升势头,而连续的绿色K线可能意味...
在两条均线指标中,需要绘制图形以便于观察价格走势。该文档提供了图形绘制的公式: JJ:A>B&&C>B&&HHV(H,BARSLAST(A>B)),NODRAW; JJ,ICON(1,'ICO1'); 其中,ICON 函数用于绘制图形。 JJ 变量用于检测价格走势的...
均线交易系统,特别是基于MA(移动平均线)的策略,是金融投资领域中常见的技术分析工具,尤其在股票、期货和外汇市场中广泛使用。移动平均线是通过计算一定时间段内的收盘价平均值来平滑价格波动,揭示出趋势的方向...
变色均线则是在传统均线基础上进行了优化,当价格穿越均线时,均线颜色会发生变化,从而直观地提醒交易者可能的趋势转变。例如,当短期均线上穿长期均线形成金叉时,显示为买入信号,反之,形成死叉时,可能预示着卖...
在众多的指标公式中,通达信指标公式源码中的“黑牛均线”因其独到的分析视角和实用性,成为了交易者们青睐的工具。 首先,让我们深入了解“黑牛均线”指标公式的构成。这个指标综合运用了不同周期的移动平均线,如...
均线理论是股票、期货和外汇市场中广泛应用的技术分析工具,主要通过观察不同时间段价格平均值形成的趋势线来预测价格走势。以下是对均线理论的详细解释: 均线,又称移动平均线,是将一段时间内的收盘价、开盘价、...
均线交叉自动交易是一种基于技术分析策略的自动化交易系统,尤其适用于MetaTrader 4(MT4)平台。在金融市场上,投资者通常使用移动平均线(Moving Average,简称MA)来识别趋势变化和潜在的买卖信号。当短期MA从...
非常好用的一组变色顾比均线组,趋势一目了然,适用于MT4平台