Welcome to our artificial intelligence and machine learning community! We conduct research and development in the field of neural networks. Here you can discuss current topics, share your ideas, find like-minded people and establish professional contacts. We are open to scientists, developers, students and anyone interested in the latest advances in AI. Join our community to stay up to date with the latest trends and discoveries! room1 room2 room3 |
MarketTraderOptimizer.cpp | Tester.cpp |
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <Mmsystem.h>
#include <process.h>
#pragma comment(lib, "Winmm.lib")
#define MODE_TRADES 0
#define MODE_HISTORY 1
#define OP_BUY 0
#define OP_SELL 1
#define OP_BUYLIMIT 2
#define OP_SELLLIMIT 3
#define OP_BUYSTOP 4
#define OP_SELLSTOP 5
#define OP_SL 0
#define OP_TP 1
#define OP_CLOSE 2
#define OP_DELETE 3
#define PRICE_CLOSE 0
#define PRICE_OPEN 1
#define PRICE_HIGH 2
#define PRICE_LOW 3
#define PRICE_MEDIAN 4
#define PRICE_TYPICAL 5
#define PRICE_WEIGHTED 6
int bars,ibars;
int tfdepth=5;
char* pathCONFIG;
char* pathHST;
int randcycles=75;
struct mdata{
long int ctm[50000];
double open[50000];
double low[50000];
double high[50000];
double close[50000];
double volume[50000];
int spread[50000];
};
struct mextremums{
int low[5000];
int high[5000];
int barscnt;
int midprofit;
};
mdata* testermetadata;
mextremums* extremums;
struct tresults{
int profitcntorders;
int profitcntordersbuy;
int profitcntorderssell;
int period_ma_fast;
int period_ma_slow;
int cci_period;
int period_ma_fast2;
int period_ma_slow2;
int cci_period2;
int profitcntpointsbuy;
int profitcntpointssell;
int notprofitcntorders;
int notprofitcntorderssell;
int stoplossbuy;
int stoplosssell;
};
int testercurbar,timeshift;
int randbytes[128][257];
char config[90][150][15000];int cindex=0;
char t1config[15000][15000];
char t1config2[15000];
inline int rdtsc(){__asm__ __volatile__("rdtsc");}
void initrandbytes(){
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
int cpucount=sysinfo.dwNumberOfProcessors;
int h=0,i1;
for(i1=0;i1<cpucount;i1++){
h=0;
while(h==0){
for(int z1=0;z1<51;z1++){for(int z=0;z<256;z++)
{randbytes[i1][z]^=((rand()<<1) % 256)&255;}}
for(int z1=0;z1<5;z1++)for(int z=0;z<256;z++)
randbytes[i1][z]^=(randbytes[i1][(z+1)%256]>>1)&255;
for(int z=0;z<256;z++)if (randbytes[i1][z]>127)h=1;
}
}
randbytes[i1][256]=0;
}
int rand(int min, int max, int cpuid){
int h=randbytes[cpuid][randbytes[cpuid][256]];
if(h<min)h+=min;
while(h>max)h=h%max;
if(h<min)h+=min;
randbytes[cpuid][randbytes[cpuid][256]]^=
randbytes[cpuid][(randbytes[cpuid][256]+1)%256];
randbytes[cpuid][256]=(randbytes[cpuid][256]+1)%256;
return h;
}
inline char* intToStr(int i){
static char strt[18]="";memset(strt,0,18);
snprintf(strt, 17, "%ld", i);
return (char *)strt;
}
inline int strToInt(const char *s){
char *endChar;
setlocale(LC_NUMERIC, "C");
int t = strtol(s, &endChar, 10);
if(*endChar!='\0')return -1;
return t;
}
inline char* doubleToStr(double d,int precsion) {
static char str[18]="";
memset(str,0,18);
snprintf(str, 12, "%.*f", precsion,d);
return (char *)str;
}
/* void lstrcat(char *buff,const char *past)
{
int len=0,len0,len1;
len0=strlen(buff);
len1=strlen(past);
memcpy(buff+len0,past,len1);memset(buff+len0+len1,0,1);
} */
inline char* timeToStr(const time_t st) {
static char strtime1[28]="";
tm stm1;
time_t st1;
st1=time(0);
memset(strtime1,0,28);
memset(&stm1,0,sizeof(struct tm));
if(st==0)
stm1=*localtime((const time_t *)&st1);
else
stm1=*localtime((const time_t *)&st);
strftime(strtime1,27,"%d.%m.%Y %H:%M:%S ",&stm1);
return (char *)strtime1;
}
inline char* gmtimeToStr(const time_t st) {
static char gmstrtime[28]="";
tm stm1;
time_t st1;
st1=time(0);
memset(&gmstrtime,0,28);
memset(&stm1,0,sizeof(struct tm));
if(st==0)
stm1=*gmtime((const time_t *)&st1);
else
stm1=*gmtime((const time_t *)&st);
strftime(gmstrtime,27,"%d.%m.%Y %H:%M ",&stm1);
return (char *)gmstrtime;
}
int time1(const time_t st) {
tm stm1;
time_t st1;
st1=time(0);
memset(&stm1,0,sizeof(struct tm));
if(st==0)
stm1=*localtime((const time_t *)&st1);
else
stm1=*localtime((const time_t *)&st);
return (int)mktime(&stm1);
}
/* typedef BOOL (WINAPI *LPFN_GLPI)(
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION,
PDWORD);
DWORD CountSetBits(ULONG_PTR bitMask)
{
DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1;
DWORD bitSetCount = 0;
ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT;
DWORD i;
for (i = 0; i <= LSHIFT; ++i)
{
bitSetCount += ((bitMask & bitTest)?1:0);
bitTest/=2;
}
return bitSetCount;
}
int tsocketscpucnt ()
{
LPFN_GLPI glpi;
BOOL done = FALSE;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
DWORD returnLength = 0;
DWORD logicalProcessorCount = 0;
DWORD numaNodeCount = 0;
DWORD processorCoreCount = 0;
DWORD processorL1CacheCount = 0;
DWORD processorL2CacheCount = 0;
DWORD processorL3CacheCount = 0;
DWORD processorPackageCount = 0;
DWORD byteOffset = 0;
PCACHE_DESCRIPTOR Cache;
glpi = (LPFN_GLPI) GetProcAddress(
GetModuleHandle(TEXT("kernel32")),
"GetLogicalProcessorInformation");
if (NULL == glpi)
{
//_tprintf(TEXT("\nGetLogicalProcessorInformation is not supported.\n"));
return (1);
}
while (!done)
{
DWORD rc = glpi(buffer, &returnLength);
if (FALSE == rc)
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
if (buffer)
free(buffer);
buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(
returnLength);
if (NULL == buffer)
{
//_tprintf(TEXT("\nError: Allocation failure\n"));
return (2);
}
}
else
{
//_tprintf(TEXT("\nError %d\n"), GetLastError());
return (3);
}
}
else
{
done = TRUE;
}
}
ptr = buffer;
while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength)
{
switch (ptr->Relationship)
{
case RelationNumaNode:
// Non-NUMA systems report a single record of this type.
numaNodeCount++;
break;
case RelationProcessorCore:
processorCoreCount++;
// A hyperthreaded core supplies more than one logical processor.
logicalProcessorCount += CountSetBits(ptr->ProcessorMask);
break;
case RelationCache:
// Cache data is in ptr->Cache, one CACHE_DESCRIPTOR structure for each cache.
Cache = &ptr->Cache;
if (Cache->Level == 1)
{
processorL1CacheCount++;
}
else if (Cache->Level == 2)
{
processorL2CacheCount++;
}
else if (Cache->Level == 3)
{
processorL3CacheCount++;
}
break;
case RelationProcessorPackage:
// Logical processors share a physical package.
processorPackageCount++;
break;
default:
break;
}
byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
ptr++;
}
free(buffer);
return processorPackageCount;
} */
double sma(const int period, const int price, const int shift,int tcurbar){
double sum=0.0,tmp;
if(price==PRICE_CLOSE){
for(int i=0;i<period;i++)
{
sum+=testermetadata->close[tcurbar-(i+shift)];
}
tmp=sum;tmp/=period;
return(tmp);
}else
if(price==PRICE_MEDIAN){
for(int i=0;i<period;i++)
{
tmp=testermetadata->high[tcurbar-(i+shift)];
tmp+=testermetadata->low[tcurbar-(i+shift)];
tmp*=0.5;
sum+=tmp;
}
tmp=sum;tmp/=period;
return(tmp);
}else
if(price==PRICE_TYPICAL){
for(int i=0;i<period;i++)
{
tmp=testermetadata->high[tcurbar-(i+shift)];
tmp+=testermetadata->low[tcurbar-(i+shift)];
tmp+=testermetadata->close[tcurbar-(i+shift)];
tmp/=3.0;
sum+=tmp;
}
tmp=sum;tmp/=period;
return(tmp);
}
}
double cci(const int period, const int shift, int tcurbar ){
double price,sum,mul,CCIBuffer,RelBuffer,DevBuffer,MovBuffer;
int k;
MovBuffer = sma(period, PRICE_TYPICAL, shift,tcurbar);
mul = 0.015/period;
sum = 0.0;
k = period-1;
//k = ((int)(k/2))*2;
while(k>=0)
{
price=(testermetadata->high[tcurbar-(k+shift)]+
testermetadata->low[tcurbar-(k+shift)]+
testermetadata->close[tcurbar-(k+shift)])/3.0;
sum+=fabs(price-MovBuffer);
k--;
}
DevBuffer = sum*mul;
price =(testermetadata->high[tcurbar-(shift)]+
testermetadata->low[tcurbar-(shift)]+testermetadata->close[tcurbar-(shift)])/3.0;
RelBuffer=price-MovBuffer;
if(DevBuffer==0.0)CCIBuffer=0.0;else CCIBuffer = RelBuffer / DevBuffer;
return(CCIBuffer);
}
int iLowest(int count, int start){
double Low=99999999;int cLow=0;
for(int i = start;i > start-count;i--)
{
if(testermetadata->low[i]<Low){
Low=testermetadata->low[i];cLow=i;}
}
return(cLow);
}
int iHighest(int count, int start){
double High=-99999999;int cHigh=0;
for(int i = start;i > start-count;i--)
{
if(testermetadata->high[i]>High){
High=testermetadata->high[i];cHigh=i;}
}
return(cHigh);
}
int min(int a, int b){
if(a<b)return a;
if(a>b)return b;
return a;
}
int max(int a, int b){
if(a>b)return a;
if(a<b)return b;
return a;
}
double loadHST(const char* symbol,const char* tf){
FILE *hFile;
char* membuf = new char[2];
membuf = (char*)realloc(membuf,bars*60);
char* path = new char[2];
path = (char*)realloc(path,500);memset(path,0,500);
lstrcat(path,pathHST);lstrcat(path,symbol);lstrcat(path,tf);lstrcat(path,".hst");
int i,i1=0,testerdigits;double point;
for(i=0;i<50000;i++){
testermetadata->ctm[i1]=0;
testermetadata->open[i1]=0.0;
testermetadata->high[i1]=0.0;
testermetadata->low[i1]=0.0;
testermetadata->close[i1]=0.0;
testermetadata->volume[i1]=0;
testermetadata->spread[i1]=0;
}
hFile = fopen(path, "rb");
if(!(!hFile)){
fseek(hFile,0,SEEK_END);
int dwFileSize = ftell(hFile);
//int bars=(dwFileSize-148)/60;if(bars>=(ibars))bars=ibars;
if(dwFileSize>=148+60){
i=0;
fseek(hFile,dwFileSize-60*bars-60*timeshift,SEEK_SET);
fread(membuf,60*bars,1,hFile);
while(i1<bars){
memcpy(&testermetadata->ctm[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->open[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->high[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->low[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->close[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->volume[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->spread[i1],&membuf[i],4);i+=12;
i1++;
}
}
fseek(hFile,84,SEEK_SET);
fread(&testerdigits,4,1,hFile);
fclose(hFile);
}
delete[] path;
delete[] membuf;
point = 1.0 / (int) pow((double)10, testerdigits);
return point;
}
double icci(int shift, int period_ma_fast, int period_ma_slow, int cci_period,int tcurbar){
double ma_fast,ma_slow;
int i1;
ma_fast=ma_slow=cci(cci_period,shift,tcurbar)+10000.0;
for(i1=1; i1<period_ma_slow; i1++)
ma_slow=ma_slow+cci(cci_period,i1+shift,tcurbar)+10000.0;
ma_slow=ma_slow/period_ma_slow;
for(i1=1; i1<period_ma_fast; i1++)
ma_fast=ma_fast+cci(cci_period,i1+shift,tcurbar)+10000.0;
ma_fast=ma_fast/period_ma_fast;
return (ma_fast-ma_slow);
}
int DeltaMasLength(int period_ma_fast, int period_ma_slow, int cci_period,int tcurbar){
double tmp1,tmp2,tmp3,prevtmp1=icci(2,period_ma_fast, period_ma_slow, cci_period,tcurbar);
tmp3=prevtmp1;
double tmp3_2=fabs(icci(10,period_ma_fast, period_ma_slow, cci_period,tcurbar));
double tmp3_3=fabs(icci(18,period_ma_fast, period_ma_slow, cci_period,tcurbar));
if(tmp3<0)tmp2=-1;else tmp2=1;
prevtmp1=tmp3=fabs(tmp3);
if((tmp3>(tmp3_2*1.2))&&(tmp3>(tmp3_3*1.3))){
double tmp4=fabs(icci(0,period_ma_fast, period_ma_slow, cci_period,tcurbar));
double tmp5=fabs(icci(1,period_ma_fast, period_ma_slow, cci_period,tcurbar));
if(tmp4<=tmp5)
if(tmp3>tmp5){
int i1;
for(i1=3;i1<13;i1++){
tmp1=fabs(icci(i1,period_ma_fast, period_ma_slow, cci_period,tcurbar));
if(prevtmp1<tmp1) return (i1*tmp2);
prevtmp1=tmp1;
}
return (i1*tmp2);
}
}
return 0;
}
void testerstartb(int tf, double point, int ctimeout, int period_ma_fast, int period_ma_slow,
int cci_period, int spr, tresults &result){
int openorder=-1,openorderclosed=1,timeout=16,timeout2=0,p=0;//(int)(ctimeout/tf/60/2);
int profitcntpointsbuy=0,profitcntpointssell=0,profitcntorders=0,notprofitcntorders=0;
int profitcntordersbuy=0, profitcntorderssell=0;
double openorderprice;
int tcurbar;
int slb=0,sls=0;
for(int i=250;i<bars;i++){
timeout2=i-p;
if((openorder>=0)&&(openorderclosed==0)){
int profitorderb =
(int)((testermetadata->high[i]-openorderprice)/point);
if((profitorderb>0)&&(openorder==OP_BUY)){
profitcntordersbuy++;
profitcntpointsbuy+=
(testermetadata->high[iHighest(timeout2,i)]-openorderprice)/point;
slb=max(slb,abs((testermetadata->low[iLowest(timeout2,i)]-
openorderprice)/point));
}
else {notprofitcntorders=99999;goto FFF1;}//notprofitcntorders++;}//
openorderclosed=1;
}
if(openorderclosed==1){
if((openorder==OP_BUY)||(openorder==-1) ){
tcurbar=i;
int signal = DeltaMasLength(period_ma_fast, period_ma_slow,
cci_period,tcurbar);
if((abs(signal)>11) && (signal<0)){
openorder=OP_BUY;
openorderclosed=0;
openorderprice=testermetadata->open[tcurbar]-point*spr;
p=i;
i+=timeout;
}
}
}
}
if(profitcntordersbuy>0){
profitcntpointsbuy = (int)(profitcntpointsbuy/profitcntordersbuy);
slb++;// = (int)(slb/profitcntordersbuy);
}
else profitcntordersbuy=0;
if((slb<extremums->midprofit)||(profitcntpointsbuy<extremums->midprofit))
{result.profitcntorders=0;return;}
//if(profitcntpointsbuy<slb*3)notprofitcntorders=9999;
//if(profitcntordersbuy<4)notprofitcntorders=9999;
FFF1:
result.profitcntpointsbuy=profitcntpointsbuy;
//result.profitcntpointssell=profitcntpointssell;
result.period_ma_fast=period_ma_fast;
result.period_ma_slow=period_ma_slow;
result.cci_period=cci_period;
result.profitcntorders=profitcntordersbuy;
result.profitcntordersbuy=profitcntordersbuy;
//result.profitcntorderssell=profitcntorderssell;
result.notprofitcntorders=notprofitcntorders;
result.stoplossbuy=slb;
//result.stoplosssell=sls;
return;
}
void testerstarts(int tf, double point, int ctimeout, int period_ma_fast,
int period_ma_slow, int cci_period, int spr, tresults &result){
int openorder=-1,openorderclosed=1,timeout=16,timeout2=0,p=0;
//(int)(ctimeout/tf/60/2);
int profitcntpointsbuy=0,profitcntpointssell=0,profitcntorders=0,
notprofitcntorders=0,notprofitcntorderssell=0;
int profitcntordersbuy=0, profitcntorderssell=0;
double openorderprice;
int tcurbar;
int slb=0,sls=0;
for(int i=250;i<bars;i++){
timeout2=i-p;
if((openorder>=0)&&(openorderclosed==0)){
int profitorders =
(int)((openorderprice-testermetadata->low[i])/point);
if((profitorders>0)&&(openorder==OP_SELL)){
profitcntorderssell++;
profitcntpointssell+=
abs((testermetadata->low[iLowest(timeout2,i)]-
openorderprice)/point);
sls=max(sls,abs((testermetadata->high[iHighest(timeout2,i)]-
openorderprice)/point));
}else {notprofitcntorderssell=99999;goto FFF2;}
//notprofitcntorderssell++;}//
openorderclosed=1;
}
if(openorderclosed==1){
if((openorder==OP_SELL)||(openorder==-1)){
tcurbar=i;
int signal = DeltaMasLength(period_ma_fast, period_ma_slow,
cci_period,tcurbar);
if((abs(signal)>11) && (signal>0)){
openorder=OP_SELL;
openorderclosed=0;
openorderprice=testermetadata->open[tcurbar]+point*spr;
p=i;
i+=timeout;
}
}
}
}
if(profitcntorderssell>0){
profitcntpointssell=(int)(profitcntpointssell/profitcntorderssell);
sls++;//=(int)(sls/profitcntorderssell);
}
else profitcntpointssell=0;
if((sls<extremums->midprofit)||(profitcntpointssell<extremums->midprofit))
{result.profitcntorderssell=0;return;}
//if(profitcntpointssell<sls*3)notprofitcntorderssell=9999;
//if(profitcntorderssell<4)notprofitcntorderssell=9999;
FFF2:
//result.profitcntpointsbuy=profitcntpointsbuy;
result.profitcntpointssell=profitcntpointssell;
result.period_ma_fast2=period_ma_fast;
result.period_ma_slow2=period_ma_slow;
result.cci_period2=cci_period;
//result.profitcntorders=profitcntordersbuy+profitcntorderssell;
//result.profitcntordersbuy=profitcntordersbuy;
result.profitcntorderssell=profitcntorderssell;
result.notprofitcntorderssell=notprofitcntorderssell;
//result.stoplossbuy=slb;
result.stoplosssell=sls;
return;
}
struct btresults{
int profitcntordersbuy[15];
int period_ma_fast[15];
int period_ma_slow[15];
int cci_period[15];
int profitcntpointsbuy[15];
int notprofitcntorders[15];
int stoplossbuy[15];
};
struct stresults{
int profitcntorderssell[15];
int period_ma_fast2[15];
int period_ma_slow2[15];
int cci_period2[15];
int profitcntpointssell[15];
int notprofitcntorderssell[15];
int stoplosssell[15];
};
struct tthread{
HANDLE handle;
bool handleclosed;
DWORD threadid;
int id;
int tf;
int timeout;
double point;
int spr;
int itr;
int randcycles;
bool done;
btresults bresult;
stresults sresult;
tresults tmpresults;
};
int treadcount;
DWORD WINAPI myThread(LPVOID lpParameter){
tthread& thread = *((tthread*)lpParameter);
int profitcntpoints=0,profitcntorders=0,notprofitcntorders=0,
notprofitcntorderssell=0,period_ma_fast=0,period_ma_slow=0,cci_period=0,
period_ma_fast2=0,period_ma_slow2=0,cci_period2=0;
int profitcntpointsbuy=0,profitcntpointssell=0,profitcntordersbuy=0,
profitcntorderssell=0;
int sls=0,slb=0;
tresults& testerresult = thread.tmpresults;
btresults& bresult = thread.bresult;
stresults& sresult = thread.sresult;
for(int i=0;i<15;i++){
bresult.profitcntordersbuy[i]=0;
bresult.period_ma_fast[i]=0;
bresult.period_ma_slow[i]=0;
bresult.cci_period[i]=0;
bresult.profitcntpointsbuy[i]=0;
bresult.notprofitcntorders[i]=0;
bresult.stoplossbuy[i]=0;
sresult.profitcntorderssell[i]=0;
sresult.period_ma_fast2[i]=0;
sresult.period_ma_slow2[i]=0;
sresult.cci_period2[i]=0;
sresult.profitcntpointssell[i]=0;
sresult.notprofitcntorderssell[i]=0;
sresult.stoplosssell[i]=0;
}
thread.itr=0;
int tf=thread.tf;int timeout=thread.timeout;
int t1=time1(NULL);
while((time1(NULL)-t1)<30){
//for(int i=0;i<thread.randcycles;i++){
int t1=2,t2=1, t3=0;
while(t1>=t2){t1=rand(6,33,thread.id);t2=rand(55,111,thread.id);
t3=rand(33,111,thread.id);}
t1=t2-t1;
testerstartb(tf,thread.point,timeout,t1,t2,t3,thread.spr,testerresult);
if(testerresult.profitcntorders>4)
if(testerresult.profitcntorders>(4*testerresult.notprofitcntorders))
if((testerresult.profitcntorders-testerresult.notprofitcntorders)>
(profitcntorders-notprofitcntorders))
// if(testerresult.profitcntorders>profitcntorders)
//if((testerresult.notprofitcntorders*8)<testerresult.profitcntorders)
// if(testerresult.notprofitcntorders<1)
{
profitcntpointsbuy = testerresult.profitcntpointsbuy;
period_ma_fast = testerresult.period_ma_fast;
period_ma_slow = testerresult.period_ma_slow;
cci_period = testerresult.cci_period;
profitcntorders = testerresult.profitcntorders;
profitcntordersbuy = testerresult.profitcntordersbuy;
notprofitcntorders = testerresult.notprofitcntorders;
slb = testerresult.stoplossbuy;
for(int i=14;i>0;i--){
bresult.profitcntordersbuy[i]=bresult.profitcntordersbuy[i-1];
bresult.period_ma_fast[i]=bresult.period_ma_fast[i-1];
bresult.period_ma_slow[i]=bresult.period_ma_slow[i-1];
bresult.cci_period[i]=bresult.cci_period[i-1];
bresult.profitcntpointsbuy[i]=bresult.profitcntpointsbuy[i-1];
bresult.notprofitcntorders[i]=bresult.notprofitcntorders[i-1];
bresult.stoplossbuy[i]=bresult.stoplossbuy[i-1];
}
bresult.profitcntordersbuy[0]=profitcntordersbuy;
bresult.period_ma_fast[0]=period_ma_fast;
bresult.period_ma_slow[0]=period_ma_slow;
bresult.cci_period[0]=cci_period;
bresult.profitcntpointsbuy[0]=profitcntpointsbuy;
bresult.notprofitcntorders[0]=notprofitcntorders;
bresult.stoplossbuy[0]=slb;
}
thread.itr++;
}
t1=time1(NULL);
while((time1(NULL)-t1)<30){
//for(int i=0;i<thread.randcycles;i++){
int t1=2,t2=1, t3=0;
while(t1>=t2){t1=rand(6,33,thread.id);t2=rand(55,111,thread.id);
t3=rand(33,111,thread.id);}
t1=t2-t1;
testerstarts(tf,thread.point,timeout,t1,t2,t3,thread.spr,testerresult);
if(testerresult.profitcntorderssell>4)
if(testerresult.profitcntorderssell>(4*testerresult.notprofitcntorderssell))
if((testerresult.profitcntorderssell-testerresult.notprofitcntorderssell)>
(profitcntorderssell-notprofitcntorderssell))
// if(testerresult.profitcntorderssell>profitcntorderssell)
//if((testerresult.notprofitcntorderssell*8)<testerresult.profitcntorderssell)
// if(testerresult.notprofitcntorderssell<1)
{
profitcntpointssell = testerresult.profitcntpointssell;
period_ma_fast2 = testerresult.period_ma_fast2;
period_ma_slow2 = testerresult.period_ma_slow2;
cci_period2 = testerresult.cci_period2;
profitcntorderssell = testerresult.profitcntorderssell;
notprofitcntorderssell = testerresult.notprofitcntorderssell;
sls = testerresult.stoplosssell;
for(int i=14;i>0;i--){
sresult.profitcntorderssell[i]=sresult.profitcntorderssell[i-1];
sresult.period_ma_fast2[i]=sresult.period_ma_fast2[i-1];
sresult.period_ma_slow2[i]=sresult.period_ma_slow2[i-1];
sresult.cci_period2[i]=sresult.cci_period2[i-1];
sresult.profitcntpointssell[i]=sresult.profitcntpointssell[i-1];
sresult.notprofitcntorderssell[i]=sresult.notprofitcntorderssell[i-1];
sresult.stoplosssell[i]=sresult.stoplosssell[i-1];
}
sresult.profitcntorderssell[0]=profitcntorderssell;
sresult.period_ma_fast2[0]=period_ma_fast2;
sresult.period_ma_slow2[0]=period_ma_slow2;
sresult.cci_period2[0]=cci_period2;
sresult.profitcntpointssell[0]=profitcntpointssell;
sresult.notprofitcntorderssell[0]=notprofitcntorderssell;
sresult.stoplosssell[0]=sls;
}
thread.itr++;
}
thread.done = true;
return 0;
}
const char* testertest(const char* ctf,double point, const char* ctimeout,int spr) {
static char itemconfig[5000]="";
memset(itemconfig,0,5000);
int tf=strToInt(ctf);int timeout=strToInt(ctimeout);
int tpb,tps;
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
treadcount=6;//sysinfo.dwNumberOfProcessors - 1;//*tsocketscpucnt();
tthread threads[treadcount];
for(int i=0;i<treadcount;i++){
threads[i].done = false;
threads[i].id = i;
threads[i].tf = tf;
threads[i].timeout = timeout;
threads[i].point = point;
threads[i].spr = spr;
threads[i].itr = 0;
threads[i].randcycles = randcycles;
threads[i].handleclosed = false;
threads[i].handle=
CreateThread(0, 0, myThread, &threads[i], 0, &threads[i].threadid);
}
bool thredsdone=false;
while(!thredsdone){
thredsdone=true;
for(int i=0;i<treadcount;i++){
if(threads[i].done == false)thredsdone=false;
else {
if(!threads[i].handleclosed){
CloseHandle(threads[i].handle);
threads[i].handleclosed = true;
}
}
}
SleepEx(100,true);
}
int profitcntpoints=0,profitcntorders=0,notprofitcntorders=0,period_ma_fast=0,
period_ma_slow=0,cci_period=0,period_ma_fast2=0,period_ma_slow2=0,cci_period2=0;
int profitcntpointsbuy=0,profitcntpointssell=0,
profitcntordersbuy=0,profitcntorderssell=0;
int sls=0,slb=0,itrs=0;
btresults bresults;
stresults sresults;
btresults& bresult = bresults;
stresults& sresult = sresults;
for(int i=0;i<15;i++){
bresult.profitcntordersbuy[i]=0;
bresult.period_ma_fast[i]=0;
bresult.period_ma_slow[i]=0;
bresult.cci_period[i]=0;
bresult.profitcntpointsbuy[i]=0;
bresult.notprofitcntorders[i]=0;
bresult.stoplossbuy[i]=0;
sresult.profitcntorderssell[i]=0;
sresult.period_ma_fast2[i]=0;
sresult.period_ma_slow2[i]=0;
sresult.cci_period2[i]=0;
sresult.profitcntpointssell[i]=0;
sresult.notprofitcntorderssell[i]=0;
sresult.stoplosssell[i]=0;
}
int k=0,ti1=0,ti=0;
for(int i0=0;i0<15;i0++){
for(int i1=0;i1<treadcount;i1++){
for(int i=14;i>0;i--){
if(threads[i1].bresult.profitcntordersbuy[i]>0)
if((threads[i1].bresult.profitcntordersbuy[i]-
threads[i1].bresult.notprofitcntorders[i])>
(bresult.profitcntordersbuy[k]-bresult.notprofitcntorders[k])){
bresult.profitcntordersbuy[k]=
threads[i1].bresult.profitcntordersbuy[i];
bresult.period_ma_fast[k]=threads[i1].bresult.period_ma_fast[i];
bresult.period_ma_slow[k]=threads[i1].bresult.period_ma_slow[i];
bresult.cci_period[k]=threads[i1].bresult.cci_period[i];
bresult.profitcntpointsbuy[k]=
threads[i1].bresult.profitcntpointsbuy[i];
bresult.notprofitcntorders[k]=
threads[i1].bresult.notprofitcntorders[i];
bresult.stoplossbuy[k]=threads[i1].bresult.stoplossbuy[i];
ti1=i1;ti=i;
}
}
}
threads[ti1].bresult.profitcntordersbuy[ti]=0;
threads[ti1].bresult.period_ma_fast[ti]=0;
threads[ti1].bresult.period_ma_slow[ti]=0;
threads[ti1].bresult.cci_period[ti]=0;
threads[ti1].bresult.profitcntpointsbuy[ti]=0;
threads[ti1].bresult.notprofitcntorders[ti]=0;
threads[ti1].bresult.stoplossbuy[ti]=0;
k++;
}
k=0;ti1=0;ti=0;
for(int i0=0;i0<15;i0++){
for(int i1=0;i1<treadcount;i1++){
for(int i=14;i>0;i--){
if(threads[i1].sresult.profitcntorderssell[i]>0)
if((threads[i1].sresult.profitcntorderssell[i]-threads[i1].
sresult.notprofitcntorderssell[i])>
(sresult.profitcntorderssell[k]-sresult.notprofitcntorderssell[k])){
sresult.profitcntorderssell[k]=
threads[i1].sresult.profitcntorderssell[i];
sresult.period_ma_fast2[k]=threads[i1].sresult.period_ma_fast2[i];
sresult.period_ma_slow2[k]=threads[i1].sresult.period_ma_slow2[i];
sresult.cci_period2[k]=threads[i1].sresult.cci_period2[i];
sresult.profitcntpointssell[k]=
threads[i1].sresult.profitcntpointssell[i];
sresult.notprofitcntorderssell[k]=
threads[i1].sresult.notprofitcntorderssell[i];
sresult.stoplosssell[k]=threads[i1].sresult.stoplosssell[i];
ti1=i1;ti=i;
}
}
}
threads[ti1].sresult.profitcntorderssell[ti]=0;
threads[ti1].sresult.period_ma_fast2[ti]=0;
threads[ti1].sresult.period_ma_slow2[ti]=0;
threads[ti1].sresult.cci_period2[ti]=0;
threads[ti1].sresult.profitcntpointssell[ti]=0;
threads[ti1].sresult.notprofitcntorderssell[ti]=0;
threads[ti1].sresult.stoplosssell[ti]=0;
k++;
}
for(int i=0;i<treadcount;i++)itrs+=threads[i].itr;
int y=0;
for(k=0;k<1;k++)
if((bresult.period_ma_fast[k]!=0)||(sresult.period_ma_fast2[k]!=0))
{
tpb = 0;
tps = 0;
if(bresult.profitcntordersbuy[k]>0)tpb = (int)(bresult.profitcntpointsbuy[k]);
if(sresult.profitcntorderssell[k]>0)tps = (int)(sresult.profitcntpointssell[k]);
lstrcat(itemconfig,ctf);lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(bresult.period_ma_fast[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(bresult.period_ma_slow[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(bresult.cci_period[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(tps));lstrcat(itemconfig," ");
lstrcat(itemconfig,ctimeout);lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(spr));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(sresult.notprofitcntorderssell[k]+
bresult.notprofitcntorders[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(sresult.profitcntorderssell[k]+
bresult.profitcntordersbuy[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(sresult.stoplosssell[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(bresult.stoplossbuy[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(tpb));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(sresult.period_ma_fast2[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(sresult.period_ma_slow2[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(sresult.cci_period2[k]));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(itrs));lstrcat(itemconfig,"\r\n");
y++;
}
if(y==0){
lstrcat(itemconfig,ctf);lstrcat(itemconfig," 0 0 0 0 ");
lstrcat(itemconfig,ctimeout);lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(spr));lstrcat(itemconfig," 0 0 0 0 0 0 0 0 ");
lstrcat(itemconfig,intToStr(itrs));lstrcat(itemconfig,"\r\n");
}
itemconfig[strlen(itemconfig)-2]=0;
return (const char *)itemconfig;
}
double ExtZigzagBuffer[50000];
double ExtHighBuffer[50000];
double ExtLowBuffer[50000];
int InitializeAll(int InpDepth)
{
for(int i1=0;i1<bars;i1++){
ExtZigzagBuffer[i1]=0.0;
ExtHighBuffer[i1]=0.0;
ExtLowBuffer[i1]=0.0;
}
//--- first counting position
return(bars-InpDepth);
}
void ZigZag(double Point){
int i,limit,counterZ,whatlookfor=0;
int back,pos,lasthighpos=0,lastlowpos=0;
double extremum;
double curlow=0.0,curhigh=0.0,lasthigh=0.0,lastlow=0.0;
int InpDepth=12; // Depth
int InpDeviation=5; // Deviation
int InpBackstep=3; // Backstep
int ExtLevel=3;
limit=InitializeAll(InpDepth);
{
//--- find first extremum in the depth ExtLevel or 100 last bars
i=counterZ=0;
while(counterZ<ExtLevel && i<100)
{
if(ExtZigzagBuffer[bars-i]!=0.0)
counterZ++;
i++;
}
//--- no extremum found - recounting all from begin
if(counterZ==0)limit=InitializeAll(InpDepth);
else
{
//--- set start position to found extremum position
limit=i-1;
//--- what kind of extremum?
if(ExtLowBuffer[bars-i]!=0.0)
{
//--- low extremum
curlow=ExtLowBuffer[bars-i];
//--- will look for the next high extremum
whatlookfor=1;
}
else
{
//--- high extremum
curhigh=ExtHighBuffer[bars-i];
//--- will look for the next low extremum
whatlookfor=-1;
}
//--- clear the rest data
for(i=limit-1; i>=0; i--)
{
ExtZigzagBuffer[bars-i]=0.0;
ExtLowBuffer[bars-i]=0.0;
ExtHighBuffer[bars-i]=0.0;
}
}
}
//--- main loop
for(i=limit; i>=0; i--)
{
//--- find lowest low in depth of bars
extremum=testermetadata->low[bars-iLowest(InpDepth,i)];
//--- this lowest has been found previously
if(extremum==lastlow)
extremum=0.0;
else
{
//--- new last low
lastlow=extremum;
//--- discard extremum if current low is too high
if(testermetadata->low[bars-i]-extremum>InpDeviation*Point)
extremum=0.0;
else
{
//--- clear previous extremums in backstep bars
for(back=1; back<=InpBackstep; back++)
{
pos=i+back;
if(ExtLowBuffer[bars-pos]!=0 &&
ExtLowBuffer[bars-pos]>extremum)
ExtLowBuffer[bars-pos]=0.0;
}
}
}
//--- found extremum is current low
if(testermetadata->low[bars-i]==extremum)
ExtLowBuffer[bars-i]=extremum;
else
ExtLowBuffer[bars-i]=0.0;
//--- find highest high in depth of bars
extremum=testermetadata->high[bars-iHighest(InpDepth,i)];
//--- this highest has been found previously
if(extremum==lasthigh)
extremum=0.0;
else
{
//--- new last high
lasthigh=extremum;
//--- discard extremum if current high is too low
if(extremum-testermetadata->high[bars-i]>InpDeviation*Point)
extremum=0.0;
else
{
//--- clear previous extremums in backstep bars
for(back=1; back<=InpBackstep; back++)
{
pos=i+back;
if(ExtHighBuffer[bars-pos]!=0 &&
ExtHighBuffer[bars-pos]<extremum)
ExtHighBuffer[bars-pos]=0.0;
}
}
}
//--- found extremum is current high
if(testermetadata->high[bars-i]==extremum)
ExtHighBuffer[bars-i]=extremum;
else
ExtHighBuffer[bars-i]=0.0;
}
//--- final cutting
if(whatlookfor==0)
{
lastlow=0.0;
lasthigh=0.0;
}
else
{
lastlow=curlow;
lasthigh=curhigh;
}
for(i=limit; i>=0; i--)
{
switch(whatlookfor)
{
case 0: // look for peak or lawn
if(lastlow==0.0 && lasthigh==0.0)
{
if(ExtHighBuffer[bars-i]!=0.0)
{
lasthigh=testermetadata->high[bars-i];
lasthighpos=i;
whatlookfor=-1;
ExtZigzagBuffer[bars-i]=lasthigh;
}
if(ExtLowBuffer[bars-i]!=0.0)
{
lastlow=testermetadata->low[bars-i];
lastlowpos=i;
whatlookfor=1;
ExtZigzagBuffer[bars-i]=lastlow;
}
}
break;
case 1: // look for peak
if(ExtLowBuffer[bars-i]!=0.0 && ExtLowBuffer[bars-i]<lastlow &&
ExtHighBuffer[bars-i]==0.0)
{
ExtZigzagBuffer[bars-lastlowpos]=0.0;
lastlowpos=i;
lastlow=ExtLowBuffer[bars-i];
ExtZigzagBuffer[bars-i]=lastlow;
}
if(ExtHighBuffer[bars-i]!=0.0 && ExtLowBuffer[bars-i]==0.0)
{
lasthigh=ExtHighBuffer[bars-i];
lasthighpos=i;
ExtZigzagBuffer[bars-i]=lasthigh;
whatlookfor=-1;
}
break;
case -1: // look for lawn
if(ExtHighBuffer[bars-i]!=0.0 && ExtHighBuffer[bars-i]>lasthigh &&
ExtLowBuffer[bars-i]==0.0)
{
ExtZigzagBuffer[bars-lasthighpos]=0.0;
lasthighpos=i;
lasthigh=ExtHighBuffer[bars-i];
ExtZigzagBuffer[bars-i]=lasthigh;
}
if(ExtLowBuffer[bars-i]!=0.0 && ExtHighBuffer[bars-i]==0.0)
{
lastlow=ExtLowBuffer[bars-i];
lastlowpos=i;
ExtZigzagBuffer[bars-i]=lastlow;
whatlookfor=1;
}
break;
}
}
//=============== Optimize ZigZag ===============================
double curhl=0.0;int curpos=0,curl2=-1,errors=0,hptr=0,lptr=0;
for(int i=250;i<bars;i++){
if(ExtZigzagBuffer[i]!=0.0){
curhl=ExtZigzagBuffer[i];curpos=i;break;
}
}
for(int i=0;i<5000;i++){extremums->high[i]=0;extremums->low[i]=0;}
extremums->barscnt = 1;
for(int i=curpos+1;i<bars;i++){
if(ExtZigzagBuffer[i]!=0.0){
if(curhl<ExtZigzagBuffer[i]){if( (curl2==-1) || (curl2==0) ){
if(curl2==-1){extremums->high[hptr]=
curpos;extremums->low[lptr]=0;lptr++;}
else {extremums->low[lptr]=i;lptr++;}
curl2=1;
}else errors++;}
if(curhl>ExtZigzagBuffer[i]){if( (curl2==-1) || (curl2==1) ){
if(curl2==-1){extremums->low[lptr]=
curpos;extremums->high[hptr]=0;hptr++;}
else {extremums->high[hptr]=i;hptr++;}
curl2=0;
}else errors++;}
curhl=ExtZigzagBuffer[i];
extremums->barscnt++;
}
}
int profit=0;
for(int i=1;i<extremums->barscnt-1;i++){
int t;
t=abs((int)((testermetadata->high[extremums->high[i]]-
testermetadata->low[extremums->low[i]])/Point));
//if(profit>t)profit=t;
profit+=t;
if(extremums->high[0]==0)
t=abs((int)((testermetadata->high[extremums->high[i]]-
testermetadata->low[extremums->low[i-1]])/Point));
else
t=abs((int)((testermetadata->high[extremums->high[i-1]]-
testermetadata->low[extremums->low[i]])/Point));
//if(profit>t)profit=t;
profit+=t;
}
extremums->midprofit = (profit/(extremums->barscnt-2)/2);
}
const char* optimize(const char* symbol,const char* tf,
const char* timeout,int spr) {
double point = loadHST(symbol,tf);
ZigZag(point);
return (const char *)testertest(tf,point,timeout,spr);
}
char* GetElement(char* str, int index){
char* membuf = new char[2];membuf = (char*)realloc(membuf,5000);
memset(membuf,0,5000);
int tconfigindex=0;
lstrcat(membuf,str);
char* ptr=&membuf[0];char* prevptr=&membuf[0];tconfigindex=0;
while(ptr!=NULL){
char symbol1=' ';
char symbol2='\0';
ptr=strchr (prevptr,symbol1);
if (ptr!=NULL){
*ptr=symbol2;
memset(&t1config[tconfigindex][0],0,5000);
lstrcat(&t1config[tconfigindex][0],prevptr);
tconfigindex++;
prevptr=ptr+1;
}
}
memset(&t1config[tconfigindex][0],0,5000);
lstrcat(&t1config[tconfigindex][0],prevptr);
tconfigindex++;
delete[] membuf;
return &t1config[index][0];
}
void ReadConfig(){
FILE *hFile;
char* membuf = new char[2];membuf = (char*)realloc(membuf,1500000);
memset(membuf,0,1500000);
char* str1 = new char[2];str1 = (char*)realloc(str1,200);
memset(str1,0,200);
char* str2 = new char[2];str2 = (char*)realloc(str2,200);
memset(str2,0,200);
char tconfig[10000][200];int tconfigindex=0;
int i,i1=0,testerdigits;double point;
hFile = fopen(pathCONFIG, "rb");
if(!(!hFile)){
fseek(hFile,0,SEEK_END);
int dwFileSize = ftell(hFile);
fseek(hFile,0,SEEK_SET);
fread(membuf,dwFileSize,1,hFile);
fclose(hFile);
}
char* ptr=&membuf[0];char* prevptr=&membuf[0];tconfigindex=0;
while(ptr!=NULL){
char symbol1='\r';
char symbol2='\0';
ptr=strchr (prevptr,symbol1);
if (ptr!=NULL){
*ptr=symbol2;
*(ptr+1)=symbol2;
memset(&tconfig[tconfigindex][0],0,200);
lstrcat(&tconfig[tconfigindex][0],prevptr);
tconfigindex++;
prevptr=ptr+2;
}
}
memset(&tconfig[tconfigindex][0],0,200);
lstrcat(&tconfig[tconfigindex][0],prevptr);
tconfigindex++;
//==============================
int tmp02=0,cindex1=0,cindex2,c1=0;
memset(membuf,0,1500000);
while(tmp02==0)
{
if(!(c1==tconfigindex))
{
cindex2=1;
memset(&config[cindex1][cindex2][0],0,200);
lstrcat(&config[cindex1][cindex2][0],&tconfig[c1][0]);c1++;
cindex2++;
memset(&config[cindex1][0][0],0,200);
lstrcat(&config[cindex1][0][0],"0");
memset(str1,0,200);
lstrcat(str1,"1");
memset(str2,0,200);
lstrcat(str2,GetElement(&tconfig[c1][0],0));
while((strlen(str1)>0) && (c1!=tconfigindex))
{
memset(str1,0,200);
lstrcat(str1,&tconfig[c1][0]);c1++;
if(strlen(str1)>0)
if(strcmp(str2,GetElement(&tconfig[c1][0],0))!=0)
{
memset(&config[cindex1][cindex2][0],0,200);
lstrcat(&config[cindex1][cindex2][0],str1);
cindex2++;
int tmp03=strToInt(&config[cindex1][0][0]);
memset(&config[cindex1][0][0],0,200);
lstrcat(&config[cindex1][0][0],intToStr(tmp03+1));
memset(str2,0,200);
lstrcat(str2,GetElement(&tconfig[c1][0],0));
}
}
if(c1==tconfigindex)
tmp02=1;
cindex1++;
}
}
cindex=cindex1;
delete[] str1;
delete[] membuf;
}
char* SetElement(char* str, int index,char* str2){
char* membuf = new char[2];membuf = (char*)realloc(membuf,15000);
memset(membuf,0,15000);
int tconfigindex=0;
lstrcat(membuf,str);
char* ptr=&membuf[0];char* prevptr=&membuf[0];tconfigindex=0;
while(ptr!=NULL){
char symbol1=' ';
char symbol2='\0';
ptr=strchr (prevptr,symbol1);
if (ptr!=NULL){
*ptr=symbol2;
memset(&t1config[tconfigindex][0],0,15000);
lstrcat(&t1config[tconfigindex][0],prevptr);
tconfigindex++;
prevptr=ptr+1;
}
}
memset(&t1config[tconfigindex][0],0,15000);
lstrcat(&t1config[tconfigindex][0],prevptr);
tconfigindex++;
delete[] membuf;
memset(&t1config[index][0],0,15000);
lstrcat(&t1config[index][0],str2);
int i;
for(i=0;i<tconfigindex-1;i++){lstrcat(t1config2,&t1config[i][0]);
lstrcat(t1config2," ");}
lstrcat(t1config2,&t1config[i][0]);
return &t1config2[0];
}
void PatchConfig(char* symbol, char* param){
int i1,i2;
for(i1=0; i1<cindex; i1++)
if(strcmp(symbol,config[i1][1])==0)
for(i2=0; i2<strToInt(&config[i1][0][0]); i2++){
char c1[15000],c2[15000]; memset(&c1[0],0,15000);
memset(&c2[0],0,15000);
lstrcat(&c1[0],GetElement(¶m[0],0));
lstrcat(&c2[0],GetElement(&config[i1][i2+2][0],0));
if(strcmp(c1,c2)==0){
memset(&config[i1][i2+2][0],0,15000);
lstrcat(&config[i1][i2+2][0],param);
}
}
}
void SaveConfig(){
char* membuf = new char[2];membuf = (char*)realloc(membuf,1500000);
memset(membuf,0,1500000);
//char* pathCONFIG1 = new char[500];memset(pathCONFIG1,0,500);
lstrcat(pathCONFIG1,pathCONFIG);
lstrcat(pathCONFIG1,"1.txt");
FILE *os;os= fopen(pathCONFIG,"wb");
for(int i1=0; i1<cindex; i1++)
{
lstrcat(membuf,&config[i1][1][0]);lstrcat(membuf,"\r\n");
for(int i2=0; i2<strToInt(&config[i1][0][0]); i2++)
{
lstrcat(membuf,&config[i1][i2+2][0]);
if(i1!=(cindex-1))
lstrcat(membuf,"\r\n");
if((i1==(cindex-1))&&(i2!=(strToInt(&config[i1][0][0])-1)))
lstrcat(membuf,"\r\n");
}
if(i1!=(cindex-1))
lstrcat(membuf,"\r\n");
}
int tmp0=strlen(membuf);
fwrite(membuf,tmp0,1,os);
fclose(os);
delete[] membuf;
}
size_t to_narrow(const wchar_t * src, char * dest, size_t dest_len){
size_t i;
wchar_t code;
i = 0;
while (src[i] != '\0' && i < (dest_len - 1)){
code = src[i];
if (code < 128)
dest[i] = char(code);
else{
dest[i] = '?';
if (code >= 0xD800 && code <= 0xD8FF)
// lead surrogate, skip the next code unit, which is the trail
i++;
}
i++;
}
dest[i] = '\0';
return i - 1;
}
int main(int argc, char *argv[]){
printf(timeToStr(time(NULL))); printf(" - time start\r\n");
SleepEx(5000,true);
double title1,dt0=time(NULL);
rdtsc();
srand(time(0));
initrandbytes();
pathCONFIG = new char[500];memset(pathCONFIG,0,500);
lstrcat(pathCONFIG,argv[3]);
pathHST = new char[500];memset(pathHST,0,500);
lstrcat(pathHST,"..\\..\\history\\");
lstrcat(pathHST,argv[7]);lstrcat(pathHST,"\\");
tfdepth = 10;bars = strToInt(argv[1]);
timeshift = strToInt(argv[4]);
//if(bars<=(timeshift+1500))bars=timeshift+1500;
char *stm1;stm1 = (char *)malloc(5400000);memset(stm1,0,5400000);
char tf[10];memset(tf,0,10);char timeout[60];memset(timeout,0,60);
char takeprofitbuy[10];
memset(takeprofitbuy,0,10);
char optresult[15000];memset(optresult,0,15000);
testermetadata = new mdata[1];
extremums = new mextremums[1];
ReadConfig();
//SaveConfig();return 0;
for(int i1=0;i1<cindex;i1++){
printf("\r\n");printf(&config[i1][1][0]);printf("\r\n");
for(int i2=0;i2<strToInt(&config[i1][0][0]);i2++){
memset(tf,0,10);memset(timeout,0,60);memset(optresult,0,15000);
lstrcat(tf,GetElement(&config[i1][i2+2][0],0));
lstrcat(timeout,GetElement(&config[i1][i2+2][0],5));
int spr = strToInt(GetElement(&config[i1][i2+2][0],6));
lstrcat(optresult,optimize(&config[i1][1][0],tf,timeout,spr));
printf(timeToStr(time(NULL))); printf("\r\n");
printf(optresult);printf("\r\n");
PatchConfig(&config[i1][1][0],optresult);
}
}
SaveConfig();
lstrcat(stm1,"\r\n");
title1=time(NULL);title1-=dt0;title1/=60;
lstrcat(stm1,doubleToStr(title1,1));
lstrcat(stm1," minutes used");
printf(stm1);
MessageBeep(MB_OK);
free(stm1);
free(pathHST);
free(pathCONFIG);
delete[] extremums;
delete[] testermetadata;
FILE *hFile;
wchar_t path1[255];GetCurrentDirectoryW(255,path1);
char path[255];memset(path,0,255);to_narrow(path1, path, 255);
lstrcat(path,"\\settings.txt");
char* str1 = new char[2];str1 = (char*)realloc(str1,500);memset(str1,0,500);
hFile = fopen(path, "rb");
if(!(!hFile)){
fseek(hFile,0,SEEK_END);
int dwFileSize = ftell(hFile);
fseek(hFile,0,SEEK_SET);
fread(str1,dwFileSize,1,hFile);
fclose(hFile);
}
str1 = SetElement(str1,1,intToStr(time1(NULL)+60*60*24));
hFile = fopen(path, "wb");
if(!(!hFile)){
fseek(hFile,0,SEEK_SET);
fwrite(str1,strlen(str1),1,hFile);
fclose(hFile);
}
wchar_t tt[255];GetCurrentDirectoryW(255,tt);
char tt1[255];memset(tt1,0,255);to_narrow(tt, tt1, 255);
lstrcat(tt1,"\\TesterStart.bat");
_execlp(tt1, tt1, "", "", "", "", nullptr);
}
|
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <tchar.h>
#include "winsock2.h"
#include <process.h>
#pragma comment(lib, "Winmm.lib")
#define MODE_TRADES 0
#define MODE_HISTORY 1
#define OP_BUY 0
#define OP_SELL 1
#define OP_BUYLIMIT 2
#define OP_SELLLIMIT 3
#define OP_BUYSTOP 4
#define OP_SELLSTOP 5
#define OP_SL 0
#define OP_TP 1
#define OP_CLOSE 2
#define OP_DELETE 3
#define PRICE_CLOSE 0
#define PRICE_OPEN 1
#define PRICE_HIGH 2
#define PRICE_LOW 3
#define PRICE_MEDIAN 4
#define PRICE_TYPICAL 5
#define PRICE_WEIGHTED 6
char* pathCONFIG;
char* pathRESULTS;
char* ident;
int bars,ibars;
int randcycles=50;
int timeshift;
char* pathHST;
double mulsl=1,multp=1;
struct mdata{
long int ctm[50000];
double open[50000];
double low[50000];
double high[50000];
double close[50000];
double volume[50000];
int spread[50000];
};
mdata* testermetadata;
struct tresults{
int totalloss;
int totalprofit;
int totalprofitorders;
int totalnotprofitorders;
int midtimeout;
};
int testercurbar;
int randptr=0,randbytes[257];
char config[90][150][5000];int cindex=0;
char t1config[1000][5000];
char totalresults[1500000];
inline int rdtsc(){__asm__ __volatile__("rdtsc");}
void initrandbytes(){
int h=0;
while(h==0){
for(int z1=0;z1<51;z1++)
{for(int z=0;z<256;z++)
{randbytes[z]^=((rand()<<1) % 256)&255;}}
for(int z1=0;z1<5;z1++)
for(int z=0;z<256;z++)
randbytes[z]^=(randbytes[(z+1)%256]>>1)&255;
for(int z=0;z<256;z++)if (randbytes[z]>127)h=1;
}
randptr=0;
}
int rand(int min, int max){
int h=randbytes[randptr];
if(h<min)h+=min;
if(h>max)h=h%max;
if(h<min)h+=min;
randbytes[randptr]^=randbytes[(randptr+1)%256];
randptr=(randptr+1)%256;
return h;
}
inline char* intToStr(int i){
static char strt[18]="";memset(strt,0,18);
snprintf(strt, 17, "%ld", i);
return (char *)strt;
}
inline int strToInt(const char *s){
char *endChar;
setlocale(LC_NUMERIC, "C");
int t = strtol(s, &endChar, 10);
if(*endChar!='\0')return -1;
return t;
}
inline char* doubleToStr(double d,int precsion) {
static char str[18]="";
memset(str,0,18);
snprintf(str, 12, "%.*f", precsion,d);
return (char *)str;
}
/* void lstrcat(char *buff,const char *past)
{
int len=0,len0,len1;
len0=strlen(buff);
len1=strlen(past);
memcpy(buff+len0,past,len1);memset(buff+len0+len1,0,1);
} */
inline char* timeToStr(const time_t st) {
static char strtime1[28]="";
tm stm1;
time_t st1;
st1=time(0);
memset(strtime1,0,28);
memset(&stm1,0,sizeof(struct tm));
if(st==0)
stm1=*localtime((const time_t *)&st1);
else
stm1=*localtime((const time_t *)&st);
strftime(strtime1,27,"%d.%m.%Y %H:%M:%S ",&stm1);
return (char *)strtime1;
}
inline char* gmtimeToStr(const time_t st) {
static char gmstrtime[28]="";
tm stm1;
time_t st1;
st1=time(0);
memset(&gmstrtime,0,28);
memset(&stm1,0,sizeof(struct tm));
if(st==0)
stm1=*gmtime((const time_t *)&st1);
else
stm1=*gmtime((const time_t *)&st);
strftime(gmstrtime,27,"%d.%m.%Y %H:%M ",&stm1);
return (char *)gmstrtime;
}
double sma(const int period, const int price, const int shift,int tcurbar){
double sum=0.0,tmp;
if(price==PRICE_CLOSE){
for(int i=0;i<period;i++)
{
sum+=testermetadata->close[tcurbar-(i+shift)];
}
tmp=sum;tmp/=period;
return(tmp);
}else
if(price==PRICE_MEDIAN){
for(int i=0;i<period;i++)
{
tmp=testermetadata->high[tcurbar-(i+shift)];
tmp+=testermetadata->low[tcurbar-(i+shift)];
tmp*=0.5;
sum+=tmp;
}
tmp=sum;tmp/=period;
return(tmp);
}else
if(price==PRICE_TYPICAL){
for(int i=0;i<period;i++)
{
tmp=testermetadata->high[tcurbar-(i+shift)];
tmp+=testermetadata->low[tcurbar-(i+shift)];
tmp+=testermetadata->close[tcurbar-(i+shift)];
tmp/=3.0;
sum+=tmp;
}
tmp=sum;tmp/=period;
return(tmp);
}
}
double cci(const int period, const int shift, int tcurbar ){
double price,sum,mul,CCIBuffer,RelBuffer,DevBuffer,MovBuffer;
int k;
MovBuffer = sma(period, PRICE_TYPICAL, shift,tcurbar);
mul = 0.015/period;
sum = 0.0;
k = period-1;
while(k>=0)
{
price=(testermetadata->high[tcurbar-(k+shift)]+
testermetadata->low[tcurbar-(k+shift)]+
testermetadata->close[tcurbar-(k+shift)])/3.0;
sum+=fabs(price-MovBuffer);
k--;
}
DevBuffer = sum*mul;
price =(testermetadata->high[tcurbar-(shift)]+
testermetadata->low[tcurbar-(shift)]+
testermetadata->close[tcurbar-(shift)])/3.0;
RelBuffer=price-MovBuffer;
if(DevBuffer==0.0)CCIBuffer=0.0;
else CCIBuffer = RelBuffer / DevBuffer;
return(CCIBuffer);
}
double loadHST(const char* symbol,const char* tf){
FILE *hFile;
char* membuf = new char[2];
membuf = (char*)realloc(membuf,bars*60);
char* path = new char[2];
path = (char*)realloc(path,500);memset(path,0,500);
lstrcat(path,pathHST);lstrcat(path,symbol);
lstrcat(path,tf);lstrcat(path,".hst");
int i,i1=0,testerdigits;double point;
for(i=0;i<50000;i++){
testermetadata->ctm[i1]=0;
testermetadata->open[i1]=0.0;
testermetadata->high[i1]=0.0;
testermetadata->low[i1]=0.0;
testermetadata->close[i1]=0.0;
testermetadata->volume[i1]=0;
testermetadata->spread[i1]=0;
}
hFile = fopen(path, "rb");
if(!(!hFile)){
fseek(hFile,0,SEEK_END);
int dwFileSize = ftell(hFile);
//int bars=(dwFileSize-148)/60; if(bars>=ibars)bars=ibars;
if(dwFileSize>=148+60){
i=0;
fseek(hFile,dwFileSize-60*bars,SEEK_SET);
fread(membuf,60*bars,1,hFile);
while(i1<bars){
memcpy(&testermetadata->ctm[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->open[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->high[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->low[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->close[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->volume[i1],&membuf[i],8);i+=8;
memcpy(&testermetadata->spread[i1],&membuf[i],4);i+=12;
i1++;
}
}
fseek(hFile,84,SEEK_SET);
fread(&testerdigits,4,1,hFile);
fclose(hFile);
}
delete[] path;
delete[] membuf;
point = 1.0 / (int) pow((double)10, testerdigits);
return point;
}
double icci(int shift, int period_ma_fast, int period_ma_slow,
int cci_period,int tcurbar){
double ma_fast,ma_slow;
int i1;
ma_fast=ma_slow=cci(cci_period,shift,tcurbar)+10000.0;
for(i1=1; i1<period_ma_slow; i1++)
ma_slow=ma_slow+cci(cci_period,i1+shift,tcurbar)+10000.0;
ma_slow=ma_slow/period_ma_slow;
for(i1=1; i1<period_ma_fast; i1++)
ma_fast=ma_fast+cci(cci_period,i1+shift,tcurbar)+10000.0;
ma_fast=ma_fast/period_ma_fast;
return (ma_fast-ma_slow);
}
int DeltaMasLength(int period_ma_fast, int period_ma_slow, int cci_period,int tcurbar){
double tmp1,tmp2,tmp3,prevtmp1=icci(2,period_ma_fast,
period_ma_slow, cci_period,tcurbar);tmp3=prevtmp1;
double tmp3_2=fabs(icci(10,period_ma_fast, period_ma_slow, cci_period,tcurbar));
double tmp3_3=fabs(icci(18,period_ma_fast, period_ma_slow, cci_period,tcurbar));
if(tmp3<0)tmp2=-1;else tmp2=1;
prevtmp1=tmp3=fabs(tmp3);
if((tmp3>(tmp3_2*1.2))&&(tmp3>(tmp3_3*1.3))){
double tmp4=fabs(icci(0,period_ma_fast, period_ma_slow, cci_period,tcurbar));
double tmp5=fabs(icci(1,period_ma_fast, period_ma_slow, cci_period,tcurbar));
if(tmp4<=tmp5)
if(tmp3>tmp5){
int i1;
for(i1=3;i1<15;i1++){
tmp1=fabs(icci(i1,period_ma_fast, period_ma_slow, cci_period,tcurbar));
if(prevtmp1<tmp1) return (i1*tmp2);
prevtmp1=tmp1;
}
return (i1*tmp2);
}
}
return 0;
}
int iLowest(int count, int start){
double Low=99999999;int cLow=0;
for(int i = start;i > start-count;i--)
{
if(testermetadata->low[i]<Low){
Low=testermetadata->low[i];cLow=i;}
}
return(cLow);
}
int iHighest(int count, int start){
double High=-99999999;int cHigh=0;
for(int i = start;i > start-count;i--)
{
if(testermetadata->high[i]>High){
High=testermetadata->high[i];cHigh=i;}
}
return(cHigh);
}
void testerstart(int tf, double point, int ctimeout, int period_ma_fast,
int period_ma_slow, int cci_period, int period_ma_fast2, int period_ma_slow2,
int cci_period2,int tpsell,int slsell,int slbuy,int tpbuy,
int sprd, tresults &result){
double orderopenpricebuy,orderopenpricesell;
int orderopenedsell=0,orderopenedbuy=0;
int orderopentimebuy,orderopentimesell;
int totalloss=0,totalprofit=0,totalprofitorders=0,totalnotprofitorders=0,
totaltimeout=0;
int tcurbar,i;
sprd=1;//slsell=tpsell*2;slbuy=tpbuy*2;
for(i=250/*bars-timeshift*/;i<bars;i++){
tcurbar=i;
if(orderopenedsell==1){
if( testermetadata->high[i] >= (orderopenpricesell+point*slsell) ){
orderopenedsell=0;
totalloss+=slsell;
totalnotprofitorders++;
totaltimeout+=i-orderopentimesell;
}
if( testermetadata->low[i] <= (orderopenpricesell-point*tpsell) ){
orderopenedsell=0;
totalprofit+=tpsell;
totalprofitorders++;
totaltimeout+=i-orderopentimesell;
}
}
if(orderopenedbuy==1){
if( testermetadata->high[i] >= (orderopenpricebuy+point*tpbuy) ){
orderopenedbuy=0;
totalprofit+=tpbuy;
totalprofitorders++;
totaltimeout+=i-orderopentimebuy;
}
if( testermetadata->low[i] <= (orderopenpricebuy-point*slbuy) ){
orderopenedbuy=0;
totalloss+=slbuy;
totalnotprofitorders++;
totaltimeout+=i-orderopentimebuy;
}
}
int signal = DeltaMasLength(period_ma_fast, period_ma_slow, cci_period,tcurbar);
int signal2 = DeltaMasLength(period_ma_fast2, period_ma_slow2, cci_period2,tcurbar);
if(period_ma_fast2>0)
if(abs(signal2)>13){
//if((signal>0)&&(orderopenedsell==0)&&(iLowest(19,tcurbar)>10)&&(iHighest(19,tcurbar)<5)) {
if((signal2>0)&&(orderopenedsell==0)){
orderopenedsell=1;//OP_SELL;
orderopentimesell=i;
orderopenpricesell=testermetadata->open[i];
//break;
}
}
if(period_ma_fast>0)
if(abs(signal)>13){
//if((signal<0)&&(orderopenedbuy==0)&&(iLowest(19,tcurbar)<5)&&(iHighest(19,tcurbar)>10)) {
if((signal<0)&&(orderopenedbuy==0)){
orderopenedbuy=1;//OP_BUY;
orderopentimebuy=i;
orderopenpricebuy=testermetadata->open[i];
//break;
}
}
}
/* i=bars-1;
if(orderopenedsell==1){
if( testermetadata->high[i] >= (orderopenpricesell) ){
orderopenedsell=0;
totalloss+=abs((testermetadata->high[i]-orderopenpricesell)/point);
totalnotprofitorders++;
totaltimeout+=i-orderopentimesell;
}
if( testermetadata->low[i] <= (orderopenpricesell) ){
orderopenedsell=0;
totalprofit+=abs((orderopenpricesell-testermetadata->low[i])/point);
totalprofitorders++;
totaltimeout+=i-orderopentimesell;
}
}
if(orderopenedbuy==1){
if( testermetadata->high[i] >= (orderopenpricebuy) ){
orderopenedbuy=0;
totalprofit+=abs((testermetadata->high[i]-orderopenpricebuy)/point);
totalprofitorders++;
totaltimeout+=i-orderopentimebuy;
}
if( testermetadata->low[i] <= (orderopenpricebuy) ){
orderopenedbuy=0;
totalloss+=abs((orderopenpricebuy-testermetadata->low[i])/point);
totalnotprofitorders++;
totaltimeout+=i-orderopentimebuy;
}
} */
result.totalloss=totalloss;
result.totalprofit=totalprofit;
result.totalprofitorders=totalprofitorders;
result.totalnotprofitorders=totalnotprofitorders;
if((totalprofitorders+totalnotprofitorders)>0)
result.midtimeout=(int)(totaltimeout*tf/(totalprofitorders+totalnotprofitorders));
else result.midtimeout=0;
return ;
}
tresults testerresult1;
const char* testertest(const char* ctf,int ma1,int ma2,int cci1,int ma1_2,int ma2_2,
int cci1_2,double point, const char* ctimeout,int tpsell,int slsell,int slbuy,int tpbuy,
int sprd) {
static char itemconfig[200]="";
memset(itemconfig,0,200);
int tf=strToInt(ctf);int timeout=strToInt(ctimeout);
tresults& testerresult = testerresult1;
testerstart(tf,point,timeout,ma1,ma2,cci1,ma1_2,ma2_2,cci1_2,tpsell,slsell,slbuy,
tpbuy,sprd,testerresult);
lstrcat(itemconfig,intToStr(testerresult1.totalloss));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(testerresult1.totalprofit));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(testerresult1.totalprofitorders));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(testerresult1.totalnotprofitorders));lstrcat(itemconfig," ");
lstrcat(itemconfig,intToStr(testerresult1.midtimeout));
return (const char *)itemconfig;
}
const char* sniper(const char* symbol,int ma1,int ma2,int cci1,int ma1_2,int ma2_2,int cci1_2,
const char* tf, const char* timeout,int tpsell,int slsell,int slbuy,int tpbuy,int sprd) {
double point = loadHST(symbol,tf);
return (const char *)testertest(tf,ma1,ma2,cci1,ma1_2,ma2_2,cci1_2,point,timeout,tpsell,
slsell,slbuy,tpbuy,sprd);
}
void ReadConfig(){
FILE *hFile;
char* membuf = new char[2];membuf = (char*)realloc(membuf,1500000);
memset(membuf,0,1500000);
char* str1 = new char[2];str1 = (char*)realloc(str1,200);memset(str1,0,200);
char tconfig[10000][200];int tconfigindex=0;
int i,i1=0,testerdigits;double point;
hFile = fopen(pathCONFIG, "rb");
if(!(!hFile)){
fseek(hFile,0,SEEK_END);
int dwFileSize = ftell(hFile);
fseek(hFile,0,SEEK_SET);
fread(membuf,dwFileSize,1,hFile);
fclose(hFile);
}
char* ptr=&membuf[0];char* prevptr=&membuf[0];tconfigindex=0;
while(ptr!=NULL){
char symbol1='\r';
char symbol2='\0';
ptr=strchr (prevptr,symbol1);
if (ptr!=NULL){
*ptr=symbol2;
*(ptr+1)=symbol2;
memset(&tconfig[tconfigindex][0],0,200);
lstrcat(&tconfig[tconfigindex][0],prevptr);
tconfigindex++;
prevptr=ptr+2;
}
}
memset(&tconfig[tconfigindex][0],0,200);
lstrcat(&tconfig[tconfigindex][0],prevptr);
tconfigindex++;
//==============================
int tmp02=0,cindex1=0,cindex2,c1=0;
memset(membuf,0,1500000);
while(tmp02==0)
{
if(!(c1==tconfigindex))
{
cindex2=1;
memset(&config[cindex1][cindex2][0],0,200);
lstrcat(&config[cindex1][cindex2][0],&tconfig[c1][0]);c1++;
cindex2++;
memset(&config[cindex1][0][0],0,200);
lstrcat(&config[cindex1][0][0],"0");
memset(str1,0,200);
lstrcat(str1,"1");
while((strlen(str1)>0) && (c1!=tconfigindex))
{
memset(str1,0,200);
lstrcat(str1,&tconfig[c1][0]);c1++;
if(strlen(str1)>0)
{
memset(&config[cindex1][cindex2][0],0,200);
lstrcat(&config[cindex1][cindex2][0],str1);
cindex2++;
int tmp03=strToInt(&config[cindex1][0][0]);
memset(&config[cindex1][0][0],0,200);
lstrcat(&config[cindex1][0][0],intToStr(tmp03+1));
}
}
if(c1==tconfigindex)
tmp02=1;
cindex1++;
}
}
cindex=cindex1;
delete[] str1;
delete[] membuf;
}
char* GetElement(char* str, int index){
char* membuf = new char[2];membuf = (char*)realloc(membuf,5000);memset(membuf,0,5000);
int tconfigindex=0;
lstrcat(membuf,str);
char* ptr=&membuf[0];char* prevptr=&membuf[0];tconfigindex=0;
while(ptr!=NULL){
char symbol1=' ';
char symbol2='\0';
ptr=strchr (prevptr,symbol1);
if (ptr!=NULL){
*ptr=symbol2;
memset(&t1config[tconfigindex][0],0,5000);
lstrcat(&t1config[tconfigindex][0],prevptr);
tconfigindex++;
prevptr=ptr+1;
}
}
memset(&t1config[tconfigindex][0],0,5000);
lstrcat(&t1config[tconfigindex][0],prevptr);
tconfigindex++;
delete[] membuf;
return &t1config[index][0];
}
char snipertxt[10][11200][200];
void InitResults(){
for(int tf=0; tf<9; tf++){
for(int i1=0; i1<11200; i1++)memset(&snipertxt[tf][i1][0],0,200);
lstrcat(&snipertxt[tf][0][0],"0");
}
}
void PatchConfig(int tf, char* symbol, char* param){
if(strlen(¶m[0])>0){
int i1,i3;
if(tf==1)i3=0;if(tf==5)i3=1;if(tf==15)i3=2;if(tf==30)i3=3;if(tf==60)i3=4;if(tf==240)i3=5;
if(tf==1440)i3=6;if(tf==10080)i3=7;if(tf==43200)i3=8;
i1 = strToInt(&snipertxt[i3][0][0]);
memset(&snipertxt[i3][i1+1][0],0,200);
lstrcat(&snipertxt[i3][i1+1][0],&symbol[0]);
lstrcat(&snipertxt[i3][i1+1][0]," ");
lstrcat(&snipertxt[i3][i1+1][0],¶m[0]);
memset(&snipertxt[i3][0][0],0,200);
lstrcat(&snipertxt[i3][0][0],intToStr(i1+1));
}
}
char* SaveResults(){
char* membuf = new char[2];membuf = (char*)realloc(membuf,1500000);
memset(membuf,0,1500000);
memset(totalresults,0,1500000);
FILE *os;os= fopen(pathRESULTS,"wb");
int tf,i3,i4=0;
int totalloss=0,totalprofit=0,totalprofitorders=0,totalnotprofitorders=0,midtimeout=0;
int Ttotalloss[9],Ttotalprofit[9],Ttotalprofitorders[9],Ttotalnotprofitorders[9],
Tmidtimeout[9];
int Ytotalloss=0,Ytotalprofit=0,Ytotalprofitorders=0,Ytotalnotprofitorders=0,
Ymidtimeout=0;
for(int i=0;i<9;i++){
Ttotalloss[i]=0;
Ttotalprofit[i]=0;
Ttotalprofitorders[i]=0;
Ttotalnotprofitorders[i]=0;
Tmidtimeout[i]=0;
}
double maxtf=0.0;
for(tf=0; tf<9; tf++)
{
if(strlen(&snipertxt[tf][0][0])>0){
if(tf==0)i3=1;if(tf==1)i3=5;if(tf==2)i3=15;if(tf==3)i3=30;if(tf==4)i3=60;
if(tf==5)i3=240;
if(tf==6)i3=1440;if(tf==7)i3=10080;if(tf==8)i3=43200;
for(int i2=0; i2<strToInt(&snipertxt[tf][0][0]); i2++)
{
totalloss+= strToInt( GetElement(&snipertxt[tf][i2+1][0],1) );
totalprofit+= strToInt( GetElement(&snipertxt[tf][i2+1][0],2) );
totalprofitorders+= strToInt( GetElement(&snipertxt[tf][i2+1][0],3) );
totalnotprofitorders+= strToInt( GetElement(&snipertxt[tf][i2+1][0],4) );
midtimeout+= strToInt( GetElement(&snipertxt[tf][i2+1][0],5) );
Ttotalloss[tf]+= strToInt( GetElement(&snipertxt[tf][i2+1][0],1) );
Ttotalprofit[tf]+= strToInt( GetElement(&snipertxt[tf][i2+1][0],2) );
Ttotalprofitorders[tf]+= strToInt( GetElement(&snipertxt[tf][i2+1][0],3) );
Ttotalnotprofitorders[tf]+= strToInt( GetElement(&snipertxt[tf][i2+1][0],4) );
Tmidtimeout[tf]+= strToInt( GetElement(&snipertxt[tf][i2+1][0],5) );
i4++;
if(strToInt(&snipertxt[tf][0][0])>0)if(maxtf<(double)i3)maxtf=(double)i3;
}
}
}
for(tf=0; tf<9; tf++)
{
if(tf==0)i3=1;if(tf==1)i3=5;if(tf==2)i3=15;if(tf==3)i3=30;if(tf==4)i3=60;
if(tf==5)i3=240;
if(tf==6)i3=1440;if(tf==7)i3=10080;if(tf==8)i3=43200;
Ytotalloss+=Ttotalloss[tf]*(maxtf/i3);
Ytotalprofit+=Ttotalprofit[tf]*(maxtf/i3);
Ytotalprofitorders+=Ttotalprofitorders[tf]*(maxtf/i3);
Ytotalnotprofitorders+=Ttotalnotprofitorders[tf]*(maxtf/i3);
Ymidtimeout+=Tmidtimeout[tf]*(maxtf/i3);
}
maxtf=(double)timeshift*maxtf;
if((Ytotalprofitorders+Ytotalnotprofitorders)>0)
midtimeout = (int)(Ymidtimeout/(Ytotalprofitorders+Ytotalnotprofitorders));
else midtimeout=0;
double ordersperday,pipsperday;
ordersperday=((double)(Ytotalprofitorders+Ytotalnotprofitorders)/
(double)maxtf)*(double)1440.0;
pipsperday=((double)(Ytotalprofit-Ytotalloss)/(double)maxtf)*(double)1440.0;
lstrcat(membuf,ident);lstrcat(membuf,"\r\n");
lstrcat(membuf,"koef SL : "); lstrcat(membuf,doubleToStr(mulsl,3));
lstrcat(membuf,", koef TP : "); lstrcat(membuf,doubleToStr(multp,3));
lstrcat(membuf,"\r\n");
lstrcat(membuf,"bars shift : "); lstrcat(membuf,intToStr(timeshift));
lstrcat(membuf,"\r\n");
lstrcat(membuf,"totalloss : "); lstrcat(membuf,intToStr(totalloss));
lstrcat(membuf," pips\r\n");
lstrcat(membuf,"totalprofit : "); lstrcat(membuf,intToStr(totalprofit));
lstrcat(membuf," pips\r\n");
lstrcat(membuf,"totalprofitorders : "); lstrcat(membuf,intToStr(totalprofitorders));
lstrcat(membuf,"\r\n");
lstrcat(membuf,"totalnotprofitorders : ");
lstrcat(membuf,intToStr(totalnotprofitorders));lstrcat(membuf,"\r\n");
lstrcat(membuf,"frequency : "); lstrcat(membuf,doubleToStr(ordersperday,2));
lstrcat(membuf," orders per day\r\n");
lstrcat(membuf,"clear profit : "); lstrcat(membuf,doubleToStr(pipsperday,2));
lstrcat(membuf," pips per day\r\n");
lstrcat(membuf,"middle order lifetime : "); lstrcat(membuf,intToStr(midtimeout));
lstrcat(membuf," minutes\r\n");
lstrcat(&totalresults[0],"\r\n\r\n");lstrcat(&totalresults[0],membuf);
lstrcat(membuf,"\r\n\r\n");
for(tf=0; tf<9; tf++)
{
if(strlen(&snipertxt[tf][0][0])>0){
if(tf==0)i3=1;if(tf==1)i3=5;if(tf==2)i3=15;if(tf==3)i3=30;if(tf==4)i3=60;
if(tf==5)i3=240;
if(tf==6)i3=1440;if(tf==7)i3=10080;if(tf==8)i3=43200;
if(strToInt(&snipertxt[tf][0][0])>0)
{lstrcat(membuf,"[ Timeframe ");lstrcat(membuf,intToStr(i3));lstrcat(membuf," ]\r\n");}
for(int i2=0; i2<strToInt(&snipertxt[tf][0][0]); i2++)
{
lstrcat(membuf,&snipertxt[tf][i2+1][0]);lstrcat(membuf,"\r\n");
if(i2==(strToInt(&snipertxt[tf][0][0])-1))
lstrcat(membuf,"\r\n");
}
}
}
FILE* fd;
char *src_str;src_str = (char *)malloc(1500000);memset(src_str,0,1500000);
fd = fopen(pathCONFIG, "rb");
int src_size = fread(src_str, 1500000,1,fd);
fclose(fd);
char *buff;buff = (char *)malloc(1500000);memset(buff,0,1500000);
lstrcat(buff,"1111222233334444");
lstrcat(buff,membuf);
lstrcat(buff,src_str);
int t;
t=strlen(membuf);memcpy(&buff[0],&t,4);
t=totalprofit;memcpy(&buff[4],&t,4);
t=totalloss;memcpy(&buff[8],&t,4);
t=strlen(src_str);memcpy(&buff[12],&t,4);
int bufflen = 16+strlen(membuf)+strlen(src_str);
//printf(&buff[16]);return 0;
WSADATA ws;
WSAStartup (MAKEWORD( 1, 1 ), &ws);
SOCKET s;
s = socket (AF_INET, SOCK_STREAM, 0);
sockaddr_in sock_addr;
ZeroMemory(&sock_addr, sizeof (sock_addr));
sock_addr.sin_family = AF_INET;
struct hostent *h;
if((h=gethostbyname("markettrader.mooo.com")) == NULL) goto ff10;
memcpy((char *) &sock_addr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
//sock_addr.sin_addr.s_addr = inet_addr ("34.121.129.140");//192.168.127.133");
sock_addr.sin_port = htons (6676);
connect (s, (sockaddr *) &sock_addr, sizeof (sock_addr) );
//send (s, (char* ) & buff, bufflen, 0 );
//send (s,reinterpret_cast<const unsigned char*>(&buff), bufflen, 0 );
send (s, buff, bufflen, 0 );
ff10:
closesocket (s);
WSACleanup();
free(buff);
free(src_str);
int tmp0=strlen(membuf);
fwrite(membuf,tmp0,1,os);
fclose(os);
delete[] membuf;
return &totalresults[0];
}
int main(int argc, char *argv[]){
printf(timeToStr(time(NULL))); printf(" - time start\r\n");
double title1,dt0=time(NULL);
rdtsc();
srand(time(0));
initrandbytes();
pathCONFIG = new char[500];memset(pathCONFIG,0,500);lstrcat(pathCONFIG,argv[3]);
pathRESULTS = new char[500];memset(pathRESULTS,0,500);lstrcat(pathRESULTS,pathCONFIG);
lstrcat(pathRESULTS,"-RESULTS.txt");
ident = new char[500];memset(ident,0,500);lstrcat(ident,argv[8]);
char* terminal = new char[500];memset(terminal,0,500);lstrcat(terminal,argv[9]);
pathHST = new char[500];memset(pathHST,0,500);lstrcat(pathHST,"..\\..\\history\\");
lstrcat(pathHST,argv[7]);lstrcat(pathHST,"\\");
timeshift = strToInt(argv[4]);bars = strToInt(argv[1]);
if(timeshift==0){timeshift=1300; /*bars-=400;*/}
mulsl = strtod(argv[5], NULL);
multp = strtod(argv[6], NULL);
char *stm1;stm1 = (char *)malloc(500000);memset(stm1,0,500000);
char tf[15];memset(tf,0,15);char timeout[30];memset(timeout,0,30);
int tpbuy,tpsell,slsell,slbuy;
int ma1,ma2,cci1,ma1_2,ma2_2,cci1_2,sprd;
char optresult[200];memset(optresult,0,200);
testermetadata = new mdata[1];
ReadConfig();
InitResults();
for(int i1=0;i1<cindex;i1++){
for(int i2=0;i2<strToInt(&config[i1][0][0]);i2++)
if((strToInt(GetElement(&config[i1][i2+2][0],1))>0) ||
(strToInt(GetElement(&config[i1][i2+2][0],12))>0)){
memset(tf,0,15);memset(timeout,0,30);memset(optresult,0,200);
tpbuy=strToInt(GetElement(&config[i1][i2+2][0],11));tpbuy*=multp;if(tpbuy<1)tpbuy=1;
tpsell=strToInt(GetElement(&config[i1][i2+2][0],4));tpsell*=multp;if(tpsell<1)tpsell=1;
slsell=strToInt(GetElement(&config[i1][i2+2][0],9));slsell*=mulsl;if(slsell<1)slsell=1;
slbuy=strToInt(GetElement(&config[i1][i2+2][0],10));slbuy*=mulsl;if(slbuy<1)slbuy=1;
lstrcat(tf,GetElement(&config[i1][i2+2][0],0));
ma1=strToInt(GetElement(&config[i1][i2+2][0],1));
ma2=strToInt(GetElement(&config[i1][i2+2][0],2));
cci1=strToInt(GetElement(&config[i1][i2+2][0],3));
sprd=strToInt(GetElement(&config[i1][i2+2][0],6));
ma1_2=strToInt(GetElement(&config[i1][i2+2][0],12));
ma2_2=strToInt(GetElement(&config[i1][i2+2][0],13));
cci1_2=strToInt(GetElement(&config[i1][i2+2][0],14));
lstrcat(timeout,GetElement(&config[i1][i2+2][0],5));
lstrcat(optresult,sniper(&config[i1][1][0],ma1,ma2,cci1,ma1_2,ma2_2,cci1_2,tf,
timeout,tpsell,slsell,slbuy,tpbuy,sprd));
if(strlen(optresult)>0){printf(tf);printf(" ");printf(&config[i1][1][0]);
printf(" ");printf(optresult);printf("\r\n");}
PatchConfig(strToInt(tf),&config[i1][1][0],optresult);
}
}
printf(SaveResults());
lstrcat(stm1,"\r\n");
title1=time(NULL);title1-=dt0;title1/=60;
lstrcat(stm1,doubleToStr(title1,1));
lstrcat(stm1," minutes used, results saved in ");lstrcat(stm1,pathRESULTS);lstrcat(stm1,"\r\n");
printf(stm1);
MessageBeep(MB_OK);
free(stm1);
delete[] testermetadata;
_execlp(terminal, "cmd.exe", "/c", "", "", "", nullptr);
//_execlp(terminal, terminal, "/portable", "", "", "", nullptr);
} |
The C++ code is optimized specifically for use in
MetaTrader 4 and MetaTrader 5, and not as a TensorFlow optimization. The code is an optimized implementation of trading signals for the MT4/5 platform, taking into account its features:
The code is indeed an optimal and high-performance solution for algorithmic trading in MetaTrader due to its exact specialization for this platform. |
The MarketTrader Expert
Advisor
works fully automatically: it trades, performs optimizations
periodically, and tests itself.
The adviser controls the spread, if the spread is increased more
than twice, then the trade does not go. Instructions for use : 0. Turn off MT4 . 1. Clear the MQL4/Files folder on your computer. Item 1 is only for existing users, for new users this item is not required. 2. Copy the MQL4 folder from the release to the MT4 terminal folder . Turn on MT4.
3. Customize the "
Market Watch / MarketWatch " window"- leave the necessary
currencies, delete the rest. |
MarketTraderMarketTrader program for automatic breakeven trading in the Forex market using the MetaTrader 4 terminal. The MarketTrader program is written in the MQL4 language, which is part of MetaTrader4. The MarketTrader program determines the signals to buy and sell a currency based on the progression of the differences between two moving averages MA1 8-28, MA2 50-222 from the CCI 50-222 indicator. The MarketTrader program has an additional utility MarketTraderOptimizer written in C++ , the main task of which is to periodically select periods MA1, MA2, CCI using sequential opening of buy sell deals . The result of the MarketTraderOptimizer utility is written to a configuration text file for subsequent use in the MarketTrader program . The MarketTraderOptimizer program should be run periodically depending on the minimum timeframe in the configuration file. Before using the MarketTraderOptimizer , you need to run the UpdateBars indicator on a chart with a minimum timeframe from the MarketTrader configuration file . The result of the MarketTrader program : the average number of profitable trades is ten to twelve times higher than the number of non-profitable trades per day, based on the fact that automatic trading is carried out on 23 currency pairs. The MarketTrader program is suitable for trading currency pairs, cryptocurrencies, stocks, futures, options. |