Sunday, August 24, 2014

First attempt at a rustic buy-and-hold forex bot

I've achieved detecting the beginning of tiny price jumps. Maybe if I tune the program's parameters I can make it find the beginning of larger jumps.

Here's the source code. It's a little rough, fyi:


 /*  
      Rustic buy-and-hold  
      Requires fine tuning. Perhaps make it adaptive.  
   
      Vicente Oscar Mier Vela  
      <vomv1988@gmail.com>  
   
      Example  
      $ cat samples_1407099600_1407531600_M1.dat | ./t6 1407099600 1407531600 60 200 20 20  
   
      Example of samples_START_END_TB.dat  
      $ cat samples_1407099600_1407531600_M1.dat | head  
      1407099600  
      1.34247  
      1.34359  
      1407099660  
      1.34285  
      1.34326  
      1407099720  
      1.34291  
      1.34331  
      1407099780  
   
      This one trys to link density of losses with absolute losses using  
      local average minimum / maximum algorithm.  
 */  
 #include <stdio.h>  
 #include <stdlib.h>  
 #include <math.h>  
   
 #define closeBid 0  
 #define closeAsk 1  
   
 int cmp(const void *x, const void *y);  
   
 int main(int argc, char *argv[]){  
      int startdate = atoi(argv[1]);  
      int enddate = atoi(argv[2]);  
      int timebase = atoi(argv[3]);  
   
      int nsamples = (enddate - startdate) / timebase;  
      int expdate;  
      int currdate;  
   
      double *samples[2];  
      samples[closeBid] = malloc(sizeof(double)*nsamples);  
      samples[closeAsk] = malloc(sizeof(double)*nsamples);  
   
      int i;  
      for(i = 0, expdate = startdate; i < nsamples; i ++, expdate += timebase){  
           scanf("%d", &currdate);  
 /*  
           The following assumes that the first date from the  
           dataset will always yield a value. That is: 1st date is  
           always "ticked".  
 */  
           if(currdate == expdate){  
                scanf("%lf", samples[closeBid] + i);  
                scanf("%lf", samples[closeAsk] + i);  
           } else {  
                while(currdate != expdate && i < nsamples){  
                     samples[closeBid][i] = samples[closeBid][i-1];  
                     samples[closeAsk][i] = samples[closeAsk][i-1];  
                     expdate += timebase;  
                     i++;  
                }  
                if(i < nsamples){  
                     scanf("%lf", samples[closeBid] + i);  
                     scanf("%lf", samples[closeAsk] + i);  
                }  
           }  
      }  
   
      int *diffs = (int *) malloc(sizeof(int) * nsamples);  
      diffs[0] = 0;  
      for(i = 1; i < nsamples; i ++)  
           if(samples[closeBid][i] - samples[closeBid][i - 1] < 0)  
                diffs[i] = -1;  
           else if(samples[closeBid][i] - samples[closeBid][i - 1] > 0)  
                diffs[i] = 1;  
           else  
                diffs[i] = 0;  
   
      int *diffs2 = (int *) malloc(sizeof(int) * nsamples);  
      int k;  
      int range = 10;  
      for(i = 0; i < range; i ++)  
           diffs2[i] = 0;  
      for(i = nsamples; i > nsamples - range; i --)  
           diffs2[i] = 0;  
   
      for(i = range; i < nsamples - range; i ++)  
           for(k = range * -1 ; k < 0 ; k ++){  
                if(diffs[i + k] == -1)  
                     diffs2[i] ++;  
           }  
   
      for(i = 0; i < nsamples; i ++)  
           printf("%d >> %d\n", diffs2[i], diffs[i]);  
   
      int j, t = atoi(argv[4]), mxs = atoi(argv[5]), mns = atoi(argv[6]);  
      double *s = (double *) malloc(sizeof(double) * t);  
      double *q;  
      double sum, loc_avg_min = 0, loc_avg_max = 0, bal = 0;  
      int flag = 0;  
      for(i = 0; i + t < nsamples; i ++){  
           q = samples[closeBid] + i;  
   
           for(j=0;j<t;j++)  
                s[j] = q[j];  
   
           qsort(s, t, sizeof(double), cmp);  
   
           for(j = 0, sum = 0; j < mns; sum += s[j], j ++);  
           loc_avg_min = sum / (double) mns;  
   
           for(j = t - 1, sum = 0; j >= t - mxs; sum += s[j], j --);  
           loc_avg_max = sum / (double) mxs;  
   
           if(flag == 0 && samples[closeBid][i + t - 1] <= loc_avg_min){  
                bal -= samples[closeAsk][i + t - 1];  
                flag = 1;  
           }  
   
           if(flag == 1 && samples[closeBid][i + t - 1] >= loc_avg_max){  
                bal += samples[closeBid][i + t - 1];  
                flag = 0;  
                printf("%f %d\n", bal, diffs2[i + t - 1]);  
           }  
      }  
   
      free(samples[closeBid]);  
      free(samples[closeAsk]);  
      free(s);  
      free(diffs);  
      free(diffs2);  
   
      return 0;  
 }  
   
 int cmp(const void *x, const void *y){  
      double xx = *(double*)x, yy = *(double*)y;  
      if (xx < yy) return -1;  
      if (xx > yy) return 1;  
      return 0;  
 }  

1 comment:

  1. eToro is the #1 forex trading platform for beginning and professional traders.

    ReplyDelete