NexusFi: Find Your Edge


Home Menu

 





Translate ThinkScript to EasyLanguage with Switch Statement


Discussion in EasyLanguage Programming

Updated
    1. trending_up 521 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 2 posts
    2. attach_file 0 attachments




 
Search this Thread

Translate ThinkScript to EasyLanguage with Switch Statement

  #1 (permalink)
ABTrades
Houston, Texas
 
Posts: 2 since Mar 2023
Thanks Given: 0
Thanks Received: 0

Good Afternoon,
I am new to coding and trying to translate some ThinkScript Code into EasyLanguage and having a hard time with the Switch Statement. I pasted the ThinkScript and my EasyLanguage code below for reference. Any assistance would be greatly appreciated.
Sincerely, Adam



ThinkScript Code:

input period = 50;
input movingAverageType = {default TEMA, Hull, Exponential};

def openMA;
def closeMA;
def highMA;
def lowMA;

switch (movingAverageType) {
case Exponential:
openMA = CompoundValue(1, ExpAverage(open, period), open);
closeMA = CompoundValue(1, ExpAverage(close, period), close);
highMA = CompoundValue(1, ExpAverage(high, period), high);
lowMA = CompoundValue(1, ExpAverage(low, period), low);
case Hull:
openMA = CompoundValue(1, HullMovingAvg(open, period), open);
closeMA = CompoundValue(1, HullMovingAvg(close, period), close);
highMA = CompoundValue(1, HullMovingAvg(high, period), high);
lowMA = CompoundValue(1, HullMovingAvg(low, period), low);
case TEMA:
openMA = CompoundValue(1, TEMA(open, period), open);
closeMA = CompoundValue(1, TEMA(close, period), close);
highMA = CompoundValue(1, TEMA(high, period), high);
lowMA = CompoundValue(1, TEMA(low, period), low);
}

def haOpen;
def haClose;

haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0 ) / 2.0), open);
haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);

plot o = haOpen ;
plot k = haClose;

AssignPriceColor(if haClose >= haOpen
then Color.GREEN else
if haClose < haOpen
then Color.RED else Color.WHITE);




EasyLanguage Code:

inputs:
period(50),
number(1);

Vars: openMA(0),
closeMA(0),
highMA(0),
lowMA(0),
haOpen(0),
haClose(0),
haLow(0),
haHigh(0);

once
Begin
switch (number)
begin
case = 1:

If barnumber()==1 then Begin
openMA = Open;
End
else Begin
openMA = XAverage(open, period);
End;

If barnumber()==1 then Begin
closeMA = Close;
End
else Begin
closeMA = XAverage(close, period);
End;

If barnumber()==1 then Begin
highMA = high;
End
else Begin
highMA = XAverage(high, period);
End;

If barnumber()==1 then Begin
lowMA = low;
End
else Begin
lowMA = XAverage(low, period);
End;

case = 2 :

If barnumber()==1 then Begin
openMA = Open;
End
else Begin
openMA = HMA(open, period);
End;

If barnumber()==1 then Begin
closeMA = close;
End
else Begin
closeMA = HMA(close, period);
End;

If barnumber()==1 then Begin
highMA = high;
End
else Begin
highMA = HMA(high, period);
End;

If barnumber()==1 then Begin
lowMA = low;
End
else Begin
lowMA = HMA(low, period);
End;

case = 3:

If barnumber()==1 then Begin
openMA = Open;
End
else Begin
openMA = TEMA(open, period);
End;

If barnumber()==1 then Begin
closeMA = close;
End
else Begin
closeMA = TEMA(close, period);
End;

If barnumber()==1 then Begin
highMA = high;
End
else Begin
highMA = TEMA(high, period);
End;

If barnumber()==1 then Begin
lowMA = low;
End
else Begin
lowMA = TEMA(low, period);
End;

End;

If barnumber()==1 then Begin
haOpen = open;
End
else Begin
haOpen = ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0) / 2.0);
End;

haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);

Begin
If haClose crosses over haOpen then buy ("BUY") next bar at Market;
If haClose crosses below haOpen then sell short ("SELL SHORT") next bar at market;
End

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
What broker to use for trading palladium futures
Commodities
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
Trade idea based off three indicators.
Traders Hideout
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
  #2 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

ABTrades,

can you describe the exact problems you are having?
In general, there is no need to use a switch statement, and you can accomplish the same behavior using if... else conditional branching.

Your repeated usage of "If barnumber()==1 then" can also be condensed into just one check. Something along the lines of:
 
Code
if CurrentBar = 1 then 
begin
   openMA = Open;
   highMA = high;
   //add your other variables here, too
end
else
begin
    ///either use a switch of if... else statement to check which MA type should be computed

end ;
Regards,

ABCTG

Follow me on Twitter Reply With Quote
  #3 (permalink)
ABTrades
Houston, Texas
 
Posts: 2 since Mar 2023
Thanks Given: 0
Thanks Received: 0


Thank you ABCTG,

I think I have this crossover working now. I appreciate the help!


if CurrentBar = 1 then
begin
openMA = Open;
highMA = high;

end
else
begin
switch (number)
begin
case = 1:
openMA = TEMA(open, period);
closeMA = TEMA(close, period);
highMA = TEMA(high, period);
lowMA = TEMA(low, period);
case = 2:
openMA = HMA(open, period);
closeMA = HMA(close, period);
highMA = HMA(high, period);
lowMA = HMA(low, period);
case = 3:
openMA = XAverage(open, period);
closeMA = XAverage(close, period);
highMA = XAverage(high, period);
lowMA = XAverage(low, period);
end;

haOpen = ((haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0) / 2.0);
haClose = (((openMA + highMA + lowMA + closeMA) / 4.0 + haOpen + maxlist(highMA, haOpen) + minlist(lowMA, haOpen)) / 4.0);

end;

plot1 (haOpen);
plot2 (haClose);

Reply With Quote




Last Updated on March 25, 2023


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts