coin_alerts/Picasso.cs
2025-01-12 18:35:52 +05:30

157 lines
6.9 KiB
C#

using SignalsTest;
using System;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Newtonsoft.Json;
public class Picasso{
public static void DrawChart(List<TAReport> reports, decimal max, decimal min, decimal volMax, string filename="test"){
// float height = (float)Math.Ceiling(max - min) * 2f;
// float width = ((float)height / 9f) * 16f;
decimal heightRange = max-min;
float height=1080;
float width = 1920;
float widthMultiplier = width / (float)reports.Count;
float heightMultiplier = (height/ (float)heightRange) * 0.6f;
float candlesOffset = 0.3f * height;
float volumeSize = 0.2f * height;
using (Image img = new Image<Rgba32>((int)width + 100, (int)height, Color.FromRgb(13,18,25))){
#region CANDLES
for(int i=0; i < reports.Count; i++){
// img.Mutate(ctx=> ctx.DrawLine()))
PointF[] points = new PointF[2];
float openVal1 = (float)(reports[i].candle.Open - min) * heightMultiplier;
float closeVal1 = (float)(reports[i].candle.Close - min)* heightMultiplier;
points[0] =new PointF(i * widthMultiplier, openVal1 + candlesOffset);
points[1] =new PointF(i * widthMultiplier, closeVal1+ candlesOffset);
PointF[] rangePoints = new PointF[2];
float highVal = (float)(reports[i].candle.High-min)* heightMultiplier;
float lowVal = (float)(reports[i].candle.Low-min)* heightMultiplier;
rangePoints[0] = new PointF(i * widthMultiplier, highVal+ candlesOffset);
rangePoints[1] = new PointF(i * widthMultiplier, lowVal+ candlesOffset);
PointF[] volumePoints = new PointF[2];
volumePoints[0] = new PointF(i * widthMultiplier, 0);
volumePoints[1] = new PointF(i * widthMultiplier, ((float)reports[i].candle.Volume / (float)volMax) * volumeSize);
Color candleColor = reports[i].candle.Close > reports[i].candle.Open ? Color.Green : Color.Red;
img.Mutate(ctx=> ctx.
DrawLine(candleColor, 10, points).
DrawLine(candleColor, 3, rangePoints).
DrawLine(candleColor, 10, volumePoints)
);
#endregion
#region TWS
PointF[] bottomPoints = new PointF[2];
bottomPoints[0] = new PointF(i * widthMultiplier, lowVal+ candlesOffset);
bottomPoints[1] = new PointF(i * widthMultiplier, lowVal- 15+ candlesOffset);
if(reports[i].TWS > 1){
Color TWSColor = Color.Blue;
if(reports[i].TWS == 2){
TWSColor = Color.Cyan;
}else if(reports[i].TWS== 3){
TWSColor = Color.White;
}
float TWSHeight = 15;
if(i > 22){
bool hasCrossedST = false;
bool hasCrossedMA = false;
for(int z = 0; z < 20; z++){
if(!reports[i-z].STUp && reports[i].STUp){
hasCrossedST=true;
}
if(reports[i-z].SMA20 < reports[i-z].Open && reports[i].SMA20 > reports[i].Open){
hasCrossedMA=true;
}
}
if(hasCrossedMA) TWSHeight+=15;
if(hasCrossedST) TWSHeight+=15;
}
img.Mutate(ctx=>ctx.DrawLine(TWSColor, TWSHeight, bottomPoints));
}
#endregion
}
// Console.WriteLine("Getting paths");
// object t = GetPropByName(reports[reports.Count- 1], "SMA7");
// Console.WriteLine(t);
// Console.WriteLine(float.Parse(t.ToString()));
#region OVERLAY LINES
//Overlay
IPath sma20Path = GetPath(reports, "SMA20", widthMultiplier,(float)heightMultiplier, (float)min, candlesOffset);
IPath sma50Path = GetPath(reports, "SMA50", widthMultiplier,(float)heightMultiplier, (float)min, candlesOffset);
IPath sma200Path = GetPath(reports, "SMA200", widthMultiplier,(float)heightMultiplier, (float)min, candlesOffset);
IPath stPath = GetPath(reports, "ST", widthMultiplier,(float)heightMultiplier, (float)min, candlesOffset);
#endregion
//NewChart
FontFamily fontFamily;
float TextFontSize = 64f;
string TextFont = "Noto Sans Mono";
if (!SystemFonts.TryGet(TextFont, out fontFamily))
throw new Exception($"Couldn't find font {TextFont}");
var font = fontFamily.CreateFont(TextFontSize, FontStyle.Regular);
img.Mutate(ctx => ctx.Draw(Color.Yellow, 2, sma20Path).
Draw(Color.Purple, 2, sma50Path).
Draw(Color.Cyan, 2, sma200Path).
Draw(Color.Green, 3, stPath).
Flip(FlipMode.Vertical).
DrawText(filename, font,new Color(Rgba32.ParseHex("#FFFFFF")), new PointF(10,10)));
img.Save($"{filename}.png");
}
}
#region HELPERS
static IPath GetPath(List<TAReport> reports, string propName, float widthMultiplier,float heightMultiplier, float min, float offset =0){
PathBuilder builder = new PathBuilder();
builder.SetOrigin(new PointF(0,0));
for(int i=0; i < reports.Count; i++){
float newVal = float.Parse(GetPropByName(reports[i],propName).ToString() ?? "0");
// Console.WriteLine( newVal);
float prevVal = i > 0 ? float.Parse(GetPropByName(reports[i-1],propName).ToString() ?? "0") : 0;
// Console.WriteLine( prevVal);
float preValY =( prevVal - min) * heightMultiplier ;
float newValY=(newVal - min) * heightMultiplier;
PointF prevPoint = new PointF((i-1)* widthMultiplier, (preValY) +offset);
PointF newPoint = new PointF(i * widthMultiplier, (newValY) + offset);
// Console.WriteLine(newValY);
builder.AddLine(prevPoint, newPoint);
}
return builder.Build();
}
static object GetPropByName(TAReport report, string propName){
string json = report.toJson();
Dictionary<string,string> dic = JsonConvert.DeserializeObject<Dictionary<string,string>>(json) ?? new Dictionary<string, string>();
// Console.WriteLine(dic[propName]);
return dic[propName];
}
#endregion
}