YasmanyArciaCorcho / trading-stocks

Platforms to develop, test, and run stock trading strategies
1 stars 1 forks source link

To review my change on the code on why No exiting at Profit Target? #31

Open Carto1984 opened 2 years ago

Carto1984 commented 2 years ago

image

image

Carto1984 commented 2 years ago

class BuyVWAPStrategyAction(VWAPStrategyAction): def IsGoodBrokenCandle(self, vwap, trading_equity): candle = trading_equity.CurrentTradingWindow[0] return (not vwap is None and (candle.Low < vwap.Current.Value
and candle.Close >= vwap.Current.Value))

def UpdateLastBrokenCandle(self, trading_equity):
    current_trading_window = trading_equity.CurrentTradingWindow[0]
    vwap = trading_equity.GetIndicator('vwap')
    if vwap is None:
        return
    if (not self.GetLastBrokenCandle(trading_equity.Symbol()) is None 
        and current_trading_window.Low < vwap.Current.Value
        and current_trading_window.Close < vwap.Current.Value):
        self.UpdateLastBrokenCandleData(trading_equity.Symbol(), None)
        return
    if (self.GetLastBrokenCandle(trading_equity.Symbol()) is None
        and self.IsGoodBrokenCandle(vwap, trading_equity)):
        self.UpdateLastBrokenCandleData(trading_equity.Symbol(), current_trading_window)

def TryToUpdateStopOrderPrice(self, vwap_algo, trading_equity, equity_current_price):
    if trading_equity.LastExitOrder is None:
        return
    if (not trading_equity.LastExitOrder.Status is OrderStatus.Filled
        and equity_current_price - trading_equity.LastEntryPrice > trading_equity.StopOrderUpdatePriceByRish):
        vwap_algo.Log('e-')
        current_stop_price = trading_equity.LastEntryPrice + trading_equity.StopOrderUpdatePriceByRish
        trading_equity.LastEntryPrice = trading_equity.LastEntryPrice + trading_equity.StopOrderUpdatePriceByRish
        update_settings = UpdateOrderFields()
        update_settings.StopPrice = current_stop_price
        update_settings.LimitPrice = current_stop_price - 0.05
        trading_equity.LastExitOrder.Update(update_settings)

def ShouldEnterToBuy(self, vwap_algo, trading_equity, equity_current_price):
    vwap = trading_equity.GetIndicator('vwap')
    return (#not self.GetLastBrokenCandle(trading_equity.Symbol()) is None
            #and self.IsGoodBrokenCandle(vwap, trading_equity)
            #and (trading_equity.CurrentTradingWindow[0].Time - self.GetLastBrokenCandle(trading_equity.Symbol()).Time).total_seconds() >= vwap_algo.AccumulatePositiveTimeRan
            #and equity_current_price > trading_equity.CurrentTradingWindow[0].Close
            equity_current_price > trading_equity.HistoryData[0].High
            and equity_current_price < trading_equity.HistoryData[0].High + 0.10)
            #and trading_equity.HistoryData[0].Close > trading_equity.HistoryData[0].Open)
            #and trading_equity.CurrentTradingWindow[0].Open > trading_equity.CurrentTradingWindow[0].Close)

def SetTradingEquityBuyPriceData(self, trading_equity, equity_current_price):
    trading_equity.LastEntryPrice = equity_current_price
    trading_equity.LastStopEntryPrice = trading_equity.LastEntryPrice - 0.30 #min(trading_equity.LowPriceWindow[0].Low, trading_equity.CurrentTradingWindow[0].Low) - 0.05
    trading_equity.StopOrderUpdatePriceByRish = abs(trading_equity.LastEntryPrice - trading_equity.LastStopEntryPrice)      

def PerformOrder(self, vwap_algo, symbol, quantity):
    return vwap_algo.MarketOrder(symbol, quantity)

#def OnOrderEvent(self, vwap_algo, orderEvent):
#     if (orderEvent.Status == OrderStatus.Filled):
#         trading_equity = vwap_algo.stocksTrading.GetEquity(orderEvent.Symbol)
#         if (not trading_equity is None
#             and trading_equity.LastExitOrder is None):
#            self.SetTradingEquityBuyPriceData(trading_equity, orderEvent.FillPrice)
#            ticket = vwap_algo.StopLimitOrder(orderEvent.Symbol, -orderEvent.Quantity, trading_equity.LastStopEntryPrice, trading_equity.LastStopEntryPrice - 0.5)
#            trading_equity.LastExitOrder = ticket.OrderId
#            trading_equity.LasEntryOrderId = None
#         elif(not trading_equity.LastExitOrder is None
#             and trading_equity.LastExitOrder == orderEvent.OrderId):
#             vwap_algo.ResetEquityTradePrice(trading_equity)
#         trading_equity.SetLastTradeTime(vwap_algo.Time)

def AddStopLose(self, vwap_algo, trading_equity, quantity, equity_current_price):
    self.SetTradingEquityBuyPriceData(trading_equity, equity_current_price)
    trading_equity.LastExitOrder = vwap_algo.StopLimitOrder(trading_equity.Symbol(), -quantity, trading_equity.LastStopEntryPrice, trading_equity.LastStopEntryPrice - 0.5)