#include // Create canvas for drawing int width = 800; // Canvas width int height = 600; // Canvas height string canvas_name = "ButterflyCurve"; // Create canvas object CCanvas canvas; if(!canvas.CreateBitmapLabel(canvas_name, 200, 50, width, height, COLOR_FORMAT_XRGB_NOALPHA)) { Print("Error creating canvas: ", GetLastError()); return; } // Set up drawing parameters int points = 1000; // Number of points double t_start = 0; // Start angle double t_end = 12 * M_PI; // End angle double step = (t_end - t_start) / (points - 1); // Calculate curve points double x[], y[]; ArrayResize(x, points); ArrayResize(y, points); for(int i = 0; i < points; i++) { double t = t_start + i * step; double expr = MathExp(MathCos(t)) - 2 * MathCos(4 * t) - MathPow(MathSin(t/12), 5); x[i] = MathSin(t) * expr; y[i] = MathCos(t) * expr; } // Find coordinate bounds for scaling double x_min = x[ArrayMinimum(x)]; double x_max = x[ArrayMaximum(x)]; double y_min = y[ArrayMinimum(y)]; double y_max = y[ArrayMaximum(y)]; // Scale points to canvas coordinates int x_px[], y_px[]; ArrayResize(x_px, points); ArrayResize(y_px, points); double x_scale = (width - 40) / (x_max - x_min); double y_scale = (height - 40) / (y_max - y_min); double scale = MathMin(x_scale, y_scale); // Maintain aspect ratio for(int i = 0; i < points; i++) { x_px[i] = (int)((x[i] - x_min) * scale) + 20; y_px[i] = height - (int)((y[i] - y_min) * scale) - 20; // Flip Y-axis for canvas coordinates // Draw the curve canvas.Erase(ColorToARGB(clrWhite, 255)); // White background // Set line color - we'll pass this directly to Line() method uint line_color = ColorToARGB(clrBlue, 255); // Draw the polyline for(int i = 1; i < points; i++) { canvas.Line(x_px[i-1], y_px[i-1], x_px[i], y_px[i], line_color); } // Title canvas.FontSet("Arial", 20, FW_BOLD); canvas.TextOut(300, 20, "Butterfly Curve", ColorToARGB(clrBlack, 255)); // Update display canvas.Update(); //--- plot Butterfly #property indicator_label1 "Butterfly Oscillator" #property indicator_type1 DRAW_LINE #property indicator_color1 DodgerBlue #property indicator_width1 2 #property indicator_style1 STYLE_SOLID //--- indicator levels #property indicator_level1 2.5 #property indicator_level2 -2.5 #property indicator_level3 0.0 //--- input parameters input bool UsePriceStep = false; // Use ClosePrice as Step size for t increment input double tmStep = 0.05; // Step size for t increment double CalButterflyValue(int bar_index, double bar_close, double bar_open) { double tStep = UsePriceStep ? MathMod((bar_close - bar_open) / _Point, tmStep) : tmStep; double t = bar_index * tStep; // Butterfly curve formula double x = MathSin(t) * (MathExp(MathCos(t)) - 2.0 * MathCos(4.0 * t) - MathPow(MathSin(t / 12.0), 5)); return (x); } int pStart = prev_calculated == 0 ? 0 : prev_calculated - 1; for(int i = pStart; i < rates_total; i++) { double bar_close = close[i]; double bar_open = open[i]; ButterflyBuffer[i] = CalButterflyValue(i, bar_close, bar_open); }