conversion of pine script to Amibroker afl

#1
Hi All, could anyone please help me convert the following pine script code to afl ? This is code for Larry Williams Large Trade Index a.k.a. LWLTI.
per= 8, smthper =5,
" ma = ta.sma(close - nz(close[per]), per)
atr = ta.atr(per)
out = ma/atr * 50 + 50
out := smthit ? variant(type, out, smthper) : out
colorout = out > 50 ? greencolor : redcolor
plot(out, color = colorout, linewidth = 2) "

Thanks in advance.
 
#2
The solution was easy ... I solved it.
By way if anyone needs amibroker code or mt5 code ?
 
Last edited by a moderator:
#5
Here is the mt5 code for LWTI:

#property description "Larry Large Trade Index"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots 1
//--- plot LWTI
#property indicator_label1 "LWTI"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- input parameters
input uint InpPeriod = 8; // Period
//--- indicator buffers
double BufferLWTI[];
double BufferRAW[];
double BufferATR[];
//--- global variables
int period_ma;
int handle_atr;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- set global variables
period_ma=int(InpPeriod<1 ? 1 : InpPeriod);
//--- indicator buffers mapping
SetIndexBuffer(0,BufferLWTI,INDICATOR_DATA);
SetIndexBuffer(1,BufferRAW,INDICATOR_CALCULATIONS);
SetIndexBuffer(2,BufferATR,INDICATOR_CALCULATIONS);
//--- setting indicator parameters
IndicatorSetString(INDICATOR_SHORTNAME,"LWTI("+(string)period_ma+")");
IndicatorSetInteger(INDICATOR_DIGITS,Digits());
//--- setting buffer arrays as timeseries
ArraySetAsSeries(BufferLWTI,true);
ArraySetAsSeries(BufferRAW,true);
ArraySetAsSeries(BufferATR,true);
//--- create ATR handle
ResetLastError();
handle_atr=iATR(NULL,PERIOD_CURRENT,period_ma);
if(handle_atr==INVALID_HANDLE)
{
Print("The iATR(",(string)period_ma,") object was not created: Error ",GetLastError());
return INIT_FAILED;
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//--- Check for the minimum number of bars for calculation

if(rates_total<period_ma) return 0;
//--- Set up buffer arrays as time series.

ArraySetAsSeries(open,true);
ArraySetAsSeries(close,true);
//--- Verification and calculation of the number of bars being calculated
int limit=rates_total-prev_calculated;
if(limit>1)
{
limit=rates_total-period_ma-1;
ArrayInitialize(BufferLWTI,EMPTY_VALUE);
ArrayInitialize(BufferRAW,0);
ArrayInitialize(BufferATR,0);
}
//--- Data preparation
int copied=0,count=(limit==0 ? 1 : rates_total);
copied=CopyBuffer(handle_atr,0,0,count,BufferATR);
if(copied!=count) return 0;
for(int i=limit; i>=0 && !IsStopped(); i--)
BufferRAW=close-close[i+period_ma-1];
//--- Calculation of the indicator

for(int i=limit; i>=0 && !IsStopped(); i--)
{
double MA=MAOnArray(BufferRAW,0,period_ma,0,MODE_SMA,i);
double ATR=BufferATR;
BufferLWTI=(ATR!=0 ? 50*MA/ATR+50 : 0);
}

//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| iMAOnArray() https://www.mql5.com/ru/articles/81 |
//+------------------------------------------------------------------+
double MAOnArray(double &array[],int total,int period,int ma_shift,int ma_method,int shift)
{
double buf[],arr[];
if(total==0) total=ArraySize(array);
if(total>0 && total<=period) return(0);
if(shift>total-period-ma_shift) return(0);
//---
switch(ma_method)
{
case MODE_SMA :
{
total=ArrayCopy(arr,array,0,shift+ma_shift,period);
if(ArrayResize(buf,total)<0) return(0);
double sum=0;
int i,pos=total-1;
for(i=1;i<period;i++,pos--)
sum+=arr[pos];
while(pos>=0)
{
sum+=arr[pos];
buf[pos]=sum/period;
sum-=arr[pos+period-1];
pos--;
}
return(buf[0]);
}
case MODE_EMA :
{
if(ArrayResize(buf,total)<0) return(0);
double pr=2.0/(period+1);
int pos=total-2;
while(pos>=0)
{
if(pos==total-2) buf[pos+1]=array[pos+1];
buf[pos]=array[pos]*pr+buf[pos+1]*(1-pr);
pos--;
}
return(buf[shift+ma_shift]);
}
case MODE_SMMA :
{
if(ArrayResize(buf,total)<0) return(0);
double sum=0;
int i,k,pos;
pos=total-period;
while(pos>=0)
{
if(pos==total-period)
{
for(i=0,k=pos;i<period;i++,k++)
{
sum+=array[k];
buf[k]=0;
}
}
else sum=buf[pos+1]*(period-1)+array[pos];
buf[pos]=sum/period;
pos--;
}
return(buf[shift+ma_shift]);
}
case MODE_LWMA :
{
if(ArrayResize(buf,total)<0) return(0);
double sum=0.0,lsum=0.0;
double price;
int i,weight=0,pos=total-1;
for(i=1;i<=period;i++,pos--)
{
price=array[pos];
sum+=price*i;
lsum+=price;
weight+=i;
}
pos++;
i=pos+period;
while(pos>=0)
{
buf[pos]=sum/weight;
if(pos==0) break;
pos--;
i--;
price=array[pos];
sum=sum-lsum+price*period;
lsum-=array;
lsum+=price;
}
return(buf[shift+ma_shift]);
}
default: return(0);
}
return(0);
}
//+------------------------------------------------------------------+
 

hmsanil

Active Member
#9
Just corrected the error in the Chatgpt code, all credit goes to Mr. bck2ftr

Code:
// Parameters
per = 8;
smthper = 5;

// Moving Average calculation
ma1 = MA(C - Nz(Ref(C, -per)), per);

// ATR calculation
atr1 = ATR(per);

// Output calculation
out = ma1 / atr1 * 50 + 50;

// Conditional smoothed variant function (you'll need to define or adjust this for your needs)
smthit = 1; // Define this according to your logic
variant_type = 1; // Define the variant type, as AFL does not support 'variant' directly

if (smthit)
{
    // Smooth the output with chosen parameters
    out = MA(out, smthper);
}

// Color logic
colorout = IIf(out > 50, colorGreen, colorRed);

// Plot
Plot(out, "Output", colorout, styleThick);
 
Last edited:
#10
Ohh great... That will surely help the afl / pinescript users in this forum.
Btw , I have never used afl or pinescript :(. I have recently started using chatgpt :p

Just corrected the error in the Chatgpt code, all credit goes to Mr. bck2ftr

Code:
// Parameters
per = 8;
smthper = 5;

// Moving Average calculation
ma1 = MA(C - Nz(Ref(C, -per)), per);

// ATR calculation
atr1 = ATR(per);

// Output calculation
out = ma1 / atr1 * 50 + 50;

// Conditional smoothed variant function (you'll need to define or adjust this for your needs)
smthit = 1; // Define this according to your logic
variant_type = 1; // Define the variant type, as AFL does not support 'variant' directly

if (smthit)
{
    // Smooth the output with chosen parameters
    out = MA(out, smthper);
}

// Color logic
colorout = IIf(out > 50, colorGreen, colorRed);

// Plot
Plot(out, "Output", colorout, styleThick);