!=====================================================================
!   Skadron Prize 2005
!   The stock game.
!   This is the master program to test your subroutine.
!====================================================================
           Program stock_game
           integer, parameter :: itmax=500
           integer, parameter :: ipmax=1
           integer :: it,ip
           real :: time,price,nshares(ipmax),aprice(itmax)
           integer :: buysellhold(ipmax)
           integer :: buysellhold1
           real :: cash(ipmax), stock(ipmax)
           real :: cash1, stock1,nshares1

           call openfiles()
           do ip=1,ipmax
              cash(ip)=10.
              stock(ip)=0.
           end do

           call allprice(itmax,aprice)
           do it=1, itmax
              write(55,*) it, aprice(it)
           end do

           do it=1,itmax
               time=it
               price=aprice(it)

               ip=1; cash1=cash(ip); stock1=stock(ip)
               call stu1(time,price,cash1,stock1,buysellhold1,nshares1)
                  buysellhold(ip)=buysellhold1
                  nshares(ip)=nshares1

               call trade(price,cash,stock,buysellhold,nshares)
               write(*,*)'pr ca st tot',price, cash(ip),stock(ip), &
        &         cash(ip)+stock(ip)*price
           end do
           call closefiles()
           stop
           end

           subroutine stu1(time,price,cash,stock,buysellhold,nshares)
!=====================================================================
!  Replace this subroutine with the one you write.
!=====================================================================
           real :: time, price, cash, stock,nshares
           integer :: buysellhold
              if (price .lt. 10. .and. cash .gt. 0.) then
                 buysellhold=1;nshares=cash/price
                 cash=0.
              elseif (price .ge. 10. .and. stock .gt. 0.) then
                 buysellhold=2;nshares=stock;cash=cash+nshares*price
              else
                 buysellhold=3
              end if
           return
           end

           subroutine allprice(itmax,aprice)
!=====================================================================
!  This is a simplified price function than the one that will be used
!  in the real competiton.
!=====================================================================
           integer :: itmax, it, imax
           real :: aprice(itmax),tpi
           imax=10
           tpi = 8.*atan(1.)

           do it=1, itmax
                 aprice(it)=10.+0.15*sin(tpi*it/200.)
           end do
           return
           end


           subroutine trade(price,cash,stock,buysellhold,nshares)
           integer, parameter :: ipmax=1
           integer :: ip
           real :: price,cash(ipmax),stock(ipmax),nshares(ipmax)
           integer :: buysellhold(ipmax)

           do ip=1,ipmax
               if (buysellhold(ip).eq.1 .and. &
        &       (cash(ip) .lt. (price*nshares(ip)) ) ) then    ! buy signal
                           stock(ip) = stock(ip) + (cash(ip)/price)
                           cash(ip)=0.
               elseif (buysellhold(ip).eq.1 .and. &
        &       (cash(ip) .ge. (price*nshares(ip)) ) ) then
                           stock(ip) = stock(ip) + nshares(ip)
                           cash(ip)=cash(ip)-nshares(ip)*price
               elseif (buysellhold(ip).eq.2 .and. &
        &       (stock(ip) .lt. nshares(ip)) ) then   ! sell signal
                      cash(ip)=cash(ip)+stock(ip)*price
                      stock(ip)=0.
               elseif (buysellhold(ip).eq.2 .and. &
        &       (stock(ip) .ge. nshares(ip)) ) then   ! sell signal
                      cash(ip)=cash(ip)+nshares(ip)*price
                      stock(ip)=stock(ip)-nshares(ip)
               else                                   ! hold signal
               end if
           end do
           return
           end


!====================================================================
         subroutine openfiles()
           open(unit=55,file='stock.dat')   ! Output x-y trajectory
         return
         end
!====================================================================
         subroutine closefiles()
           close(unit=55)
         return
         end

!====================================================================

