переделать приложения программу F в программу Python:

Settings={
Name="gazp_imoex2_2_v1",
prf=0, -- =1 - показать профит		
sign=0,	-- =1 - показать сигнал	
Len1=40, -- длина средней
Len2=30, -- длина средней
Len3=5, -- длина средней
SLP=1.0,	-- stop loss в %
line=                                     
                {  
                    {  
                        Name = "line1",
                        Type =TYPE_LINE,
                        Width = 1,
                        Color = RGB(0,0,255)
                    },
                    {  
                        Name = "line2",
                        Type =TYPE_LINE,
                        Width = 1,
                        Color = RGB(0,0,255)
                    },					
					{  
                        Name = "TRIANGLE_DOWN",
                        Type =TYPE_TRIANGLE_DOWN,
                        Width = 5,
                        Color = RGB(255, 0, 0)
                    },
					{  
                        Name = "TRIANGLE_UP",
                        Type =TYPE_TRIANGLE_UP,
                        Width = 5,
                        Color = RGB(0, 0, 255)
                    },
					{  
                        Name = "cur5",
                        Type =TYPE_POINT,
                        Width = 5,
                        Color = RGB(255, 0, 255)
                    },
					{  
                        Name = "cur6",
                        Type =TYPE_POINT,
                        Width = 5,
                        Color = RGB(0, 0, 255)
                    }					
                }
}

function Init()
  prof = {}
  sl = {}    
  pos = {}   
  otn = {}
  return 6
  
end


function OnCalculate(index)

  Len1 = Settings.Len1  
  Len2 = Settings.Len2  
  Len3 = Settings.Len3     

if index == 1 then
  brs = Size()
	
  imoex2 = {}
  gazp = {}
  otn = {}  
end 

if index == 1 or brs ~= Size() then
  brs = Size()
  
  imoex2_id = "imoex2"
  number_of_candles_imoex2 = getNumCandles(imoex2_id)
  imoex2_from_graph, a, b = getCandlesByIndex(imoex2_id, 0, 0, number_of_candles_imoex2)  
  for i = 1, number_of_candles_imoex2 do
	imoex2[i] = imoex2_from_graph[i-1].open
	if i > 1 and imoex2[i] == 0 then 
		imoex2[i] = imoex2[i-1]
	end 
  end    
  
  gazp_id = "gazp"
  number_of_candles_gazp = getNumCandles(gazp_id)
  gazp_from_graph, a, b = getCandlesByIndex(gazp_id, 0, 0, number_of_candles_gazp)  
  for i = 1, number_of_candles_gazp do
	gazp[i] = gazp_from_graph[i-1].open
	if i > 1 and gazp[i] == 0 then 
		gazp[i] = gazp[i-1]
	end 
  end  

else

  imoex2[index] = imoex2_from_graph[index-1].open
	if index-2 >= 0 and imoex2[index] ~= nil then 
	  if imoex2[index] == 0 then 
	    if imoex2[index-1] ~= nil then 
          imoex2[index] = imoex2[index-1]
		end 
	  end 
	else  
      imoex2[index] = imoex2[index-1]	
	end 
	
  gazp[index] = gazp_from_graph[index-1].open
	if index-2 >= 0 and gazp[index] ~= nil then 
	  if gazp[index] == 0 then 
		if gazp[index-1] ~= nil then 
          gazp[index] = gazp[index-1]
		end 		
	  end 
	else  
      gazp[index] = gazp[index-1]	
	end 		

end 

  if imoex2[index]~= 0 then 
    otn[index] = gazp[index]/imoex2[index]
  else 
    otn[index] = 0
  end 

  if imoex2[index]~= nil and gazp[index]~= nil then

    
	  -- SMA1
	  if index == 1 then 
        sum1 = {}
		sma1 = {}  	
		n1 = {}				
		sum1[index] = imoex2[index]
		n1[index]=1	
	  else		
		sum1[index] = imoex2[index] + sum1[index-1]
		n1[index]= n1[index-1] + 1			
		if index > Len1 then 
		  sum1[index] = sum1[index-1] + imoex2[index]
		  sum1[index] = sum1[index] - imoex2[index-Len1]
		  n1[index] = n1[index-1]
        end    		
	  end 
	  
	  -- SMA2
	  if index == 1 then 
        sum2 = {}
		sma2 = {}  	
		n2 = {}				
		sum2[index] = gazp[index]
		n2[index]=1	
	  else		
		sum2[index] = gazp[index] + sum2[index-1]
		n2[index]= n2[index-1] + 1			
		if index > Len2 then 
		  sum2[index] = sum2[index-1] + gazp[index]
		  sum2[index] = sum2[index] - gazp[index-Len2]
		  n2[index] = n2[index-1]
        end    		
	  end 	    

	 
	  -- SMA3
	  if index == 1 then 
        sum3 = {}
		sma3 = {}  	
		n3 = {}				
		sum3[index] = otn[index]
		n3[index]=1	
	  else		
	    if sum3[index-1] == nil then 
		  sum3[index] = 0
		  n3[index] = 1
		else
		sum3[index] = otn[index] + sum3[index-1]
		n3[index]= n3[index-1] + 1			
		if index > Len3 then 
		  sum3[index] = sum3[index-1] + otn[index]
		  sum3[index] = sum3[index] - otn[index-Len3]
		  n3[index] = n3[index-1]
        end  
        end   		
	  end 	 	  
	  
	  
	  if n1[index] ~= nil then 
		if n1[index] ~= 0 then 
		  sma1[index] = sum1[index]/n1[index]
		end  
	  end 
	  
	  if n2[index] ~= nil then 
		if n2[index] ~= 0 then 
		  sma2[index] = sum2[index]/n2[index]
		end  
	  end 	

     
	  if n3[index] ~= nil then 
		if n3[index] ~= 0 then 
		  sma3[index] = sum3[index]/n3[index]
		end  
	  end 
  end
	  	  
      --[[ --]]
   
 --  return otn[index], sma3[index]
 
   if index == 1 then
    prof = {}
	pos = {}
	f = {}
	prof[index] = 0  
	pos[index] = 0  
	f[index] = 0
  else 	
	prof[index] = prof[index-1] 
	pos[index] = pos[index-1]
	f[index] = f[index-1]
  end 	  
  
  if gazp[index] ~= nil and imoex2[index]  ~= nil and
     gazp[index-1]  ~= nil and imoex2[index-1]  ~= nil and
     sma1[index]  ~= nil and sma2[index]  ~= nil and
     sma1[index-1]  ~= nil and sma2[index-1]  ~= nil  
  then 
  
    kf2 = imoex2[index]/gazp[index]
	kf3 = imoex2[index]/otn[index]
  
    f[index] = (imoex2[index] - sma1[index]) +
	    kf2*(gazp[index] - sma2[index]) +
		kf3*(otn[index] - sma3[index])
  
	  if (
	    f[index] > 0 	
        ) and 
		pos[index] ~= 1
	  then -- long 
		pos[index] = 1
	  else   
		if 
	      f[index] < 0 	and 
		  pos[index] ~= -1    
		  --otn[index-1] > sma3[ind1[index-1] ] 
		then -- short
		  pos[index] = -1    
		end 
	  end   
  
  end   
 
  -- signals
  if index == Size() then 
   if Settings.sign == 1 then   
    for i = 2, Size() do 
	  if pos[i] ~= nil and pos[i-1] ~= nil then 
	    if pos[i-1] ~= 1 and pos[i] == 1 then
	      SetValue(i, 4, gazp[i])	
	    else	  
	     if pos[i-1] ~= -1 and pos[i] == -1 then
	       SetValue(i, 3, gazp[i])		
	     else
	       SetValue(i, 3, nil)	
	       SetValue(i, 4, nil)	
	     end 
         if pos[i] ~= pos[i-1] and pos[i] == 0 then
		   if pos[i-1] == 1 then 
	         SetValue(i, 5, gazp[i])
           else 	
             SetValue(i, 6, gazp[i])				 
           end 			 
         end  
	    end 
	  end 
    end 
   end	
  end   
  
  -- profit 
  if Settings.prf==1 then  
  
    if pos[index-1] ~= nil then 
      if pos[index-1] == 1 then 
        prof[index] = prof[index-1] +	gazp[index] - gazp[index-1]  
	  end 
      if pos[index-1] == -1 then 
        prof[index] = prof[index-1] +	gazp[index-1] - gazp[index]
	  end 	
	  if pos[index-1] == 0 then 
	    prof[index] = prof[index-1]
	  end 
    end  
    
	return prof[index]
  else   
    if Settings.sign == 1 then   
      return nil 
	else   
	  return imoex2[index]
	end 
  end    
end