SPY Option returns calculations 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tonycsa

//@version=4
study("Option Calc O-C & C-C")

//input data
lookback = input(defval = 0, title = "Lookback Period")
HISTORICALCLOSE = input(defval = 20, title = "History open-close")
HISTORICALOPEN = input(defval = 20, title = "History close-close")
difference1 = abs(close-round(close[1]))
difference2 = abs(close-round(open))
difference3 = abs(close[1]-open)
Credit = input(defval = 0, step = 0.01, title ="Strategy -cost/+credit")
Option1 = input(defval = 0, title ="Option1 Dist. From Open")
Option2 = input(defval = 2, title ="Option2 Dist. From Open")
Option3 = input(defval = 4, title ="Option3 Dist. From Open")
Contracts1 = input(defval = 1, title ="# of Contracts #1")
Contracts2 = input(defval = -3, title ="# of Contracts #2")
Contracts3 = input(defval = 2, title ="# of Contracts #3")

//background colors to screen off days of the week
bgcolor( dayofweek==dayofweek.monday ? color.black : color.white, title="Mon", transp=50)
bgcolor( dayofweek==dayofweek.tuesday ? color.black : color.white, title="Tues", transp=50)
bgcolor( dayofweek==dayofweek.wednesday ? color.black : color.white, title="Wed", transp=50)
bgcolor( dayofweek==dayofweek.thursday ? color.black : color.white, title="Thurs", transp=50)
bgcolor( dayofweek==dayofweek.friday ? color.black : color.white, title="Fri", transp=50)

//Calculations for options values
float CLOSECLOSE = 0
for i = 1 to lookback
    if (difference1 < Option1) and ((dayofweek==dayofweek.monday) or (dayofweek==dayofweek.wednesday) or (dayofweek==dayofweek.friday))
        CLOSECLOSE := Credit
    if (difference1 >= Option1) and (difference1 < Option2) and ((dayofweek==dayofweek.monday) or (dayofweek==dayofweek.wednesday) or (dayofweek==dayofweek.friday))
        CLOSECLOSE := Contracts1 * ( abs(round(close[i+1]) - close[i]) - Option1 ) + Credit
    if (difference1 >= Option2) and (difference1 < Option3) and ((dayofweek==dayofweek.monday) or (dayofweek==dayofweek.wednesday) or (dayofweek==dayofweek.friday))
        CLOSECLOSE := Contracts1 * ( abs(round(close[i+1]) - close[i]) - Option1 ) + Contracts2 * ( abs(round(close[i+1]) - close[i]) - Option2 ) + Credit
    if (difference1 >= Option3) and ((dayofweek==dayofweek.monday) or (dayofweek==dayofweek.wednesday) or (dayofweek==dayofweek.friday))
        CLOSECLOSE := Contracts1 * ( abs(round(close[i+1]) - close[i]) - Option1 ) + Contracts2 * ( abs(round(close[i+1]) - close[i]) - Option2 ) + Contracts3 * ( abs(round(close[i+1]) - close[i]) - Option3 ) + Credit

float OPENCLOSE = 0
for i = 1 to lookback
    if (difference2 < Option1) and ((dayofweek==dayofweek.monday) or (dayofweek==dayofweek.wednesday) or (dayofweek==dayofweek.friday))
        OPENCLOSE := Credit
    if (difference2 >= Option1) and (difference2 < Option2) and ((dayofweek==dayofweek.monday) or (dayofweek==dayofweek.wednesday) or (dayofweek==dayofweek.friday))
        OPENCLOSE := Contracts1 * ( abs(round(open[i]) - close[i]) - Option1 ) + Credit
    if (difference2 >= Option2) and (difference2 < Option3) and ((dayofweek==dayofweek.monday) or (dayofweek==dayofweek.wednesday) or (dayofweek==dayofweek.friday))
        OPENCLOSE := Contracts1 * ( abs(round(open[i]) - close[i]) - Option1 ) + Contracts2 * ( abs(round(open[i]) - close[i]) - Option2 ) + Credit
    if (difference2 >= Option3) and ((dayofweek==dayofweek.monday) or (dayofweek==dayofweek.wednesday) or (dayofweek==dayofweek.friday))
        OPENCLOSE := Contracts1 * ( abs(round(open[i]) - close[i]) - Option1 ) + Contracts2 * ( abs(round(open[i]) - close[i]) - Option2 ) + Contracts3 * ( abs(round(open[i]) - close[i]) - Option3 ) + Credit

//chart plots
plot (CLOSECLOSE, color = color.red, title="Close-Close")
plot (sum(CLOSECLOSE, HISTORICALCLOSE), color = color.red)
plot (OPENCLOSE, color = color.blue, title="Open-Close")
plot (sum(OPENCLOSE, HISTORICALCLOSE), color = color.blue)
plot (difference3, color = color.purple, title="Close-Open")
hline(0, color = color.fuchsia, linestyle = hline.style_solid)