PARABOLIC SAR (PSAR) strat working
#1
Hi community,

finally, after fighting windows 10 installation with bash, everything working and trying to implement my first js code I got a basic working PSAR strategy. I know it is a simple task for you experienced coders, but feel happy and quite proud (stupid ego here).

I'd like to share it and I wish that any interested person could help in developing it adding stuff like stoplosses or anything that could be helpful (can't make the log block work for example)

Maybe there is a working PSAR strat out there but i didn´t find it, just the indicator. Anyway the experience and the new opened door to js worth the time spent.

I'd like to know how to proceed correctly, do I share it here or in github?

regards

PS I'm not native english speaker so, please forgive any spelling or grammatical mistakes¡
  Reply
#2
PSAR = Parabolic SAR?

Just post it here, i am interested
  Reply
#3
(09-09-2018, 11:33 PM)BotsEdge Wrote: PSAR = Parabolic SAR?

Just post it here, i am interested

Yes, Parabolic SAR,

Here it is. Hope it help:

indicator:


Code:
var Indicator = function(settings) {
 this.input = 'candle';
 //this.acceleration = 0.02;
 this.acceleration = settings.optInAcceleration;
 //this.maximum = 0.2;
 this.maximum = settings.optInMaximum;
 this.result = 0;
 this.bull = true;
 //this.start = 0.02;
 this.start = settings.optInStart;
}

Indicator.prototype.update = function(candle) {
 if(this.result == 0) {
   this.result = candle.close;
   this.low1 = candle.low;
   this.high1 = candle.high;
   this.low2 = candle.low;
   this.high2 = candle.high;
   this.hp = candle.high;
   this.lp = candle.low;
   this.af = this.start;
   return;
 }

 if(this.bull){
   this.psar = this.result + this.af * (this.hp - this.result)
 } else {
   this.psar = this.result + this.af * (this.lp - this.result)
 }

 let reverse = false;

 if(this.bull){
   if(candle.low < this.psar){
     this.bull = false;
     reverse = true;
     this.psar = this.hp;
     this.lp = candle.low;
     this.af = this.start;
   }
 } else {
   if(candle.high > this.psar){
     this.bull = true;
     reverse = true;
     this.psar = this.lp;
     this.hp = candle.high;
     this.af = this.start;
   }
 }

 if(!reverse){
   if(this.bull){
     if(candle.high > this.hp){
       this.hp = candle.high;
       this.af = Math.min(this.af + this.acceleration, this.maximum);
     }
     if(this.low1 < this.psar)
       this.psar = this.low1;
     if(this.low2 < this.psar)
       this.psar = this.low2;
   } else {
     if(candle.low < this.lp){
       this.lp = candle.low;
       this.af = Math.min(this.af + this.acceleration, this.maximum);
     }
     if(this.high1 > this.psar)
       this.psar = this.high1;
     if(this.high2 > this.psar)
       this.psar = this.high2;
   }
 }

 this.low2 = this.low1;
 this.low1 = candle.low;
 this.high2 = this.high1;
 this.high1 = candle.high;
 this.result = this.psar;
}

module.exports = Indicator;

strategy:


Code:
//mutenroch_rev2

// helpers
var _ = require('lodash');
var log = require('../core/log.js');


// let's create our own method
var method = {};

// prepare everything our method needs
method.init = function() {

 // keep state if adviced or not
 this.adviced = false;  

 // how many candles do we need as a base
 // before we can start giving advice?
 this.requiredHistory = this.tradingAdvisor.historySize;

 // define the indicators we need
 this.addIndicator('psar', 'PSAR', this.settings);
}

// what happens on every new candle?
method.update = function(candle) {
 // nothing!
}


method.check = function(candle) {
 
 this.bull = this.indicators.psar.bull;

 if(this.bull) {
   
   if(this.adviced == false){
   // new uptrend detected
   this.advice('long');
   this.adviced = true;

   }else
   this.advice();
   

 } else {
   
   if(this.adviced == true){
   // new downtrend detected
   this.advice('short');
   this.adviced = false;

   }else
   this.advice();
   }
}

module.exports = method;

settings (for .toml):


Code:
optInAcceleration = 0.02
optInMaximum = 0.2
optInStart = 0.02

B.R.
  Reply


Forum Jump:


Users browsing this thread: