Support Board
Date/Time: Sun, 24 Nov 2024 23:08:13 +0000
Post From: StepMA
[2014-07-26 16:38:52] |
vegasfoster - Posts: 444 |
I did this, but the plot wasn't coming out, see pic. I went through it a couple times and my translation error didn't jump out at me. The only way I know how to resolve this is to break it down by section and compare the plots for the calculations on SC vs NT. However, I don't have NT installed and I have yet to find the motivation to follow through on this. So I am going to post what I have, see attachment. Unless someone else already finished this for you, maybe you can take a look and compare the codes and try to see where it is going wrong, if not then next step is to start going through the individual calculations and comparing to NT. Understand, it may not be exact because of how each program calculates the bar data, but looking at the plot you will be able to see if it working correctly or not. #include "sierrachart.h" SCDLLName("StepMA2") /*==========================================================================*/ SCFloatArrayRef StepSizeCalc(SCStudyGraphRef sc, SCFloatArrayRef result, int Index, int Len, double Km, int Size, int k, int MaType) { if (Index <= Len+1) return result; else { double ATR0, ATRmax = -100000, ATRmin = 1000000; if (Size == 0) { double AvgRange = 0; double Weight = 0; for (int i = Len-1; i >= 0; i--) { double alfa = (MaType == 0) ? 1.0 : 1.0*(Len-i)/Len; AvgRange += alfa*(sc.High[k+i]-sc.Low[k+i]); Weight += alfa; } ATR0 = AvgRange/Weight; if (ATR0 > ATRmax) ATRmax = ATR0; if (ATR0 < ATRmin) ATRmin = ATR0; result[Index] = round(0.5*Km*(ATRmax+ATRmin)/sc.TickSize); } else result[Index] = Km*Size; return result; } } /*==========================================================================*/ SCFloatArrayRef StepMACalc(SCStudyGraphRef sc, SCFloatArrayRef result, int Index, int Len, bool HL, double Size) { SCFloatArrayRef smax = sc.Subgraph[Index]; SCFloatArrayRef smin = sc.Subgraph[Index].Arrays[0]; SCFloatArrayRef trend = sc.Subgraph[Index].Arrays[1]; if (HL) { smax[Index] = sc.Low[Index]+2.0*Size*sc.TickSize; smin[Index] = sc.High[Index]-2.0*Size*sc.TickSize; } else { smax[Index] = sc.Close[Index]+2.0*Size*sc.TickSize; smin[Index] = sc.Close[Index]-2.0*Size*sc.TickSize; } if (Index == Len) { smax[Index-1] = smax[Index]; smin[Index-1] = smin[Index]; trend[Index-1] = 0; } trend[Index] = trend[Index-1]; if (sc.Close[Index] > smax[Index-1]) trend[Index] = 1; if (sc.Close[Index] < smin[Index-1]) trend[Index] = -1; if (trend[Index] > 0) { if (smin[Index] < smin[Index-1]) smin[Index] = smin[Index-1]; result[Index] = smin[Index]+Size*sc.TickSize; } else { if (smax[Index] > smax[Index-1]) smax[Index] = smax[Index-1]; result[Index] = smax[Index]-Size*sc.TickSize; } return result; } /*==========================================================================*/ SCFloatArrayRef TrendCalc(SCStudyGraphRef sc, SCFloatArrayRef trend, int Index, int Len, bool HL, double Size) { SCFloatArrayRef smax = sc.Subgraph[Index]; SCFloatArrayRef smin = sc.Subgraph[Index].Arrays[0]; if (HL) { smax[Index] = sc.Low[Index]+2.0*Size*sc.TickSize; smin[Index] = sc.High[Index]-2.0*Size*sc.TickSize; } else { smax[Index] = sc.Close[Index]+2.0*Size*sc.TickSize; smin[Index] = sc.Close[Index]-2.0*Size*sc.TickSize; } if (Index == Len) { smax[Index-1] = smax[Index]; smin[Index-1] = smin[Index]; trend[Index-1] = 0; } trend[Index] = trend[Index-1]; if (sc.Close[Index] > smax[Index-1]) trend[Index] = 1; if (sc.Close[Index] < smin[Index-1]) trend[Index] = -1; //if (trend[Index] > 0) //{ // if (smin[Index] < smin[Index-1]) smin[Index] = smin[Index-1]; // result[Index] = smin[Index]+Size*sc.TickSize; //} //else //{ // if (smax[Index] > smax[Index-1]) smax[Index] = smax[Index-1]; // result[Index] = smax[Index]-Size*sc.TickSize; //} return trend; } /*==========================================================================*/ SCSFExport scsf_StepMA2(SCStudyInterfaceRef sc) { SCSubgraphRef LineBuffer = sc.Subgraph[0]; SCSubgraphRef UpBuffer = sc.Subgraph[1]; SCSubgraphRef DnBuffer = sc.Subgraph[2]; SCSubgraphRef stepsizeout = sc.Subgraph[10]; SCSubgraphRef stepmaout = sc.Subgraph[11]; SCSubgraphRef trendout = sc.Subgraph[11]; SCInputRef length = sc.Input[0]; SCInputRef kv = sc.Input[1]; SCInputRef stepsize = sc.Input[2]; SCInputRef percentage = sc.Input[3]; SCInputRef updownshift = sc.Input[4]; SCInputRef matype = sc.Input[5]; SCInputRef highlow = sc.Input[6]; SCInputRef colormode = sc.Input[7]; if (sc.SetDefaults) { sc.GraphName = "StepMA2"; sc.AutoLoop = true; sc.FreeDLL = 1; LineBuffer.Name = "Neutral"; LineBuffer.LineWidth = 1; UpBuffer.Name = "Up"; UpBuffer.LineWidth = 1; DnBuffer.Name = "Down"; DnBuffer.LineWidth = 1; length.Name = "Length"; length.SetInt(10); kv.Name = "Sensitivity Factor"; kv.SetDouble(0.5); stepsize.Name = "Step Size"; stepsize.SetInt(20); percentage.Name = "Percentage"; percentage.SetDouble(0.5); updownshift.Name = "Shift in Points"; updownshift.SetDouble(2); matype.Name = "MA Type"; matype.SetCustomInputStrings("SMA;LWMA"); matype.SetCustomInputIndex(0); highlow.Name = "HighLow Mode"; highlow.SetCustomInputStrings("0;1"); highlow.SetCustomInputIndex(0); colormode.Name = "ColorMode"; colormode.SetCustomInputStrings("0;1;2;3"); colormode.SetCustomInputIndex(0); return; } // Do data processing double stepMA; if (sc.Index > length.GetInt()) { StepSizeCalc(sc, stepsizeout, length.GetInt(), sc.Index, kv.GetDouble(), stepsize.GetInt(), 0, matype.GetIndex()); double stepsizeoutb = stepsizeout[sc.Index]; StepMACalc(sc, stepmaout, sc.Index, length.GetInt(), highlow.GetIndex(), stepsizeoutb); TrendCalc(sc, trendout, sc.Index, length.GetInt(), highlow.GetIndex(), stepsizeoutb); stepMA = stepmaout[sc.Index]+percentage.GetDouble()/100.0*stepsizeout[sc.Index]*sc.TickSize; if (colormode.GetIndex() == 0) LineBuffer[sc.Index] = stepMA; if (colormode.GetIndex() == 1) { if (trendout[sc.Index] > 0) { UpBuffer[sc.Index] = stepMA-stepsizeout[sc.Index]*sc.TickSize; } else if (trendout[sc.Index] < 0) { DnBuffer[sc.Index] = stepMA+stepsizeout[sc.Index]*sc.TickSize; } } else if (colormode.GetIndex() == 2) { if (trendout[sc.Index] > 0) { UpBuffer[sc.Index] = stepMA+updownshift.GetDouble()*sc.TickSize; if (trendout[sc.Index-1] < 0) UpBuffer[sc.Index-1] = DnBuffer[sc.Index-1]; } if (trendout[sc.Index] < 0) { DnBuffer[sc.Index] = stepMA-updownshift.GetDouble()*sc.TickSize; if (trendout[sc.Index-1] > 0) DnBuffer[sc.Index-1] = UpBuffer[sc.Index-1]; } } //end if ( colormode == 2) else if (colormode.GetIndex() == 3) { if (trendout[sc.Index] > 0) { UpBuffer[sc.Index] = stepMA+(stepsizeout[sc.Index]+updownshift.GetDouble())*sc.TickSize; } else if (trendout[sc.Index] < 0) { DnBuffer[sc.Index] = stepMA-(stepsizeout[sc.Index]+updownshift.GetDouble())*sc.TickSize; } } //end if ( colormode == 3) else { LineBuffer[sc.Index] = stepMA; } } } |
StepMA-2.cpp - Attached On 2014-07-26 16:33:44 UTC - Size: 8 KB - 653 views StepMA.png / V - Attached On 2014-07-26 16:37:43 UTC - Size: 57.85 KB - 603 views |