using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSAR_Analyze
{
// PSAR Class
public class PSAR
{
public PSAR()
{
this.m_high_prices_5 = new List<double>();
this.m_low_prices_5 = new List<double>();
this.m_ep = -9999;
this.m_prev_ep = -9999;
this.m_acc = 0.02;
this.m_acc_upper = 0.2;
this.m_init_psar = -9999;
this.m_psar = -9999;
this.m_psar_inter = -9999;
this.m_trend_up = false;
this.m_prev_trend_up = false;
}
public void CalculatePSAR(double p_open_price, double p_close_price, double p_high_price, double p_low_price)
{
if (this.m_ep == -9999)
{
this.m_ep = p_low_price;
this.m_psar = p_high_price;
this.m_trend_up = false;
}
this.m_high_prices_5.Add(p_high_price);
// Keep only the latest close prices
if (this.m_high_prices_5.Count == 6)
{
this.m_high_prices_5.RemoveAt(0);
}
this.m_low_prices_5.Add(p_low_price);
// Keep only the latest close prices
if (this.m_low_prices_5.Count == 6)
{
this.m_low_prices_5.RemoveAt(0);
}
this.m_psar_inter = (this.m_psar - this.m_ep) * this.m_acc;
if (this.m_trend_up == false)
{
List<double> init_psar_list = new List<double>();
init_psar_list.Add(this.m_psar - this.m_psar_inter);
if (this.m_high_prices_5.Count == 1)
{
init_psar_list.Add(m_high_prices_5[0]);
}
else
{
init_psar_list.Add(this.m_high_prices_5[this.m_high_prices_5.Count - 2]);
init_psar_list.Add(this.m_high_prices_5.Last());
}
this.m_init_psar = init_psar_list.Max();
}
else
{
List<double> init_psar_list = new List<double>();
init_psar_list.Add(this.m_psar - this.m_psar_inter);
if (this.m_low_prices_5.Count == 1)
{
init_psar_list.Add(m_low_prices_5[0]);
}
else
{
init_psar_list.Add(this.m_low_prices_5[this.m_low_prices_5.Count - 2]);
init_psar_list.Add(this.m_low_prices_5.Last());
}
this.m_init_psar = init_psar_list.Min();
}
if (this.m_trend_up == false && this.m_high_prices_5.Last() < this.m_init_psar)
{
this.m_psar = this.m_init_psar;
}
if (this.m_trend_up == true && this.m_low_prices_5.Last() > this.m_init_psar)
{
this.m_psar = this.m_init_psar;
}
if (this.m_trend_up == false && this.m_high_prices_5.Last() >= this.m_init_psar)
{
this.m_psar = this.m_ep;
}
if (this.m_trend_up == true && this.m_low_prices_5.Last() <= this.m_init_psar)
{
this.m_psar = this.m_ep;
}
if (this.m_psar > p_close_price)
{
this.m_prev_trend_up = this.m_trend_up;
this.m_trend_up = false;
}
else
{
this.m_prev_trend_up = this.m_trend_up;
this.m_trend_up = true;
}
if (this.m_trend_up == false)
{
this.m_prev_ep = this.m_ep;
this.m_ep = Math.Min(this.m_ep, p_low_price);
}
else
{
this.m_prev_ep = this.m_ep;
this.m_ep = Math.Max(this.m_ep, p_high_price);
}
if (this.m_trend_up == this.m_prev_trend_up && this.m_ep != this.m_prev_ep && this.m_acc < this.m_acc_upper)
{
this.m_acc += 0.02;
}
if (this.m_trend_up != this.m_prev_trend_up)
{
this.m_acc = 0.02;
}
}
public List<double> m_high_prices_5;
public List<double> m_low_prices_5;
public double m_ep { get; set; }
public double m_prev_ep { get; set; }
public double m_acc { get; set; }
public double m_acc_upper { get; set; }
public double m_init_psar { get; set; }
public double m_psar_inter { get; set; }
public double m_psar { get; set; }
public bool m_trend_up { get; set; }
public bool m_prev_trend_up { get; set; }
}
class Program
{
static void Main(string[] args)
{
PSAR psar = new PSAR();
psar.CalculatePSAR(249.419998, 249.080002, 249.699997, 248.809998);
psar.CalculatePSAR(249.880005, 250.050003, 250.490005, 248.869995);
psar.CalculatePSAR(249.729996, 250.350006, 250.440002, 249.630005);
psar.CalculatePSAR(250.339996, 251.229996, 251.320007, 250.130005);
psar.CalculatePSAR(251.490005, 252.320007, 252.320007, 251.289993);
psar.CalculatePSAR(252.320007, 252.860001, 252.889999, 252.229996);
psar.CalculatePSAR(252.690002, 253.160004, 253.440002, 252.559998);
psar.CalculatePSAR(253.539993, 254.660004, 254.679993, 253.199997);
psar.CalculatePSAR(254.149994, 254.369995, 254.699997, 253.850006);
psar.CalculatePSAR(254.630005, 253.949997, 254.699997, 253.649994);
psar.CalculatePSAR(254.600006, 254.619995, 255.050003, 253.979996);
psar.CalculatePSAR(254.509995, 255.020004, 255.020004, 254.320007);
psar.CalculatePSAR(254.660004, 254.639999, 255.059998, 254.369995);
psar.CalculatePSAR(255.139999, 254.949997, 255.270004, 254.639999);
psar.CalculatePSAR(255.210007, 255.289993, 255.509995, 254.820007);
psar.CalculatePSAR(255.229996, 255.470001, 255.520004, 254.979996);
psar.CalculatePSAR(255.899994, 255.720001, 255.949997, 255.5);
psar.CalculatePSAR(254.830002, 255.789993, 255.830002, 254.350006);
psar.CalculatePSAR(256.700012, 257.109985, 257.140015, 255.770004);
psar.CalculatePSAR(257.480011, 256.109985, 257.51001, 256.019989);
psar.CalculatePSAR(256.600006, 256.559998, 256.829987, 256.149994);
psar.CalculatePSAR(256.179993, 255.289993, 256.309998, 254);
psar.CalculatePSAR(255.990005, 255.619995, 256.299988, 255.479996);
psar.CalculatePSAR(256.470001, 257.709991, 257.890015, 255.630005);
psar.CalculatePSAR(256.470001, 256.75, 257.600006, 256.410004);
psar.CalculatePSAR(257.179993, 257.149994, 257.440002, 256.809998);
psar.CalculatePSAR(258.040009, 257.48999, 258.429993, 257.070007);
psar.CalculatePSAR(257.410004, 257.589996, 257.75, 256.190002);
psar.CalculatePSAR(257.769989, 258.450012, 258.5, 257.299988);
DateTime cur_day = new DateTime(2017, 11, 3);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(258.299988, 258.850006, 259, 258.220001);
cur_day = cur_day.AddDays(3);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(258.970001, 258.670013, 259.350006, 258.089996);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(258.470001, 259.109985, 259.220001, 258.149994);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(257.730011, 258.170013, 258.390015, 256.359985);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(257.730011, 258.089996, 258.290009, 257.369995);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(257.309998, 258.329987, 258.589996, 257.269989);
cur_day = cur_day.AddDays(3);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(257.410004, 257.730011, 257.850006, 256.519989);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(256.619995, 256.440002, 257.220001, 255.630005);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(257.519989, 258.619995, 259.040009, 257.470001);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(258.220001, 257.859985, 258.589996, 257.769989);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(258.140015, 258.299988, 258.519989, 257.859985);
cur_day = cur_day.AddDays(3);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(259.179993, 259.98999, 260.200012, 258.26001);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(260, 259.76001, 260.149994, 259.570007);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(260.320007, 260.359985, 260.480011, 260.160004);
cur_day = cur_day.AddDays(2);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(260.410004, 260.230011, 260.75, 260);
cur_day = cur_day.AddDays(3);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(260.76001, 262.869995, 262.899994, 260.649994);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(263.019989, 262.709991, 263.630005, 262.200012);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(263.76001, 265.01001, 266.049988, 263.670013);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(264.76001, 264.459991, 265.309998, 260.76001);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(266.309998, 264.140015, 266.799988, 264.079987);
cur_day = cur_day.AddDays(3);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
psar.CalculatePSAR(263.190002, 263.190002, 265.149994, 263.040009);
cur_day = cur_day.AddDays(1);
Console.WriteLine(String.Format("DATE: {0}, PSAR: {1}, TREND_UP: {2}", cur_day.ToShortDateString(), Math.Round(psar.m_psar, 2), psar.m_trend_up));
Console.ReadLine();
}
}
}
|