Where is the error ?

 

I don't understand where is the error because i get an invalid stake .

//+------------------------------------------------------------------+
//|Funzione Investimento                                             |
//+------------------------------------------------------------------+
   if(contractFound==0)
     {
      amount=(Cassa*Rendita)/(100*N_da_prendere*Quota);
      Print("First Trade");
      Print("Amount: "+DoubleToString(amount));
     }
   else
     {
      //+------------------------------------------------------------------+
      //|  Get Previous Amount                                             |
      //+------------------------------------------------------------------+
      GetGlobalVariable("Amount",valueArray);
      GetGlobalVariable("N_da_prendere",valueArray);
      double previousAmount=StringToDouble(StringFromBuffer(valueArray));
      double N_da_prendere=StringToDouble(StringFromBuffer(valueArray));

      if(isWon==1)
        {
         Newcassa=Cassa+amount*Quota;
         N_da_prendere=N_da_prendere-1;
         amount=amount;
        }
       if(isWon==0)
        {
         Newcassa=Cassa-amount;
         amount=previousAmount+fabs(Cassa-Newcassa)/(Quota*N_da_prendere);
        }

      Print("Previous ContractId: "+contractId);
      Print("Previous Amount: "+DoubleToString(previousAmount));
      Print("Is Previous Trade Won: "+(isWon ? "True" : "False"));
      Print("Previous Trade Sell Price: "+DoubleToString(sellPrice));
      Print("New Amount: "+DoubleToString(amount));
     }